Help with normal mapping

Hi,
i try to implement a normal mapper. i have some problems with the texture-space-matrix (t, b, n).
Here is my fragment-shader:


varying vec3 halfAngle;
varying vec3 lightDirection;

void main()
{
    vec3 normal = gl_Normal;
    vec3 tangent = gl_MultiTexCoord1.xyz;

    vec4 position = gl_ModelViewMatrix * gl_Vertex;
	
    normal = gl_NormalMatrix * normalize(normal);
    tangent = gl_NormalMatrix * normalize(tangent);
    vec3 binormal = cross(normal, tangent);
	
    //mat3 textureSpaceMatrix = mat3(tangent, binormal, normal);
    mat3 textureSpaceMatrix = mat3(tangent.x, binormal.x, normal.x,
                          tangent.y, binormal.y, normal.y,
                          tangent.z, binormal.z, normal.z);
						  
    lightDirection = gl_LightSource[0].position.xyz;
    lightDirection = textureSpaceMatrix * lightDirection;
    
    halfAngle = gl_LightSource[0].halfVector.xyz;
    halfAngle = textureSpaceMatrix * halfAngle;

    gl_TexCoord[0] = gl_MultiTexCoord0;
    
    gl_Position = ftransform();
}

my vertex-shader:


uniform sampler2D normalMap;
uniform sampler2D colorMap;

varying vec3 halfAngle;
varying vec3 lightDirection;

void main()
{
    vec3 lightDir = normalize(lightDirection);
    vec3 halfA = normalize(halfAngle);
	vec3 normal = normalize(texture2D(normalMap, gl_TexCoord[0].st).rgb * 2.0 - 1.0);      
    
    vec4 ambient = gl_FrontLightProduct[0].ambient;
    vec4 diffuse = gl_FrontLightProduct[0].diffuse *max(dot(normal, lightDir), 0.0);
    vec4 specular = gl_FrontLightProduct[0].specular * pow(max(dot(normal, halfA), 0.0), gl_FrontMaterial.shininess);
    vec4 diffuseColor = texture2D(colorMap, gl_TexCoord[0].st);
    
    gl_FragColor = diffuseColor* (ambient+diffuse)+specular;
}

With the matrix

mat3 textureSpaceMatrix = mat3(tangent, binormal, normal);

i get this result:

this looks not bad, but the lighting dosen’t follow the lightposition at:

float position[] = {-4.0, 1.0, 2.0, 1.0};

this matrix

  mat3 textureSpaceMatrix = mat3(tangent.x, binormal.x, normal.x,
                          tangent.y, binormal.y, normal.y,
                          tangent.z, binormal.z, normal.z);

give me strange results, but the lightposition seems to be ok

can anyone help me please.

One point caught my eye:

in your vertex-shader (you mislabeled your vertex-shader as fragment-shader and vice versa) you use the tbn-matrix to transform the position of the light source, but - as far as i understand normal mapping - you should actually use the direction to the light source.
Unless your light source is supposed to be directional, of course :wink:

And btw. your first tbn-matrix should be the correct one.

In GLSL, gl_NormalMatrix * mat3(tangent, binormal, normal) is going to transform from tangent space to eye space, which is used for true reflective bumpmapping (see slide 28, and keep in mind that the equivalent GLSL matrix is the transpose of the one shown). You pass this matrix to the fragment shader, and transform each lookup into your normal map into eye space.

However, I believe you’ll want your second matrix to transform from eye space to tangent space, so that you’re performing your operations in the same space (tangent space) in the fragment shader (assuming that your normal map is not in object space already).

The second matrix in your code is actually an estimate of the inverse of the gl_NormalMatrix * mat3(tangent, binormal, normal) matrix operating on the assumption that the tangent and binormal vectors are orthogonal.

Thanks for help,

but one question, why looks the bump-map better “deeper” in object-space?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.