Results 1 to 2 of 2

Thread: Positioned light moves when using quads

  1. #1
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    1

    Positioned light moves when using quads

    Hello

    I want to add a light in my 3d scene in a corner. And when I move with my mouse/keyboard around in my scene/looking around, I want this light to stay like it is. Just a normal light in a house.

    When I use the function glutSolidSphere it works great.
    When I draw a quad on the screen and give his correct normals it doesn't work.
    If I rotate the light changes.

    Any idea how to solve this?

    Thank you

    Code :
    void View::paintGL()
    {
        // clear screen and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // reset current modelview matrix
        glLoadIdentity();
     
       // _camera->setup(); 
        glRotatef(_rotX, 1.0,0.0,0.0);
        glRotatef(_rotY, 0.0,1.0,0.0);
        glTranslated(-_moveX,-_moveY,-_moveZ);
     
        gluLookAt(_camPosX , _camPosY , _camPosZ,
                  _camViewX, _camViewY, _camViewZ,
                  _camUpX,   _camUpY,   _camUpZ );
     
        glMatrixMode( GL_MODELVIEW );
        glPushMatrix( );
     
        glEnable(GL_LIGHTING);
        _light0->setPosition(5.0f, 8.0f, 8.0f, 1.0f);
        _light0->setAmbientLight(0.0f, 0.0f, 0.5f, 1.0f);
        _light0->setSpecularLight(0.6f, 0.8f, 0.5f, 1.0f);
        _light0->setDiffuseLight(0.6f, 0.8f, 0.5f, 1.0f);
        //_light0->assignComponentsToGLLightX();
        glLightfv(_light, GL_AMBIENT, _ambientLight);
        glLightfv(_light, GL_DIFFUSE, _diffuseLight);
        glLightfv(_light, GL_SPECULAR, _specularLight);
        glLightfv(_light, GL_POSITION, _position);
     
    //    _light0->turnOn();
        glEnable(GL_LIGHT0);
     
         float no_mat[] = {0.0f, 0.0f, 0.0f, 1.0f};
         float mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
         float mat_ambient_color[] = {0.8f, 0.8f, 0.2f, 1.0f};
         float mat_diffuse[] = {0.1f, 0.5f, 0.8f, 1.0f};
         float mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
         float no_shininess = 0.0f;
         float low_shininess = 5.0f;
         float high_shininess = 100.0f;
         float mat_emission[] = {0.3f, 0.2f, 0.2f, 0.0f};
     
     
    // WORKS
         float temp[] = {0.7f, 0.0f, 0.0f, 1.0f};
         glPushMatrix();
     
         glTranslatef(3.75f, 3.0f, 0.0f);
         glMaterialfv(GL_FRONT, GL_AMBIENT, temp);
         glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
         glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
         glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
         glutSolidSphere( 3.0, 25, 25 );
         glPopMatrix();
     
    //WORKS
       glPushMatrix();
       glTranslatef(10.25f, 3.0f, 0.0f);
           glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
           glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
           glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
           glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
           glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
           glutSolidSphere( 3.0, 25, 25 );
       glPopMatrix();
     
     // DOESNT WORK
       glPushMatrix();
           glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
           glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
           glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
           glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
           glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
     
           glBegin(GL_QUADS);
     
           //Front
           //glNormal3f(0.0f, 0.0f, 1.0f);
           glNormal3f(-1.0f, 0.0f, 1.0f);
     
           glVertex3f(-1.5f, -1.0f, 1.5f);
           glNormal3f(1.0f, 0.0f, 1.0f);
           glVertex3f(1.5f, -1.0f, 1.5f);
           glNormal3f(1.0f, 0.0f, 1.0f);
           glVertex3f(1.5f, 1.0f, 1.5f);
           glNormal3f(-1.0f, 0.0f, 1.0f);
           glVertex3f(-1.5f, 1.0f, 1.5f);
     
           //Right
           //glNormal3f(1.0f, 0.0f, 0.0f);
           glNormal3f(1.0f, 0.0f, -1.0f);
           glVertex3f(1.5f, -1.0f, -1.5f);
           glNormal3f(1.0f, 0.0f, -1.0f);
           glVertex3f(1.5f, 1.0f, -1.5f);
           glNormal3f(1.0f, 0.0f, 1.0f);
           glVertex3f(1.5f, 1.0f, 1.5f);
           glNormal3f(1.0f, 0.0f, 1.0f);
           glVertex3f(1.5f, -1.0f, 1.5f);
     
           //Back
           //glNormal3f(0.0f, 0.0f, -1.0f);
           glNormal3f(-1.0f, 0.0f, -1.0f);
           glVertex3f(-1.5f, -1.0f, -1.5f);
           glNormal3f(-1.0f, 0.0f, -1.0f);
           glVertex3f(-1.5f, 1.0f, -1.5f);
           glNormal3f(1.0f, 0.0f, -1.0f);
           glVertex3f(1.5f, 1.0f, -1.5f);
           glNormal3f(1.0f, 0.0f, -1.0f);
           glVertex3f(1.5f, -1.0f, -1.5f);
     
           //Left
           //glNormal3f(-1.0f, 0.0f, 0.0f);
           glNormal3f(-1.0f, 0.0f, -1.0f);
           glVertex3f(-1.5f, -1.0f, -1.5f);
           glNormal3f(-1.0f, 0.0f, 1.0f);
           glVertex3f(-1.5f, -1.0f, 1.5f);
           glNormal3f(-1.0f, 0.0f, 1.0f);
           glVertex3f(-1.5f, 1.0f, 1.5f);
           glNormal3f(-1.0f, 0.0f, -1.0f);
           glVertex3f(-1.5f, 1.0f, -1.5f);
           glEnd();
       glPopMatrix();
     
       glutSwapBuffers();
    }

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Positioned light moves when using quads

    Lighting works against the current ModelViewMatrix.
    Looking at your code you have one too many glPushMatrix command without a corresponding glPopMatrix;

    Code :
        glMatrixMode( GL_MODELVIEW );
        glPushMatrix( );
    May be that's the cause?

Similar Threads

  1. mesh is wrong positioned
    By 3DPrgmer in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 12-05-2016, 02:39 PM
  2. Positioned spot light - white light appearing from nowhere behind
    By windchine in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 08-03-2012, 02:30 AM
  3. Positioned light problem
    By Kojot in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 06-01-2010, 06:03 AM
  4. light: Lighting moves with camera
    By Structural in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 11-25-2002, 04:22 AM
  5. Arbitrarily positioned view planes
    By ThomasHarte in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 05-15-2001, 05:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean