world rotation v. body rotation

Pick up a copy of the red book (OpenGL Programming Guide) or find it online and read (several times if necessary) the chapter on this stuff. Practice by implementing a simple solar system with gluSphere’s or something.

i have the book, but i am still unclear which is why i am posting this. thanks for the tip tho

a little OT but
Deisiumm said he stores a matrix them

What I’ve usually done is to keep track of a rotation matrix for the object. Then for each new rotation applied, you PRE-multiply the new rotation to your stored rotation matrix.

isnt that a little subject to accumulating floating point errors? so when you rotate 360 degs then undo that rotation by rotating -360 degs, your final matrix is not the same as your initial matrix?

i thought that was one reason why we pushed ,rotated then popped. Rather than just rotate and unrotate.
feel free to correct me

You keep track of your matrix transformations in variables because if you had a scene with 10000 objects, it will be impossible to derrive which one of their previous position from the current matrix. There is a limit to how many matrices you can push and pop. So accumulating matrix transformations is not an option and floating point error is not even a concern.

And I dont think he meant to say to unrotate what you rotate. I think he meant that before a new matrix transformation takes place, you push the matrix, you load the identity to clear the cumulative matrix transformations, apply the transformation, and pop the matrix to restore your previous matrix, which kind of sounds like you are undoing what you do. This allows for indepenent movement of objects where each object has its won position and orientation.

[This message has been edited by mancha (edited 03-06-2003).]

// Possibly called on a key event
// Similar functions for rotating Y and Z
void OnRotateX(float angle, object o)
{
Matrix rot = createRotationMatrix(angle, 1.0, 0.0, 0.0);

// PRE multiply so it’s that this last
// rotate is the last rotation done
o.rotMatrix = rot * o.rotMatrix;
}

void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

foreach(object o in objectlist)
{
glPushMatrix();
glTranslatef(o.pos.x, o.pos.y, o.pos.z);
glMultMatrixfv(o.rotMatrix);
o.Draw();
glPopMatrix();
}

}

By using a method like this you don’t restrict the order of which axes you rotate around. So if you wanted you could rotate 45 degrees around the X, 25 around the Z, 10 around the X again, 15 around the Y, etc.

[This message has been edited by Deiussum (edited 03-06-2003).]

thanks a bunch Del:

will this method also work if i switch between the rotation modes: ie: world and object? like i try to rotate an object around world’s x-axis and then want to rotate an object around it’s z-axis?

your method should work since these are after all just rotations that are kep in the matrix…is that correct?

In that case you might want to keep 2 rotation matrices, and then do your render like so…

glPushMatrix();
glMultMatrix(o.worldRotMatrix);
glTranslatef(o.pos.x, o.pos.y, o.pos.z)
glMultMatrix(o.localRotMatrix);
o.Draw();
glPopMatrix();

Or, an alternative would be when you rotate it around the world axis, you could calculate a new x,y,z for the position.

hmmm, why can’t i just store all of them in one matrix ( rotation matrix).
after all of them are really just rorations that are done with glRotate() with different parameters

oh one thing i have neglected to tell you is that i need to remember all of the transformations that were done to the object prior to rotation. So, if i have first rotated it around world’s y-axis and then around its axis, i need to remember that worlds rotation in order to do that correctly.

The order of operations does make a difference. Assume for instance that your model is such that it’s local center is at 0,0,0. Initially when you draw it, it will be at the same location as the world origin, so rotations done on it appear to influence just the object. Now when you translate it away from the origin, your rotations will no longer appear to rotate it around it’s own origin, but around the world origin.

Because the last thing applied to the matrix is effectively the first thing done, your order of operations is something like…

glRotate(rotation around world origin);
glTranslate();
glRotate(rotation around local origin);
drawObject();

About storing the location, etc. You should be doing that anyway. How else do you expect to re-draw the scene if the screen needs to be refreshed? Or for animation when you need to update the scene?

Hello again,

I don’t known how you people draw your object in the world, but what i do is that for each object that i am going to draw that can be later moved or rotated then i use internal variables to store the current world x,y,z object position and internal variable for store current xangle, yangle, zangle rotation for each object. This is easily done coding a litle class.

Then i use my x,y,z current object position and xangle, yangle and zangle in follow way:

//you said you want to rotate the world too, so then:

glpushmatrix(); //save current world state.
glrotate(world.xangle,1,0,0); //rotate by x
glrotate(world.yangle,0,1,0); //rotate by y
glrotate(world.zangle,0,0,1); //rotate by z

//now draw each object in follow way.

glpushmatrix(); //save current world matrix
gltraslatef(x,y,z); //move the world to the model position. x,y,z are object position.
glrotate(xangle,1,0,0); //rotate by x
glrotate(yangle,0,1,0); //rotate by y
glrotate(zangle,0,0,1); //rotate by z

drawobject(); //redraw object with current matrix

glpopmatrix(); //restore world to previus state. so we can continue drawing another object.

//Now do the same for next object.
//after all objects are draw then restore the matrix that we had before rotation the world.

glpopmatrix();

that’s all you need to do for draw a object at different world position and be rotated correctly.

A note: me particulary I never rotate the world, i move/rotate the camera instead to the place that i want to see in my scene.

you can see few pics about my opengl world level editor at:
http://www.geocities.com/cyber_delphi/
http://www.geocities.com/cyber_delphi/tre3d/index.html

good luck litle girl.

Turbo Pascal.

[This message has been edited by Turbo_Pascal (edited 03-07-2003).]

[This message has been edited by Turbo_Pascal (edited 03-07-2003).]

thanks to everyone who contributed to this post!!!
i have now figured out a way to do this, my problem was that i put all my misundertsandings together which made it a lot harder to solve