   Problem with ShadowMap: shadow moves
  Hi, everyone. I'm new to the forum, this is my first post.I'm a game developer/programmer, I have a student project that involves lighting and shadowing. The technique I must use is ShadowMapping.I have it almost done copying stuff from examples from the web but I didn't quite actually accomplished the effect. The main problem is that the shadow moves along with the light's position or something. The image projected is correct but it should stay still as I move the light.I reviewed/debugged the code for weeks now but if it's something wrong I guess is in the technique itself and I just don't see where.If anyone can help I'd really appreciate it thanks. I'll leave the code of the vertex and pixel shader and a link with a video of what it looks like.Thanks in advance==================================================================================VIDEO: ">==================================================================================VERTEX SHADER://-----------------------------------------------------------------------------// VM: Lighting Vertex Shader// This shader only process a vertex and sends it out to the PS// The actual lighting is done on the PS//-----------------------------------------------------------------------------float4x4 gWorldViewProj;float4x4 gWorldView;float4x4 gWorld;float4x4 gProjection;float4x4 gInverseView;float4x4 gCameraView;float4x4 gCameraProj;float4x4 gLightView;float4x4 gLightProj;float4 gLightDir;float4 gLightPos;//-----------------------------------------------------------------------------// VM: Color variablesfloat4 gEmissive = float4( 0.85f, 0.05f, 0.05f, 1.0f );float4 gAmbient = float4( 0.1f, 0.8f, 0.8f, 1.0f );float4 gSpecular = float4( 1.0f, 1.0f, 1.0f, 1.0f );float4 gAmbientControl = float4( 0.1f, 0.1f, 0.1f ,1.0f );float gShininess = 50;//-----------------------------------------------------------------------------struct VS_INPUT{float3 vPosition: POSITION;float3 vNormal: NORMAL;float2 vTexCoord: TEXCOORD0;};//-----------------------------------------------------------------------------struct VS_OUTPUT{float4 vPosition: POSITION;float4 vColor: COLOR0;float2 LightColor: COLOR1;float2 TextureUV: TEXCOORD0;float4 vPosProj: TEXCOORD1;float4 LightTexC: TEXCOORD2;};//-----------------------------------------------------------------------------VS_OUTPUT main( VS_INPUT IN ){VS_OUTPUT OUT = (VS_OUTPUT) 0;// VM: Lighing calculation// Positionfloat4 tPos = float4(IN.vPosition, 1.0f);// Power for specularfloat4 tPower = mul( tPos, gWorld );// Normal (for specular and diffuse)float3 tNormal = mul(IN.vNormal, (float3x3) gWorld);// VM: Light coord where we get the coord to project the shadowmap...// VM: Leave the Normal on Negative!!OUT.LightTexC = float4(mul(-tNormal, (float3x3) gLightView),1.0);// Output position    OUT.vPosition = mul( tPos, gWorldViewProj );// VM: Without this the PS draws nothing!    OUT.vPosProj = mul( tPos, gWorldViewProj );// Output color    OUT.vColor = gEmissive + gAmbientControl * gAmbient; //IN.color0;// Diffusefloat diffuse = dot(-tNormal, gLightDir);OUT.LightColor.x = max(diffuse, 0);// avoid negative// Specular// The Vn vector points from the eyepoint to the current vertex (Pw).// The Inverse View column 3 has the Camera eyefloat Vn = normalize( tPower.xyz - gInverseView[3].xyz );// The Hn vector is the sum of the Vn vector and the light vector.float Hn = normalize( Vn + gLightDir );// The specular basis is the dot product of the Hn vector with // again the negative of the normal vector.float specularBasis = dot ( Hn, -tNormal);specularBasis = max( specularBasis, 0 );float specularStrength = pow(specularBasis, gShininess);    OUT.LightColor.y = specularStrength;// Other output    OUT.TextureUV = IN.vTexCoord;    return OUT;}//-----------------------------------------------------------------------------==================================================================================PIXEL SHADER://-----------------------------------------------------------------------------// VM: Lighting Pixel Shader (perPixelLighting)// This shader process the color of every fragment given a Material// and the Light position, direction and light parameters// Also mix the textures with the Grog texture for a cool effect on objects//-----------------------------------------------------------------------------samplergrog2Map;samplergrogMap;samplergShadowMap;float4x4 gWorldViewProj;float4x4 gWorldView;float4x4 gWorld;float4x4 gProjection;float4x4 gInverseView;float4x4 gCameraView;float4x4 gCameraProj;float4x4 gLightView;float4x4 gLightProj;float4 gLightDir;float4 gLightPos;//-----------------------------------------------------------------------------// VM: Color variablesfloat4 gSpecular = float4( 1.0f, 1.0f, 1.0f, 1.0f );//-----------------------------------------------------------------------------struct PS_INPUT{float4 vPosition: POSITION;float4 vColor: COLOR0;float2 LightColor: COLOR1;float2 TextureUV: TEXCOORD0;float4 vPosProj: TEXCOORD1;float4 LightTexC: TEXCOORD2;};//-----------------------------------------------------------------------------struct PS_OUTPUT{float4 color : COLOR;};//------------------------------------------------------------------------------float4 GetPositionFromLight(float4 position){float4x4 WorldViewProjection = mul(mul(gWorld, gLightView), gLightProj);return mul(position, WorldViewProjection);  }//------------------------------------------------------------------------------float4 GetPositionFromCamera(float4 position){float4x4 WorldViewProjection = mul(mul(gWorld, gCameraView), gCameraProj);return mul(position, WorldViewProjection);  }//-----------------------------------------------------------------------------PS_OUTPUT main( PS_INPUT IN ){PS_OUTPUT OUT = (PS_OUTPUT)0;float4 diffuse = tex2D( grog2Map, IN.TextureUV ) * 1.0f;// Grog texturefloat4 texColor = tex2D( grogMap, IN.TextureUV ) * 0.05f;// Original texture//float4 color1 = float4(0.8, 0.5, 0.5, 1.0);//float4 color1 = IN.color0;    float4 resultColor = IN.vColor;    resultColor.xyz += gSpecular * IN.LightColor.y;    resultColor.xyz += diffuse.xyz * IN.LightColor.x;    resultColor.w = diffuse.w;// Now, consult the ShadowMap to see if we're in shadowfloat4 tLightingPos = GetPositionFromLight(IN.vPosProj);// Get our position on the shadow map// Get the shadow map depth value for this pixel   float2 ShadowTexC = 0.5 * tLightingPos.xy / tLightingPos.w + float2( 0.5, 0.5 );ShadowTexC.y = 1.0f - ShadowTexC.y;// Depth on ShadowMapIN.LightTexC.xy = 0.5 * IN.LightTexC.xy / IN.LightTexC.w + float2( 0.5, 0.5 );IN.LightTexC.y = 1.0f - IN.LightTexC.y;float shadowdepth = tex2Dproj(gShadowMap, IN.LightTexC).x;// Check our value against the depth valuefloat ourdepth = 1 - (tLightingPos.z / tLightingPos.w);// Check the shadowdepth against the depth of this pixel// a fudge factor is added to account for floating-point errorif (shadowdepth-0.001 > ourdepth){// we're in shadow, cut the lightresultColor = float4(0,0,0,1);//resultColor.a = 0.07;// VM: Let the pixel see a little through the shadow}//-------------------------------------------------------------------------OUT.color = (resultColor + texColor);return OUT;}//-----------------------------------------------------------------------------==================================================================================   
  The projected shadows would move when the light position moves...?  I noticed the shadows casted on the 'floor' object looked a bit odd - are you sure the matrices are being set correctly for each object drawn?   ;Quote:Original post by RobMaddisonThe projected shadows would move when the light position moves...?  I noticed the shadows casted on the 'floor' object looked a bit odd - are you sure the matrices are being set correctly for each object drawn?Hi, thanks for the response. Yes, the projected shadow moves when the position of the light changes. I'm really not sure the matrices are set correctly. Maybe I have to check the light's position on its matrix ?