Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Light working correctly with negative normals why ??

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2005
    Posts
    2

    Light working correctly with negative normals why ??

    I was testing it with more complicated object also but for purpose of this post i added QUAD and removed some trash.

    My problem is. I have got light just before my "eyes" (i can se it's position becouse i have drawn cross in this point) and there is a quad after the light pos "eyepos">>"light_pos">>"object"
    Normals points from the object toward eye cordinates. But the object is not enlightened until i move it toward eye and pass the light_pos so it looks like that eye>>object>>light. But when i change Normal's xyz to negative (-x,-y,-z) i can see the object properly shading.Why do i have to flip the normals direction (toward object) to make it correct ??

    ///////////////////////THE codE////////////
    void TestRendering()
    {
    int IndV;
    int IndN;
    float px,py,pz;
    float LPos[]={0.0f,0.0f,0.0f};
    float LDif[]={0.6f,0.6f,0.6f};
    float LSpc[]={0.4f,0.4f,0.4f};
    float Amb[]={0.5f,0.5f,0.5f};
    float MAmbDif[]={0.75f,0.75f,0.75f};
    float MSpc[]={1.0f,1.0f,1.0f};
    //-----------------Test Rendering
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);

    glShadeModel(GL_SMOOTH);
    glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,1,0.01,100);
    gluLookAt(0,0,3.5,0,0,0,0,1,0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //Turning and positioning Light
    glLightfv(GL_LIGHT0,GL_DIFFUSE,LDif);
    glLightfv(GL_LIGHT0,GL_POSITION,LPos);
    glEnable(GL_LIGHT0);

    #define DCROSS //drawing cross where light is
    #if defined (DCROSS)
    glDisable(GL_LIGHTING);
    glColor3f(1.0f,1.0f,1.0f);
    glBegin(GL_LINES);
    glVertex3f(LPos[0]+0.3,LPos[1],LPos[2]); glVertex3f(LPos[0]-0.3,LPos[1],LPos[2]); glVertex3f(LPos[0],LPos[1]+0.3,LPos[2]); glVertex3f(LPos[0],LPos[1]-0.3,LPos[2]); glVertex3f(LPos[0],LPos[1],LPos[2]+0.3); glVertex3f(LPos[0],LPos[1],LPos[2]-0.3);
    glEnd();
    glEnable(GL_LIGHTING);
    #endif

    //rotating object and setting distance
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0,0,Dist-1.5);
    glRotatef(CamRotX,0,1,0);
    glRotatef(CamRotY,1,0,0);
    //End Turning and drawing Light
    //--------------- //one dimensional quad to lighten//
    glBegin( GL_QUADS );
    glNormal3f( 0.0f, 0.0f, 1.0f);// Normal Pointing Towards Viewer
    glVertex3f(-1.0f, -1.0f, 0.0f); // Point 1 (Front)
    glVertex3f( 1.0f, -1.0f, 0.0f); // Point 2 (Front)
    glVertex3f( 1.0f, 1.0f, 0.0f); // Point 3 (Front)
    glVertex3f(-1.0f, 1.0f, 0.0f);
    glEnd();
    //---------------

    //DRAWING TEXT
    glPushMatrix();
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(-1,1,-1,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    glColor3f(1.0f,1.0f,1.0f);
    GLScreen->Write2DText("Ilosc Wierzcholkow: ",(int)VectVertex3f.size(),-1.0f,0.9f);
    GLScreen->Write2DText("Ilosc Trojkatow: ",(int)VectFace.size()/3,-1.0f,0.8f);
    glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    //DRAWING TExt
    //----------------ENDOF IT
    }
    Signatures can appear at the bottom of your posts. This option may be disabled by the message board administrators at any time, however. You may use UBB Code in your signature, but not HTML. UBBCode Images are permitted.

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: Light working correctly with negative normals why ??

    Geometry code looks ok, the quad is front facing and the normal points to the viewer. (You should put the single glNormal3f call outside the glBegin-glEnd.)

    >>(i can se it's position because i have drawn cross in this point)<<

    You think so? The glLightfv parameters must be float[4]! Your light position is probably completely wrong or using the 0.6 from the next array as w.

    You should also not enable color material before you specified glColorMaterial or you fall into pit #14 http://www.opengl.org/resources/feat...es/oglpitfall/

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2005
    Posts
    2

    Re: Light working correctly with negative normals why ??

    That was the problem. Yep Light pos should be float[4]. I will take notice of this .
    Thanks
    Signatures can appear at the bottom of your posts. This option may be disabled by the message board administrators at any time, however. You may use UBB Code in your signature, but not HTML. UBBCode Images are permitted.

  4. #4
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: Light working correctly with negative normals why ??

    Mind, not only the position, all colors, too.

Posting Permissions

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