Original Post
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!
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();
}
}