【问题标题】:How to rotate camera in openGL using GLFW?如何使用 GLFW 在 openGL 中旋转相机?
【发布时间】:2019-03-24 09:46:25
【问题描述】:

我正在使用 GLFW 在 C++ 中创建一个 OpenGL 应用程序。基于this 教程,我设法创建了类似相机的 FPS(WASD 移动 + 鼠标移动的俯仰偏航)。

我正在使用的相机鼠标移动

glfwSetCursorPosCallback(window, mouse_callback);
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
    if (firstMouse)
    {
        lastX = xpos;
        lastY = ypos;
        firstMouse = false;
    }

    float xoffset = xpos - lastX;
    float yoffset = lastY - ypos; 
    lastX = xpos;
    lastY = ypos;

    float sensitivity = 0.1f;
    xoffset *= sensitivity;
    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);
}

这工作正常,但问题是我的应用程序窗口不是全屏的,所以如果我想旋转鼠标光标会离开窗口,然后很难控制相机。

是否可以做与 glfwSetCursorPosCallback 相同的事情,但前提是按下左键?我希望相机做和现在一样的事情,但前提是我按下左键。

【问题讨论】:

  • 我不明白您在问什么,您如何在 glfw 中旋转相机或如何在窗口外接收鼠标位置?
  • 我已经根据鼠标光标移动完成了旋转,我想做的是只有在按下鼠标左键时才进行旋转

标签: c++ opengl camera glfw


【解决方案1】:

是否可以与glfwSetCursorPosCallback 做同样的事情,但前提是按下左键?我希望相机做和现在一样的事情,但前提是我按下左键。

glfwSetMouseButtonCallback 设置回调,当按下鼠标按钮时通知。
当前鼠标(光标)位置可以通过glfwGetCursorPos获取。

添加鼠标按键回调:

glfwSetMouseButtonCallback(window, mouse_button_callback);

并在回调中获取鼠标位置:

void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
    {
        double xpos, ypos;     
        glfwGetCursorPos(window, &xpos, &ypos); 

        // [...]
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多