Search code examples
c++openglcameraquaternions

OpenGL Quaternion class usage


For a coding project I'm doing, I was given a Quaternion class to use to make the rotation of my camera easier and to solve Gimbal lock.

I'm not that well versed in using Quaternions so I was wondering how I would implement it into my camera class.

Currently to rotate the camera I'm using the built-in glRotatef function.

Camera functions

void Camera::Pitch(float aAngle)
{
    m_fPitchAngle += aAngle;
}

void Camera::Roll(float aAngle)
{
    //aAngle = 5.0f;
    m_fRollAngle += aAngle;
}

void Camera::Yaw(float aAngle)
{
    m_fYawAngle += aAngle;
}

void Camera::MoveForward(float aDistance)
{
    m_vPosition.z += aDistance;
}

void Camera::Strafe(float aDistance)
{
    m_vPosition.x += aDistance;
}

These variables are being used inside the camera's render function.

Inside Camera's Render function

// Yaw
glRotatef(m_fYawAngle, m_vUp.x, m_vUp.y, m_vUp.z);

// Pitch
glRotatef(m_fPitchAngle, m_vRight.z, m_vRight.y, m_vRight.z);

//Roll
glRotatef(m_fRollAngle, m_vFacing.x, m_vFacing.y, m_vFacing.z);

//angleBetween = cosf(m_fYawAngle) + m_vPosition.z;

// Move Forward
glTranslatef(m_vPosition.x, m_vPosition.y, m_vPosition.z);

Which are being utilised in the camera's update function, inside a switch statement.

Camera Update function

case SDLK_a:
        Yaw(-kAngleToTurn);
break;

case SDLK_d:
        Yaw(kAngleToTurn);
break;

And so on for the other variables. Here's the basic Quaternion header file I was given.

Quaternion.h

struct Quaternion
{
    float w;
    Vector3D vec;

    Quaternion()
    {
        vec.x = 0.0f;
        vec.y = 0.0f;
        vec.z = 0.0f;
    }

    Quaternion(float startW, Vector3D startVec)
    {
        vec.x = startVec.x;
        vec.y = startVec.y;
        vec.z = startVec.z;
    }
};

class QuaternionMath
{
public:
    ~QuaternionMath();

     static QuaternionMath* Instance();

    void QuaternionToMatrix(Quaternion* q, float m[4][4]);
    void MatrixToQuaternion(float m[4][4], Quaternion* q);
    void EulerToQuaternion(float roll, float pitch, float yaw, Quaternion* q);

    void Multiply(Quaternion* q1, Quaternion* q2, Quaternion* resultingQuaternion);
    void RotateVector(Quaternion* q, Vector3D* v, Vector3D* resultingVector);

private:
    QuaternionMath();

private:
    static QuaternionMath* mInstance;

};

Solution

  • Instead of using a chain of glRotate calls, retrieve a 4×4 matrix from the quaternion instance using MatrixToQuaternion and multiply that onto the matrix on top of the stack with glMultMatrix.

    In a further step you should get rid of using any code using the OpenGL fixed function matrix stack and use something like GLM or similar.