# 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 '19 at 9:48
• @Bálint sorry, my bad! – wookie Mar 26 '19 at 10:24

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: