Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Problem with lighting & texturing

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2016
    Location
    Thailand
    Posts
    11

    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.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,476
    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.

  3. #3
    Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    388
    ... 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:

    http://postimage.org/

    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 ...

    Click image for larger version. 

Name:	Gulf.jpg 
Views:	96 
Size:	71.7 KB 
ID:	2229

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2016
    Location
    Thailand
    Posts
    11
    The results are here!, thanks for the tricky method to insert figures.

    This is to use glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)


    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!
    Last edited by kiattisak; 01-25-2016 at 07:50 PM.

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,476
    Quote Originally Posted by kiattisak View Post
    What did I do wrong?
    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.

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2016
    Location
    Thailand
    Posts
    11
    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.
    Last edited by kiattisak; 01-26-2016 at 01:48 AM.

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,476
    Quote Originally Posted by kiattisak View Post
    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.
    You probably need
    Code :
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    (or GL_CLAMP_TO_EDGE or GL_CLAMP_TO_BORDER).

    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 (i.e.in this case, between blue and red, resulting in (0.5,0.0,0.5) = purple).

    Quote Originally Posted by kiattisak View Post
    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
    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.

  8. #8
    Junior Member Newbie
    Join Date
    Jan 2016
    Location
    Thailand
    Posts
    11
    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.
    Last edited by kiattisak; 01-26-2016 at 08:04 PM.

  9. #9
    Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    388
    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.

    Code :
    void Display (void)
    {
        static float x = 2.0;
     
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
        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 ();
     
        glutSwapBuffers();
    }

    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.
    Last edited by Carmine; 01-27-2016 at 12:10 AM.

  10. #10
    Junior Member Newbie
    Join Date
    Jan 2016
    Location
    Thailand
    Posts
    11
    Thank you very much, carmine, for your demonstration.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •