Type of GL_LIGHT (going from LL of Lights to specifying which gl light...)

Hi, I ran into a problem and I’m wondering what would be the best solution to employ here…

I need to pass a command to Opengl (working in C++) in the form of:

glLightfv(GL_LIGHT0, GL_POSITION, float array[4]);

My Light class has an int glLightIndex, which holds the value specifying which light is it in opengl light number designation.

than I have a Scene object which has an array of 8 lights.

Now, I need to find a way so that I can pass proper light number to the above command.

For instance, say I have Light* lt. In the lightarray[] it is in index 0 and is therefore GL_LIGHT0 (and the glLightIndex is also set to 0). Therefore when I called the glLightfv, the first parameter would be GL_LIGHT0. If the light was a third light, index 2, then the command would be called with GL_LIGHT2, ok?

The problem I have is how can I do this?

My first thought was enum, like this:


and say light’s glLightIndex = 3, then Gllight[3] would return be GL_LIGHT3 and I could call the above function like:

glLightfv(Gllight[lt->glLightIndex], GL_POSITION, float array[4]);

but obviously enums don’t work that way.

Second tought was to use some function that would concatenate ‘GL_LIGHT’ + glLightIndex, but that would have to return a string or char*, so that’s no good either…

I guess I have no idea as to what type GL_LIGHT is and how I could dynamically call it.

Of course the easiest solution would be to create a switch statement with 8 glLightfv(…_ versions, but that’s pretty ugly coding imho… so… is there a better way?

Thank you,


Fortunately it is much simpler than that. You can just use (GL_LIGHT0 + i) where i is the light index. This works because GL_LIGHT{0-7} are defined to be consecutive numbers so (GL_LIGHT0 + 3) = 0x4000 + 3 = 0x4003 = GL_LIGHT3

This is a C/C++ question, not an OpenGL question. And it’s not very advanced, to boot.

The answer, if you want to structure it the top way, is to use an array rather than an enum.

For further questions, I’d suggest going to www.gamedev.net and asking in the beginners forums; you’re likely to get more on-topic help there.

Zen, thank you for the help, didn’t know it was that easy. jwatte, thanks for the directions :slight_smile: