   Shadow mapping: culling away unnecessary shadow casters
  Hi,in my experimental 3D engine I've got shadow mapping implemented for basic light types (point, spot, directional). However, right now I render every shadow caster visible in a light's shadow camera frustum, even if it does not actually contribute to the portion of light that is visible in the main camera frustum. Is there a nice way to check for each shadow caster whether it actually projects to a visible part of the view, when the following are known?- shadow caster's bounding box- main camera frustum- shadow camera frustumMy math kind of sucks so apologies in advance if this is something simple and obvious :) And note that I don't want to take into account shadow receiver geometry at all, so I'll be perfectly happy with false positives, as long as I get some rough culling.[Edited by - AgentC on May 6, 2010 11:01:58 AM]
  Perhaps (and this is just a guess, but I'm not too experienced on this subject) you can do a ray cast from the shadow caster to check onto what object it's going to cast its shadow on.Then, you can check if the object that is receiving the shadow is within your frustum or not, this can of course already be done in a seperate step like with spatial partitioning etc.It might be a bit bulky, but it will certainly beat creating a shadow map! :)   ;  Thanks, that is indeed one idea worth considering. Unfortunately I'm testing with scenes that have some large objects (like a whole ground mesh) so the raycast would undoubtedly return the ground mesh most of the time, while only a small part of it would be visible :)Right now I'm experimenting with a method where I transform both the main camera frustum & the shadow caster bounding box into the light's view space, then extrude the bounding box up to the far clip distance of the light, and finally check the extruded box against the transformed frustum. That seems to work, and depending on the view angle it manages to cull the shadow casters quite nicely. However, what gets me a bit suspicious is that so far I'm not taking into account the shadow projection at all (ortho for directional light, perspective otherwise.) Must keep testing...   ;Quote:Original post by AgentCThanks, that is indeed one idea worth considering. Unfortunately I'm testing with scenes that have some large objects (like a whole ground mesh) so the raycast would undoubtedly return the ground mesh most of the time, while only a small part of it would be visible :)Right now I'm experimenting with a method where I transform both the main camera frustum & the shadow caster bounding box into the light's view space, then extrude the bounding box up to the far clip distance of the light, and finally check the extruded box against the transformed frustum. That seems to work, and depending on the view angle it manages to cull the shadow casters quite nicely. However, what gets me a bit suspicious is that so far I'm not taking into account the shadow projection at all (ortho for directional light, perspective otherwise.) Must keep testing...About the whole ground mesh: you should always chop up your landscapes into several sections, or chunks. This way you avoid rendering pieces of land that aren't visible. You should never treat it as a whole. If you do that, you can easily get away with the ray casting.   ;  Yeah, you're very right of course :)To my reply above: the transformation can naturally also be done to the light's projective space as well, that way it should behave with any projection. For some odd reason I first thought an ortho projection would squish the main frustum flat, and because of that I was afraid to do that at first..   ;  Ray casting seems like a bad idea, since it is likely to miss many objects(unless you cast a huge number of rays).Using extrusion, while it will work, I would guess that it will probably be slower than actually drawing the objects in a lot of cases. Although some smart choices for this could be beneficial. It would also need to be an inscribed bounding box, again to prevent incorrect results.[Edit] Not sure what you are trying to do with the extrusion, I would guess using the extruded frustra to cull other shadow casters within the shadow of the larger one.Spliting large occluders/casters makes sense. This will allow smaller light frustra to be computed and drawing less for objects partially in the light frustra. Except perhaps you run the risk of costing more due to worse batching(modern GPUs have a lot of vertex/cull throughput).Also, you could try applying something like occlusion queries to the light(or whatever you use for your main scene occlusion handling) to reduce overdraw. See http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter06.html. If this is a win for lights will depend on how much the light moves and the range of the light etc.Not everything will need to cast shadows, so things like translucent objects(eg grass), flat ground objects, sky box objects can be flagged and then culled from rendering into the shadow map.You can also use very low LODs and cull things which are only a couple of pixels(esp if you blur your shadow maps). This is probably especially useful for low resolution/wide shadow maps.I am sure there are more ways to improve shadow performance, but those are the things I have or am considering...:-)David   ;  Thanks for everyone's responses so far. My initial assumptions were unoptimal; one should indeed take the scene objects into account, because using the whole camera view frustum is wasteful in respect to these checks (especially when looking toward the ground.)However, I do that in the simplest way possible: when initially getting the set of visible objects, I find out the furthest viewspace Z-coordinate from the objects' bounding boxes. This value allows to generate a reduced view frustum for the shadowcaster visibility checks, instead of using the full view frustum.Furthermore, for directional lights, one does not need to extrude the shadowcasters' bounding boxes all the way to the light's far clip distance. Instead, each can be extruded (in the light's direction) only by its distance to the reduced view frustum's far plane, resulting in less false positives.To make things clearer, here's a diagram of the extrusion method (a camera looks down upon a ground mesh; out of the 4 box-shaped meshes only one casts a visible shadow)Diagram