Okay so, I noticed when you rotate around an axis, say you do this:

glRotatef(90.0f, 1.0f, 0.0f, 0.0f);

That will rotate things 90 degrees around the x-axis. However, it also sort of rotates the y and z axes as well. So now the y-axis is pointing in and out of the screen, instead of up and down. So when I try to do stuff like this:

glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glRotatef(whatever, 0.0f, 1.0f, 0.0f);
glRotatef(whatever2, 0.0f, 0.0f, 1.0f);

The rotations around the y and z-axes end up not how I want them. I was wondering if there is any way I can sort of rotate just the axes back to their initial position after using glRotatef(), without rotating the object back. Or something like that, just so that when I rotate around the y-axis, it rotates around a vertical axis.

EDIT: After further research, it appears I will have to learn quaternions. EDIT2: Wait, I don't think that's right. Does anyone know? I saw this thread on another site: http://java-gaming.org/index.php?topic=24177.0 but I don't understand what he did.

  • 1
    \$\begingroup\$ Are you thinking of push/pop matrix? \$\endgroup\$ Commented Jun 28, 2011 at 8:34
  • \$\begingroup\$ I'm not sure how I could use that in my situation. I have a bunch of cubes on screen at once, each with their own rotations around x, y, and z. Before I render each one, I use glRotatef() three times for each of their rotations. \$\endgroup\$ Commented Jun 28, 2011 at 8:41
  • \$\begingroup\$ So you push the matrix before doing the rotations, and pop it afterwards so the rotations don't affect other stuff. I probably just didn't understand your question :P. (Oh and you need to rotate in Z,X,Y order) \$\endgroup\$ Commented Jun 28, 2011 at 8:43
  • \$\begingroup\$ Yeah. I meant that after rotating around X 90 degrees, and then rotate around Y, it treats it like a rotation around Z, because the Y axis itself rotated as well. It's not an issue of a rotation affecting an object I didn't want it too. \$\endgroup\$ Commented Jun 28, 2011 at 8:50
  • 2
    \$\begingroup\$ Quaternions are a nice representation of rotations that will stop you from getting gimbal lock. If you have a local rotation matrix for each object and you apply the rotations in the right order you will end up with the same thing. Check this out. \$\endgroup\$ Commented Jun 28, 2011 at 9:32

3 Answers 3


I personally don't care much for answers that are just links to other stuff, but these two tutorials explain matrices and transformations really well. They explain why it is that creating a Y-rotation matrix doesn't rotate about world-space Y, and the second link explains how to rotate things in different spaces, and how to overcome these kinds of issues.

Yes, the tutorials are for shader-based programming. But transformation matrices are the same, whether using shaders or fixed-function.

In the interest of full disclosure, I wrote these, so take the above for what it's worth.


Once your matrix is no longer aligned with the world axes, you have to apply the rotations about the local space axes, not the world space axes.

Doesn't OpenGl have a method for rotating around an arbitrary axis?

If so, store the basis vectors frome the previous matrix's rotation as the Local space axis vectors and use them as an arbitrary axes to rotate the matrix about during the next rotation.

  • \$\begingroup\$ glRotate* constructs an arbitrary axis/angle rotation matrix -- the last three parameters are the components of the vector specifying the axis to rotate about. \$\endgroup\$
    – user1430
    Commented Jun 28, 2011 at 14:35
  • \$\begingroup\$ That makes sense. The OP just has to keep track of his basis vectors then and utilze them in the subsequent operations. \$\endgroup\$
    – Steve H
    Commented Jun 28, 2011 at 16:29

You need to do the following.

Store the rotation around the x, y and z axis separately for each object (as floats).

Every frame:

  • reset the rotation matrix (glLoadIdentity();)
  • apply roll
  • apply pitch
  • apply yaw

If you do it in this order you should not have any problems. Just make sure you don't do 'repeated rotations' since then you get a lot of problems.

Edit: sorry I first had the wrong order here, I probably got confused because of left-hand / right-hand differences.

  • \$\begingroup\$ -1 It is exactly what the asker did. if you apply a 90 degrees rotation for pitch you have the same problem. Zero solution \$\endgroup\$ Commented Jun 28, 2011 at 12:44
  • \$\begingroup\$ Sorry, we're both wrong, I just mixed up the order in this answer. The correct order is roll, pitch, yaw. I've updated the answer. \$\endgroup\$
    – Roy T.
    Commented Jun 28, 2011 at 16:04
  • \$\begingroup\$ Hey @roy, yes the order does not matter. The point is that each rotation is in local space not in world space, it rotates around the local y-axis, not around world's y-axis, that's what confused the asker, and that will be the same with your setup and each other rotation order if one just calls 3 times glRotate for each axis like the asker did \$\endgroup\$ Commented Jun 28, 2011 at 16:10
  • \$\begingroup\$ Oh, I C something dawning now about OpenGL not having a 'world matrix' only an 'local object matrix' and a worldview matrix. Is that what you mean? \$\endgroup\$
    – Roy T.
    Commented Jun 28, 2011 at 16:13
  • \$\begingroup\$ Not really roy, its a normal property of euler angles and you usually want it that way. Imagine you fly an airplane, you dont care about the world's coordinate system, all you care about is your local refence frame, the one of the airplane. Otherwise a change of heading would be extremly complicated, you just want to turn 20 degrees to the right and where right is is defined by the airplane, not by the world. \$\endgroup\$ Commented Jun 28, 2011 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .