Jump to content
  • Advertisement
Sign in to follow this  
howie_007

Help me understand shadow mapping

This topic is 2562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just need some help connecting the dots. I've read a bunch of examples on this subject but they all assume prior knowledge. I just want to understand (high level) it's most basic implementation.

This is my understanding.

1) Render shadow map to a float buffer from the lights view.

Question 1): Is this buffer the same size of the display buffer?
Question 2): The examples show a light source image representation of this buffer but it looks like a normal image, just from a different view. How is this distorted into the scene from camera view? Shadows always look elongated or squished based on the light source.

2) Is this the order in which things are done?

Render scene without lights
Clear Z buffer
Render to shadow buffer
Render scene based on shadow buffer (somehow)

As you can see, I'm rather confused.

Share this post


Link to post
Share on other sites
Advertisement

I just need some help connecting the dots. I've read a bunch of examples on this subject but they all assume prior knowledge. I just want to understand (high level) it's most basic implementation.

This is my understanding.

1) Render shadow map to a float buffer from the lights view.

Question 1): Is this buffer the same size of the display buffer?
Question 2): The examples show a light source image representation of this buffer but it looks like a normal image, just from a different view. How is this distorted into the scene from camera view? Shadows always look elongated or squished based on the light source.

2) Is this the order in which things are done?

Render scene without lights
Clear Z buffer
Render to shadow buffer
Render scene based on shadow buffer (somehow)

As you can see, I'm rather confused.


1)

q1) the shadow map and display buffer sizes are unrelated, shadow map size relates to the shadow detail precision on screen.
q2) the shadow map is a depth buffer rendered from point of view of the light. Simply put, you can consider the light source as a projector which projects the shadowmap in the 3d scene. Each screen pixel is transformed to the shadow maps space to perform simple test "is pixel closer to the light than the shadow maps depth value, if so, the pixel is lit"

2)

Simple deferred rendering goes something like this:

- Clear shadow (depth) map, Render shadow map from each lights point of view
- Clear color, normal, depth buffers
- Render color, normal, depth to render targets
- Render lights to light accumulation buffer with their shadow maps
- Combine color and light buffers
- Show image

Best regards!

Share this post


Link to post
Share on other sites

- Render color, normal, depth to render targets


Are you saying this is where the scene is rendered in ambient lighting to simulate the shadowed area?

Share this post


Link to post
Share on other sites
Here is a link to a basic tutorial for XNA but the same basic principles still apply:
http://www.riemers.n.../Shadow_map.php


The basic theory behind shadow mapping is simple. Anything the light will get too, or that the light can see, will be lit. That means that the inverse which is everything that the light cannot reach or cannot see, will be dark. To do this in our 3d world we need to be able to compare what the light can, and cannot see. So we take a snapshot of our scene from the lights point on view and we record the distance from each pixel to the light, for comparison later, to our shadow buffer. When we go to finally rendering our scene we take the 3d position of each pixel, and transform it, not using the normal world/view/projection matrices but the lights view/projection matrices. This tells us where the 3d point will be on the 'shadow map'. After that we can compare depths to see if the point should be in shadow. This is because the light will only record the depth of the closest face so any other pixel hidden behind this will have a much higher depth that the one recorded on the 'shadow map'. That's the basic theory. It takes a little maths (which can be found at the linked site with a little digging) to get everything how you need it for the comparison but that's not too complicated.

Share this post


Link to post
Share on other sites
Yeah, I've read that one as well as a bunch of others. Parts of these examples are magic as to why they work.

Your explanation helps tie up some loose ends for me.

Share this post


Link to post
Share on other sites

[quote name='kauna' timestamp='1323200819' post='4891191']
- Render color, normal, depth to render targets


Are you saying this is where the scene is rendered in ambient lighting to simulate the shadowed area?
[/quote]

Shadow is lack of light and in this case lack of directional light. Ambient light simulates light coming from every direction so the calculated shadowing value doesn't affect the ambient light.

You may fill your ambient light to the light buffer at any point before combining the final image. So you may do it while "Rendering color, normal, depth to render targets".
In my configuration I calculate the ambient factor when calculating the sun light so the ambient is part of "[color=#1C2837][size=2]Render lights to light accumulation buffer with their shadow maps"

of course, you may have totally different method for calculating ambient light so there isn't one correct answer for it.

Best regards!

Share this post


Link to post
Share on other sites
I think I almost got it now. These discussions and everyone's input just needed to roll around my head for a while.

What I also realize (i think) is that the light source can't be real far away. This will make the rendering of the items in the shadow map real small, thus the final shadows in the scene real course. Seems like what you would want to do is, render the shadow map as big as possible. May even want the shadow map buffer to be as big as the render resolution or more, perhaps?

Also, when rendering to the shadow map buffer, does the scene need to be rendered back faced due to z fighting or is that no longer needed?

Share this post


Link to post
Share on other sites

I think I almost got it now. These discussions and everyone's input just needed to roll around my head for a while.

What I also realize (i think) is that the light source can't be real far away. This will make the rendering of the items in the shadow map real small, thus the final shadows in the scene real course. Seems like what you would want to do is, render the shadow map as big as possible. May even want the shadow map buffer to be as big as the render resolution or more, perhaps?

Also, when rendering to the shadow map buffer, does the scene need to be rendered back faced due to z fighting or is that no longer needed?


Directional lights are "infinitely far away" and shadow mapping works fine with them, since the perspective doesn't make far away things smaller or close things bigger. Shadow mapping spot lights / Omni lights on the other hand will have issues with distance, just as you described. Of course, people use things like cascaded shadow maps to improve the shadow quality where it matters.

You may try shadow maps with different sizes to find good performance vs image quality. Also, there are lots of ways to improve shadow map quality on screen without using really huge big shadow map size.

There are discussions about if it is good to render back faces instead of front faces. if I remember correctly the latest post I read about the matter was that rendering front faces is better for reducing "peter-panning" and rendering back faces is better for reducing surface-acne. So both have good and bad sides.

Cheers!

Share this post


Link to post
Share on other sites

...rendering front faces is better for reducing "peter-panning" and rendering back faces is better for reducing surface-acne.


I believe I've seen "surface-acne" but what is "peter-panning"?

Share this post


Link to post
Share on other sites

[quote name='kauna' timestamp='1323268360' post='4891456']
...rendering front faces is better for reducing "peter-panning" and rendering back faces is better for reducing surface-acne.


I believe I've seen "surface-acne" but what is "peter-panning"?
[/quote]

Peter-panning stands for objects that seem to float although they aren't supposed to.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!