From 308a92a9119badb67875b10d036687cc69a88421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 19 Nov 2012 16:20:59 +0100 Subject: [PATCH] Shaders: pass normal matrix the right way. Besides that this was long overdue, GLSL ES doesn't have mat3x3(). --- src/Shaders/PhongShader.cpp | 1 + src/Shaders/PhongShader.h | 4 +++- src/Shaders/PhongShader.vert | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-) 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);