Variable number of light sources

I'm going to be writing a very simple renderer, mainly for learning purposes (using OpengGL). I've been wondering about how to implement support for dynamic number of light sources. Two solutions that come to my mind are:

1. Define an upper limit for each kid of light source (point, directional etc) and pass an array of lights in an uniform.

2. Write a shader for each of the light source type, iterate over each light in the scene and invoke it's appropriate shader, giving it as input the gBuffer with positions, normals and etc. This seems inefficient for me, as we will loop n times through the fragments in the framebuffer, where n is the number of lights.

Are there any more solutions?

• Please keep questions like "how does X do this" and "Can you give me resources about topic Y" out of the questions. They aren't supported here – Bálint Mar 26 at 9:48
• @Bálint sorry, my bad! – wookie Mar 26 at 10:24

1 Answer

The Doom 3 engine used your option 2, but obviously with a forward rather than deferred renderer. The basic workflow looked like this:

• Clear the scene to black.
• Do a Z-only pre-pass.
• For each light in the scene:
• Do stencil shadows.
• Turn on additive blending - glBlendFunc (GL_ONE, GL_ONE);
• Render objects hit by that light.

The Doom 3 source code was released under the GNU GPL and can be viewed as a working example of one way of doing this. The subsequent Doom 3 BFG Edition - a slightly cleaned-up and modernised variant - was also released, and both may be downloaded from id Software's Github:

You may also be interested in the following overview of the Doom 3 renderer: http://fabiensanglard.net/doom3/renderer.php

• That's exactly what I've looking for. Thanks a lot! – wookie Mar 26 at 10:26