Sign in to follow this  

help with shadow map

Recommended Posts

VISQI    113
So, i am trying to render a simple scene with "Tiny" as an animated skinned mesh in it with shadows. The shadow map itself is good(checked from PIX), and tiny and the scene are getting rendered fine but when i implement the shadow mapping code in the FX file, everything goes wrong.

here is a screen shot to describe the situation

Without shadow code in FX:

With shadow code in FX:

P.S: The spot light has a position of D3DXVECTOR3(125.0f,50.0f,0.0f) and is looking at D3DXVECTOR3(0.0f,0.0f,0.0f) which is where tiny and the scene are.

here is the FX file for rendering the scene(the FX file for rendering the skinned mesh is identical, just some extra code for blending the skinned mesh)

static const float MAP_SIZE = 512.0f;
static const float SHADOW_EPSILLON = 0.00002f;

vOut VShader(float3 PosL : POSITION0
, float3 NormL : NORMAL0
, float2 TexC : TEXCOORD0)
vOut V = (vOut)0;

V.PosH = mul(float4(PosL, 1.0f), gMatWVP);
V.NormW = mul(float4(NormL, 0.0f), gMatITW);
V.PosW = mul(float4(PosL, 1.0f), gMatW).xyz;
V.EyeVecW = gEyePosW - V.PosW;

V.TexC = TexC;

V.ShadowPos = mul(float4(PosL,1.0f), gShadowWVP);
return V;

float4 PShader(float3 EyeVecW: TEXCOORD0
, float3 NormW : TEXCOORD1
, float3 PosW : TEXCOORD2
, float2 TexC : TEXCOORD3
, float4 ShadowPos : TEXCOORD4) : COLOR0
//Shadow Code /= ShadowPos.z;
float2 ShadowTexC = ShadowPos.xy;
ShadowTexC.x = 0.5f*ShadowTexC.x + 0.5f;
ShadowTexC.y = -0.5f*ShadowTexC.y + 0.5f;

float Depth = ShadowPos.z;
float dx = 1.0f/MAP_SIZE;
float2 TexelPos = ShadowTexC * MAP_SIZE;
float2 lerps = frac(TexelPos);

float c1 = (tex2D(sShadowMap, ShadowTexC).r + SHADOW_EPSILLON < Depth) ? 0.0f : 1.0f;
float c2 = (tex2D(sShadowMap, ShadowTexC + float2(dx, 0.0f)).r + SHADOW_EPSILLON < Depth) ? 0.0f : 1.0f;
float c3 = (tex2D(sShadowMap, ShadowTexC + float2(0.0f, dx)).r + SHADOW_EPSILLON < Depth) ? 0.0f : 1.0f;
float c4 = (tex2D(sShadowMap, ShadowTexC + float2(dx, dx)).r + SHADOW_EPSILLON < Depth) ? 0.0f : 1.0f;

float ShadowValue = lerp(
lerp(c1,c2, lerps.x),
lerp(c3,c4, lerps.x),

//Lighting Stuff
float3 LightVecW = normalize(PosW - gLight.PosW);
float3 LightDirW = gLight.DirW;
NormW = normalize(NormW);
EyeVecW = normalize(EyeVecW);

float3 RefLightVec = reflect(LightVecW, NormW);

float d = max(dot(NormW, -LightVecW), 0.0f);
float s = pow(max(dot(EyeVecW, RefLightVec), 0.0f), gMtrl.SpecPower);
float Spot = pow(max(dot(LightVecW,gLight.DirW),0.0f), gLight.Power);

float4 TexColor = tex2D(sTex, TexC);

float3 Diff = d * (gMtrl.Diff.rgb * gLight.Diff.rgb);
float3 Amb = (gMtrl.Amb * gLight.Amb);
float3 Spec = s * (gMtrl.Spec * gLight.Spec).rgb;

float3 FinalColor = (Diff+Amb)*TexColor.rgb + Spec;

return float4(FinalColor, gMtrl.Diff.a*TexColor.a);
i do a 2x2 filtering to eliminate stair stepping in the shadows.

Thank you very much

Share this post

Link to post
Share on other sites
programci_84    336
Change this line (1st line of your pixel shader):
[code]//Shadow Code /= ShadowPos.z;[/code]

to this:

[code]//Shadow Code /= ShadowPos.w;[/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