# OpenGL rotate quad around centre

## Recommended Posts

deepdene    292
Hi, I have a ortho matrix setup for the projection matrix, with the following values
	glOrtho(-100, 100, -100, 100, -1, 1);

Essentially I have a quad which is being rendered which has the coordinates set to the lower left hand corner. As so:

const GLfloat coords[] =
{
0.0f, 0.0f, // top left
0.0f + size.x, 0.0f, // top right
0.0f + size.x, 0.0f + size.y, // bottom right
0.0f, 0.0f + size.y // bottom left
};

glPushMatrix();

// set the current colour (white R=G=B=1)
glColor3f(color.x, color.y , color.z);

glTranslatef(location.x, location.y, 0.0f);
glRotatef(rotationDegreesAngle, 0.0f, 0.0f, 1.0f);

glVertexPointer(2, GL_FLOAT, 0, coords);
glEnableClientState(GL_VERTEX_ARRAY);

glPopMatrix();

The issue I am having is the glRotatef works correctly, however, it pivots the quad on the corner, not in the centre. How would I change glRotate to pivot around the centre of the object. Thanks in advance.

##### Share on other sites
Brother Bob    10344
Rotations are always around the origin. So to rotate around another point, you need to translate the object to the origin, rotate, and then reverse the translation.
glTranslate(x, y, z);glRotate(...);glTranslate(-x, -y, -z);

The origin of rotation will be (x,y,z).

Or, you can specify the object around it's center to begin with. That is, specify the quad as extending from -size/2 to size/2, instead of from 0 to size.

##### Share on other sites
deepdene    292
Quote:
 Original post by Brother BobRotations are always around the origin. So to rotate around another point, you need to translate the object to the origin, rotate, and then reverse the translation.glTranslate(x, y, z);glRotate(...);glTranslate(-x, -y, -z);The origin of rotation will be (x,y,z).Or, you can specify the object around it's center to begin with. That is, specify the quad as extending from -size/2 to size/2, instead of from 0 to size.

Hi Brother Bob.

Changing the quad to -size/2 and size/2 was the best solution for me.

Thanks.