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.