Opengl 鼠标相机问题 (gluLookAt)

Opengl mouse Camera issue (gluLookAt)

您好,我在 openGL 中的鼠标移动遇到了一个奇怪的问题。这是我用鼠标移动相机的代码

void camera(int x, int y)
{
    GLfloat xoff = x- lastX;
    GLfloat yoff = lastY - y; // Reversed since y-coordinates range from bottom to top
    lastX = x;
    lastY = y;

    GLfloat sensitivity = 0.5f;
    xoff *= sensitivity;
    yoff *= sensitivity;

    yaw += xoff;                // yaw is x
    pitch += yoff;              // pitch is y

    // Limit up and down camera movement to 90 degrees
    if (pitch > 89.0)
        pitch = 89.0;
    if (pitch < -89.0)
        pitch = -89.0;

    // Update camera position and viewing angle
    Front.x = cos(convertToRads(yaw) * cos(convertToRads(pitch)));
    Front.y = sin(convertToRads(pitch));
    Front.z = sin(convertToRads(yaw)) * cos(convertToRads(pitch));
}

convertToRads() 是我创建的一个小函数,用于将鼠标坐标转换为弧度。

使用这段代码,我可以随心所欲地移动我的相机,但是如果我尝试一直向上移动,当我达到 45 度左右时,它会围绕 x 轴旋转 1-2 次,然后继续增加 y-轴。我不明白我是否做错了什么,所以如果有人能提供帮助,我将不胜感激。

看来你放错了括号:

Front.x = cos(convertToRads(yaw) * cos(convertToRads(pitch)));

而不是:

Front.x = cos(convertToRads(yaw)) * cos(convertToRads(pitch));