Need some help with shadow mapping

Started by
7 comments, last by antmck2013 11 years ago

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??

Advertisement

Right, so no-one has an idea what's going on then? Guess I'm never going to finish this - damn.sad.png

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

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.

VladR My 3rd person action RPG on GreenLight: http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

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

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 ?

VladR My 3rd person action RPG on GreenLight: http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

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.

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).

I only have terrain. I want to get the self shadowing working. I used code from tutes, so I assumed it would work.

I've looked at tutes on http://www.riemers.net and http://takinginitiative.net/2011/05/15/directx10-tutorial-10-shadow-mapping/

This topic is closed to new replies.

Advertisement