Results 1 to 6 of 6

Thread: Illumination changes when rotating camera

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    3

    Illumination changes when rotating camera

    Hello, I have a problem with lighting in OpenGL, when I rotate camera changes the illumination of the scene.
    The light is directional: incident at the same angle for all objects, like the sun.


    const GLfloat lightAmbient[] = {0.2, 0.2, 0.2, 1.0};
    const GLfloat lightDiffuse[] = {1.0, 0.6, 0.0, 1.0};
    const GLfloat matAmbient[] = {0.6, 0.6, 0.6, 1.0};
    const GLfloat matDiffuse[] = {1.0, 1.0, 1.0, 1.0};
    const GLfloat matSpecular[] = {1.0, 1.0, 1.0, 1.0};
    const GLfloat lightPosition[] = {0.0, 0.0, 1.0, 0.0};
    const GLfloat lightShininess = 100.0;

    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, matAmbient);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, matDiffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, matSpecular);
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, lightShininess);
    glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);




    I tried calling glLightfv on each frame after tell opengl which is the matrix of the camera, but still happening:





    glMatrixMode(GL_MODELVIEW);




    float mat[4*4]={ CamMatrix->iM[0], CamMatrix->iM[4], -CamMatrix->iM[ 8], 0,
    CamMatrix->iM[1], CamMatrix->iM[5], -CamMatrix->iM[ 9], 0,
    CamMatrix->iM[2], CamMatrix->iM[6], -CamMatrix->iM[10], 0,
    CamMatrix->iM[3], CamMatrix->iM[7], -CamMatrix->iM[11], 1 };


    glLoadMatrixf( mat );
    glPushMatrix();

    float lightPosition[] = {0.0f, 2.0f, 1.0f, 0.0f};
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);



    Thanks in advance.

  2. #2
    Super Moderator OpenGL Guru imported_dorbie's Avatar
    Join Date
    Jul 2000
    Location
    Bay Area, CA, USA
    Posts
    3,966

    Re: Illumination changes when rotating camera

    This should work, you need to ensure that the matrix has the viewing transform and only the viewing transform on the matrix stack.

    If you load another viewing matrix differently then you'll get a discrepancy.

    You're dicking around with that matrix, it needs to be the SAME matrix as the one loaded for viewing and loaded in the same way as viewing.

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    3

    Re: Illumination changes when rotating camera

    Quote Originally Posted by dorbie
    This should work, you need to ensure that the matrix has the viewing transform and only the viewing transform on the matrix stack.

    If you load another viewing matrix differently then you'll get a discrepancy.

    You're dicking around with that matrix, it needs to be the SAME matrix as the one loaded for viewing and loaded in the same way as viewing.
    But in my engine, I set modelview matrix for each 3D object:



    //---Generate rotate matrix for OpenGl.
    //---Final matrix is CamMatrix *ObjectMatrix
    float mat[4*4]={ FinalMatrix->iM[0], FinalMatrix->iM[4], -FinalMatrix->iM[ 8], 0,
    FinalMatrix->iM[1], FinalMatrix->iM[5], -FinalMatrix->iM[ 9], 0,
    FinalMatrix->iM[2], FinalMatrix->iM[6], -FinalMatrix->iM[10], 0,
    FinalMatrix->iM[3], FinalMatrix->iM[7], -FinalMatrix->iM[11], 1 };

    glLoadMatrixf( mat );

    .
    .
    .

    glDrawElements( GL_TRIANGLES, iGlTriCount, GL_UNSIGNED_SHORT, iGlTris );



    Where should I put glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); ?

    I do not tell opengl which is the camera matrix, I only use camera matrix to generate FinalMatrix. I do not use glLookAt, it is not available in iphone.

    Thank you very much for your help

  4. #4
    Member Contributor
    Join Date
    Jul 2009
    Location
    Buenos Aires
    Posts
    67

    Re: Illumination changes when rotating camera

    Where should I put glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); ?
    Right after the camera transform, before drawing any object

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    3

    Re: Illumination changes when rotating camera

    Quote Originally Posted by DandyYuyo
    Where should I put glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); ?
    Right after the camera transform, before drawing any object
    Yes, I am doing it there.

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2009
    Location
    sweden
    Posts
    8

    Re: Illumination changes when rotating camera

    and you pop the matrix after the camera transform?

Similar Threads

  1. Rotating the camera
    By Ehsan Nekooee in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 09-05-2009, 02:10 PM
  2. Rotating camera in 4pi.
    By brambilla in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 01-25-2009, 07:05 AM
  3. Camera rotating
    By Vlasko in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-13-2004, 10:48 AM
  4. rotating the camera around y axis
    By GameMaker in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 11-11-2002, 06:17 PM
  5. Rotating camera / scene
    By Bahamut_france in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 03-22-2001, 07:12 AM

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