Sign in to follow this  
antmck2013

Need some help with shadow mapping

Recommended Posts

antmck2013    113

I'm new to shadow mapping.  I've followed some tutes on the net, but can't get this working.

I've created a terrain using a heightmap and textured it comparing slope and height.  All that's working.

Then I tried getting shadows to work.  I've created a shadow map by using the orthographic projection (for the sun) - projection matrix and view matrix from the light.  I've translated the current vertex/pixel to light-space.  I then used a texture sampler to lookup depth value from the shadow map texture.  At the moment I'm just testing if a pixel is in shadow or not.  If it's in shadow I add the shadow by darkening the pixel.  I've checked the tutes and my code "seems" OK, but no shadows show up.  I've attached the shadow map the was generated and some HLSL code.

 

Vertex Shader:

...

output.LightPosition = mul(input.Position, lightWorldViewProj);

output.LightPosition /= output.LightPosition.w;

 

return output;

}

 

Pixel Shader:

...

 

float2projectedTexCoords;

 

 

projectedTexCoords[0] = input.LightPosition.x / 2.0f + 0.5f;

 

 

projectedTexCoords[1] = input.LightPosition.y / -2.0f + 0.5f;

 

 

float depthStoredInShadowMap = shadowMap.Sample(shadowMapSampler, projectedTexCoords).r;

 

float realDistance = input.LightPosition.z;

 

if ((realDistance - 1.0f / 100.0f) <= depthStoredInShadowMap)

{

colour.xyz *= 0.25f;

}

 

return colour;

}

 

 

edit:  It seems that the line float depthStoredInShadowMap = shadowMap.Sample(shadowMapSampler, projectedTexCoords).r; is always returning zero??

What is happening??

Edited by antmck2013

Share this post


Link to post
Share on other sites
phil_t    8084

It seems like you've already debugged the shader a bit, since you said the sample is always returning 0. So keep going. What could cause that?

 

Either:

- projectedTexCoords is incorrect, or

- you're sampling from the wrong (or no) texture

Share this post


Link to post
Share on other sites
VladR    722

It looks like you are trying to accomplish self-shadowing of the terrain ? I would not start with that - do some search and you will find out that it is problematic to do even if you already have your Shadow Mapping up&running correctly for all objects (except for terrain).

 

I would at first start with the shadows of other objects over the terrain, so that you can easily check the stuff in debugger.

 

Simplify things at first:

 

1. Render a simple cube above a terrain.

2. Put light source directly ABOVE the cube in a given distance.

3. Adjust / double-check your Shadow Mapping Projection Matrix - this can easily make the Shadow way too small or way too big, especially at the beginning, when you don't fully grasp the concept.

4. Check that your code for displaying Shadow Map REALLY works - bind the Shadow Map and stretch it over a terrain (or some other plane) to see that it displays correctly - put some test pixels into it at first (to double check)

5. Enhance the shadow while debugging. Don't do 0.25f*Color. Put Bright RED over a WHITE texture to make sure you are not just missing the shadow.

Share this post


Link to post
Share on other sites
antmck2013    113

As I'm using SharpDX I posted their forums and found out that I need to set the render target before binding the shadow map.

Now it renders shadows - there not quite right yet, but at least I have something to work with.smile.png

Share this post


Link to post
Share on other sites
VladR    722

Yeah, it's usually a good idea to set the RT, if you plan to render anything to it smile.png

 

What resolution are you using ? 2048x2048 ?

Share this post


Link to post
Share on other sites
antmck2013    113

I've set the size of shadow map to equal the window client size so 800x600., but the window size can be changed - that doesn't cause any problems.

I am still having trouble with shadows though.  At lease the shadow map is actually adding shadows now, but shadows still aren't right.

Maybe something to do with converting the current vertex/pixel position into light space and then into texture space??

 

I have in the vertex shader:

output.LightPosition = mul(input.Position, lightWorldViewProj);

output.LightPosition /= output.LightPosition.w;

 

And in the pixel shader:

 

 

float2 projectedTexCoords;

projectedTexCoords[0] = input.LightPosition.x / 2.0f + 0.5f;

projectedTexCoords[1] = input.LightPosition.y / -2.0f + 0.5f;

 

float shadowMapDepth = shadowMap.Sample(shadowMapSampler, projectedTexCoords).r;

 

float realDistance = input.LightPosition.z;

 

if ((realDistance - 1.0f / 100.0f) <= shadowMapDepth)

{

    colour *= 0.25;

}

 

return colour;

 

I 'm using a left handed co-ord system.

I added a screenshot.  The sun is 25deg above the east (x axis) and looking at the origin.

The camera is looking west or in the -x direction.  There should not be that much shadow.

The greyscale image in the corner is the heightmap.

Share this post


Link to post
Share on other sites
phil_t    8084

It looks like just the terrain. Like another poster mentioned, can you test your algorithm without trying self-shadowing terrain? Add an object above the terrain and see if it casts a shadow on the terrain (and don't draw the terrain to the shadow map for this test - just the object).

 

If that looks good, then you're probably in need of a small bias term for your depth comparison to get terrain-self shadowing working (if you care at all to get that working).

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