0
votes

Let's say we have a square, and after it is translated to some location, I want to rotate it around a point within the object.

     glPushMatrix();
       glTranslatef(50, 50, 0.0);
       glRotatef(joint, 0.0, 0.0, 1.0);
       glScalef(10,20, 1.0);
       glTranslatef(0.0, -0.5, 0.0);
       glColor3f(1.0, 0.0, 0.0);
       drawSquare(1.0);
     glPopMatrix();

The one above only rotates around the (0.0) point after it's scaled. If I change the glRotatef(joint, 0.0, 0.0, 1.0) to glRotatef(joint, 0.0, 5.0, 1.0), then the object starts getting twisted.

enter image description here

1

1 Answers

1
votes

Rotations in OpenGL rotate around an axis placed at the origin (0, 0, 0). In order to rotate around a specific point, you need first to translate that point to the origin, perform your rotation. In the OP, you would be rotating around the point (-50,-50, 0) (since translations move coordinate systems, and not specific points), and then rotating joint degrees around the Z axis.

From the OP, assuming that joint is updated each frame, I would expect that code to have the square orbiting around the point (-50, -50, 0).