Sign in to follow this  
blueShadow

DirectX 9 : shadow mapping problem

Recommended Posts

blueShadow    126
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) :

[code]
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;

}
[/code]


shadowWarpMatrix variable is lightViewProjMatrix * texMatrix:
[code]

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 );
[/code]


And the lightSpacePosition variable is (in the vertex shader) :
[code]
OUT.lightSpacePosition = mul( position, lightViewProj ).z ;
[/code]


[attachment=4911:bug.png]

Share this post


Link to post
Share on other sites
Dawoodoz    461
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[i].CameraToImage._43 / (CameraSpace.z * (1 - Lights[i].ShadowOffset))) + Lights[i].CameraToImage._33;

Share this post


Link to post
Share on other sites
blueShadow    126
i use :

[code]
D3DXMatrixOrthoLH( &lightWVProjMatrix, 512.0f, 512.0f, 1.0f, 1000.0f );
[/code]


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.

Share this post


Link to post
Share on other sites
blueShadow    126
Hidden
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 ?

Share this post


Link to post
blueShadow    126
Hidden
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 ?

Share this post


Link to post
blueShadow    126
[font="arial, verdana, tahoma, sans-serif"][size="2"]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 ?[/size][/font]
[font="arial, verdana, tahoma, sans-serif"][size="2"]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.[/size][/font]

Share this post


Link to post
Share on other sites
Tom KQT    1704
[quote name='blueShadow' timestamp='1313305751' post='4848875']
i use :

[code]
D3DXMatrixOrthoLH( &lightWVProjMatrix, 512.0f, 512.0f, 1.0f, 1000.0f );
[/code]


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.
[/quote]

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.

Share this post


Link to post
Share on other sites
Dawoodoz    461
LightSpaceDepth = (Lights[i].CameraToImage._43 / (CameraSpace.z * (1 - Lights[i].ShadowOffset))) + Lights[i].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.

Share this post


Link to post
Share on other sites
blueShadow    126
[quote name='Tom KQT' timestamp='1313316551' post='4848907']
[quote name='blueShadow' timestamp='1313305751' post='4848875']
i use :

[code]
D3DXMatrixOrthoLH( &lightWVProjMatrix, 512.0f, 512.0f, 1.0f, 1000.0f );
[/code]


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.
[/quote]

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 :

[code] D3DXMatrixOrthoLH( &lightWVProjMatrix, 32.0f, 32.0f, 20.0f, 700.0f ) [/code]

But now the orientation is still not correct :( and the shadow is duplicated [img]http://public.gamedev.net/public/style_emoticons/default/sad.gif[/img] 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):
[code]

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 );
}
[/code]

[color="#1C2837"][size="2"][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 ?[/color][/size][/color]

Share this post


Link to post
Share on other sites
blueShadow    126
[quote name='Dawoodoz' timestamp='1313320310' post='4848918']
LightSpaceDepth = (Lights[i].CameraToImage._43 / (CameraSpace.z * (1 - Lights[i].ShadowOffset))) + Lights[i].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]

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)

Share this post


Link to post
Share on other sites
Dawoodoz    461
[quote name='blueShadow' timestamp='1313321970' post='4848925']
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)
[/quote]

Yes, shadow offset / bias / epsilon.

CameraSpace.z is linear depth in the light source's orthogonal camera space.

Share this post


Link to post
Share on other sites
blueShadow    126
[quote name='Dawoodoz' timestamp='1313323082' post='4848927']
[quote name='blueShadow' timestamp='1313321970' post='4848925']
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)
[/quote]

Yes, shadow offset / bias / epsilon.

CameraSpace.z is linear depth in the light source's orthogonal camera space.
[/quote]

Ok thanks for your infos :)


Share this post


Link to post
Share on other sites
blueShadow    126
[quote name='blueShadow' timestamp='1313321707' post='4848921']
[quote name='Tom KQT' timestamp='1313316551' post='4848907']
[quote name='blueShadow' timestamp='1313305751' post='4848875']
i use :

[code]
D3DXMatrixOrthoLH( &lightWVProjMatrix, 512.0f, 512.0f, 1.0f, 1000.0f );
[/code]


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.
[/quote]

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 :

[code] D3DXMatrixOrthoLH( &lightWVProjMatrix, 32.0f, 32.0f, 20.0f, 700.0f ) [/code]

But now the orientation is still not correct :( and the shadow is duplicated [img]http://public.gamedev.net/public/style_emoticons/default/sad.gif[/img] 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):
[code]

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 );
}
[/code]

[color="#1C2837"][size="2"][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 ?[/color][/size][/color]
[/quote]

[font=arial, verdana, tahoma, sans-serif][size=2]This my current result (to illustrate the message above) : [attachment=4924:bug.png][/size][/font]

Share this post


Link to post
Share on other sites
blueShadow    126
[quote name='Dawoodoz' timestamp='1313346304' post='4849055']
Don't add any light when the UV sampling the depth map is out of bound.
[/quote]


What do you mean by [b]don't add any light[/b] in this case ?

Share this post


Link to post
Share on other sites
Dawoodoz    461
Pseudo:
[code]
SumOfDiffuseLight = AmbientLight
SumOfSpecularLight = Black
For each light {
Measure distance between light source and pixel.
if close enough {
Do matrix inverse by multiplying with the transpose of an orthogonal unprojected 3x3 light space matrix
and handle the position using subtraction to convert the world space position to orthogonal light space.
Divide the sideway coordinates with the slopes and the depth to get the projected UV coordinate.
Lerp the UV to pick the current tile in your depth atlas.
if UV is inside bound {
SumOfDiffuseLight += Diffuse light from phong model
SumOfSpecularLight += Specular light from phong model
}
}
}
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this