Chris Condy
Chris Condy

Reputation: 636

Opengl Camera and multiplying matrixes

I Am currently having alot of problems with the camera I am making. The problem occurs with my matrix rotation I am doing as this website says to avoid gimble lock..

One of the first problems you will note is that the order you apply these rotations matter. As previously stated, a rotation matrix is an orientation transform. Each transform defines a new coordinate system, and the next transform is based on an object in the new space. For example, if we apply the roll first, we have now changed what the axis for the subsequent yaw is.

And when i perform this for example if I am wanted to pitch around the current x axis the x axis also changes in my axis to rotation method which is obviously wrong. Ive look around alot and cant find any solution. I have tried alot of differenet version of the axis angle rotation matrix..

void FrustumCamera::xAxisRotation(float angle)
{
    Vector3<float> x = m_orientation.getXAxis();
    Matrix4<float> matrix = m_orientation.axisAngleRotation(x,angle);
    m_orientation = matrix*m_orientation;
    normalise(m_orientation.getXAxis());
    normalise(m_orientation.getYAxis());
    normalise(m_orientation.getZAxis());
}
void FrustumCamera::yAxisRotation(float angle)
{
    Vector3<float> y = m_orientation.getYAxis();

    Matrix4<float> matrix = m_orientation.axisAngleRotation(y,angle);
    m_orientation = matrix*m_orientation;

    normalise(m_orientation.getXAxis());
    normalise(m_orientation.getYAxis());
    normalise(m_orientation.getZAxis());
}

Matrix4<Type> Matrix4<Type>::operator*(Matrix4& matrix)
{
    Matrix4<Type> temp(m_matrix);
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            Type total = 0;
            for(int k=0;k<4;k++)
            {

                total += m_matrix[i][k]*matrix.getAt(k,j);;

            }
            temp.setAt(i,j,total);
        }
    }
    return temp;
}
template <class Type>
Matrix4<Type> Matrix4<Type>::axisAngleRotation(Vector3<Type> axis, const Type angle)
{
    Type radians = angle * (double)degToRad;
    Matrix4<Type> temp;
    float c = cosf(radians);
    float s = sinf(radians);
    float t = 1.0f - c;

    float x = axis.x;
    float y = axis.y;
    float z = axis.z;

    temp.setAt(0,0, c+x*x*(t));
    temp.setAt(0,1, x*y*(t)-z*s);
    temp.setAt(0,2, x*z*(t)+y*s);
    temp.setAt(0,3, 0.0f);

    temp.setAt(1,0, y*x*(t)+z*s);
    temp.setAt(1,1, c+y*y*(t));
    temp.setAt(1,2, y*z*(t)-x*s);
    temp.setAt(1,3, 0.0f);

    temp.setAt(2,0, z*x*(t)-y*s);
    temp.setAt(2,1, z*y*(1-c)+x*s);
    temp.setAt(2,2, c+z*z*(t));
    temp.setAt(2,3, 0.0f);

    temp.setAt(3,0, 0.0f);
    temp.setAt(3,1, 0.0f);
    temp.setAt(3,2, 0.0f);
    temp.setAt(3,3, 1.0f);

    return temp;
}
void OpenGLRenderer::startDraw(unsigned long mask)
{
    //sortBuffer();                                     // sort draw queue
    clearBuffers(mask);                                 // clear buffers
    loadIdentity();
    glTranslatef(-1*m_frustumCamera->getViewMatrix().getTranslationAxis().x,-1*m_frustumCamera->getViewMatrix().getTranslationAxis().y,-1*m_frustumCamera->getViewMatrix().getTranslationAxis().z);// load identity
    glMultMatrixf(m_frustumCamera->getViewMatrix().getMatrix());
    glTranslatef(m_frustumCamera->getViewMatrix().getTranslationAxis().x,m_frustumCamera->getViewMatrix().getTranslationAxis().y,m_frustumCamera->getViewMatrix().getTranslationAxis().z);

    matrixStackPush();                                          
}

Upvotes: 0

Views: 337

Answers (1)

LebRon
LebRon

Reputation: 755

I think order of multiplication can cause the problem, instead of

m_orientation = matrix*m_orientation;

try

m_orientation = m_orientation * matrix;

Upvotes: 2

Related Questions