# OpenGL Problems with rotations

This topic is 3875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i'm new here. I was looking for a good opengl forum, and i think this would be. I want to rotate the axes first 90 degree around the x axe, then 90 degree around y axe and then 90 degree around z axe. This is easy, and works fine in my program. But if I want to rotate 90 degree around the x axe, then 90 degree around z axe and then 90 degree around y axe, I have a problem. I use this: glRotatef(90,1,0,0); glRotatef(90,0,1,0); glRotatef(90,0,0,1); So the problem is the order in the matrix multiplication (always multiply the y axe matrix before z axe matrix). But I don't know how to do this only using glRotatef. (I have read about quaternions, euler angle, ... , but I want to do, if it is possible, only with glRotatef). Thanks

##### Share on other sites
Actually, if I'm not much mistaken, you've posted the code for rotations about the z, y and x-axes in that order, rather than the other way around. Since the matrices are post-multiplied, the last operation is essentially performed first on the vertices. That is, in your example, if, before your glRotate calls are performed, the transformation matrix is A, and we call your rotations B, C and D respectively, then the result after making the glRotate calls should be:

ABCD (A multiplied by B multiplied by C multiplied by D)

In matrix mathematics, this results in the operation D "taking effect" first when the matrix is applied to a vertex, followed by C, B and A, in that order.

(A note: this is the result of the matrix mathematics, as I recall, rather than OpenGL doing anything funny with the actual calls.)

PS: The English singular of "axes" is "axis", by the way - one axis, two axes. ^_^

##### Share on other sites
I know.

So you say that I have to change the order of glRotatef. I have tried and this is not the main problem.

I have a picture to show you the problem, but i don't know how to attach a picture here.

Thanks and thanks for the english correction.

[Edited by - zorro68 on May 7, 2008 8:18:48 AM]

##### Share on other sites
glRotatef(90,1,0,0);
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);

This is doing what you wanted to do. It rotates X then Y then Z if you want X Z Y

This will do X,Z,Y
glRotatef(90,1,0,0);
glRotatef(90,0,0,1);
glRotatef(90,0,1,0);

##### Share on other sites
Quote:
 Original post by ThaumaturgeThat is, in your example, if, before your glRotate calls are performed, the transformation matrix is A, and we call your rotations B, C and D respectively, then the result after making the glRotate calls should be:ABCD (A multiplied by B multiplied by C multiplied by D)

No, that's not the order OpenGL uses. If A is the original matrix and you call a matrix function with a matrix B, the resulting matrix is BA, not AB. Your sequence will be DCBA.

Zorro68: I'm not quite sure what your problem is, but are trying to say that you wonder why the resulting rotation is different when you change the order? Or are you experiencing gimbal lock, which is what happens when you rotate so, that rotating around one axis no longer is possible?

##### Share on other sites
Quote:
Original post by SnotBob
Quote:
 Original post by ThaumaturgeThat is, in your example, if, before your glRotate calls are performed, the transformation matrix is A, and we call your rotations B, C and D respectively, then the result after making the glRotate calls should be:ABCD (A multiplied by B multiplied by C multiplied by D)

No, that's not the order OpenGL uses. If A is the original matrix and you call a matrix function with a matrix B, the resulting matrix is BA, not AB. Your sequence will be DCBA.

Err, well, Thaumaturge isn't wrong but right! OpenGL uses column vectors, and OpenGL does post-multiplication (i.e. on the right side) when glTranslate, glRotate, glScale, or glMultMatrix is invoked. Hence, in an abstract form, the sequence
glAnyTransformation(A);
glAnyTransformation(B);
glAnyTransformation(C);
glVertex(v);
means mathematically
v' := A * B * C * v
Although OpenGL computes that (due to the order matrices are supplied) as
v' := ( ( A * B ) * C ) * v
it is true that the parantheses have no mathematical effect. E.g. the form
v' := A * ( B * ( C * v ) )
will give the identical result! I've chosen this form because it shows best what's "logically" happens: It is C that is applied to v, and B that is applied to the already transformed vertex, and A that is applied to those already twice transformed vertex.

##### Share on other sites
You made me check the Spec! Serves me right for not refreshing my memory earlier.

##### Share on other sites
Thank you, Haegarr. ^_^

Zorro, in order to display an image, first upload it to an image host and then include the relevant code in html <img> tags.

If you use ImageShack, then I believe that the code given for an image under "sites" should work (without any additional tags).

Quote:
 Originally posted by MARS_999glRotatef(90,1,0,0);glRotatef(90,0,1,0);glRotatef(90,0,0,1);This is doing what you wanted to do. It rotates X then Y then Z if you want X Z YThis will do X,Z,YglRotatef(90,1,0,0);glRotatef(90,0,0,1);glRotatef(90,0,1,0);

I'm pretty sure that the order is the opposite way around - respectively, I believe that those sets of calls would produce rotations about z, x and then y, and y, z, and then x.

##### Share on other sites
Thanks to all for a quickly answer.

But I (due to my bad english) don't explain you what is my problem. First at all, i say that i know the order of matrix, and what happend if you change this order. I know the gimbal lock problem, so I'm going to try to explain better:

I have 3 slices, one to rotate in the x axis, other to rotate in the y axis, and other for z axis.
If I move the slices in this order, first x , y and z (the program execute glRotatef(90,1,0,0), glRotatef(90,0,1,0), glRotatef(90,0,0,1);) and all works right.
But if I move the slices in this order, first x, z and y (the program execute the same, so the rotation is bad, I have to change the code).

So if I move the slices in different order, I have to change the code, and I need a code with glRotatef that always work.

I don't know if this is possible or not (with glRotatef) or if I need to implement quaternions or Euler angles or ....

Thanks and I hope you understand my problem.

##### Share on other sites
What I don't understand is that, if your rotating an object about its own local coordinates, which is what I assume your trying to do, those 3 rotations in any order is going to produce a similar affect? The main difference is going to be which faces are facing forward.

For instance if you have a square rotated about x, then y, then z at 90 degrees a piece, your always going to get the same square back, but by switching the order of rotations a different face will be front facing.

From your picture it looks to me that the rotations are working as they should, if you want it to produce a different result then you need to change the rotations.

1. 1
2. 2
Rutin
20
3. 3
khawk
17
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633757
• Total Posts
3013711
×