I am trying to do normal mapping but no matter what I try, when I rotate my model, the light appears to rotate with it.
Other models with ADS lighting using the same normals (but not the newly calculated tangent) work as expected.
I suspect I'm doing something wrong with the lightDirection vector.
I also tried the method which calculates tangents in the shader from here http://www.thetenthplanet.de/archives/1180 and it has the same problem.
Vertex Shader:
#version 120
uniform mat4 viewMatrix;
uniform mat3 normalMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform vec3 lightPosition; /* World coordinates */
uniform vec3 cameraPosition; /* World coordinates */
attribute vec3 vertex3;
attribute vec3 normal;
attribute vec4 tangent; /* Calculated as per http://www.terathon.com/code/tangent.html */
attribute vec2 texCoords0;
varying vec3 lightDirection;
varying vec3 viewDirection;
varying vec2 texCoord0;
void main()
{
vec4 vertex = vec4( vertex3.xyz, 1.0 );
vec3 n = normalize( normalMatrix * normal );
vec3 t = normalize( normalMatrix * tangent.xyz );
vec3 b = normalize( normalMatrix * ( cross( normal, tangent.xyz ) * tangent.w ) );
vec3 vertexView = ( modelViewMatrix * vertex ).xyz;
mat3 tbn = transpose( mat3( t, b, n ) );
vec3 temp = ( viewMatrix * vec4( lightPosition, 1.0 ) ).xyz - vertexView;
lightDirection = tbn * temp;
temp = ( viewMatrix * vec4( cameraPosition, 1.0 ) ).xyz - vertexView;
viewDirection = tbn * temp;
texCoord0 = texCoords0;
gl_Position = modelViewProjectionMatrix * vertex;
}
Fragment Shader:
uniform sampler2D normalMap;
varying vec3 lightDirection;
varying vec3 viewDirection;
varying vec2 texCoord0;
void main (void)
{
vec3 pixelNormal = normalize( texture2D( normalMap, texCoord0.st ).xyz * 2.0 - 1.0 );
vec3 normalizedLightDirection = normalize( lightDirection );
float lambert = max( 0.0, dot( pixelNormal, normalizedLightDirection ) );
gl_FragColor = vec4( lambert, lambert, lambert, 1.0 );
}
My understanding of this is:
vec3 n = normalize( normalMatrix * normal );
vec3 t = normalize( normalMatrix * tangent.xyz );
vec3 b = normalize( normalMatrix * ( cross( normal, tangent.xyz ) * tangent.w ) );
mat3 tbn = transpose( mat3( t, b, n ) );
t, b, n converts from tangent space to model space, so by multiplying each element by the normalMatrix it should go from tangent space to view space. Then by transposing tbn, the final tbn matrix should convert from view space to tangent space.
vec3 temp = ( viewMatrix * vec4( lightPosition, 1.0 ) ).xyz - vertexView;
lightDirection = tbn * temp;
temp = ( viewMatrix * vec4( cameraPosition, 1.0 ) ).xyz - vertexView;
viewDirection = tbn * temp;
Both the light position and the camera position are passed as world coordinates, so these lines should make temp a direction in view space to the camera and light positions.
Then when each temp direction vector is multiplied by the tbn it should move those vectors from view space to tangent space where I can use them directly in the pixel shader to calculate the lambert value by doing a dot product against the normal from the normal map (which is in tangent space).
vec3 pixelNormal = normalize( texture2D( normalMap, texCoord0.st ).xyz * 2.0 - 1.0 );
vec3 normalizedLightDirection = normalize( lightDirection );
float lambert = max( 0.0, dot( pixelNormal, normalizedLightDirection ) );
Somewhere in here, I'm turning the light but I can't figure where.
normalMatrix
uniform transform from model-space to view-space or from model-space to world-space?gl_FragColor
just to debug the normals.