PDA

View Full Version : glRotatef() and glTranslatef()



06-12-2002, 02:16 PM
hi everyone,

like most beginners i have my problems with rotate & translate. i tried to create a rotating cube, which position (x,y,z) one can change by key-commands. each of the functions is working quite fine (moving without rotating and the other way around), but together the cube behaves chaotically.
i put most of the code in onTimer:

glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f, 1.0, 0.1f, 100.0f);

glMatrixMode(GL_MODELVIEW);

glPushMatrix();
glLoadIdentity();
glPopMatrix();

if (!moved){
glTranslatef(x_Achse, y_Achse, z_Achse);
moved = TRUE;
}
else
glRotatef(1.0f, 0.0f, 1.0f, 1.f);

glEnable(GL_CULL_FACE);

//Cube
for(int i=0; i< 6; i++){
glBegin(GL_QUADS);
//Code for the cube, only 'glVertex3f'
glEnd();}

glFlush();
SwapBuffers(dc.m_hDC);
}


i know i'm doing something wrong with the matrix but i'm not yet knowing how it is correct.
may anyone help me?

bye stephan

(sorry for my english, i'm from germany and not used to post messages in english)

yoale
06-12-2002, 02:29 PM
There are a couple of things to keep in mind when using glRotate and glTranslate. When you rotate, the translate is relative to the new direction caused by the rotation. Hence, rotating on y-axis 90 degrees, and then translating in the x-axis will actually "appear" to translate in the z-axis.

Also,
glPushMatrix();
glLoadIdentity();
glPopMatrix();

wastes cycels because it does nothing useful.

yoale
06-12-2002, 02:31 PM
When I say "appear", I really mean some complicated stuff for a beginner, but I didn't think you'd want delve into it http://www.opengl.org/discussion_boards/ubb/biggrin.gif

chmod
06-12-2002, 09:45 PM
You seem to not reset your MODELVIEW matrix
when you make the call to glPushMatrix you are storing the current modelview matrix on the stack. Your call to glLoadIdentity() resets the new matrix and the you glPopMatrix the saved matrix from the stack. This results in a sort of accumulation of the current ModelView matrix, like yoale said it does nothing.
Consider this: If you start out and translate along the -Z axis by 5.0 units, and do not reset the MODELVIEW matrix, the next time you translate by 5.0 on the -Z you will actually have translated by 10.0 on -Z.

Thus use:
//set up projection matrix

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//draw cube

this might solve your problem. if not scream :-) well try and help some more.

Oh, BTW, you do not need to set up the projection matrix each time you render a scene, unless you need to change it the whole time. Try setting it up once the app starts, and then redo it only when the window changes.