Jump to content
  • Advertisement
Sign in to follow this  
aeltani

Push Pop Translation

This topic is 3662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to translate a square. I am using glPush and glPop. But no translation is happening. I am doing this because I am trying to rotate the square around it's center. If i don't use push and pop the center of rotation changes when I translate. But I can't figure how to use push and pop. thanks. Display() glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslated(-150,-150,0); glPopMatrix(); glBegin(GL_QUADS); glVertex2i(100,100); glVertex2i(100,200); glVertex2i(200,200); glVertex2i(200,100); glEnd(); ... }

Share this post


Link to post
Share on other sites
Advertisement
Quote:
glPushMatrix();
glTranslated(-150,-150,0);
glPopMatrix();


glPopMatrix() restores the matrix that was saved by glPushMatrix(). Thus the above code doesn't change the current matrix at all.
For more information read the documentation: http://www.opengl.org/sdk/docs/man/xhtml/glPushMatrix.xml

If you want to rotate then translate call glTranslated and then glRotate. Each of those functions will create a matrix M that is multiplied by the current matrix from right side(
CurrentMatrix=CurrentMatrix*M). Since the vertexes transform like CurrentMatrix*v the last multiplied matrix will act first:

CurrentMatrix*v = Identity*Translate*Rotate*v = Identity*(Translate*(Rotate*v))

As you can see Rotation applies first then the result (rotated vertex) is translated.

To create a rotation matrix with a center at (x,y) we call
glTranslatef(x,y,0);
glRotatef(...)
glTranslatef(-x,-y,0);

The last transformation will make (x,y) the new (0,0), then we rotate and translate (0,0) back to (+x,+y).

Share this post


Link to post
Share on other sites
You need to draw the quads between the push and pop commands; otherwise nothing is going to appear. This should work:

glPushMatrix();

glTranslated(-150,-150,0);
glBegin(GL_QUADS);
glVertex2i(100,100);
glVertex2i(100,200);
glVertex2i(200,200);
glVertex2i(200,100);
glEnd();

glPopMatrix();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!