I am trying to rotate a quad around its center, but when I run the app the quad rotates around the 0,0 position of the screen, I cant work out why, as everything looks fine to me. Any help would be greatly appreciated.
glPushMatrix();
glTranslatef(pxob->x, pxob->y, 0);
glRotatef(pxob->theta,0.0f,0.0f,1.0f);
glBegin(GL_QUADS);
glColor3f((28.0f / 255.0f), (28.0f / 255.0f), (28.0f / 255.0f));
glVertex3f(pxob->x - (SIZE / 2), pxob->y- (SIZE / 2), 0);
glVertex3f(pxob->x + (SIZE / 2), pxob->y- (SIZE / 2), 0);
glVertex3f(pxob->x + (SIZE / 2), pxob->y + (SIZE / 2), 0);
glVertex3f(pxob->x- (SIZE / 2), pxob->y + (SIZE / 2), 0);
glEnd();
glPopMatrix();
EDIT:
Thank you Krom, for all your help and guiding me through it. I did have to call glTranslatef before glRotatef though as it wanted to still rotate around the 0,0 position. Thanks again!
You need to
glRotateffirst, thenglTranslatef.This is because first object needs to be multiplied by rotation matrix (rotate object around its center), and then apply the translation matrix (move the rotated object to desired position).
If you do it the other way round the translated object will be rotated around 0;0 coordinates.
EDIT: Looking at your code – you build the quad with X/Y offset whereas you’ve already applied it to
glTranslatef. Thats probably a mistake. The code should be: