# 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.

## 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 on other sites
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.

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 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);
glVertex2i(100,100);
glVertex2i(100,200);
glVertex2i(200,200);
glVertex2i(200,100);
glEnd();

glPopMatrix();

1. 1
2. 2
3. 3
Rutin
20
4. 4
khawk
14
5. 5

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013186
×