PDA

View Full Version : tangents in fragmentshader



Dragon89
03-15-2007, 11:05 AM
im trying to calculate the tangents in the fragment shader as described in the book shaderx5... but it doesnt work properly.. the tangents seem to change with the view... any idea whats wrong?

#version 110

varying vec3 VertPos;

void main()
{
VertPos = gl_Vertex.xyz;

gl_TexCoord[0] = gl_MultiTexCoord0;

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

#version 110

varying vec3 VertPos;

mat3 GetTBN(vec3 Normal, vec3 Point, vec2 TexCoord)
{
vec3 dp1 = dFdx(Point);
vec3 dp2 = dFdy(Point);
vec2 duv1 = dFdx(TexCoord);
vec2 duv2 = dFdy(TexCoord);

mat3 M = mat3(dp1, dp2, vec3(0.0,0.0,0.0));
vec3 Tangent = vec3(duv1.x, duv2.x, 0.0) * M;
vec3 BiTangent = vec3(duv1.y, duv2.y, 0.0) * M;

return mat3(normalize(Tangent), normalize(BiTangent), Normal);
}


void main()
{
vec2 TexCoord = gl_TexCoord[0].st;

mat3 TBN = GetTBN(vec3(0.0,1.0,0.0), VertPos, TexCoord);

gl_FragColor = vec4(TBN[0].xyz, 1.0);
}