dashingstag

06-13-2017, 02:07 AM

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.

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.