diff --git a/src/Shaders/PhongShader.cpp b/src/Shaders/PhongShader.cpp index 60be3eb89..8ca78b44d 100644 --- a/src/Shaders/PhongShader.cpp +++ b/src/Shaders/PhongShader.cpp @@ -58,6 +58,7 @@ PhongShader::PhongShader() { shininessUniform = uniformLocation("shininess"); transformationMatrixUniform = uniformLocation("transformationMatrix"); projectionMatrixUniform = uniformLocation("projectionMatrix"); + normalMatrixUniform = uniformLocation("normalMatrix"); lightUniform = uniformLocation("light"); lightColorUniform = uniformLocation("lightColor"); diff --git a/src/Shaders/PhongShader.h b/src/Shaders/PhongShader.h index 5662c8f34..efccc6f88 100644 --- a/src/Shaders/PhongShader.h +++ b/src/Shaders/PhongShader.h @@ -84,11 +84,12 @@ class SHADERS_EXPORT PhongShader: public AbstractShaderProgram { } /** - * @brief Set transformation matrix + * @brief Set transformation matrix and normal matrix * @return Pointer to self (for method chaining) */ inline PhongShader* setTransformation(const Matrix4& matrix) { setUniform(transformationMatrixUniform, matrix); + setUniform(normalMatrixUniform, matrix.rotation()); return this; } @@ -128,6 +129,7 @@ class SHADERS_EXPORT PhongShader: public AbstractShaderProgram { shininessUniform, transformationMatrixUniform, projectionMatrixUniform, + normalMatrixUniform, lightUniform, lightColorUniform; }; diff --git a/src/Shaders/PhongShader.vert b/src/Shaders/PhongShader.vert index b544d63f8..066afd94d 100644 --- a/src/Shaders/PhongShader.vert +++ b/src/Shaders/PhongShader.vert @@ -5,6 +5,7 @@ uniform highp mat4 transformationMatrix; uniform highp mat4 projectionMatrix; +uniform mediump mat3 normalMatrix; uniform highp vec3 light; #ifdef EXPLICIT_ATTRIB_LOCATION @@ -25,7 +26,7 @@ void main() { highp vec3 transformedPosition = transformedPosition4.xyz/transformedPosition4.w; /* Transformed normal vector */ - transformedNormal = normalize(mat3x3(transformationMatrix)*normal); + transformedNormal = normalMatrix*normal; /* Direction to the light */ lightDirection = normalize(light - transformedPosition);