Browse Source

Shaders: pass normal matrix the right way.

Besides that this was long overdue, GLSL ES doesn't have mat3x3().
pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
308a92a911
  1. 1
      src/Shaders/PhongShader.cpp
  2. 4
      src/Shaders/PhongShader.h
  3. 3
      src/Shaders/PhongShader.vert

1
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");

4
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;
};

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

Loading…
Cancel
Save