Reputation: 20421
I'm trying to rotate a triangle around it's center point. I'm aware that OpenGL rotates about the origin so I need to translate the middle point to the origin, then rotate, and translate back. I've commented out this last line to ensure that it at least rotates about its center at the origin. It does not. It appears to be rotating about its old origin despite the translation... Note that ccc4 and ccp generate floats. Here's my code:
ccColor4B colors[] = {
ccc4(255, 0, 0, 255),
ccc4(0, 255, 0, 255),
ccc4(0, 0, 255, 255)
};
CGPoint vertices[] = {
ccp(0,0),
ccp(50,100),
ccp(100,0),
};
CGPoint middle[] = {ccp(50,50)};
CGPoint origin[] = {ccp(0,0)};
// Rotate the triangle
glPushMatrix();
glTranslatef(-50, -50, 0);
glRotatef(45, 0, 0, 1.0);
// glTranslatef(50, 50, 0);
// Draw the triangle
glLineWidth(2);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
glColor4ub(0, 0, 255, 255);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
// Revert rotation, we only want triangle to rotate
glPopMatrix();
// Draw the points
glDisableClientState(GL_COLOR_ARRAY);
glPointSize(5);
glColor4ub(255, 255, 255, 255);
glVertexPointer(2, GL_FLOAT, 0, middle);
glDrawArrays(GL_POINTS, 0, 1);
glPointSize(5);
glColor4ub(0, 255, 0, 255);
glVertexPointer(2, GL_FLOAT, 0, origin);
glDrawArrays(GL_POINTS, 0, 1);
glEnableClientState(GL_COLOR_ARRAY);
// End points
Here's the output:
Upvotes: 2
Views: 16003
Reputation: 185852
You need to think of transforms as applying in reverse relative to the order in which you call them.
Actually, it's easier to think in terms of transforming the local coordinate system (LCS), not the object, which allows you to mentally apply transforms in the order they're called. To rotate about the center, translate the LCS to the center, rotate, then translate it back out again:
glTranslatef(50, 50, 0);
glRotatef(45, 0, 0, 1);
glTranslatef(-50, -50, 0);
Upvotes: 6