Change normal matrix from 4x4 to 3x3

This commit is contained in:
Mario Hüttel 2020-03-26 23:03:49 +01:00
parent 251adef6b5
commit b9a007dd51
6 changed files with 18 additions and 11 deletions

View File

@ -15,12 +15,12 @@ class OpenGlGraphics
virtual void realize() = 0; virtual void realize() = 0;
virtual void render() = 0; virtual void render() = 0;
void setModelMatrix(const glm::mat4 &model_matrix); void setModelMatrix(const glm::mat4 &model_matrix);
glm::mat4 getModelMatrix(); const glm::mat4 &getModelMatrix();
glm::mat4 getNormalMatrix(); const glm::mat3 &getNormalMatrix();
protected: protected:
std::shared_ptr<OpenGlShaderProgram> shaderprog; std::shared_ptr<OpenGlShaderProgram> shaderprog;
glm::mat4 model_matrix; glm::mat4 model_matrix;
glm::mat4 normal_matrix; glm::mat3 normal_matrix;
}; };
#endif // OPENGLGRAPHICS_H #endif // OPENGLGRAPHICS_H

View File

@ -21,6 +21,7 @@ class OpenGlShaderProgram
void setUniformVec2(const std::string &uniform, float *vector); void setUniformVec2(const std::string &uniform, float *vector);
void setUniformVec3(const std::string &uniform, const glm::vec3 &vector); void setUniformVec3(const std::string &uniform, const glm::vec3 &vector);
void setUniformMat4(const std::string &uniform, const glm::mat4 &matrix); void setUniformMat4(const std::string &uniform, const glm::mat4 &matrix);
void setUniformMat3(const std::string &uniform, const glm::mat3 &matrix);
void setUniformVec4(const std::string &uniform, const glm::vec4 &vector); void setUniformVec4(const std::string &uniform, const glm::vec4 &vector);
void setUniformBool(const std::string &uniform, bool value); void setUniformBool(const std::string &uniform, bool value);
private: private:

View File

@ -5,7 +5,7 @@ layout (location = 1) in vec3 aInputNormal;
layout (location = 2) in vec2 aTextureCoord; layout (location = 2) in vec2 aTextureCoord;
uniform mat4 model_matrix; uniform mat4 model_matrix;
uniform mat4 normal_matrix; uniform mat3 normal_matrix;
uniform mat4 projection_view_matrix; uniform mat4 projection_view_matrix;
uniform vec3 camera_position; uniform vec3 camera_position;
@ -17,7 +17,7 @@ out vec3 ViewPos;
void main() void main()
{ {
gl_Position = projection_view_matrix * model_matrix * vec4(aInputPosition, 1.0); gl_Position = projection_view_matrix * model_matrix * vec4(aInputPosition, 1.0);
Normal = vec3(normal_matrix * vec4(aInputNormal, 0.0)); Normal = normal_matrix * aInputNormal;
FragPos = vec3(model_matrix * vec4(aInputPosition, 1.0)); FragPos = vec3(model_matrix * vec4(aInputPosition, 1.0));
TexCoord = aTextureCoord; TexCoord = aTextureCoord;
ViewPos = camera_position; ViewPos = camera_position;

View File

@ -47,7 +47,7 @@ void Mesh::render(const glm::mat4 &model_matrix, const glm::mat4 &normal_matrix)
{ {
this->shaderprog->use(); this->shaderprog->use();
this->shaderprog->setUniformMat4("model_matrix", model_matrix); this->shaderprog->setUniformMat4("model_matrix", model_matrix);
this->shaderprog->setUniformMat4("normal_matrix", normal_matrix); this->shaderprog->setUniformMat3("normal_matrix", normal_matrix);
this->shaderprog->setUniformMat4("projection_view_matrix", GlobalCanvasSettings::getPVMatrix()); this->shaderprog->setUniformMat4("projection_view_matrix", GlobalCanvasSettings::getPVMatrix());
this->shaderprog->setUniformVec3("camera_position", GlobalCanvasSettings::getCameraPosition()); this->shaderprog->setUniformVec3("camera_position", GlobalCanvasSettings::getCameraPosition());

View File

@ -1,7 +1,5 @@
#include <opengl-playground/openglgraphics.hpp> #include <opengl-playground/openglgraphics.hpp>
OpenGlGraphics::OpenGlGraphics(std::shared_ptr<OpenGlShaderProgram>shader_prog) OpenGlGraphics::OpenGlGraphics(std::shared_ptr<OpenGlShaderProgram>shader_prog)
{ {
this->shaderprog = shader_prog; this->shaderprog = shader_prog;
@ -16,15 +14,15 @@ OpenGlGraphics::~OpenGlGraphics()
void OpenGlGraphics::setModelMatrix(const glm::mat4 &model_matrix) void OpenGlGraphics::setModelMatrix(const glm::mat4 &model_matrix)
{ {
this->model_matrix = model_matrix; this->model_matrix = model_matrix;
this->normal_matrix = glm::transpose(glm::inverse(model_matrix)); this->normal_matrix = glm::mat3(glm::transpose(glm::inverse(model_matrix)));
} }
glm::mat4 OpenGlGraphics::getModelMatrix() const glm::mat4 &OpenGlGraphics::getModelMatrix()
{ {
return this->model_matrix; return this->model_matrix;
} }
glm::mat4 OpenGlGraphics::getNormalMatrix() const glm::mat3 &OpenGlGraphics::getNormalMatrix()
{ {
return this->normal_matrix; return this->normal_matrix;
} }

View File

@ -200,6 +200,14 @@ void OpenGlShaderProgram::setUniformMat4(const std::string &uniform, const glm::
} }
} }
void OpenGlShaderProgram::setUniformMat3(const std::string &uniform, const glm::mat3 &matrix)
{
int loc = this->getUniformLoc(uniform);
if (loc >= 0) {
glUniformMatrix3fv(loc, 1, GL_FALSE, &matrix[0][0]);
}
}
void OpenGlShaderProgram::setUniformVec4(const std::string &uniform, const glm::vec4 &vector) void OpenGlShaderProgram::setUniformVec4(const std::string &uniform, const glm::vec4 &vector)
{ {
int loc = this->getUniformLoc(uniform); int loc = this->getUniformLoc(uniform);