openglglulookat

Understanding gluLookAt


I'm drawing axes at origin and keeping them fixed in position, I'm trying to rotate my camera with glLookAt:

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
DrawAxes();     // Draws X-axis in red, Y in green, Z in blue
glRotated( m_dRotX, 1.0, 0.0, 0.0 );
glRotated( m_dRotY, 0.0, 1.0, 0.0 );
glRotated( m_dRotZ, 0.0, 0.0, 1.0 );
gluLookAt( m_dCameraPos_X, m_dCameraPos_Y, m_dCameraPos_Z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
SwapBuffers( m_pDC->m_hDC );

Starting from a position ( 0, 0, 100 ), I'm rotating around Y-Axis and I expect to see the red bar (X-axis) become short and blue bar (Z-axis) become longer, but nothing is moving. What am I missing?


Solution

  • Your problem is caused by the order your operations are given in the code: You reset the matrix stack, draw everything, and then set the camera parameters. But these get resetted by glLoadIdentity before the next drawcall.

    A corrected version of your code would look as follows

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glMatrixMode( GL_MODELVIEW );
    
    //Reset stack
    glLoadIdentity();
    
    //Set viewmatrix from camera
    glRotated( m_dRotX, 1.0, 0.0, 0.0 );
    glRotated( m_dRotY, 0.0, 1.0, 0.0 );
    glRotated( m_dRotZ, 0.0, 0.0, 1.0 );
    gluLookAt( m_dCameraPos_X, m_dCameraPos_Y, m_dCameraPos_Z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
    
    //Draw using the viewmatrix
    DrawAxes();     // Draws X-axis in red, Y in green, Z in blue
    
    SwapBuffers( m_pDC->m_hDC );