   Normal mapping (cg, opengl)
  I've tried for a several hours now to figure out why my lightingmodell doesnt work as planed :)1) Im using the gluLookAt function to place my camera and I draw a single triangle on the floor like: normal  = [0, 1, 0]vertices = [0, 0, 0] - [1, 0, 0] - [1, 0, -1]texcoords = [0, 0] - [1, 0] - [1, 1]2) Now I have a function to calculate the tangent vector for this triangle which gives me a tangent [1, 0, 0] which I send to the vertex program.3) I place a lightsource above the triangle at [0, 1, 0] which I can move around in X,ZNow to the Shader programs.uniform float4x4 ModelViewProj;uniform float4x4 ModelView;uniform float3x3 ModelViewIT;uniform float3light_pos[3];  struct vertexin{float4Position: POSITION;float3Normal : NORMAL;float4Color: COLOR0; float2 skin_coords: TEXCOORD0;float2 normal_coords: TEXCOORD1; uniform float3vert_tan;}; struct vertexout{float4Position: POSITION;float4Color: COLOR0;float3world_pos; float2 skin_coords: TEXCOORD0;float2 normal_coords: TEXCOORD1; float3light_dir[3]: TEXCOORD5;  }; vertexout ds_vs(vertexin IN) {vertexout OUT;  OUT.world_pos= IN.Position;OUT.Position= mul(ModelViewProj, IN.Position);  OUT.Color= IN.Color;OUT.skin_coords= IN.skin_coords;OUT.normal_coords= IN.normal_coords;  float3 tangent = normalize(IN.vert_tan);float3 binormal = normalize(cross(IN.Normal, tangent)); float3x3 tMatrix= float3x3(tangent, binormal, IN.Normal); float3 lightDir = normalize(light_pos[0] - OUT.world_pos); OUT.light_dir[0] = mul(tMatrix, lightDir); return OUT;}   float4 ds_ps(vertexout IN, uniform sampler2D diffuseMap : TEXUNIT0, uniform sampler2D NormalMap : TEXUNIT1) : COLOR{  float3 Normal= 2.0 * (tex2D(NormalMap, IN.normal_coords) - 0.5);Normal = normalize(Normal); Normal = float3(0,0,1);// Temp. float3 Direction= normalize(IN.light_dir[0]);   float light_dot= saturate(dot(Normal, Direction)); return light_dot; }Since Im using the lookat function I draw the vertices in worldspace so the OUT.world_pos = IN.Position; should be correct right? Ive tried to set the OUT.Color to the vertex, light positions and normals to see if they are really correct and yes the values seems allright.I dont need to rotate the lightvector into object space since the object matrix is and ident anyway?Rotating the lightposition with the inverse of tangentspace matrix should line it up with the normals from the normalmap (which I temporary set to 0,1,0)So what am I doing wrong? why isnt this working ? heh :)[Edited by - Dies_Irae on July 22, 2006 5:27:57 AM]
  But what space are the normals in? Maybe should be put in world space...   ;  They are in world space.I've created a small demo that might help understanding the problem and help me out.Download!Edit: It seems that the light_dir vector doesnt interpolate correctly throughout the surface.Edit: I got it to work by not normalizing the lightDir vector in the vertex program, very strange in my opinion but what the heck, it works./Thanks in advance.[Edited by - Dies_Irae on July 22, 2006 7:07:57 AM];Quote:I got it to work by not normalizing the lightDir vector in the vertex program, very strange in my opinion but what the heck, it works.That's actually not completely surprising because you're transforming by the projection matrix before you create your light vector.  Transform your position into world space first, as well as your normal and tangent vectors, and THEN generate the light vector from that.  Then you do your view+projection transforms.For the simple (LightPos - vertexPos) to be valid, you need them both to be in the same space.  What you have puts the vertex into camera frustum space without transforming the light vector into the same space.  Notably, you're also not transforming the normal vectors, which means they will stay still without rotating along with the object.   