# OpenGL Taking desired world rotation and applying it locally

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

## Recommended Posts

I've been searching around and scratching my head, and while the math I get, actually implementing in OpenGL seems the tricky part.

So I have an object and right now I am using glRotatef to spin it using x, y, and z angles.

My user interface simply lets you choose (looking at the object) rotate left, right, up, down, clockwise, counter clockwise etc. I'm sure you can guess that things go wrong as soon as a few rotations have been applied since these are all being applied to the local rotation and this incrementing 90 degrees along x, y, z doesn't get the desired effect while the local axis are moving.

So what's the best way I can take in a desired world rotation and convert it to what needs to change locally to the object. I was working on a complicated way for taking the current rotation values, converting to a rotation matrix, multiplying with the desired world rotation matrix.... and then I realized I didn't know how to take the resulting matrix and pull the X, Y, and Z angles back out for the new local values.

When I did this same project in XNA I had things represented my matrices (I have several things being held in a 3x3 grid to keep track of their location in relation to one another). There I can just apply some matrix math. Now that I'm doing it in opengl I wanted to abstract the visual from the background representations so there's less impact and cross coding etc.

Any help is appreciated.

##### Share on other sites
If the problem you're having is that you can't get the objects to rotate relative to the world (about the object's origin) then wrap the calls glPushMatrix(); glPopMatrix(); commands.

For example:

glPushMatrix();// Render ObjectglPopMatrix();

If the problem you're having is that the objects don't rotate relative about their own origin, then translate the object to the world's origin, do the rotation, and then translate it back again.

##### Share on other sites
The first solution is already what I'm doing, but before I go ahead with the translation here is what's happening.

User clicks let's say the left arrow.

So they want to rotate the object from their view to the left 90 degrees.
rotX -= 90;

(I use X here because the view is rotated 90 degrees counter clockwise to start. So X is now the vertical axis, y horizontal, and Z is the depth axis)

then in my view drawing function when i'm drawing the object I dong for the rotation values in each axis.

glPushMatrix();    // scaling and vertices loading etc.glRotatef(rotX, 1, 0, 0);glRotatef(rotY, 0, 1, 0);glRotatef(rotZ, 0, 0, 1);    // set textures and colorsglPopMatrix();

As mentioned the only problem with this is as the user clicks the various buttons the axis will begin to change their orientation and the current method applies rotations locally. So I need to convert the world view desired rotation (ex. Left) to what needs to happen locally. Unless there's something I'm not getting about how glRotatef works, perfectly possible since matrix math has always given me trouble.

[Edited by - Zmaker5 on September 26, 2010 2:08:03 PM]

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 15
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631724
• Total Posts
3001904
×