Quaternion object rotation

I have a device that returns me a quaternion.

I want to view this quaternion on a object in my opengl scene.

I currently convert the quaternion to a matrix and load it into the scene with glLoadMatrixf(mat); then drawing the object

static inline void QuatTo4x4Matrix(float* quat, float* mat) {
	float X = quat[0];
	float Y = quat[1];
	float Z = quat[2];
	float W = quat[3];

	float xx = X * X;
	float xy = X * Y;
	float xz = X * Z;
	float xw = X * W;
	float yy = Y * Y;
	float yz = Y * Z;
	float yw = Y * W;
	float zz = Z * Z;
	float zw = Z * W;

	mat[0] = 1 - 2 * (yy + zz);
	mat[1] = 2 * (xy - zw);
	mat[2] = 2 * (xz + yw);
	mat[4] = 2 * (xy + zw);
	mat[5] = 1 - 2 * (xx + zz);
	mat[6] = 2 * (yz - xw);
	mat[8] = 2 * (xz - yw);
	mat[9] = 2 * (yz + xw);
	mat[10] = 1 - 2 * (xx + yy);
	mat[3] = mat[7] = mat[11] = mat[12] = mat[13] = mat[14] = 0;
	mat[15] = 1;

However the quaternion seems to be rotating about a different axis that I anticipate. Is there anyway I can correct this? I tried offseting the quaternion with a offset quaternion to set it to identity but it still rotates about it’s own axis.
Am I doing anything wrong? Been working for this for a couple days now, just can’t wrap my head around this.

Your matrix appears to be transposed. So try changing that, either by changing the indices or using glLoadTransposeMatrixf().

There are many ways to represent the orientation of an object. Most programmers use 3x3 rotation matrices or three Euler angles to store this information. Each of these solutions works fine until you try to smoothly interpolate between two orientations of an object. Imagine an object that is not user controlled, but which simply rotates freely in space (for example, a revolving door). If you chose to store the door’s orientations as rotation matrices or Euler angles, you’d find that smoothly interpolating between the rotation matrices’ values would be computationally costly and certainly wouldn’t appear as smooth to a player’s eye as quaternion interpolation.

Trying to correct this problem using matrices or Euler angles, an animator might simply increase the number of predefined (keyed) orientations. However, one can never be sure how many such orientations are enough, since the games run at different frame rates on different computers, thereby affecting the smoothness of the rotation. This is a good time to use quaternions, a method that requires only two or three orientations to represent a simple rotation of an object, such as our revolving door. You can also dynamically adjust the number of interpolated positions to correspond to a particular frame rate.

Before we begin with quaternion theory and applications, let’s look at how rotations can be represented. I’ll touch upon methods such as rotation matrices, Euler angles, and axis and angle representations and explain their shortcomings and their relationships to quaternions. If you are not familiar with some of these techniques, I recommend picking up a graphics book and studying them.

To date, I haven’t seen a single 3D graphics book that doesn’t talk about rotations using 4x4 or 3x3 matrices. Therefore, I will assume that most game programmers are very familiar with this technique and I’ll just comment on its shortcomings. I also highly recommend that you re-read Chris Hecker’s article in the June 1997 issue of the Game Developer (“Physics, Part 4: The Third Dimension,” pp.15-26), since it tackles the problem of orienting 3D objects.

Rotations involve only three degrees of freedom (DOF), around the x, y, and z coordinate axes. However, nine DOF (assuming 3x3 matrices) are required to constrain the rotation - clearly more than we need. Additionally, matrices are prone to “drifting,” a situation that arises when one of the six constraints is violated and the matrix introduces rotations around an arbitrary axis. Combatting this problem requires keeping a matrix orthonormalized - making sure that it obeys constraints. However, doing so is not computationally cheap. A common way of solving matrix drift relies on the Gram-Schmidt algorithm for conversion of an arbitrary basis into an orthogonal basis. Using the Gram-Schmidt algorithm or calculating a correction matrix to solve matrix drifting can take a lot of CPU cycles, and it has to be done very often, even when using floating point math.