How to use a single shadow map for multiple point light sources?
Asked Answered
E

1

7

As I understand it, shadow-mapping is done by rendering the scene from the perspective of the light to create a depth map. Then you re-render the scene from the POV of the camera, and for each point (fragment in GLSL) in the scene you calculate the distance from there to the light source; if it matches what you have in your shadow map, then it's in the light, otherwise it's in the shadow.

I was just reading through this tutorial to get an idea of how how to do shadow mapping with a point/omnidirectional light.

Under section 12.2.2 it says:

We use a single shadow map for all light sources

And then under 12.3.6 it says:

1) Calculate the squared distance from the current pixel to the light source.
...
4) Compare the calculated distance value with the fetched shadow map value to determine whether or not we're in shadow.

Which is roughly what I stated above.

What I don't get is if we've baked all our lights into one shadow map, then which light do we need to compare the distance to? The distance baked into the map shouldn't correspond to anything, because it's a blend of all the lights, isn't it?

I'm sure I'm missing something, but hopefully someone can explain this to me.


Also, if we are using a single shadow map, how do we blend it for all the light sources?

For a single light source the shadow map just stores the distance of the closest object to the light (i.e., a depth map), but for multiple light sources, what would it contain?

Elsie answered 12/3, 2012 at 2:15 Comment(0)
E
5

You've cut the sentence short prematurely:

We use a single shadow map for all light sources, creating an image with multipass rendering and performing one pass for each light source.

So the shadow map contains the data for a single light source at a time but they use only one map because they render only one light at a time.

I think this flows into your second question — light is additive so you combine the results from multiple lights simply by adding them together. In GPU Gems' case, they add together directly in the frame buffer, no doubt because of the relatively limited number of storage texture samplers available on GPUs at the time. Nowadays you probably want to do a combination of combining in the frame buffer and directly in the fragment shader.

You also generally apply the test of "pixel is lit if it's less than or equal to the distance in the shadow buffer plus a little bit" rather than exactly equal, due to floating point rounding error accumulation.

Eccrine answered 12/3, 2012 at 2:55 Comment(4)
I thought the rest of that sentence meant simply that they built the shadow map by rendering to the same map (perhaps additively) for each light source; not that they clear and re-use it. That makes more sense now... it's a shame though -- I was really hoping I could get away with one shadow map :pElsie
You kind of can if you flip things around and use what is often called deferred rendering. You render the scene from the camera to output depth + normals + some surface information per pixel then for each light you draw a full screen quad that effectively iterates across the camera rendering. Even if your GPU can only output 8bit RGBA (still common in the mobile world) you can do the first bit to multiple textures in multiple passes.Eccrine
Great. You just opened a whole new can of worms for me. I'm making a game, and most of the objects are static (or rarely change), so I was thinking of precomputing the shadow maps for all the static objects, keeping a copy, and updating it for just the moving objects. I want to support as many lights as possible, so I don't think re-rendering the scene 100 times from different angles 60 times per second is a possibility for me. But a shadow map uses up an active texture, doesn't it? I have to bind all of them at once for the final rendering pass, don't I?Elsie
The final pass only needs to be those lights not yet accumulated in the frame buffer. If your objects and lights were completely static and you have enough storage, you could prepare partial light maps consisting of just the static objects to which to add the others each frame. Though it sounds like deferred might be faster — especially if any of your lights have distance cut offs well within the frame (in which case they needn't be applied to every pixel).Eccrine

© 2022 - 2024 — McMap. All rights reserved.