0

我目前正在制作的相机有很多问题。正如本网站所说,为了避免万向节锁定,我正在做的矩阵旋转出现问题。

您将注意到的第一个问题是您应用这些旋转的顺序很重要。如前所述,旋转矩阵是方向变换。每个变换定义一个新的坐标系,下一个变换基于新空间中的一个对象。例如,如果我们先应用滚动,我们现在已经更改了后续偏航的轴。

例如,当我执行此操作时,如果我想围绕当前的 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();                                          
}
4

1 回答 1

2

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

m_orientation = matrix*m_orientation;

尝试

m_orientation = m_orientation * matrix;
于 2012-04-04T04:57:33.513 回答