Questions about Lighting and Shadowing


Soon I will be dealing with lighting and shadowing problems and I hope for some of more experienced contributors to point me in right direction.

– Lights

  1. Should I calculate lighting together witch texturing and other effects (excluding shadows) in single shader or is it better to do lighting in different pass.?

  2. Would it be a good solution to calculate, which lights affect which object and then set those
    lights (as uniforms) per object - that method could be more precise but would require setting
    light uniforms for every object and also calculating, which lights affect object (this could be done each frame)?

  3. Alternatively - is setting many lights as a lighting group, which affects many objects any good?
    That could limit uniform setting but would require much more lighting calculations inside shaders that will not contribute to final image.

– Shadows

  1. When creating shadowmap for one light should I select only the objects that the light affects. I suppose so but just have to ask.

  2. I just can’t get a gripe on how exactly to put shadows on objects. First I should draw objects affected by single shadow casting light to shadowmap but what then, should I apply the shadowmap to all objects in certain range in FOV or select objects that are affected by this specific shadowmap?


How many light sources max?

How many of them cast shadows max?

I assume all are point light sources, except possibly one?

And what is your minimum supported GPU requirement? Do they all support GL 3.0+?

Thanks for taking interest.

  • How many light sources max?

It really depends on my questions 2 and 3.
If I would do it like (2) then there should be max 2 - 3 lights per object but it would change with distance ( lights that are far would be eliminated ) and of course it would be precisely calculated, which light affects, which object but would require setting light uniforms for each object.

If I would use approach (3) then the number of lights could be even <= 8, but I suppose that it is bad idea although it is much easier to implement obviously but efficiency is more important factor than easiness of implementation.

  • How many of them cast shadows max?

Probably 4 most dominant, closest lights, which would be selected each frame depending on the FOV.

  • I assume all are point light sources, except possibly one?

Only point lights for now + main directional light ( like sun )

  • And what is your minimum supported GPU requirement? Do they all support GL 3.0+?

It would be best if all of it could work on GLES 2.0 but it doesn’t really matter that much.

That’s not that many lights.

Considering the lighting alone (ignoring shadows)…

Given the speed of today’s GPUs, depending on your specific GPU, you may find it works well to just compile support for up to 4 lights in your shaders and just apply them all at once to everything you draw (Forward Shading). If you instead had tons of lights, then you might want to consider a Deferred Rendering technique so you could more easily reduce the fill associated with applying the lighting for each light.

Now as far as shadows, what you do there depends on the complexity of your world. Worst case you probably want to cull your scene per light to determine what objects can cast shadows from the light, render its shadow map, and then bind it for lookups when rendering the light’s contribution into the scene, in whatever way you decide to do that. Doing projective lookups into 3-4 cube maps for point light shadows may be too much load for Forward Shading. If so, consider a Deferred technique.

Your suggestion of determining what objects are affected by what lights is possible, but leads to problems because you can’t split your objects at a granularity finer than a batch without rewriting batches or using stencil which is a pain, and can lead to “shader permutation hell” if you’re trying to apply all lights you can in one pass.

Thanks for the answer.

I will stick to Forward renderer for now just to finish it because I hate leaving unfinished business and then I will check out Deferred Renderer in practice.