DirectX 9 : shadow mapping problem

Started by
12 comments, last by Dawoodoz 12 years, 8 months ago
Hi everybody !
My shadow mapping is not correct : with some scenes i get the shadow somewhere but not at the right place no matter what is the value of the epsilon, and with some others a big part of the scene is in the dark and the other not...
I have only one spot light in my scene.

here is my code which does it (with explanations below) :


float shadowing(psIn IN)
{

float4 texCoord = mul( tex2D(positionMap, IN.texCoord), shadowWarpMatrix );
texCoord.xyz /= texCoord.w ;

float shadowed = (tex2D( shadowMap, texCoord ).r) > (IN.lightSpacePosition + epsilon ) ? 0.20 : 1.0 ;
return shadowed;

}



shadowWarpMatrix variable is lightViewProjMatrix * texMatrix:


float textureOffsetX = 0.5f + (0.5f / SHADOW_MAP_SIZE_X);
float textureOffsetY = 0.5f + (0.5f / SHADOW_MAP_SIZE_Y);


D3DXMATRIX texMatrix( 0.5f, 0.0f, 0.0f, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f
textureOffsetX, textureOffsetY, 0.0f, 1.0f );



And the lightSpacePosition variable is (in the vertex shader) :

OUT.lightSpacePosition = mul( position, lightViewProj ).z ;



[attachment=4911:bug.png]
Advertisement
If your light source have perspective, the depth buffer is in an inverted representation. Use scalar _33 and _43 from the projection matrix that made the depth map to convert to or from linear camera space depth.

This is the conversion from linear to inverted depth so that 4 point filter comparisons can be used:
LightSpaceDepth = (Lights.CameraToImage._43 / (CameraSpace.z * (1 - Lights.ShadowOffset))) + Lights.CameraToImage._33;
i use :


D3DXMatrixOrthoLH( &lightWVProjMatrix, 512.0f, 512.0f, 1.0f, 1000.0f );



Should I use what you told me ?
Can you explain me a little more the code you wrote ? I have problems to understand what are the variables.
this is my shadow map, I have a white part at the bottom, do you know why ? [attachment=4919:shadow map.bmp]

This shadow map gives me this rendering : [attachment=4920:bug.png]

The shadow is at the wrong place :( I don't know why... Do you see what is uncorrect ?
this is my shadow map, I have a white part at the bottom, do you know why ? [attachment=4921:shadow map.png]

This shadow map gives me this rendering : [attachment=4920:bug.png]

The shadow is at the wrong place :( I don't know why... Do you see what is uncorrect ?
[font="arial, verdana, tahoma, sans-serif"]this is my shadow map, I have a white part at the bottom, do you know why ? [attachment=4922:shadow map.bmp]

This shadow map gives me this rendering : [attachment=4923:bug.png]

The shadow is at the wrong place :( and is not oriented correctlty, I don't know why... Do you see what is uncorrect ?[/font]
[font="arial, verdana, tahoma, sans-serif"]The spot light is supposed to be about to the upper left corner of the screenshot, and its target must look at the bottom right corner.[/font]

i use :


D3DXMatrixOrthoLH( &lightWVProjMatrix, 512.0f, 512.0f, 1.0f, 1000.0f );



Should I use what you told me ?
Can you explain me a little more the code you wrote ? I have problems to understand what are the variables.


What scale does your scene have? I mean how large is the car - 0.1 units or 1 unit or 10 units or 100 units...?
My point is that you should set your shadow camera as tightly around the scene as you can, especially the near and far planes, because that will limit your shadow precision and can lead to really strange behaviour in extreme cases.
LightSpaceDepth = (Lights.CameraToImage._43 / (CameraSpace.z * (1 - Lights.ShadowOffset))) + Lights.CameraToImage._33;

CameraToImage is the projection matrix that rendered the depth map.

ShadowOffset is an automatically calculated value that reduce acne. By multiplying with (1 - offset), I get more bias where the texels are larger when using perspective.

CameraSpace.z is the linear depth in the camera that rendered the depth map.

LightSpaceDepth is the non-linear depth that can be compared with the depth map using 4 point comparison filter.

[quote name='blueShadow' timestamp='1313305751' post='4848875']
i use :


D3DXMatrixOrthoLH( &lightWVProjMatrix, 512.0f, 512.0f, 1.0f, 1000.0f );



Should I use what you told me ?
Can you explain me a little more the code you wrote ? I have problems to understand what are the variables.


What scale does your scene have? I mean how large is the car - 0.1 units or 1 unit or 10 units or 100 units...?
My point is that you should set your shadow camera as tightly around the scene as you can, especially the near and far planes, because that will limit your shadow precision and can lead to really strange behaviour in extreme cases.
[/quote]

I reduced the values to fit correctly to the car :

D3DXMatrixOrthoLH( &lightWVProjMatrix, 32.0f, 32.0f, 20.0f, 700.0f )

But now the orientation is still not correct :( and the shadow is duplicated sad.gif The width of my car is of 5 units like say 3ds max but when i put these values, the car fits in width the shadow map.
Am I doing something wrong in the shaders ?

pixel shader which renders the shadow map (there are questions below):


float4 pixel_main( VSOUTPUT_SHADOW IN ) : COLOR0
{
IN.fDepth.x /= IN.fDepth.y ;// where x is in fact 'z' and y the 'w' coordinate of the position of the pixel in light space, is it right ?? Or am I supposed to divide by the far clip value ?
// Output the scene depth
return float4( IN.fDepth.x, IN.fDepth.x, IN.fDepth.x, 1.0f );
}


[color="#1C2837"][color="#880000"]where x is in fact 'z' and y the 'w' coordinate of the position of the pixel in light space, is it right ?? Or am I supposed to divide by the far clip value ?

LightSpaceDepth = (Lights.CameraToImage._43 / (CameraSpace.z * (1 - Lights.ShadowOffset))) + Lights.CameraToImage._33;

CameraToImage is the projection matrix that rendered the depth map.

ShadowOffset is an automatically calculated value that reduce acne. By multiplying with (1 - offset), I get more bias where the texels are larger when using perspective.

CameraSpace.z is the linear depth in the camera that rendered the depth map.

LightSpaceDepth is the non-linear depth that can be compared with the depth map using 4 point comparison filter.


ShadowOffset is what we sometimes call "epsilon" ? The value to correct the imprecision of floats ?
CameraSpace is in fact the depth of the scene ? (just to be sure :P)

This topic is closed to new replies.

Advertisement