No – X is not the axis that defines the depth after the rotations. You have to be more specific. Which X-axis are you referring to? The fixed-x axis or the object’s x-axis? If you said the objects X-axis you would be correct because that is the same as the fixed z-axis after rotations! – all very confusing. Hang in there, its like a puzzle once you see it things become easy!
Imagine a fixed coordinate system that does NOT change when you apply rotations/translations/etc: x=right, y=up, z=out-of-monitor. As you apply the rotations your object rotates but the fixed coordinate system still is x=right, y=up, z=out-of-monitor at every step. In the following code I make this distinction more apparent by referring to the fixed-x,y,z coordinate system differently from the object-x,y,z coordinate system.
3: glTranslatef (0, 0, -10); // third translate along fixed z-axis (=object x-axis!)
2: glRotatef(90, 0, 0, -1); // second rotate about fixed z-axis (=object x-axis!)
1: glRotatef(90, 0, -1, 0); // first rotate about fixed y-axis (initially same as object y-axis)
The object starts with the face (U) you want to view on the right.
Step 1: glRotatef(90, 0, -1, 0) rotates the object so U is now facing you in the xy-plane (x=right, y=up still!) but the “view look at up direction” is (0,1,0)
Step 2: glRotatef(90, 0, 0, -1) rotates the object around the z-axis (the z-axis still pointing out of the monitor!) so U is still facing you in the xy-plane (x=right,y=up) while achieving the desired “view look up direction” (0,0,1).
Step 3: glTranslatef (0, 0, -10) moves the object away from you into the monitor (z=out-of_monitor), hence the -10 in the z direction.
I wrote the following code to verify what I said. The result is rather boring – it starts out viewing that object before any rotations or gluLookAt are applied showing U on the right. If you press a key then you will see the effect of applying the translate/rotate pair – U moved to the center which is now on the z-axis (out-of-monitor). If you press another key the view stays the same even though the modelview is changed to use gluLookat.
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
int useLookAt = 0;
void Keyboard (unsigned char key, int q, int s)
{
switch (key) {
case 27: exit(0);
break;
default : ++useLookAt %= 3;
}
glutPostRedisplay();
}
// modified from code by maxN in post #262420
void Triad (char xlab, char ylab, char zlab)
{
static const float XUP[3] = {1,0,0},
YUP[3] = {0,1,0},
ZUP[3] = {0,0,1},
ORG[3] = {0,0,0};
static float siz = 1.5;
static GLfloat color[3] = {1,1,0};
glLineWidth (2.0);
glColor3fv (color);
glPushMatrix ();
glScalef (siz, siz, siz);
glBegin (GL_LINES);
glVertex3fv (ORG); glVertex3fv (XUP);
glVertex3fv (ORG); glVertex3fv (YUP);
glVertex3fv (ORG); glVertex3fv (ZUP);
glEnd ();
glRasterPos3f (1.1, 0.0, 0.0);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, xlab);
glRasterPos3f (0.0, 1.1, 0.0);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, ylab);
glRasterPos3f (0.0, 0.0, 1.1);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, zlab);
glRasterPos3f (1.1, 1.1, 1.1);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 48+useLookAt);
glPopMatrix ();
}
void Render_Scene (void)
{
static GLfloat color[3] = {1,1,0};
glClear (GL_COLOR_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
switch (useLookAt) {
case 0: // do nothing but zoom out to view triad without rotations
glTranslatef (0, 0, -10);
printf ("glTranslatef (0, 0, -10);
");
break;
case 1:
glTranslatef (0, 0, -10); // third translate along fixed z-axis (object x-axis!)
glRotatef(90, 0, 0, -1); // second rotate about fixed z-axis (object x-axis!)
glRotatef(90, 0, -1, 0); // first rotate about fixed y-axis
printf ("glTranslatef (0, 0, -10); glRotatef(90, 0, 0, -1); glRotatef(90, 0, -1, 0)
");
break;
default:
gluLookAt(10, 0, 0, 0, 0, 0, 0, 0, 1);
printf ("gluLookAt(10, 0, 0, 0, 0, 0, 0, 0, 1)
");
break;
}
Triad ('u', 'v', 'w'); // draw the coordinate axis of object
//distinguish fixed coordinates (x,y,z) from object coordinates(u,v,w)
glutSwapBuffers();
}
int main (int argc, char **argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutCreateWindow ("OpenGL matrix operations");
glutDisplayFunc (Render_Scene);
glutKeyboardFunc ( Keyboard );
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, 1.33, 1, 10);
glutMainLoop ();
return 1;
}