PDA

View Full Version : rotatef problems



AngryHippo
12-02-2010, 03:37 AM
Hi. I am simply trying to rotate a triangle using rotatef but whenever I use it it not only rotates but travels around the screen. I have read multiple threads here about rotatef but they don't seem to fix my problem. Here is my code:

glPushMatrix();
glRotatef(100,0,0,1);
glTranslatef(400,200, 0);
glBegin(GL_TRIANGLES);
glVertex2i(-40, -40);
glVertex2i( 40, -40);
glVertex2i(-40, 40);
glEnd();
glPopMatrix();

I have seen people give reasonable explanations about how it is rotating around the origin and I need to translate to a new origin but I am pretty sure I am using glTranslate correctly? Is rotatef only to be used for 3d?

ZbuffeR
12-02-2010, 06:21 AM
Swap your rotate and translate lines .

AngryHippo
12-02-2010, 09:40 AM
Wow... Why does it magically work when you tell me?! I swear I had it that way at one time. All those useless hours spent.. I could have also sworn that some site said that you rotate first then translate. But thank you for giving me peace of mind!

aqnuep
12-02-2010, 11:33 AM
Actually you first rotate and then translate, that's how you should position your object.
The only thing you did not considered that OpenGL virtually executes the transforms in the opposite direction:

glRotatef(100,0,0,1);
glTranslatef(400,200, 0);
...
glVertex2i(-40, -40);

=> Rotate * Translate * Vertex

So actually in your case the translation was first applied, instead of the rotation.