1
0
Fork 0

Pass MVP and object transformation to model draw

This commit is contained in:
Meoweg 2015-11-13 06:59:38 +00:00
parent 97867c90f4
commit 9b03876a3e
6 changed files with 24 additions and 15 deletions

View File

@ -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);

View File

@ -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));

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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());
}