   normal mapping and tangent space
  greetings!I am having issues with my normal maps and tangent space. The regular lighting (lambert, specular) is working just fine:// Vertex Shadervoid mainVS( in a2v IN, out v2p OUT ){float4x4 worldview = mul(gWorld, gView);float4x4 worldviewproj = mul(mul(gProj, gView), gWorld);// set default dataOUT.diffuse = IN.diffuse;OUT.uv0 = IN.uv0;OUT.uv1 = IN.uv1;OUT.uv2 = IN.uv2;OUT.uv3 = IN.uv3;// set positional dataOUT.ppos = float4(IN.pos, 1.0);OUT.pos = mul(worldviewproj, float4(IN.pos, 1.0));OUT.normal = normalize(float4(IN.normal, 1.0));OUT.tangent = normalize(float4(IN.tangent, 1.0));OUT.binormal = normalize(float4(IN.binormal, 1.0));}// Pixel Shaderfloat4 mainPS( v2p IN ) : COLOR{float4 normal = IN.normal;float4 pixelpos = IN.ppos;float4 lightpos = gLightPosition_Default;float4 campos = gCamPos;// calculate lambertfloat lambert = LightLambert(normal, pixelpos, lightpos);// calculate specularfloat specular = LightSpecular(normal, pixelpos, lightpos, gLightShininess_Default, gLightAtten_Default, gCamPos);// calculate diffusefloat4 diffuse = tex2D(gDiffuseMap, IN.uv0) * gDiffuseColor;// calculate final colorfloat4 color = ((lambert + specular + gLightAmbient_Default) * diffuse * gLightDiffuse_Default);// returrrrrrrrrrrrn!return color;}This gives me the following image:As you can see, there's a point light positioned at (0, 4, 0) with certain attenuation values and it looks very nice, as far as dull lambert and specular go.But when I add Normal Map calculation, like so:float4 mainPS( v2p IN ) : COLOR{// set to default world spacefloat4 normal = IN.normal;float4 pixelpos = IN.ppos;float4 lightpos = gLightPosition_Default;float4 campos = gCamPos;// Do we have a normal map? Yes, slow but dynamic! :)if(tex2D(gNormalMap, IN.uv0).b > 0){// calculate tangent space matrixfloat4x4 TBN;TBN[0] = IN.tangent;TBN[1] = IN.binormal;TBN[2] = IN.normal;TBN[3] = float4(0, 0, 0, 1);// set values to tangent spacenormal = normalize(tex2D(gNormalMap, IN.uv0)) * 2 - 1;pixelpos = mul(TBN, IN.ppos);lightpos = mul(TBN, gLightPosition_Default);campos = mul(TBN, gCamPos);}// calculate lambertfloat lambert = LightLambert(normal, pixelpos, lightpos, gLightAtten_Default);// calculate specularfloat specular = LightSpecular(normal, pixelpos, lightpos, gLightShininess_Default, gLightAtten_Default, gCamPos);// calculate diffusefloat4 diffuse = tex2D(gDiffuseMap, IN.uv0) * gDiffuseColor;// calculate final colorfloat4 color = ((lambert + specular + gLightAmbient_Default) * diffuse * gLightDiffuse_Default);// returrrrrrrrrrrrn!return color;}I get the following image:As you can see the lighting is suddenly very different. On the arches you can see that there is no normal map applied there, so the regular lighting is calculated, which looks good.Some information on the tangents: I receive the tangents in object space. I wrote my own exporter and I export them in MSpace::kObject, the default value. As well as the normals, so they should be in order.Now, it's my understanding that when working in tangent space, all the aspects or objects that have an influence on the lighting effect have to be transformed into tangent space, so that's what I do with my "tangentSpace" matrix.What I tried:- Leaving out several elements out of the tangent space equation, like light position, camera position (which is a float4 with its W component set to 0), but I get the craziest effects. Specular suddenly starts going haywire etc.- Multiplying the TBN with the WORLD matrix, but this also did not work well.- Multiplying the TBN values in the Vertex Shader with the WORLD matrix.- Changed the multiplication order, did not work.---I am clearly mixing up a space here and there. But where? This worked perfectly on a homework assignment and now that I am doing this for myself, it's not working anymore :)Oh yeah, I am using a RH coordinate system now that I think of it.Thanks everyone!   
  Found out that the light that I have is considered to be within model space, as every instance of sponza has a separate light. Could this have something to do with it?   ;  blump   