PDA

View Full Version : Exemplified help with quaternions and rotations

Dingo Egret
03-10-2004, 03:58 PM
Hello!

I was hoping someone could show me some examples (with code!) of how rotation can be achieved though the use of quaternions.

I have studied several quaternion tutorials that i've found on the web and they all do a great job in explaining the math and the theory behind quaternions but they all assume that i already know how to put them to work.

So far, i've been using glRotatef()'s to do sequential rotations first in one axis then in the next one and then in the next one. I often end up twiddling the order of these rotations until it does what i want, but it's all trial and error. I'm also not sure if i've encountered the infamous Gimbal Lock... to tell the truth i don't fully understand when or why it happens.

If i understand it correctly, by using quaternions to generate rotation matrices, and then applying these rotation matrices, i can solve my "order of rotations" problem and gimbal lock. Right?

Say i want to rotate an object A degrees along the x axis, B degrees along the y axis and C degrees along the z axis.

Is this information in what OpenGL describes as "euler angles" form?

Do i have to then use something like glEulerToQuat() to transform it into a quaternion and then glQuatToMat() to further transform it into a matrix that i can then apply with glMultMatrixd()?

Why is there no function to transform from euler angles to a matrix directly, BTW?

Say i have the following code:

glPushMatrix();
glRotatef(A, 1.0, 0.0, 0.0);
glRotatef(B, 0.0, 1.0, 0.0);
glRotatef(C, 0.0, 0.0, 1.0);
glutWireSphere(1.0, 30, 30);
glPopMatrix();

How can i do the same thing using quaternions?

Thank you very much for the info.

roffe
03-10-2004, 09:27 PM
I found this link searching.

It seems to explain quaternions use,representation,code and also a demo of gimbal lock.
http://www.gamedev.net/reference/programming/features/qpowers/default.asp

Do i have to then use something like glEulerToQuat() to transform it into a quaternion and then glQuatToMat() to further transform it into a matrix that i can then apply with glMultMatrixd()?

Yes and yes. You would convert your axis-angle representations to separate quaternions and multitply them together in the order you choose. Then convert the final quaternion to matrix form and submit it to OpenGL.

Why is there no function to transform from euler angles to a matrix directly, BTW?

Of course there is, glRotatef creates a rotation matrix for the specified vector and angle and multiplies it with the matrix on top of the stack. It does not use quaternions internally just beacause there is no need to, with the current interface. If there ever was a "glRotateAroundTheseVectorsInThisOrder" type of function it might, but then again it would be considered a utility function and be put in glu at best.

[This message has been edited by roffe (edited 03-10-2004).]

Dingo Egret
03-17-2004, 07:38 AM
>http://www.gamedev.net/reference/programming/features/qpowers/default.asp

This is a really poor tutorial. Finally, i ended up figuring it all out on my own, though its still not completely clear. I understand now what Gimbal Lock is, and i solved it using the following code in place of the one above:

// declare
GL_QUAT goal_quat;
GLfloat rot_matrix[4][4];

// create a quaternion out of the 3 separate angles
gluEulerToQuat_EXT(A, B, C, &goal_quat);

// make sure its a unit quaternion
gluQuatNormalize_EXT(&goal_quat);

// turn the quaternion into a rotation matrix
gluQuatToMat_EXT(&goal_quat, rot_matrix);

// multiply it with the top of the stack
glMultMatrixf((GLfloat *)rot_matrix);

// draw
glutWireSphere(1.0, 30, 30);

And i found the source for these functions here: http://www.gamasutra.com/features/19980703/bobic.zip
It compiles in both winbugs and linux.

roffe
03-17-2004, 09:00 AM
Originally posted by Dingo Egret:
>http://www.gamedev.net/reference/programming/features/qpowers/default.asp

This is a really poor tutorial. Finally, i ended up figuring it all out on my own, though its still not completely clear

Well, tutorials with code are usually not that heavy on theory.But I'm sure the "tutorial" below will be better for you. It should make you completely clear on the subject.
Hamilton, W. R. Elements of Quaternions (http://www.amazon.com/exec/obidos/ASIN/0828402191/weisstein-20/102-1969239-6110552) . London: Longmans, Green, 1866.