Asmodeus

07-01-2015, 06:57 AM

Hey, i would like to ask if i have done something wrong in my code because my normal mapping looks kinda strange. I am using Assimp to generate tangents and upload them to the vertex shader, and a flat .obj plane (that has normals,uvs)

Here is the Relevant code from the vertex and fragment shaders.

Vertex Shader

vec3 n = normalize((T_model * vec4(s_vNormal,0.0)).xyz);

vec3 t = normalize(( T_model * vec4(s_vTangent,0.0)).xyz);

t = normalize(t - dot(t,n) * n);

vec3 biTangent = cross(t,n);

tbnMatrix = mat3(t,biTangent,n);

Fragment Shader

vec3 unitNormal = texture2D(normalMap,fTexCoord).xyz;

unitNormal = (255.0/128.0 * unitNormal) - 1.0;

unitNormal = normalize(tbnMatrix * unitNormal);

Other light Relevant code

//vLight - light position in world space

//toCameraVector - vector to camera;

//toLightVector - vector to light;

for(int i=0; i<MAX_LIGHTS; i++) {

toLightVector[i] = (vLight[i] - worldPosition).xyz;

}

toCameraVector = (inverse(mView) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;

If i remove tbnMatrix from the fragment shader equasion , the result looks more similar to a normal map rather than if i leave it there. I will post some screen shots.

With TBN matrix : normalize(tbnMatrix * unitNormal);

1910

Without TBN matrix : normalize(unitNormal);

1909

I guess the problem has something to do with lights. Should i multiply light vectors with the TBN matrix as well ?

Here is the Relevant code from the vertex and fragment shaders.

Vertex Shader

vec3 n = normalize((T_model * vec4(s_vNormal,0.0)).xyz);

vec3 t = normalize(( T_model * vec4(s_vTangent,0.0)).xyz);

t = normalize(t - dot(t,n) * n);

vec3 biTangent = cross(t,n);

tbnMatrix = mat3(t,biTangent,n);

Fragment Shader

vec3 unitNormal = texture2D(normalMap,fTexCoord).xyz;

unitNormal = (255.0/128.0 * unitNormal) - 1.0;

unitNormal = normalize(tbnMatrix * unitNormal);

Other light Relevant code

//vLight - light position in world space

//toCameraVector - vector to camera;

//toLightVector - vector to light;

for(int i=0; i<MAX_LIGHTS; i++) {

toLightVector[i] = (vLight[i] - worldPosition).xyz;

}

toCameraVector = (inverse(mView) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;

If i remove tbnMatrix from the fragment shader equasion , the result looks more similar to a normal map rather than if i leave it there. I will post some screen shots.

With TBN matrix : normalize(tbnMatrix * unitNormal);

1910

Without TBN matrix : normalize(unitNormal);

1909

I guess the problem has something to do with lights. Should i multiply light vectors with the TBN matrix as well ?