glm look当F<时,FPS相机翻转垂直鼠标外观。 0

时间:2014-11-27 22:48:46

标签: c++ opengl glm-math

我正在尝试使用C ++,OpenGL和GLM实现FPS相机。

到目前为止我做了什么:

我有一个相机位置的cameraPosition矢量,还有 cameraForward(指向相机所在的位置),cameraRight和cameraUp,计算方式如下:

inline void controlCamera(GLFWwindow* currentWindow, const float& mouseSpeed, const float& deltaTime)
{
    double mousePositionX, mousePositionY;
    glfwGetCursorPos(currentWindow, &mousePositionX, &mousePositionY);

    int windowWidth, windowHeight;
    glfwGetWindowSize(currentWindow, &windowWidth, &windowHeight);

    m_cameraYaw += (windowWidth / 2 - mousePositionX) * mouseSpeed;
    m_cameraPitch += (windowHeight / 2 - mousePositionY) * mouseSpeed;
    lockCamera();

    glfwSetCursorPos(currentWindow, windowWidth / 2, windowHeight / 2);

    // Rotate the forward vector horizontally. (the first argument is the default forward vector)
    m_cameraForward = rotate(vec3(0.0f, 0.0f, -1.0f), m_cameraYaw, vec3(0.0f, 1.0f, 0.0f));

    // Rotate the forward vector vertically.
    m_cameraForward = rotate(m_cameraForward, -m_cameraPitch, vec3(1.0f, 0.0f, 0.0f));

    // Calculate the right vector. First argument is the default right vector.
    m_cameraRight = rotate(vec3(1.0, 0.0, 0.0), m_cameraYaw, vec3(0.0f, 1.0f, 0.0f));

    // Calculate the up vector.
    m_cameraUp = cross(m_cameraRight, m_cameraForward);
}

然后我"看看"像这样:

lookAt(m_cameraPosition, m_cameraPosition + m_cameraForward, m_cameraUp)

问题:我似乎错过了一些东西,因为我的FPS相机正常工作,直到我前进并落后于Z(0.0)(z变为负片)..然后我的垂直鼠标外观翻转,当我试着抬头查看我的应用程序...

这里问了同样的问题:glm::lookAt vertical camera flips when z <= 0,但我不明白问题是什么以及如何解决。

编辑:问题肯定在向前,向上和向右的向量中。当我像这样计算它们时:

        m_cameraForward = vec3(
            cos(m_cameraPitch) * sin(m_cameraYaw),
            sin(m_cameraPitch),
            cos(m_cameraPitch) * cos(m_cameraYaw)
        );

        m_cameraRight = vec3(
            sin(m_cameraYaw - 3.14f/2.0f),
            0,
            cos(m_cameraYaw - 3.14f/2.0f)
        );

        m_cameraUp = glm::cross(m_cameraRight, m_cameraForward);

然后问题消失了,但是m_cameraPitch和m_cameraYaw不匹配...我的意思是如果m_cameraYaw是250而我做了180翻转m_cameraYaw是265 ...我不能限制向后倾斜比如那样?有什么想法吗?

0 个答案:

没有答案