Problem with lighting & texturing

First of all, I am not sure this should be in beginner or advanced topic. However, I post here ultimately.

I have a problem with lighting and texturing. I knew that when we want to work with both lighting and texturing at the same time.
To make them working properly, we have to configure one relevant opengl command like this: glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE), rather than glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE). That is to choose one parameter either GL_MODULATE or GL_REPLACE.

This is my result:

Sorry I’ve tried for almost half an hour, I could not insert figures.
With GL_REPLACE, the original texture image can be attached to the object surface, but with GL_MODULATE the attached texture image was very dark and was not in what I want it to be, texture image with effect of lighting. Any suggest and help, please?

Thank you in advance.

Have you tried lighting without texturing? GL_MODULATE simply multiplies the texture colour by the lighting colour, so if the lighting is too dark, the end result will also be too dark. The end result will never be any brighter than either the lighting or the texture alone.

… tried for almost half an hour, I could not insert figures.
I also can not insert images directly from my computer. A workaround is to upload the image to free image hosting site. There are sites set up to store images specifically for posting to forums. The one I like is:

It’s very easy to use. There’s no charge. You don’t have to set up an account.
After uploading an image they give you the URL to use to refer to that image in the forum.
This is how the image below is posted …


The results are here!, thanks for the tricky method to insert figures.


I placeed texture image on just one side, the others are bright surfaces.
Then with glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE), the result is as follows:

The texture side looks dark near both ends, while the others are still bright, and is not in the correct color!

What did I do wrong? Any suggestion, please again!

It looks like, you’re modulating the texture colour with green. GL_MODULATE multiplies each component independently, i.e. the red component of the texture is multiplied by the red component of the current colour to determine the red component of the resulting colour. If the current colour is green (0,1,0), then the red and blue components of the resulting colour will always be zero.

Thanks, GClements, I start getting better understanding about texture now.

It works as shown here

But I’ve got another problem. As you can see at the end, I would like that surface to be (completely) RED by specifying glTexCoord1D(1.0) for all (GL_QUAZS) points, but it didn’t work. As well as at the other end that I want it to be completely BLUE, but it didn’t work. It turns out to work if I apply gradient of color like the other (parallel to z-axis) surfaces. I am trying to figure out what wrong it is, but if some of you can point out or pinpoint the cause of problem, please tell me, thanks.

Oh! one more thing, it seems that source/direction of light is rotated with user (mouse) rotation, then the surface first was dark is still dark wherever it is rotated to. It would be great if there will be a short description on how to configure about source/direction of light, thanks.

You probably need



The initial value is GL_REPEAT. If used with linear interpolation (i.e. anything beginning with GL_LINEAR for GL_MAG_FILTER or GL_MIN_FILTER) this will result in a texture coordinate of 0.0 or 1.0 interpolating between the first and last pixels of the texture ( this case, between blue and red, resulting in (0.5,0.0,0.5) = purple).

The light position is transformed to eye space using the model-view matrix which is in effect at the time of the glLight(GL_POSITION) call. If you want the light position to be fixed in eye space, call glLight() after setting the model-view matrix to the identity matrix (with glLoadIdentity()), before applying any user-controlled transformations.

Thanks a million, GClements.

With a lot of trial-and-error, I commented out that line, then I bring it back, it work as shown above.
Next to configure light position as you suggest, thanks to repeat a fundamental concept of lighting. Please don’t get me wrong, I meant I should not even ask this basic question in the advanced session.

Next to configure light position …
Positioning of the glLightfv statement has a major effect on lighting of the scene. In position marked AAAAAAAA the light source does not move with the objects in the scene. In position marked BBBBBBBB light source rotates with objects in scene. I believe the behavior you are getting is BBBBBBBB. What you want is AAAAAAAA. All you should have to do is move the glLightfv command up above the modeling commands.

void Display (void)
    static float x = 2.0;


    glMatrixMode   (GL_PROJECTION);
    glLoadIdentity ();

    if (proj == 0)  glOrtho        (-x*1.5, x, -x, x, 0.0, 10.0);
    else            gluPerspective ( 60.0,  1.5,  1.0, 10.0);

    glMatrixMode   (GL_MODELVIEW);
    glLoadIdentity ();

    glLightfv (GL_LIGHT1, GL_POSITION, lit1_pos);               // AAAAAAAA

    glTranslatef (xtm, ytm, -4.0);
    glRotatef    (tipp,  1, 0, 0);
    glRotatef    (turn,  0, 1, 0);
    glScalef     (scl, scl, scl );
//  glLightfv    (GL_LIGHT1, GL_POSITION, lit1_pos);            // BBBBBBBB
    Draw_Objects ();


BTW - a better way to post an image to the forum is to use the image insertion function, not the URL insertion function. That way you won’t get all the advertisements underneath your image.

Thank you very much, carmine, for your demonstration.

Let me know if it helped.

Yeah, it is really helpful, Carmine. Thanks again.

Did you realize that you don’t have to use texture mapping to get the color gradient in your example?
If you give the vertices of a polygon different colors, gl interpolates color across the surface of the poly.