Original Post
I spent the last two days trying to figure out normal mapping without much success. I had it partially working a few times but the lighting is still incorrect. A question: why must I multiply my normal, tangent and binormal with the inverse transpose of the view*world matrix? Anyway, I went through two or 3 different tuts and one of them uses the inverse transpose matrix and some other don't so I'm very confused ;S Anyway, here is some code, what am I doing wrong?? GLOBAL SHADER VALUES: VERTEX SHADER: PIXEL SHADER:
float4x4 world; // World matrix
float4x4 viewproj; // View*projection matrix
float4x4 worldviewit; // The inverse transpose of the view*world matrix
float4 WorldLight; // The light position in world
// Multiply vertex by world & viewproj matrix
output.pos=mul(float4(input.pos,1.0f),world);
output.pos=mul(output.pos,viewproj);
// Get the vertex normal and multiply it by the world matrix
float4 normal=mul(float4(input.norm,1.0f),world);
// Get the position of the vertex in world space
float4 posWorld=mul(float4(input.pos,1.0f),world);
// Get light vector
float3 lightv=normalize(WorldLight-posWorld);
// Calculate tan, binormal and normal by multiplying them with the inverse transpose of the view*world matrix
float3 tan=mul(float4(input.tan,1.0f),worldviewit).xyz;
float3 bin=mul(float4(input.binorm,1.0f),worldviewit).xyz;
float3 norm=mul(float4(input.norm,1.0f),worldviewit).xyz;
// Tangent space matrix
float3x3 tbnm=float3x3(tan,bin,norm);
// Put light into tangent space
output.light=mul(tbnm,lightv);
// Texcoord
output.texcoord=input.texcoord;
// Get the normal from the normal map
float3 bump=2*normalmap.Sample(sampler1,input.texcoord).rgb-1.0f;
// Light vector
float3 light=normalize(input.light);
// Light power
float diff=saturate(dot(bump,light));
// Color*light power
return float4(0.8f,0.0f,0.0f,1.0f)*diff;