PDA

View Full Version : By god, there's a lot of rotation q's on here



11-10-2000, 08:10 AM
Hey there. I'm trying to create a model of a hand using OpenGL and I'm getting most confused as to how to arrange and orient my objects. I'm currently using a perspective projection and doing a glTranslatef(0,0,-30) to get my objects into view. Problem is this then ruins my chances of being able to rotate my objects properly because I'm no longer at the origin. How can I rotate an object around a point that I specify? Hope that makes reasonable sense. Thanks in advance.

Succinct
11-10-2000, 08:17 AM
it's not true that your rotations will be screwed up.

see, ogl does it's transformations backwards, or so i've had it explained.

if you've translated (0,0,-30), all commands are now relative to (0,0,-30).
if you were to call glRotate and then some glVertex's, the vertices would be rotated relative to the origin. if you called glRotate,glTranslate, and then some glVertex's, then they would rotate relative to whereever you translated to.

.. or so i've been explained...

11-11-2000, 03:40 AM
But if I create a glutSolidCube say, and I want to rotate it about it's center - does that mean I have to translate it back to the origin to be able to do that? And if so, how am I going to bring the scene into view? Thanks.

ipo
11-11-2000, 08:19 AM
You have several options:

1. Draw the cube and then translate it. Both transformations are relative to the same coordinate system. After that translate the coordinate system itself so that the origin is centered within the cube. Then rotate about this new coordinate system.

2. Reset your "fixed" or "world" coordinate system. After that, draw the cube, rotate it and then translate it. All these transformations are relative to the same coordinate system.

3. As a third alternative, you could draw the cube and then translate it. Both transformations are relative to the same coordinate system. At this point compute the vector (within the same system) to which about you wish to rotate. Then perform the rotation about that vector.

I find 2) the most intuitive since it allows you to draw all the objects on the scene about the origin of a "fixed" coordinate system. Thereafter you can perform whatever transformations are required.

As an example of two cubes rotating about themselves consider this:

static GLfloat angle1 = 0.0;
static GLfloat angle2 = 0.0;

void drawScene(void) {
/*
clear background & initialize
coordinate system
*/

glClear(...);
glLoadIdentity();

/* draw & transform 1st cube */

glPushMatrix();
glTranslatef(-10.0, 0.0, 0.0);
glRotate(angle1, 1.0, 0.0, 0.0);
glutSolidCube(...);
glPopMatrix();

/*
reset system. Draw & transform
2nd cube
*/

glPushMatrix();
glTranslatef(+10.0, 0.0, 0.0);
glRotate(angle2, 0.0, 1.0, 0.0);
glutSolidCube(...);
glPopMatrix();

/* increment angles of rotation */

angle1 += 0.1;
angle2 += 0.2;

swapBuffers(...);
}

By pushing and poping the matrix in the example I perform all drawing relative to the same origin. Further, any transformations performed on an object do not affect the other objects in the scene.

Note that the transformations are written down in reverse order of hou you would compute them using matrix algebra.

There are situations where you would want to use either of the other two forms. For example, you could avoid the overhead of pushing and popping the matrices by accumulating the transformations.

11-12-2000, 04:00 AM
Thank you very much. That's most helpful. Now, have I understood it right that the last thing I should do before I draw my objects is translate them back on the Z axis so that they're in view in my frustrum? Or can I make a frustrum cover in front and behind of the origin? Thanks.

ipo
11-14-2000, 06:35 AM
Now I'm getting confused. What don't you just position the objects so that they lie within the frustrum rather than positioning them in such a way that you need to translate them back into view?

Alternatively I suppose you could specify the far plane to lie beyond your objects.

Deiussum
11-14-2000, 06:48 AM
Becuase of the way that matrix math works, the last matrix that is multiplied to the matrix stack is what is performed first.

For instance, if you have a Transformation matrix T and a rotation matrix R doing T*R effectively causes the rotation to be done first, then the transformation.

OpenGL works the same way. If you do a glTranslate before a glRotate, it does the rotate first.

Say you've got data for a cube with the center of a cube at the origin. If you do this..

glTranslatef(0.0, 0.0, -10.0);
glRotatef(45, 0.0, 1.0, 0.0);
drawCube();

You will rotate the cube 45 degrees around its center, then move it 10 units down the z-axis.

If you do it the other way...

glRotatef(45, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, -10.0);
drawCube();

You first end up moving the cube down the z-axis, then rotating it around the y-origin so it ends up not in front of you on the z-origin, but rotated at a 45 degree angle to the z-origin around the y-axis.

I hope that made helps. Not sure if it even really made sense. http://www.opengl.org/discussion_boards/ubb/smile.gif