Problems with rotations

Started by
20 comments, last by ma_hty 15 years, 11 months ago
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
Advertisement
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. ^_^

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

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]
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);
Quote:Original post by Thaumaturge
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)

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?
Quote:Original post by SnotBob
Quote:Original post by Thaumaturge
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)

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.
You made me check the Spec! Serves me right for not refreshing my memory earlier.
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).

I don't think that this site allows for uploads from free accounts, I'm afraid.

Quote:Originally posted by MARS_999
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);


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.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

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.


Free Image Hosting at www.ImageShack.us
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.

This topic is closed to new replies.

Advertisement