【问题标题】:Opengl Camera and multiplying matrixesOpengl相机和乘法矩阵
【发布时间】:2012-04-04 02:39:34
【问题描述】:

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

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

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

【问题讨论】:

  • 你想制作什么样的相机? FPS 风格的相机,还是 6DOF(宇宙飞船)?
  • 6DOF(宇宙飞船)没有z轴旋转(滚动)

标签: c++ opengl matrix camera


【解决方案1】:

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

m_orientation = matrix*m_orientation;

试试

m_orientation = m_orientation * matrix;

【讨论】:

  • 我刚试过这个,确实有点帮助。相机仍然奇怪地旋转我担心我的相机翻译是错误的。但是我已经尝试了所有可能的方式来放置翻译仍然没有运气!
猜你喜欢
  • 1970-01-01
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 2022-06-30
  • 2015-10-17
相关资源
最近更新 更多