So I want to have multiple light sources in my scene. The basic idea is to simply have an array of a (uniform) struct that has all the properties of light you care about such as positions, color, direction, cutoff and w/e you want. My problem is how to represent which lights are on/off? I will list out all the ways I can think of. Pl
I'm leaning towards the last options as it won't have branching ... but I already read that modern graphics card are more okay with branching now.
None of your ideas is really good because all of them require the shader to evaluate which lightsources are active and which aren't. You should also differentiate between scene information (which lights are present in the scene) and data necessary for rendering (which lights are on and illuminate the scene). Scene information shouldn't be stored in a shader since it is unnecessary and will only slow down the shader.
A better way for handling multiple light sources in a scene and render with only the active ones could be as follows:
In every frame:
Evaluate on CPU side which lights are on. Pass only the lights which are on to the shader uniforms together with the total count of the active lights. In pseudocode:
Shader:
uniform lightsources active_lightsources[MAX_LIGHTS];
uniform int light_count;
CPU:
i = 0
foreach (light in lightsources)
{
if (light.state == ON)
{
set_uniform("active_lightsources[i]", light);
i++
}
}
set_uniform("light_count", i);
When illuminating a fragment do the following in the shader
Shader:
for (int i = 0; i < light_count; i++)
{
//Calculate illumination from active_lightsources[i]
}
The major advantage of this approach is that you store less lights inside the shader and that the loop inside the shader only loops over lightsources that are relevant for the current frame. The evaluation which lights are relevant is done once per frame on the CPU instead of once per vertex/fragment in the shader.