Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Shadow Map shader function issue

Started by LevyDee Nov 20, 2011 at 10:16 PM 2 replies 800+ views
Original Post
LevyDee
LevyDee
I am attempting to implement so basic shadow mapping, and am still in the process of understanding it. I had started a post a while back, but it has been a while since I have been able to response to the old one, so I figured I should start a new topic.

As far as I can tell, my depth map is fine. I have checked it out in PIX and everything looks great, but there is some logic flaw in my shader functions, and was hoping for somebody to take a quick look at it. Thanks!


float4x4 world : WORLD;

//Camera view and projection
float4x4 cView : VIEW;
float4x4 cProj : PROJECTION;

//Light view and projection
float4x4 lView : VIEW;
float4x4 lProj : PROJECTION;

texture tShadowMap;
texture tObject;

//TODO set in my render loop
float maxLightRange = 300.0f;

sampler ShadowMapSampler =
sampler_state
{
Texture = <tShadowMap>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

sampler ObjectSampler =
sampler_state
{
Texture = <tObject>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

void vsRenderShadowMap(float4 iPos : POSITION0,
out float4 oPos : POSITION0,
out float oDepth : TEXCOORD0)
{
float4 vLightWorldPos = mul(iPos, world);
float4 vLightViewPos = mul(vLightWorldPos, lView);
oPos = mul(vLightViewPos, lProj);

oDepth = oPos.z;
}

float4 psRenderShadowMap(float iDepth : TEXCOORD0) : COLOR0
{
//Make depth range from 0.0f to 1.0f
return float4((iDepth / maxLightRange), (iDepth / maxLightRange), (iDepth / maxLightRange), 1.0f);
}

void vsRenderSceneWithLight(float4 iPos : POSITION0,
float4 iNormal : NORMAL,
float2 iUV : TEXCOORD0,
out float4 oPos : POSITION0,
out float2 oUV : TEXCOORD0,
out float4 oLightPos : TEXCOORD1,
out float oDepth : TEXCOORD2)
{
//Get the vertices world position
float4 vWorldPos = mul(iPos, world);

//Transform the position to the camera view and perspective
float4 vViewPos = mul(vWorldPos, cView);
oPos = mul(vViewPos, cProj);

//Get the pixel depth from the view of the camera in range 0.0f to 1.0f
oDepth = (oPos.z / maxLightRange);

//Transform the position to the light view and perspective
float4 lViewPos = mul(vWorldPos, lView);
oLightPos = mul(lViewPos, lProj);

oUV = iUV;
}

float4 psRenderSceneWithLight(float2 iUV : TEXCOORD0,
float4 iLightPos : TEXCOORD1,
float iDepth : TEXCOORD2) : COLOR0
{
//Get UV coordinates for sampling depth map
iLightPos.xyz /= iLightPos.w;
float2 depthUV = iLightPos.xy * float2(0.5f, -0.5f) + 0.5f;

//Sample depth map using new UV coordinates
float lDepth = tex2D(ShadowMapSampler, depthUV).x;

//Returning test color before including my texture colors
if(iDepth >= lDepth)
return float4(0.0f, 0.0f, 0.0f, 1.0f);

else
return float4(1.0f, 1.0, 1.0f, 1.0f);

return float4(1.0f, 0.0f, 0.0f, 1.0f);

}

void vsRenderSceneWithoutLight(float4 iPos : POSITION0,
float2 iUV : TEXCOORD0,
out float4 oPos : POSITION0,
out float2 oUV : TEXCOORD0)
{
float4 vWorldPos = mul(iPos, world);
float4 vViewPos = mul(vWorldPos, cView);
oPos = mul(vViewPos, cProj);

oUV = iUV;
}

float4 psRenderSceneWithoutLight(float2 iUV : TEXCOORD0) : COLOR0
{
return tex2D(ObjectSampler, iUV);
}

//Render a shadow map
technique RenderShadowMap
{
pass p0
{
VertexShader = compile vs_2_0 vsRenderShadowMap();
PixelShader = compile ps_2_0 psRenderShadowMap();
}
}

//Render the scene as if all objects were lit.
technique RenderSceneWithoutLight
{
pass p0
{
VertexShader = compile vs_2_0 vsRenderSceneWithoutLight();
PixelShader = compile ps_2_0 psRenderSceneWithoutLight();
}
}

//Must provide a shadow map
technique RenderSceneWithLight
{
pass p0
{
VertexShader = compile vs_2_0 vsRenderSceneWithLight();
PixelShader = compile ps_2_0 psRenderSceneWithLight();
}
}
L. Spiro
L. Spiro
What result are you getting? That would help us to have an idea as to which part of the code to study more carefully.


L. Spiro
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
LevyDee
LevyDee
Its a little weird actually. If I calculate my view and projection matrices for my light at position 0.0f, x, 0.0f, every thing is black as if it is all in shadows. Other wise, half of my objects turn out white, and the rest black. If I move my object around, if I enter the black area, my object becomes black. It's a little vague of a description, but not sure how else to describe it.
L. Spiro
L. Spiro
Then post screenshots.
Show how you calculated the matrices.
Your way of calculating the depth is abnormal. I can't put enough time into this to determine if it is actually "correct" in that it should work, but typically depth is determined by just taking the position divided by its W component. Specifically, when generating the depth values for the shadow map, you would divide by the W of the component, not by the light range (even though these could yield the same results of your projection matrix is 300 units deep).

The DirectX SDK has a shadow mapping sample which shows a more conventional way to perform shadow mapping.


L. Spiro
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.