PDA

View Full Version : problem with Rotate() and Translate()



openujs
12-26-2005, 08:29 AM
In the following codes, how come if we comment out
glRotatef( 70, 0.0, 1.0, 0.0 ), the Quad draws on right side ( positive x axis), but if that Rotate() is enabled, it draws somewhere in left ( negative x axis), also it is affectiving first Quad drawing somehow, affects even when calling glPushMatrix() and glPopMatrix(). Help me understand. Thanks.


setColor( 1.0, 0.0, 0.0);
glRotatef( 40, 0.0, 1.0, 0.0 );
glBegin( GL_QUADS );
glVertex3f(-1.0f, 1.0f, 0.0f);// Top Left
glVertex3f( 1.0f, 1.0f, 0.0f);// Top Right
glVertex3f( 1.0f,-1.0f, 0.0f);// Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f);// Bottom Left
glEnd();

setColor( 1.0, 1.0, 0.0 );
glRotatef( 70, 0.0, 1.0, 0.0 );
glTranslatef( 20.0,0.0,0.0 );
glBegin( GL_QUADS );
glVertex3f(-1.0f, 1.0f, 0.0f);// Top Left
glVertex3f( 1.0f, 1.0f, 0.0f);// Top Right
glVertex3f( 1.0f,-1.0f, 0.0f);// Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f);// Bottom Left
glEnd();

memfr0b
12-26-2005, 09:38 AM
You're rotating 40 degree around y, then another 70 degree around y (resulting in 110 degree around y), and then moving 20 units along the _rotated_ x axis.

I assume what you want is one quad centered and rotate 40 degrees, and another positioned at (20, 0, 0) and rotated 70 degrees. You would get that result with the following code:


setColor( 1.0, 0.0, 0.0);

glPushMatrix(); // store current transform, so we can undo the rotation

glRotatef( 40, 0.0, 1.0, 0.0 );
glBegin( GL_QUADS );
glVertex3f(-1.0f, 1.0f, 0.0f);// Top Left
glVertex3f( 1.0f, 1.0f, 0.0f);// Top Right
glVertex3f( 1.0f,-1.0f, 0.0f);// Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f);// Bottom Left
glEnd();

glPopMatrix(); // undo rotation

setColor( 1.0, 1.0, 0.0 );

glPushMatrix();
glTranslatef( 20.0,0.0,0.0 ); // move to new location
glRotatef( 70, 0.0, 1.0, 0.0 ); // then rotate
glBegin( GL_QUADS );
glVertex3f(-1.0f, 1.0f, 0.0f);// Top Left
glVertex3f( 1.0f, 1.0f, 0.0f);// Top Right
glVertex3f( 1.0f,-1.0f, 0.0f);// Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f);// Bottom Left
glEnd();

glPopMatrix();Notice that i changed the order of glRotate and glTranslate for the second quad.