如何使用 GLFW 在 openGL 中旋转相机?
How to rotate camera in openGL using GLFW?
我正在使用 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);
}
这工作正常,但问题是我的应用程序 window 不是全屏,所以如果我想旋转我的鼠标光标离开 window 然后很难控制相机.
是否可以执行与 glfwSetCursorPosCallback 相同的操作,但前提是按下左键单击?我想让相机做和现在一样的事情,但前提是我按下左键。
Is it possible to do the same thing as glfwSetCursorPosCallback
, but only if left click is pressed? I want the camera do the same thing as its doing now, but only if i pressed left click.
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);
// [...]
}
}
我正在使用 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);
}
这工作正常,但问题是我的应用程序 window 不是全屏,所以如果我想旋转我的鼠标光标离开 window 然后很难控制相机.
是否可以执行与 glfwSetCursorPosCallback 相同的操作,但前提是按下左键单击?我想让相机做和现在一样的事情,但前提是我按下左键。
Is it possible to do the same thing as
glfwSetCursorPosCallback
, but only if left click is pressed? I want the camera do the same thing as its doing now, but only if i pressed left click.
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);
// [...]
}
}