如何在现代opengl中正确实现四元数相机?

我试图在opengl中创建一个基于四元数的uvn相机,使用了下面列出的各种教程,并且已经阅读了四元数和轴角度旋转。 我留下了一个我似乎无法修复的奇特错误。

基本上相机似乎工作得很好,直到相机从+ z旋转约45度,此时倾斜相机向上或向下似乎倾斜相机围绕其目标轴,转动向上矢量。

当相机朝着-z向上或向下倾斜时出现相反的错觉时,向上和向下倾斜向上倾斜。

我看到其他的实现建议使用一个非四元数的系统,其中四元数被累积成一个描述当前方向为一个任意起始角度的增量。 这听起来很不错,但我似乎无法完全确定如何实现这一点,特别是从此转换为视图矩阵。

因此,我读到了关于将旋转分解成两个四元数来代表偏航和俯仰的问题,但我不相信这是问题的原因,因为在这种情况下,如果我错了,请纠正我,但我的理解是,你应用两次旋转的顺序无关紧要。

相关的源代码片段:

季度运营

Quaternion<TValue> conjugate() const{
            return Quaternion({ { -m_values[X], -m_values[Y], -m_values[Z], m_values[W] } });
};

Quaternion<TValue>& operator*=(const Quaternion<TValue>& rhs) {
        TValue x, y, z, w;
            w = rhs[W] * m_values[W] - rhs[X] * m_values[X] - rhs[Y] * m_values[Y] - rhs[Z] * m_values[Z];
            x = rhs[W] * m_values[X] + rhs[X] * m_values[W] - rhs[Y] * m_values[Z] + rhs[Z] * m_values[Y];
            y = rhs[W] * m_values[Y] + rhs[X] * m_values[Z] + rhs[Y] * m_values[W] - rhs[Z] * m_values[X];
            z = rhs[W] * m_values[Z] - rhs[X] * m_values[Y] + rhs[Y] * m_values[X] + rhs[Z] * m_values[W];          

            m_values[X] = x;
            m_values[Y] = y;
            m_values[Z] = z;
            m_values[W] = w;
            return *this;
};
static Quaternion<TValue> rotation(Vector<3, TValue> axis, TValue angle){
        float x, y, z, w;
        TValue halfTheta = angle / 2.0f;
        TValue sinHalfTheta = sin(halfTheta);
        return Quaternion<TValue>({ { axis[X] * sinHalfTheta, axis[Y] * sinHalfTheta, axis[Z] * sinHalfTheta, cos(halfTheta) } });
};

矢量旋转操作

Vector<dimensions, TValue> rotate(const Vector<3, TValue> axis, float angle){
        Quaternion<TValue> R = Quaternion<TValue>::rotation(axis, angle);
        Quaternion<TValue> V = (*this);
        Vector<dimensions, TValue> result = R * V * R.conjugate();
        return result;
}

相机方法

Camera::Camera(Vector<2, int> windowSize, float fov, float near, float far):
m_uvn(Matrix<4, float>::identity()),
m_translation(Matrix<4, float>::identity()),
m_ar(windowSize[Dimensions::X] / (float)windowSize[Dimensions::Y]),
m_fov(fov),
m_near(near),
m_far(far),
m_position(),
m_forward({ { 0, 0, 1 } }),
m_up({ { 0, 1, 0 } })
{
    setViewMatrix(Matrix<4, float>::identity());
    setProjectionMatrix(Matrix<4, float>::perspective(m_ar, m_near, m_far, m_fov));
};

Matrix<4, float> Camera::getVPMatrix() const{
    return m_vp;
};

const Vector<3, float> Camera::globalY = Vector<3, float>({ { 0, 1, 0 } });

void Camera::setProjectionMatrix(const Matrix<4, float> p){
    m_projection = p;
    m_vp = m_projection * m_view;
};

void Camera::setViewMatrix(const Matrix<4, float> v){
    m_view = v;
    m_vp = m_projection * m_view;
};

void Camera::setTranslationMatrix(const Matrix<4, float> t){
    m_translation = t;
    setViewMatrix(m_uvn * m_translation);
}

void Camera::setPosition(Vector<3, float> position){
    if (position != m_position){
        m_position = position;
        setTranslationMatrix(Matrix<4, float>::translation(-position));
    }
};

void Camera::moveForward(float ammount){
    setPosition(m_position + (m_forward * ammount));
}

void Camera::moveRight(float ammount){
    setPosition(m_position + (getRight() * ammount));
}

void Camera::moveUp(float ammount){
    setPosition(m_position + (m_up * ammount));
}

void Camera::setLookAt(Vector<3, float> target, Vector<3, float> up){
    Vector<3, float> newUp = up.normalize();
    Vector<3, float> newForward = target.normalize();
    if (newUp != m_up || newForward != m_forward){
        m_up = newUp;
        m_forward = newForward;

        Vector<3, float> newLeft = getLeft();
        m_up = newLeft * m_forward;

        m_uvn = generateUVN();
        setViewMatrix(m_uvn * m_translation);
    }
};

void Camera::rotateX(float angle){
    Vector<3, float> hAxis = (globalY * m_forward).normalize();
    m_forward = m_forward.rotate(hAxis, angle).normalize();
    m_up = (m_forward * hAxis).normalize();

    m_uvn = generateUVN();
    setViewMatrix(m_translation * m_uvn);
}

void Camera::rotateY(float angle){
    Vector<3, float> hAxis = (globalY * m_forward).normalize();
    m_forward = m_forward.rotate(globalY, angle).normalize();
    m_up = (m_forward * hAxis).normalize();

    m_uvn = generateUVN();
    setViewMatrix(m_translation * m_uvn);
}

Vector<3, float> Camera::getRight(){
    return (m_forward * m_up).normalize();
}

Vector <3, float> Camera::getLeft(){
    return (m_up * m_forward).normalize();
}

};

我猜测问题出在我实现四元数或者我使用它的方式上,但是由于系统的复杂性,我似乎无法将这个问题进一步细化。 由于出现了奇怪的错误,我不确定是否在尝试实现相机方面出现了问题。

教程

  • https://www.youtube.com/watch?v=1Aw1PDu33PI
  • http://www.gamedev.net/page/resources/_/technical/math-and-physics/a-simple-quaternion-based-camera-r1997
  • 季节/矢量数学

  • http://mathworld.wolfram.com/Quaternion.html
  • https://en.wikipedia.org/wiki/Cross_product
  • http://ogldev.atspace.co.uk/www/tutorial13/tutorial13.html
  • http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/index.htm
  • 链接地址: http://www.djcxy.com/p/81767.html

    上一篇: How to correctly implement a quaternion camera in modern opengl?

    下一篇: Issue with a Free Fly Camera And Combination of Quaternion Rotations