OpenGL的 |移动相机后模型不必要的晃动

OpenGL | Unwanted jiggle of a model after moving the camera

提问人:KrystofJan 提问时间:11/18/2023 最后编辑:KrystofJan 更新时间:11/18/2023 访问量:32

问:

我正在 OpenGl 中制作一个学校项目,但我无法弄清楚导致此问题的原因。当我用鼠标移动相机时,项目中的顶部球体似乎在晃动,或者鼠标移动有延迟。您可以在下面的视频中亲眼看到。

问题视频:https://youtu.be/o3C-mbC0DUY

GitHub 存储库:https://github.com/KrystofJan/vsb-zpg-project

我认为问题在于我使用了不太精确的数据类型作为光标位置,但这似乎没有问题。我已经为此苦苦挣扎了一段时间。它是在我实现相机移动后立即开始的,所以我认为控件存在一些基本问题。我似乎找不到它。即使在将其与其他源代码进行比较和调试之后。

相机控件源代码:

void Camera::call_cursor(double x, double y) {

    if (firstMouse) // initially set to true
    {
        lastX = x;
        lastY = y;
        firstMouse = false;
    }
    double xoffset = x - lastX;
    double yoffset = lastY - y;

    lastX = x;
    lastY = y;

    xoffset *= sensitivity; // sensitivity set to 0.1
    yoffset *= sensitivity;

    yaw += xoffset;
    pitch += yoffset;

    if (pitch > 89.0f)
        pitch = 89.0f;
    if (pitch < -89.0f)
        pitch = -89.0f;

    glm::vec3 front;
    front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
    front.y = sin(glm::radians(pitch));
    front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
    cameraFront = glm::normalize(front);
}

void Camera::controls() {
    if (glfwGetKey(this->window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);

    float currentFrame = static_cast<float>(glfwGetTime());
    deltaTime = currentFrame - lastFrame;
    lastFrame = currentFrame;

    float cameraSpeed = static_cast<float>(2.5 * deltaTime);

    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
        cameraPos += cameraSpeed * cameraFront;
    if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
        cameraPos -= cameraSpeed * cameraFront;
    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
        cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
    if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
        cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
    if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
    {
        double x, y;
        glfwGetCursorPos(this->window, &x, &y);
        this->call_cursor(x, y);

    }
    if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_RELEASE)
    {
        this->firstMouse = true;
    }
    if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
    {
        cameraSpeed = static_cast<float>(2.5 * deltaTime) * 4;
    }
}

glm::mat4 Camera::getViewMatrix() {
    this->viewMatrix = glm::lookAt(this->cameraPos, this->cameraPos + this->cameraFront, this->cameraUp);
    return this->viewMatrix;
}
glm::mat4 Camera::getProjectionMatrix()
{
    int width, height;
    glfwGetWindowSize(window, &width, &height);

    this->projectionMatrix = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
    return this->projectionMatrix;
}

尝试更精确地更改鼠标的 x 和 y 坐标的数据类型,但问题仍在发生

C++ 的opengl glfw glm数学 opengl-4

评论

0赞 Jeremy Friesner 11/18/2023
摇晃是只在垂直方向上发生的事情吗?如果是这样,则可能是缓冲问题,即当屏幕更新时,您有时会看到新帧的顶部和前一帧的底部。(如果添加一些临时代码,使每一帧都有不同的随机背景颜色,这种行为会变得更加明显)
0赞 KrystofJan 11/18/2023
摇晃发生在每个 direciton 中

答: 暂无答案