2

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.

5
  • Does your normalMatrix uniform transform from model-space to view-space or from model-space to world-space?
    – GuyRT
    Nov 19, 2013 at 14:58
  • It is the same as the OpenGL gl_NormalMatrix (inverse transpose of the upper 3x3 of the model view matrix. Nov 19, 2013 at 18:24
  • Have you tried passing the TBN matrix to your fragment shader and then transforming the tangent-space normals to world-space in the fragment shader and doing lighting in world-space? This will be much more expensive to do and should not be your final implementation (unless you are doing deferred shading), but at least you will rule out the possibility of incorrectly computing the TBN basis vectors. In fact, you might consider using the TBN matrix to transform from tangent-space to world- or view-space and writing the transformed normal by itself to gl_FragColor just to debug the normals. Nov 19, 2013 at 23:39
  • Thanks, that nobody has pointed out an obvious flaw in the math is encouraging. Debugging in world space is a good idea, I'll give that a try. Nov 20, 2013 at 5:48
  • 1
    Figured it all out! Three Normal Mapping Techniques Explained For the Mathematically Uninclined at onemanmmo.com/?normalmapping2 Nov 22, 2013 at 7:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.