Original Post
Hi all, I've been working on implementing normal mapping into my game and generally everything is ok, but there is a strange problem for some models which means the light direction is different. I.e. 90% of the models in the scene are lit from one direction, and the remaining 10% are lit from a light source 90 degrees rotated. All things should be lit from the same direction. I am using directional lights to light the scene. Light direction is passed in to the opengl light position. I have also drawn the tangent space basis vectors at every vertex and they look like they are being generated correctly. Here is my vertex and pixel shader: Can anyone see anything wrong with my code? I'm not sure why it doesn't work. Thanks in advance.
varying vec3 vecLightDir;
varying vec4 vecColour;
uniform sampler2D tex0;
attribute vec3 tangent;
attribute vec3 bitangent;
void main ()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
vecColour = gl_Color;
//Transform light vector into tangent space
mat3 tangentspace = mat3(tangent, bitangent, gl_Normal);
tangentspace = gl_NormalMatrix * tangentspace;
vec3 templightdir = normalize(gl_LightSource[0].position.xyz);
vecLightDir = templightdir * tangentspace; //multiply by inverse rotmat (multiplied in other order = transposed)
vecLightDir.xy = -vecLightDir.xy; //correct the normal direction
//Do the transform
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
varying vec3 vecLightDir;
varying vec4 vecColour;
uniform sampler2D tex0;
uniform sampler2D tex1;
void main ()
{
vec3 N, halfV;
float NdotL, NdotHV;
vec4 color;
N = normalize(texture2D(tex1, gl_TexCoord[0].st).rgb*2.0 - 1.0);
NdotL = max(dot(normalize(vecLightDir), N), 0.0);
color = clamp(vec4(0.2, 0.2, 0.2, 1.0) + vec4(0.8, 0.8, 0.8, 1.0) * NdotL, 0.0, 1.0)*vecColour;
gl_FragColor = color * texture2D(tex0, gl_TexCoord[0].st);
}