PDA

View Full Version : Rotation doesn't work with glPushMatrix()/glPopMatrix(), and it should...



05-04-2002, 06:31 PM
Can anyone please tell me why in this code the first quad won't rotate?

(BTW. I'm using GLUT, if that helps.)

Display()
{
glPushMatrix();

glBegin(GL_QUADS);
glTexCoord2i(0,0);
glVertex3f(0.0,0.0,0.0);

glTexCoord2i(0,1);
glVertex3f(0.0,1.0,0.0);

glTexCoord2i(1,1);
glVertex3f(1.0,1.0,0.0);

glTexCoord2i(1,0);
glVertex3f(1.0,0.0,0.0);
glEnd();
glRotated(2,0,1,0);
glPopMatrix();

glPushMatrix();
glBegin(GL_QUADS);
glTexCoord2i(0,0);
glVertex3f(0.0,0.0,0.0);

glTexCoord2i(0,1);
glVertex3f(0.0,1.0,0.0);

glTexCoord2i(1,1);
glVertex3f(1.0,1.0,0.0);

glTexCoord2i(1,0);
glVertex3f(1.0,0.0,0.0);
glEnd();
glPopMatrix();
}


The first object won't rotate. If I place glRotated() outside the glPushMatrix()/PopMatrix() calls, both quads will rotate. For some reason in between it won't rotate...

Old GLman
05-04-2002, 07:18 PM
Hi,

First off please use code tags! It just makes it easier to read. I took out the
tex coord calls and made each quad a different color, just so I could run it real quick.



glColor3f(1.0f,0.0f,0.0f);
glPushMatrix();
glRotatef(yrot,0,1,0);
glBegin(GL_QUADS);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,1.0,0.0);
glVertex3f(1.0,1.0,0.0);
glVertex3f(1.0,0.0,0.0);
glEnd();
glPopMatrix();


glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_QUADS);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,1.0,0.0);
glVertex3f(1.0,1.0,0.0);
glVertex3f(1.0,0.0,0.0);
glEnd();

yrot += 1.0f;


To rotate the first quad you just have to save the matrix state, then do the rotation. Then revert back to the old state, and draw the second quad.

Old GLman

[This message has been edited by Old GLman (edited 05-04-2002).]

05-04-2002, 07:58 PM
Thanks for the response, I finally found out that glRotatef has to be controlled by a variable for the angle, or else it doesn't update in between glPushMatrix and glPopMatrix.

Thanks a lot :-).

Deiussum
05-06-2002, 04:32 AM
You were also using your glRotate after drawing the first quad. You have to setup the transformations BEFORE you draw what you want to draw. http://www.opengl.org/discussion_boards/ubb/smile.gif