# Handle many lights in a scene (with shaders)

I'm curious about how to handle many lights in a scene. Given a very big map in a role playing game, with dungeons (with lights in there) etc. I know about deferred lighting, but that only answers how to render many lights. But my question is more: How to not render many lights. It's obviously desirable to only render the lights effecting the scene, but I don't know a technique/an algorithm to archive this. Nobody wants or is even able to render 500 point lights, just because there somewhere in the map. But one cannot just take a straight line from the light to the player and test if something is in between. The distance also doesn't seem like a good indicator. (Sunlight?)

tl;dr: How can I figure out which lights affect the scene to only render them?

-
OpenGL or DirectX? – kravemir May 28 '12 at 18:13
actually, 500 lights would be quite possible with deferred lighting, as long as you don't want shadows for all of them ;) – melak47 May 28 '12 at 18:32
For example for point and cone ligts you can use occulsion culling - point light is just a sphere. Directional light is seen from everywhere – Kikaimaru May 28 '12 at 18:35
@Darkwings You're basically describing here what blubb asked for. How do I walk? Well, I think the solution here is just to walk! (Worked for me.) ;-) "you need to know if that source is illuminating something in your view frustum." <- That is exactly what he asks for, he wants to know how he can do that. ^^ Although the cone/sphere thing from Kikaimaru is probably helpful. – cooky451 May 28 '12 at 21:55
To the OP it wasn't obvious to check the view frustum since he mentioned just distance. Each light will have some parameters to determine that. If directional lights are used, they will reach everywhere, spotlights will have a target zone (so it could be solved with vector-plane intersection) and so on. You should think more on the lines of which lights you want to affect the scene, given that if you really have a whole lot of lights really close to each other you'll be better off approximating (for shadow casting). I.E: 100 candles on an antique chandelier can be handled as a single light. – Darkwings May 29 '12 at 12:11

Even simpler than bounding volumes;

Assign a radius to each light. When it comes to rendering your scene, simply check to see if the distance between your scene objects and each light is less than the light's radius. If so, use the light, else, skip it.

Just a fraction more complicated, depending on how important performance is:

You can do a sort of broad phase check with this too. Just define a spacial grid that stores handles to your lights on a grid cell basis, then only perform the distance check on lights that are in the cells within the max radius of your scene objects.

-
+1 - Sounds suspiciously like a bounding sphere though ;) – Gary Buyn May 29 '12 at 20:44
A little late...anyway, that' exactly a bounding sphere. Using that method you'll still render lights that are completely obscured by walls etc. and thus not relevant for the scene. Also, not every light goes in every direction. – Darkwings Jul 4 '12 at 12:57

The simplest way I can think of (lets be honest, the only way I've thought of) is to give each light a bounding volume. If the bounding volume intersects with the view frustum (or a bounding volume of the view frustum), apply the light.

There could still be lights that are applied even though they do not affect any visible objects but it is simple and fast. Smart arrangement of your scene graph could mean that just a few intersection checks cull most of your lights (just like what is done for object culling).

Something like the sun could have no bounding volume (to denote it should always be applied).

-