0
votes

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); 
}

enter image description here

1
You don't give much detail on how exactly the result is different from your expectations. One problem I see is that you should check SpecularFactor to be positive before the pow() call. Otherwise the result of pow() is undefined.Reto Koradi

1 Answers

0
votes

My last awnsner: Normal mapping and phong shading with incorrect specular component See, how was calculated specular factor.

1) Your code:

dot(VertexToEye, LightReflect)

Must be:

max(dot(VertexToEye, LightReflect), 0.0)

That's needed for clamp negative values to zero, because in specular calculation we have exponent (how say Reto Koradi)!

2) If you don't see errors when your compiling shaders, try use glGetProgramInfoLog function. See it:

vec3 specular;
...
if(SpecularFactor > 0)
{
    specular = intensity * vec3(0.0,1.0,0.0) * SpecularFactor;  
}

If specular equal 0 we have variable is undefined error.

Replace:

vec3 specular;

by:

vec3 specular = vec3(0.0);

P.S. Better use float values(0.0) for float variables like float, vec2, vec3... That's about:

if(SpecularFactor > 0) -> if(SpecularFactor > 0.0)