View Full Version : rotation along the axis of the object

ukopenglnewbie

12-26-2008, 06:38 AM

I have a cube on screen that I can rotate using the standard: glRotatef(...). The rotation is done ok as long as it is aligned to the Global/Initial/Default XYZ axis of (1,0,0) , (0,1,0) and (0,0,1). What I am trying to do is rotate the cube along its OWN axis which obviously changes as the cube changes. How is that achieved ?

assume there is no translation. I can obtain the XYZ axis of the cube at any time and I can rotate along the local X axis for a while before the cube starts spinning in all directions. I am assuming this is due to axis no longer being 90 degrees to each other.

dletozeun

12-26-2008, 11:49 AM

To rotate around cube axis, translate to its center, rotate around the wanted axis, then translate to its position in world space.

Remember that all these transformations are done via matrices in opengl, that is why they are done in the reverse order.

ukopenglnewbie

12-26-2008, 12:08 PM

Thank you for the reply. The cube actually stays in the centre. i.e. it is centered at 0,0,0 so no translation is taking place.

When you rotate the cube say around the y-axis, the cube's X axis obviously changes. How can I then rotate around the X-Axis ? do I have to use glRotated(angle, X, Y, Z) ? and what values to put in the X , Y and Z. As far as I know that would be the X axis of the cube obtained from the matrix. This works ok and after a while the cube starts spinning out of control.

dletozeun

12-26-2008, 03:35 PM

As far as I know that would be the X axis of the cube obtained from the matrix

Yes that's correct. If the cube XYZ axis are aligned at the beginning with the world space XYZ axis then the matrix that stores the cube transformation contains in each column the transformed X, Y and Z axis. The 1st column is X, the 2nd, Y and the 3rd, Z.

I think they should be normalized, but you can renormalize them after extracting them from the matrix. Then you can use these vectors as the rotation axis in the glRotate function, but remember that they should be normalized.

Now, I don't see for now why the cube is spinning out of control... It would be interesting to have more precisions about your implementation.

The type of rotations you are asking for are sometimes called 'Object Space Rotations'. I coded these up many years ago (before OpenGL) and have been curious to see how difficult it would be in OpenGL. My goal was to do this without using any of my own rotation or matrix multiplication subroutines - just using OpenGL calls. Took me a while to work out the kinks, but I think I have it. I've posted it in the form of a simple OpenGL-GLUT program at:

Object Space Rotations (http://pastebin.com/m3c9cae18)

This program displays a cube with X-Y-Z axes labeled. You can rotate the entire scene using the arrow keys. AND - you can rotate the cube around its current X, Y,or Z axis using the 'x', 'y', or 'z' key on your keyboard. The arrow keys do conventional Euler Sequence Rotations (i.e. around either the X or Y axis). The 'x', 'y', and 'z' keys call glRotate with values extracted from the current orientation matrix (called 'max' in my code). Specifically, to do an X body rotation, glRotate is called with the first row of the current orientation matrix. To do a Y body rotation, the second row of the matrix is used, and so on.

ukopenglnewbie - I believe this example implements the approach you and dletozeun have been discussing.

ducky

04-07-2009, 07:38 PM

Can you repost this code? I have the exact same question as ukopenglnewbie. I think your code post on pastebin has expired.

Can you repost this code? I have the exact same question as ukopenglnewbie. I think your code post on pastebin has expired.

A slightly improved version of my original code is posted at: Object Space Rotations (http://pastebin.com/m343f77bd)

I have generalized the code I posted above to do both Object and Screen Space rotations. If anyone knows of a better way to do this stuff, please let us know ....

MaxH - Object and Screen Space Rotation Demo (http://pastebin.com/d625e7bf4)

For some reason the code I posted was deleted after about a day. So here it is again. This time with a lighted teapot instead of simply-colored cube.

Object & Screen Space Rotation Demo (http://pastebin.com/f27c757b2)

A cleaner version of the code I posted above is at:

Screen & Object Space Rotations (http://pastebin.com/d48822e68)

Was pointed out to me today that I messed up again and didn't post my code to pastebin correctly. Therefore it disappeared. So here it is again (should last for a month this time)....

OpenGL Screen & Object Space Rotations (http://pastebin.com/m451cf249)

Once again, the pastebin link expired. I've given up on pastebin and posted this source code to my own website -

Object and Screen Space Rotations (http://www.mfwweb.com/OpenGL/Special_Rotations/)

Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.