Pass MVP and object transformation to model draw
This commit is contained in:
parent
97867c90f4
commit
9b03876a3e
|
@ -10,7 +10,7 @@ class Model:
|
|||
public Resource
|
||||
{
|
||||
public:
|
||||
virtual void draw() const = 0;
|
||||
virtual void draw(const glm::mat4 &mvp, const glm::mat4 &transformation) const = 0;
|
||||
|
||||
protected:
|
||||
static GLuint create_array_buffer(GLenum type, GLsizeiptr size, const GLvoid *data);
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
#include <cstdio>
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
using namespace Models;
|
||||
|
||||
const std::string Raw::filename(const std::string &name)
|
||||
|
@ -33,8 +36,14 @@ Raw::Raw(__attribute__((unused)) Store &store, const std::string &name)
|
|||
id = create_array_buffer(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(GLushort), elements.data());
|
||||
}
|
||||
|
||||
void Raw::draw() const
|
||||
void Raw::draw(const glm::mat4 &mvp, const glm::mat4 &transformation) const
|
||||
{
|
||||
const glm::mat4 transform = mvp * transformation;
|
||||
glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, glm::value_ptr(transform));
|
||||
|
||||
const glm::mat3 local_modelview = glm::transpose(glm::inverse(glm::mat3(transformation)));
|
||||
glUniformMatrix3fv(local_modelview_uniform, 1, GL_FALSE, glm::value_ptr(local_modelview));
|
||||
|
||||
glEnableVertexAttribArray(INDEX_POSITION);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, positions_id);
|
||||
glVertexAttribPointer(INDEX_POSITION, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const GLvoid*>(0));
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace Models
|
|||
RESOURCE(Raw)
|
||||
|
||||
public:
|
||||
void draw() const;
|
||||
void draw(const glm::mat4 &mvp, const glm::mat4 &transformation) const;
|
||||
|
||||
private:
|
||||
GLuint positions_id;
|
||||
|
|
|
@ -6,6 +6,9 @@
|
|||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
using namespace Models;
|
||||
|
||||
const std::string Static::filename(const std::string &name)
|
||||
|
@ -103,8 +106,14 @@ Static::Static(Store &store, const std::string &name)
|
|||
id = create_array_buffer(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(GLushort), elements.data());
|
||||
}
|
||||
|
||||
void Static::draw() const
|
||||
void Static::draw(const glm::mat4 &mvp, const glm::mat4 &transformation) const
|
||||
{
|
||||
const glm::mat4 transform = mvp * transformation;
|
||||
glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, glm::value_ptr(transform));
|
||||
|
||||
const glm::mat3 local_modelview = glm::transpose(glm::inverse(glm::mat3(transformation)));
|
||||
glUniformMatrix3fv(local_modelview_uniform, 1, GL_FALSE, glm::value_ptr(local_modelview));
|
||||
|
||||
_material->use();
|
||||
|
||||
glEnableVertexAttribArray(INDEX_POSITION);
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace Models
|
|||
RESOURCE(Static)
|
||||
|
||||
public:
|
||||
void draw() const;
|
||||
void draw(const glm::mat4 &mvp, const glm::mat4 &transformation) const;
|
||||
|
||||
private:
|
||||
GLuint positions_id;
|
||||
|
|
|
@ -1,18 +1,9 @@
|
|||
#include "object.hpp"
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
void Object::draw(const glm::mat4 &mvp) const
|
||||
{
|
||||
if (!visible)
|
||||
return;
|
||||
|
||||
const glm::mat4 transform = mvp * transformation();
|
||||
glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, glm::value_ptr(transform));
|
||||
|
||||
const glm::mat3 local_modelview = glm::transpose(glm::inverse(glm::mat3(transformation())));
|
||||
glUniformMatrix3fv(local_modelview_uniform, 1, GL_FALSE, glm::value_ptr(local_modelview));
|
||||
|
||||
_model.draw();
|
||||
_model.draw(mvp, transformation());
|
||||
}
|
||||
|
|
Reference in New Issue