OpenGL Rotation Problems..

Started by
3 comments, last by haegarr 13 years, 9 months ago
Im currently exploring OpenGL, and I have finally figured out how I can rotate 2D shapes in 3D space using OpenGL. However for some reason unknown to me the triangle decides to move to the left and right instead of just rotating on the stop as I intended it to do.

Here is my code:

#include <glut.h>float _angle = 30.0f;void Draw() {glLoadIdentity();glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glColor3f(1.0, 1.0, 1.0);glRotatef(_angle,0.0f,1.0f,0.0f);glBegin(GL_TRIANGLES);glVertex3f(0.25, 0.25, 0.0);glVertex3f(0.75, 0.25, 0.0);glVertex3f(0.25, 0.75, 0.0);glEnd();glutSwapBuffers();}void Update(int value){_angle+=0.5f;if(_angle>360){_angle-=360;}glutPostRedisplay();glutTimerFunc(25,Update,0);}void Initialize() {glEnable(GL_DEPTH_TEST);glClearColor(0.0, 0.0, 0.0, 0.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);}int main() {glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowSize(400, 400);glutInitWindowPosition(200, 200);glutCreateWindow("Rotation"); Initialize();glutDisplayFunc(Draw);glutTimerFunc(25,Update,0);glutMainLoop();return 0;}



Please Help,

Thanks In Advance,

Joe
Advertisement
Rotations are performed around the origin, but your triangle is not centered at the origin so it won't rotate around its own center. If you want the triangle to rotate about its own center, you need to make sure the center is at the origin; for example:
glBegin(GL_TRIANGLES);glVertex3f(-0.25, -0.25, 0.0);glVertex3f( 0.25, -0.25, 0.0);glVertex3f(-0.25,  0.25, 0.0);glEnd();

After rotation, move the triangle to its desired location with glTranslate.
But surely if I then translated it, the origin would no longer be in the center of the shape, so it would rotate wrongly?

Please Help,

Regards,

Joe
Which is why you rotate first, and then translate, so the center when rotating is at the origin.
Quote:Original post by Joesavage1
But surely if I then translated it, the origin would no longer be in the center of the shape, so it would rotate wrongly?
Don't confuse the model local origin and the global (world) origin. You should read on local co-ordinate spaces and space parenting.

A rotation ever happens around an axis that passes through 0. When you translate after having done the rotation, the translation will have no effect on the orientation. OpenGL pseudo code
glTranslate( t );
glRotate( R );
glVertex( v );
looks as homogeneous transformation like
T * R * v
and as affine transformation like
t + R * v

But if you do the translation first and then the rotation, OpenGL pseudo code looks like
glRotate( R );
glTranslate( t );
glVertex( v );
looks as homogeneous transformation like
R * T * v
and as affine transformation like
R * ( t + v ) = R * t + R * v
so you can see that now the rotation has an effect on the translation. You observed that because you've seen your triangle wandering around when being rotated.

You haven't translated the triangle explicitely but implicitely because how you've chosen its vertex positions. So either follow Brother Bob's suggestion to incorporate another position into the triangle, or else define the center of rotation by adding an appropriate glTranslate, like so
glTranslate( c + t );
glRotate( R );
glTranslate( -c );
glVertex( v );
where c denotes the center of rotation and t the position where the local space origin should be located.

This topic is closed to new replies.

Advertisement