Lighting Troubles w/ Code

I’m having trouble when I turn my light on. I have a scene with a green polygon for the ground, a quadric cube, a quadric sphere, and a sky dome. When I turn my light on the sky looks fine but the rest of the scene is a pale green. My cube is supposed to be red, the ground green, and the sphere yellow but they all appear this lime green color. Anybody have any ideas that can help me get my colors back? Here is my light code:

/* Lighting variables */

// float ambientLight[] = {0.3f, 0.5f, 0.8f, 1.0f};
float ambientLight[] = {0.3f, 0.9f, 0.5f, 1.0f};
float diffuseLight[] = {0.25f, 0.25f, 0.25f, 1.0f};
float lightPosition[] = {0.0f, 0.0f, 10.0f, 0.0f}; //Light comes from Z = 10.0
float light2Position[] = {0.0f, 0.0f, 10.0f, 1.0f}; //Light is located at Z = 10.0

/* Lighting materials */
float matAmbient[] = {1.0f, 1.0f, 1.0f, 1.0f};
float matDiff[] = {1.0f, 1.0f, 1.0f, 1.0f};

/* Enable lighting */

/* Set up the materials for LIGHT0 */
glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiff);

/* Set up LIGHT0 */
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

/* Enable the light */

That would be because your light is kind of green… Also because when you have lighting enabled, the colors are chosen according to your material settings, not glColor calls. The glMaterial calls should be associated with the objects, not the light.

You can use glColor calls to control the material settings, for that you must enable GL_COLOR_MATERIAL. You can change color material settings with a glLightModel call, but in your case just enabling it propably does the trick.


Thanks Ilkka! I understand where I went wrong now. I didn’t realize the material settings affected the polygon color but now I do. Once again you cleared things up for this beginner. Thanks!

I just had another thought on this subject. So the reason why my sky was not affected by the color was because it had a texture applied to it? Is that correct? Do material settings affect texture colors?

It depends on your texture enviroment.

Your object has a primary color, which equals to what it would look like without a texture. Well actually each fragment (pixel) that gets drawn has it’s own primary color. Anyway…

For a texture you can set a texture enviroment. The most common one is GL_MODULATE, I think, it means that for every pixel it’s texture color gets multiplied by it’s primary color. This makes it easy to combine lighting with textures, for example, end in this case the material settings do affect the textured object. The other simple texture enviroment is GL_REPLACE where the texture color gets applied as is, so with it the material settings don’t matter. I don’t remember which one is the default, but I guess your sky is using GL_REPLACE.


Thanks again for your reply! I believe my sky texture is GL_REPLACE (I don’t have it here at work) so that would explain the behavior. And now I understand how color affects texture.