Advertisement

Quaternions

Started by February 25, 2004 01:03 PM
11 comments, last by qmuffs 21 years ago
I''m trying to use quaternions to do my rotation of my object around me and this code acts just like the glRotate function would. Any suggestions? GLfloat pMatrix[16]; glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glLoadIdentity(); glQuaternion q1; glQuaternion q2; glQuaternion q3; glQuaternion q4; q1.CreateFromAxisAngle(1,0,0,xrot); q2.CreateFromAxisAngle(0,1,0,yrot); q3.CreateFromAxisAngle(0,0,1,zrot); q4 = q1 * q2 * q3; q4.CreateMatrix(pMatrix); glMultMatrixf(pMatrix); glTranslatef(0,0,-16.0); glBegin(GL_TRIANGLES); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f( 1.0f,-1.0f, 1.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f( 1.0f,-1.0f, 1.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f,-1.0f, -1.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f,-1.0f, -1.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f(-1.0f,-1.0f, -1.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); glEnd();
QMuffs
What did you expect it to do?


"Sneftel is correct, if rather vulgar." --Flarelocke
Advertisement
I rotate my view on the x and y axises, place my object, and when it hits the 90 degree point on the axises the rotation looks like it would with glRotatef. I need it to use an arbitrary axis and I thought that is what quaterions did. I''m hoping there is more to it.
QMuffs
They do do that, but you created three euler angles and combined them thus defeating the purpose of using quats. You should specify an arbitrary axis if that is what you want rather than using the three coordinate axes
How would I go about doing that?
QMuffs
For all of your Quaternion questions, you should first check the Matrix and Quaternion FAQ. In particular, Question #56 tells how to generate a quaternion for a rotation about an arbitrary axis.

J.W.
Advertisement
Let me see if I got this. I take my axis angle and create my Quaternion, Normalize the Quaternion to get my Unit Quaternion(Arbitrary Axis), Then Rotate on that Arbitary Axis.
QMuffs
A quaternion represents an orientation in 3D space. Multiplying two (by the quaternion multiplication rule) combines the rotation. So what you need to do is store the rotation of the object in a quaternion. Store the rotation about the arbitrary axis in a quaternion and multiply these. What you seem to do is not an arbitrary axis solution, but an euler angle solution. what you want is a function like:

glQuaternion MakeRotation(float rot, vector3 axis)

Marty
_____ /____ /|| | || MtY | ||_____|/Marty
How do I extract my new axii out of my final rotation.
QMuffs
quote:
Original post by qmuffs
How do I extract my new axii out of my final rotation.

You can extract it from the X, Y, and Z values of the quaternion. But you shouldn''t really need to get the axis out of the quaternion.


"Sneftel is correct, if rather vulgar." --Flarelocke

This topic is closed to new replies.

Advertisement