Rotation of Polygon

I am trying to rotate a polygon about it’s own center (0.375, 0.375) but it is rotating around (0,0). The following is a portion of some test code that I have been using in trying to figure this out:

// Draw main polygon
glRectf (-0.75, -0.75, 0.75, 0.75);

GLint min_x(0.25);
GLint min_y(0.25);
GLint max_x(0.50);
GLint max_y(0.50);

// Draw sub-polygon with no-rotation (upper-right quadrant)
glBegin(GL_LINE_LOOP);
glVertex2f (min_x, max_y);
glVertex2f (max_x, max_y);
glVertex2f (max_x, min_y);
glVertex2f (min_x, min_y);
glEnd();

// Draw sub-polygon with 45.0 rotation (instead of rotating the polygon 45.0
// degrees on the polygon’s center (0.375, 0.375) the polygon is rotated on the
// x,y axis center (0,0) and winds up drawn on the y axis rotated 45.0 degrees)
glPushMatrix();
glRotatef (45.0, 0.0, 0.0, 1.0);
glBegin (GL_LINE_LOOP);
glVertex2f (min_x, max_y);
glVertex2f (max_x, max_y);
glVertex2f (max_x, min_y);
glVertex2f (min_x, min_y);
glEnd();

How is one to rotate the polygon about it’s own center?

You were doing it right…but you forgot the glPopMatrix() call at the bottom.

  • Halcyon

Everything in openGL is rotate based on an origin of 0,0,0.

You need to translate your object to the openGL origin before rotating.

Note the translate and rotate effect the object in reverse order.

glPushMatrix();
glRotatef(…) rotate object
glTranslatef( -0.375, -0.375, 0.0); Change objects origin to 0,0
draw_polygon();
glPopMatrix();

This should do the trick.

Originally posted by Magpie:
[b]I am trying to rotate a polygon about it’s own center (0.375, 0.375) but it is rotating around (0,0). The following is a portion of some test code that I have been using in trying to figure this out:

// Draw main polygon
glRectf (-0.75, -0.75, 0.75, 0.75);

GLint min_x(0.25);
GLint min_y(0.25);
GLint max_x(0.50);
GLint max_y(0.50);

// Draw sub-polygon with no-rotation (upper-right quadrant)
glBegin(GL_LINE_LOOP);
glVertex2f (min_x, max_y);
glVertex2f (max_x, max_y);
glVertex2f (max_x, min_y);
glVertex2f (min_x, min_y);
glEnd();

// Draw sub-polygon with 45.0 rotation (instead of rotating the polygon 45.0
// degrees on the polygon’s center (0.375, 0.375) the polygon is rotated on the
// x,y axis center (0,0) and winds up drawn on the y axis rotated 45.0 degrees)
glPushMatrix();
glRotatef (45.0, 0.0, 0.0, 1.0);
glBegin (GL_LINE_LOOP);
glVertex2f (min_x, max_y);
glVertex2f (max_x, max_y);
glVertex2f (max_x, min_y);
glVertex2f (min_x, min_y);
glEnd();

How is one to rotate the polygon about it’s own center?[/b]

what nexusone said, except that you probably also want to translate the object back to its center location after you rotate. so the code becomes

glPushMatrix();
glTranslatef(object_center); //translate object back to its center location
glRotatef(…);
glTranslatef(-object_center); //translate object center to origin
draw_polygon();
glPopMatrix();

[This message has been edited by SThomas (edited 02-25-2003).]

St Thomas, you were spot on with your suggestion. Thank you all for your contributions!!

Cheers Magpie…