Ok, this is an old thread, but I’m trying to do the same thing…
it seems that the light indices aren’t quite as simple as reverse chronological order as they are also “per layer”
I’ve tried to dynamically recognize them in the fragment shader with this code (taken from the glsl orange book) and then call the appropriate function to calculate the lighting contribution of each light.
//------------------------------
//LIGHTING
//------------------------------
//clear the light intensity accumulators
vec4 amb = vec4(0.0);
vec4 diff = vec4(0.0);
vec4 spec = vec4(0.0);
//loop through enabled lights computing the contribution from each
for (int i = 0; i < numEnabledLights; i++)
{
if (isLightEnabled(i) == true)
{
if (gl_LightSource[i].position.w == 0.0)
DirectionalLight(i, normal, amb, diff, spec);
else if (gl_LightSource[i].spotCutoff == 180.0)
PointLight(i, eye, ecPosition3, normal, amb, diff, spec);
else
SpotLight(i, eye, ecPosition3, normal, amb, diff, spec);
}
}
This works fine, and I thought “great”
I’ve been building a whole shader library based on this for a project.
the trouble is it only works “most” of the time.
Under certain indiscenable circumstances it seems to just stop working and not find any lights… sometimes when I use it on a new scene it “just works”, sometimes it just “makes up” a default light that bears no relation to those in the scene…
when it works I can chop and change the lights, add them, delete them, change their type/colours etc
when it doesn’t work nothing I do makes any difference.
When I use it on my “live scene” for my project, it just seems to get junk for the lights…
I don’t currently have a corrupt file I can share here…
anyone else had any success with this type of approach?