0

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
IssamTP
  • 2,408
  • 1
  • 25
  • 48

1 Answers1

3

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 );
BDL
  • 21,052
  • 22
  • 49
  • 55
  • Well, I might be wrong but... Aren't you rotating the axes in these way? How should I rotate only the camera? – IssamTP Dec 05 '14 at 13:11
  • Basically, that's the same thing. Rotating the camera to the right is the same thing as rotating the object to the left. In addition there is no concept in OpenGL like a camera. I explained in the answer [here](http://stackoverflow.com/questions/26754725/gluproject-converting-3d-coordinates-to-2d-coordinates-does-not-convert-the-2d-y/26756095#26756095) a bit more about the coordinate systems of OpenGL. – BDL Dec 05 '14 at 13:13
  • Well, I say "camera" to say gluLookAt's eye parameters. Is there no way to keep axes (and other draws) fixed and move only "eyes"/gluLookAt? – IssamTP Dec 05 '14 at 13:30
  • There is no way. As told before, the camera transformation has to be applied to the things you draw. – BDL Dec 05 '14 at 13:40
  • OOOK. Thanks for ya help. – IssamTP Dec 05 '14 at 13:42
  • Just another related question: is it normal that the axis are moving outside the view? – IssamTP Dec 05 '14 at 13:55