OpenGL Rotation with quaternions not working

I am trying to make a cube rotate around fixed axes using quaternions. Here is the code i have produced, using my quaternions :

            glMatrixMode( GL_MODELVIEW_MATRIX );
        Quaternion rot1;
        Quaternion rot2;
        rot1.FromAxis(Vector3(1.0,0.0,0.0),angleY);
        rot2.FromAxis(Vector3(0.0,1.0,0.0),angleX);
        rot1.normalise();
        rot2.normalise();
        QAccumulative = QAccumulative * rot1;
        QAccumulative = QAccumulative * rot2;


        Matrix4 mat;
        mat = QAccumulative.getMatrix();

        angleY = 0;
        angleX = 0;
        glMultMatrixf(mat.get());

Do you think my code isn't correct, my cube appears to be infinitly big, compared to withouth this pice of code.

EDIT :

After getting some help it turns out that normalizing QAcc makes it identity, but normalizing rot1 and rot2 makes QAcc unstable, here is my normalize method :

      void Quaternion::normalise()
{
    // Don't normalize if we don't have to
    float mag2 = w * w + x * x + y * y + z * z;
    if (fabs(mag2) > TOLERANCE && fabs(mag2 - 1.0f) > TOLERANCE) {
        float mag = sqrt(mag2);
        w /= mag;
        x /= mag;
        y /= mag;
        z /= mag;
    }
}

EDIT:

Here is my FromAxis code :

void Quaternion::FromAxis(const Vector3 &v, float angle)
{
    float sinAngle;
    angle *= 0.5f;
    Vector3 vn(v);
    vn.normalize();

    sinAngle = sin(angle);

    x = (vn.x * sinAngle);
    y = (vn.y * sinAngle);
    z = (vn.z * sinAngle);
    w = cos(angle);
}

I was indeed normalizing vectors without checking them, adding a tolerance threshold resolved my problem.

Thanks everyone for the help.

链接地址: http://www.djcxy.com/p/81856.html

上一篇: 为什么我不能在PyOpenGL中旋转我的多维数据集?

下一篇: 四元数不能运行的OpenGL旋转