Opengl相机和乘法矩阵

时间:2012-04-04 02:39:35

标签: c++ opengl matrix camera

我目前在制作相机时遇到很多问题。我正在做的矩阵旋转会出现问题,因为这个网站说要避免使用gimble锁..

  

您要注意的首要问题之一是您申请的订单   这些轮换很重要。如前所述,旋转矩阵是   方向转换。每个变换定义一个新的坐标系,   并且下一个变换基于新空间中的对象。对于   例如,如果我们首先应用滚动,我们现在已经改变了轴   随后的偏航是。

当我执行此操作时,例如,如果我想绕当前x轴旋转,x轴也会在我的轴上改变为旋转方法,这显然是错误的。我环顾四周,无法找到任何解决方案。我已经尝试了很多不同版本的轴角度旋转矩阵。

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();                                          
}

1 个答案:

答案 0 :(得分:2)

我认为乘法的顺序可能导致问题,而不是

m_orientation = matrix*m_orientation;

尝试

m_orientation = m_orientation * matrix;