Deferred shadow map

703835f9b65163f460b30bd46bc2ff87
0
XVincentX 101 Jun 23, 2009 at 14:46

Hello!
I have got a scene completely drawn using GBuffers and it’s ok, and now it’s time to apply shadows.
At first i made the usual ZOnly pass and set it as a texture.

http://img44.imageshack.us/img44/4048/porcesco.jpg

This is a projection of light on the scene, and it is not good, but really (i’m working too much) i do not remember what’s going wrong.

I tryed to fill the shadowmap from FillGBufferShader, but it’s impossible becouse it was required to output different projected position (GeometryShader and Texture2DArray) and different colors(Multiple Render Target) ,but D3D10 supports one of this for Draw call.

So at first i fill GBuffers (all ok), and after i draw using a ZOnly pass.
This pass is performed in D3D10 using a Texture2D with R32_TYPELESS and bound in 2 pipeline objects (Shader Resource View and DepthStencilView) using D3D10 casts to R32_FLOAT and D32_FLOAT.

So there is no render target bound in ZOnly pass, as this code says.

    device->OMSetRenderTargets(5,&SRenderTarget[1],ShadowDS);

SRenderTarget is a NULL array.
The Shadow Map has got SAME backbuffer’s size, so there is no projection to change.
Now that i have got ShadowMap and ALSO GBuffers, i have to perform rendering on FrameBuffer.

Current code is this:

float4 ps_draw(in float4 Pos:SV_POSITION, in float2 Tex:TEXCOORD)   :   SV_TARGET
{
    float4 TexC = BaseColor.Sample(SamplText,Tex);
    float3 Norm = 2.0f * Normal.Sample(SamplText,Tex).xyz - 1.0f;
    float NdotL = max(0.0f,dot(Norm,normalize(LightPointFrom.xyz-LightPointTo.xyz).xyz));
    
    float4 V = (EyePosition - Position.Sample(SamplText,Tex));
    float3 R = reflect(Norm.xyz,normalize(LightPointFrom.xyz-LightPointTo.xyz).xyz);
    float Specular = max(0.0f,dot(R,V));
    
    float Shadow = Shadow1.Sample(SamplText,Tex).x;
    float3 LightToVec = Position.Sample(SamplText,Tex) - LightPointFrom.xyz;
    float LightToVecDist = length(LightToVec);
    
    int ShadowValue = step(LightToVecDist/100.0f,Shadow);
    
    return (Shadow) * TexC * ShadowValue;

}

It uses a simple Phong Shading.
In return statement i do a mul with shadow to make sense of where shadow was projected (as you can see from first image).

After some hours thinking, i noticed about a my mistake in texture projection. So i modified code in this way but it’s still completely wrong.

float4 ps_draw(in float4 Pos:SV_POSITION, in float2 Tex:TEXCOORD)   :   SV_TARGET
{
    float4 TexC = BaseColor.Sample(SamplText,Tex);
    float3 Norm = 2.0f * Normal.Sample(SamplText,Tex).xyz - 1.0f;
    float NdotL = max(0.0f,dot(Norm,normalize(LightPointFrom.xyz-LightPointTo.xyz).xyz));
    
    float4 V = (EyePosition - Position.Sample(SamplText,Tex));
    float3 R = reflect(Norm.xyz,normalize(LightPointFrom.xyz-LightPointTo.xyz).xyz);
    float Specular = max(0.0f,dot(R,V));
    
    float4 NewPos = mul(mul(Position.Sample(SamplText,Tex),LightView),ProjMatrix);
    NewPos /= NewPos.w;
    
    float Shadow = Shadow1.Sample(SamplText,NewPos.xy).x;
    float3 LightToVec = Position.Sample(SamplText,Tex) - LightPointFrom.xyz;
    float LightToVecDist = length(LightToVec);
    
    int ShadowValue = step(LightToVecDist/100.0f,Shadow);
    
    return ShadowValue * TexC;

}

For now, please, do not look these problems (/100.0f, or epsilon in step function) becouse the problem is not there.
Look at shader’s code return.

return (Shadow) * TexC;

TexC is BaseColor and Shadow is the sample from shadow map. What i’m doing now (only for test purposes) is to project shadow with the scene to understand where map is placed in comparision to scene.

And, it is not right, so the problem it’s above.

Using original texcoords:

http://img44.imageshack.us/img44/4048/porcesco.jpg

with this code:

    float4 PSpot2D = mul(mul(Position.Sample(SamplText,Tex),LightView),ProjMatrix);
    
    float Shadow = Shadow1.Sample(SamplText,PSpot2D).x;

http://img14.imageshack.us/img14/2521/67551848.jpg

using this other one

    float4 PSpot2D = mul(mul(Position.Sample(SamplText,Tex),LightView),ProjMatrix);

    float2 VTexC = float2(PSpot2D.x/PSpot2D.w * 0.5f + 0.5f,-PSpot2D.y/PSpot2D.w * 0.5f + 0.5f);
    float Shadow = Shadow1.Sample(SamplText,PSpot2D).x;

http://img15.imageshack.us/img15/1271/51704351.jpg

Remember: THESE are NOT results of Shadow Lookup and comparision with camera distance. It’ JUST a projection of Depth surface on the screen in order to verify it’s position in comparision with original scene.
Really going crazy.

Thank you all for help.

0 Replies

Please log in or register to post a reply.

No replies have been made yet.