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 4 123 ... LastLast
Results 1 to 10 of 39

Thread: Specular reflection not OK

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2002
    Location
    Ghent, Belgium
    Posts
    22

    Specular reflection not OK

    Hello everybody,

    For a long time I have asked myself questions about this. Take for instance the code below. It will draw a sphere with a certain lighting. This light is directional light and coming from the negative Y-Axis : GLfloat position[] = {0.0f,-1.0f,0.0f,0.0f};

    Yet, and here is the problem: the specular lighting does not seem in order. It is shown as being fixed on a certain spot, and does NOT change when you move the camera around. This is nowhere near what you would expect as reflection.

    Can anybody tell me where the problem is located and how to solve this?

    Code :
    void CNewRightView:    [img]http://www.opengl.org/discussion_boards/ubb/biggrin.gif[/img]rawSphereWithGlut()
    {
     
     GLfloat ambient[]   = {0.6f, 0.6f,0.7f,1.0f};
     GLfloat diffuse[]   = {0.7f, 0.7f,0.7f,1.0f};
     GLfloat position[]  = {0.0f,-1.0f,0.0f,0.0f};
     GLfloat spec[]     = {1.0f, 1.0f,1.0f,1.0f};
     GLfloat specma[]   = {0.3f, 0.3f,0.3f,1.0f};
     
     glShadeModel(GL_SMOOTH);  // Smooth rendering
     glFrontFace(GL_CCW);    // Counter clock-wise polygons face out
     
     glEnable(GL_DEPTH_TEST);  // Hidden surface removal
     glEnable(GL_LIGHTING);    // Enable lighting
     
     glLightfv(GL_LIGHT1,GL_POSITION,position);  // Setup and enable light 0
     glLightfv(GL_LIGHT1,GL_AMBIENT,ambient);
     glLightfv(GL_LIGHT1,GL_DIFFUSE,diffuse);
     glLightfv(GL_LIGHT1,GL_SPECULAR,spec);
     glEnable(GL_LIGHT1);
     
     glEnable(GL_COLOR_MATERIAL);          // Set Material properties to follow glColor values
     glMaterialfv(GL_FRONT,GL_SPECULAR,specma);
     glMateriali(GL_FRONT,GL_SHININESS,128);
     
     glColor3d(0.5,0.5,0.5);
     
     glutSolidSphere(0.05,50,50);
     
     glDisable(GL_POLYGON_OFFSET_FILL);
     glDisable(GL_COLOR_MATERIAL);
     glDisable(GL_DEPTH_TEST);
     glDisable(GL_LIGHTING);
     glDisable(GL_LIGHT1);
    }
    [This message has been edited by Simon666 (edited 03-12-2003).]

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,797

    Re: Specular reflection not OK

    That code doesn't show where you "move the camera around."

    If you are doing so in the GL_PROJECTION matrix, you shouldn't be. That should only be used for setting up the shape of the viewing volume. (e.g. gluPerspective, glOrtho, etc.)

    When you set the GL_POSITION of a light, it takes the MODELVIEW matrix into account, so it also makes a difference where you call that at.
    Deiussum
    Software Engineer and OpenGL enthusiast

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2002
    Location
    Ghent, Belgium
    Posts
    22

    Re: Specular reflection not OK

    Hello,

    First of all, thanks for replying. The camera is set in the CCamera class. I use glRotated and glTranslated to view the scene from the desired angle.

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    May 2001
    Location
    France
    Posts
    768

    Re: Specular reflection not OK

    If you just "move" the camera you shouldn't notice any difference.
    If you "rotate" the camera, there should be a little difference, at least as long as the MODELVIEW matrix is the current matrix.

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2002
    Location
    Ghent, Belgium
    Posts
    22

    Re: Specular reflection not OK

    Originally posted by vincoof:
    If you just "move" the camera you shouldn't notice any difference.
    If you "rotate" the camera, there should be a little difference, at least as long as the MODELVIEW matrix is the current matrix.
    Could you tell me what this means, and what I would need to change? I currently don't understand what you mean?

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    May 2001
    Location
    France
    Posts
    768

    Re: Specular reflection not OK

    Translation only does not change the specular highlight (except in local viewer mode or with point light). If the light is directional, and if the viewer is considered at an infinite distance (it is the default state of OpenGL) then calling glTranslate has no effect on the specular contribution.

    But if you call glRotate with a non-nul angle, then the specular highlight might move and seems to "float" around the object.

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2002
    Location
    Ghent, Belgium
    Posts
    22

    Re: Specular reflection not OK

    Originally posted by vincoof:
    Translation only does not change the specular highlight (except in local viewer mode or with point light). If the light is directional, and if the viewer is considered at an infinite distance (it is the default state of OpenGL) then calling glTranslate has no effect on the specular contribution.

    But if you call glRotate with a non-nul angle, then the specular highlight might move and seems to "float" around the object.
    OK, I call glRotate with an angle different from zero in my CCamera class, but how would I solve this problem then?

  8. #8
    Advanced Member Frequent Contributor
    Join Date
    May 2001
    Location
    France
    Posts
    768

    Re: Specular reflection not OK

    It does not really solve the problem. It could explain why you thought there was a problem when in fact there wasn't. It's natural to think that the specular highlight floats, but the simple translation does not give the feeling of floating, so it's a common mistake to think that the specular is wrong when in fact it's ok. But if you call glRotate then obviously there is a problem.

    Could you post screenshots showing the problem ?

  9. #9
    Junior Member Newbie
    Join Date
    Dec 2002
    Location
    Ghent, Belgium
    Posts
    22

    Re: Specular reflection not OK

    Originally posted by vincoof:
    Could you post screenshots showing the problem ?[/B]
    Here is a picture showing the problem. I use positional light coming from the negative Y direction. Yet nomatter under what angle I look at the scene, the reflected spot on the sphere stays at the same place, which can be found by drawing a line from the center in the direction (0,-1/sqrt(2),1(sqrt(2))), or so it seems. Note that the diffuse reflection is OK and shows clearly that the directional light is coming from the negative Y-axis.

    GLfloat position[] = {0.0f,-1.0f,0.0f,0.0f};
    glLightfv(GL_LIGHT0,GL_POSITION,position);



    [This message has been edited by Simon666 (edited 03-11-2003).]

  10. #10
    Advanced Member Frequent Contributor
    Join Date
    May 2001
    Location
    France
    Posts
    768

    Re: Specular reflection not OK

    I'm sorry but the image looks correct. Perhaps you could setup online an executable of this sample program ?

Posting Permissions

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