## Quaternion rotation -&gt; OpenGL rotation matrix?

DJBlufire
Unregistered

Post: #1
I'm not sure if i'm asking the question correctly, but i'm asking for a friend of mine who is working with quaternions.

He needs to know how to take a quaternion orientation and turn it into something he can use in OpenGL to move an object forward, given its quaternion orientation.

Sorry if i'm not exactly being clear.
Moderator
Posts: 365
Joined: 2002.04
Post: #2
I was going to post code, but I'm not sure where I would stand on licensing issues, so I'll direct you (or him) to appropriate places instead. If you look at Quesa's E3Math.c source, E3Matrix4x4_SetQuaternion() can be used to convert a quaternion into a 4x4 matrix. Alternatively, if all your friend wants to do is rotate his forward vector by the quaternion, the E3Vector3D_TransformQuaternion() might be all he needs.

Quesa apparently uses matrices which are the transpose of those used by OpenGL (apparently - I've borrowed code fragments from it and I've never noticed any difference), so the maths may need adjusting slightly to suit.

Alternatively, Magic Software's maths library contains some equivalent functions.

Neil Carter
Nether - Mac games and comic art
Member
Posts: 304
Joined: 2002.04
Post: #3
This is from plib - google it and d/l it - it has tons of great utility c functions all written specifically to be used with OGL. This code snippet is taken from plib - but was based on an old gamasutra.com article - you may want to do a search for that too.

hth,
Codemattic

Code:
```#define sgFloat float #define SGfloat float #define SG_ZERO  0.0f #define SG_HALF  0.5f #define SG_ONE   1.0f #define SG_TWO   2.0f #define SG_THREE 3.0f #define SG_45    45.0f #define SG_180   180.0f #define SG_MAX   FLT_MAX #define SG_X     0 #define SG_Y     1 #define SG_Z     2 #define SG_W    3 typedef SGfloat sgVec4 [ 4 ] ; typedef SGfloat sgMat4  [4][4] ; typedef sgVec4 sgQuat ; void sgQuatToMatrix ( sgMat4 dst, const sgQuat q ) {   SGfloat two_xx = q[SG_X] * (q[SG_X] + q[SG_X]) ;   SGfloat two_xy = q[SG_X] * (q[SG_Y] + q[SG_Y]) ;   SGfloat two_xz = q[SG_X] * (q[SG_Z] + q[SG_Z]) ;   SGfloat two_wx = q[SG_W] * (q[SG_X] + q[SG_X]) ;   SGfloat two_wy = q[SG_W] * (q[SG_Y] + q[SG_Y]) ;   SGfloat two_wz = q[SG_W] * (q[SG_Z] + q[SG_Z]) ;   SGfloat two_yy = q[SG_Y] * (q[SG_Y] + q[SG_Y]) ;   SGfloat two_yz = q[SG_Y] * (q[SG_Z] + q[SG_Z]) ;   SGfloat two_zz = q[SG_Z] * (q[SG_Z] + q[SG_Z]) ;   sgSetVec4 ( dst[0], SG_ONE-(two_yy+two_zz), two_xy-two_wz, two_xz+two_wy, SG_ZERO ) ;   sgSetVec4 ( dst[1], two_xy+two_wz, SG_ONE-(two_xx+two_zz), two_yz-two_wx, SG_ZERO ) ;   sgSetVec4 ( dst[2], two_xz-two_wy, two_yz+two_wx, SG_ONE-(two_xx+two_yy), SG_ZERO ) ;   sgSetVec4 ( dst[3], SG_ZERO, SG_ZERO, SG_ZERO, SG_ONE ) ; }```

Im also a big fan of WildMagic as NCarter mentioned, so by all means download that also!
Moderator
Posts: 455
Joined: 2002.09
Post: #4
You seem to have some usable answers already, but I thought I'd mention that I got usable code from Apple's OpenGL environment mapping sample. It contains a file of vector, matrix, and quaternion functions that seem to be patterned after Quickdraw3D's old API. It's pretty easy to use, and there is a function that does exactly what you asked for.

Measure twice, cut once, curse three or four times.