# LWJGL glRotatef() without rotating axes?

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.

• Are you thinking of push/pop matrix? – Jonathan Connell Jun 28 '11 at 8:34
• 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. – Brandon oubiub Jun 28 '11 at 8:41
• 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) – Jonathan Connell Jun 28 '11 at 8:43
• 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. – Brandon oubiub Jun 28 '11 at 8:50
• 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. – Jonathan Connell Jun 28 '11 at 9:32

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.

• glRotate* constructs an arbitrary axis/angle rotation matrix -- the last three parameters are the components of the vector specifying the axis to rotate about. – user1430 Jun 28 '11 at 14:35
• That makes sense. The OP just has to keep track of his basis vectors then and utilze them in the subsequent operations. – Steve H Jun 28 '11 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.

• -1 It is exactly what the asker did. if you apply a 90 degrees rotation for pitch you have the same problem. Zero solution – Maik Semder Jun 28 '11 at 12:44
• 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. – Roy T. Jun 28 '11 at 16:04
• 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 – Maik Semder Jun 28 '11 at 16:10
• 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? – Roy T. Jun 28 '11 at 16:13
• 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. – Maik Semder Jun 28 '11 at 16:27