如何使用GLFW在openGL中旋转相机?

问题描述 投票:1回答:1

我正在使用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做同样的事情,但只有按下左键单击?我希望相机做它现在做的事情,但只有当我按下左键。

c++ opengl camera glfw
1个回答
2
投票

是否有可能与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); 

        // [...]
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.