Having an issue with my normal mapping and i'm stuck on where I have gone wrong. The map appears to be on the model but not in the correct space. The variable eye is just the camera position. The tangents are calculated in the program and they are correct.
The vertex shader:
void main()
{
vec3 EyeSpaceNormal = normalize(vec3(NormalMatrix * VertexNormal));
vec3 EyeSpaceTangent = normalize(vec3(NormalMatrix * vec3(VertexTangent)));
TexCoord = VertexUV;
vec3 bitangent = normalize(cross( EyeSpaceNormal, EyeSpaceTangent)) * VertexTangent.w;
mat3 TBN = mat3(EyeSpaceTangent, bitangent, EyeSpaceNormal);
TangentLightDirection = vec3( normalize(LightDirection) * TBN );
TangentEye = vec3(normalize( eye) * TBN );
Normal = EyeSpaceNormal;
VertPosition = vec3( ModelViewMatrix * vec4(VertexPosition,1.0));
gl_Position = MVP * vec4(VertexPosition,1.0);
}
Frag Shader:
void main()
{
vec3 ReturnColour;
vec3 TextureNormal_tangentspace = normalize(texture2D( NormalMap, TexCoord ).rgb * 2.0 - 1.0);
vec3 diffuse = intensity * vec3(0.0,1.0,0.0) * max(0,dot(normalize(TextureNormal_tangentspace), normalize(-TangentLightDirection)));
vec3 specular;
//Specular
vec3 VertexToEye = normalize(TangentEye - VertPosition);
vec3 LightReflect = normalize(reflect(normalize(TangentLightDirection), TextureNormal_tangentspace));
float SpecularFactor = dot(VertexToEye, LightReflect);
SpecularFactor = pow(SpecularFactor, Shininess);
if(SpecularFactor > 0)
{
specular = intensity * vec3(0.0,1.0,0.0) * SpecularFactor;
}
ReturnColour = diffuse + specular;
FragColor = vec4(ReturnColour, 1.0);
}
SpecularFactor
to be positive before thepow()
call. Otherwise the result ofpow()
is undefined. – Reto Koradi