Hi,
i am currently working on a normal bump shader for my game engine. The bump effect seems to be dependend on the model view matrix... but i dont know why. The shader are very simple and the tangent calculation should be correct.
To better discribe the problem i have recorded a short video clip (xvid) which can be downloaded
here.
Ignore the error messages at the bottom of the window, i just do no bump mapping in the ground shader... so the uniforms are unused.
The video shows some interaction and the wrong bump mapping effect. In the model chooser window on the left side the tangents (light blue) and the normals (yellow) of the torus are shown.
And finally, here ist the shader pair:
Vertex Shader
attribute vec3 Tangent;
varying vec3 LightDir;
void main()
{
//Put the vertex in the position passed
gl_Position = ftransform();
// Construct a 3x3 matrix from the geometry’s inverse tangent, binormal, and normal
vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
vec3 tangent = normalize(gl_NormalMatrix * Tangent);
vec3 binormal = cross(normal, tangent);
mat3 TBN = mat3(tangent, binormal, normal);
//Rotate the light into tangent space
vec4 ecposition = gl_ModelViewMatrix * gl_Vertex;
vec3 lightDirection = gl_LightSource[0].position.xyz - ecposition.xyz;
LightDir = TBN * normalize(lightDirection);
//Use the first set of texture coordinates in the fragment shader
gl_TexCoord[0] = gl_MultiTexCoord0;
}
Fragment Shader
uniform sampler2D DiffuseMap;
uniform sampler2D NormalMap;
varying vec3 LightDir;
void main()
{
//Get the color of the bump-map
vec3 BumpNormal = vec3(texture2D(NormalMap, gl_TexCoord[0].xy));
//Expand the bump-map into a normalized signed vector
BumpNormal = (BumpNormal -0.5) * 2.0;
//Find the dot product between the light direction and the normal
float NdotL = max(dot(BumpNormal, LightDir), 0.0);
//Calculate the final color gl_FragColor
vec4 Idiff = gl_FrontLightProduct[0].diffuse * NdotL;
vec4 Iamb = gl_FrontLightProduct[0].ambient;
vec4 Ifog = gl_FrontLightProduct[0].diffuse;
vec4 finalColor = max(Iamb, Idiff) * texture2D(DiffuseMap, gl_TexCoord[0].xy);
float fogDistance = pow(gl_FragCoord.z, 128.0) - 0.5;
gl_FragColor = mix(finalColor, Ifog * gl_Fog.color, clamp(fogDistance, 0.0, 1.0));
}
Please help...