我使用glfw回调函数用鼠标移动摄像机。
鼠标回调功能是:
void mouse_callback(GLFWwindow *window, double xposIn, double yposIn)
{
if (is_pressed)
{
camera.ProcessMouseMovement((static_cast<float>(yposIn) - prev_mouse.y) / 3.6f, (static_cast<float>(xposIn) - prev_mouse.x) / 3.6f);
prev_mouse.x = xposIn;
prev_mouse.y = yposIn;
}
cur_mouse.x = xposIn;
cur_mouse.y = yposIn;
}
void mouse_btn_callback(GLFWwindow *window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
prev_mouse.x = cur_mouse.x;
prev_mouse.y = cur_mouse.y;
is_pressed = true;
}
else
{
is_pressed = false;
}
}
但是,在这种情况下,即使在其他imgui窗口中操作,相机也会移动,如下所示。
我不知道该怎么处理。
我是否应该将这个逻辑放在IMGUI的开始和结束之间,使用类似于ImGui::IsWindowHovered()
的方法
就像这样:
ImGui::Begin("scene");
{
if(ImGui::IsWindowHovered())
{
//camera setting
}
}
ImGui::End()
发布于 2022-04-01 13:52:22
上面的答案是错误的。这是在亲爱的ImGui常见问题解答:https://github.com/ocornut/imgui/blob/master/docs/FAQ.md#q-how-can-i-tell-whether-to-dispatch-mousekeyboard-to-dear-imgui-or-my-application TL;DR检查io.WantCaptureMouse标志的鼠标。
发布于 2022-06-05 18:06:55
我今天也遇到了同样的问题。对于现在看到这种情况的人,您必须在初始化ImGui之前定义glfw回调。ImGui在此时设置自己的回调,并处理将输入发送到已经存在的输入,如果不是以前使用的话。如果事后定义了回调,就会覆盖ImGui创建的回调。
发布于 2022-03-30 17:45:16
我不熟悉ImGui,所以我不知道在ImGui中可能或不需要调用哪些函数。
但是,GLFW是一个相对较低级别的窗口API,它不考虑窗口上可能存在的更高级别的抽象。当您将回调传递给glfwSetCursorPosCallback
时,该回调将在窗口的任何可访问部分上调用。
如果您需要让鼠标移动(或任何鼠标交互)只在鼠标悬停在接口的相关部分时才响应,则需要某种机制来定义该部分是什么。再说一遍:我不知道如何在ImGui中做到这一点,但它可能看起来如下所示:
void mouse_callback(GLFWwindow *window, double xposIn, double yposIn)
{
//Structured Binding; we expect these values to all be doubles.
auto [minX, maxX, minY, maxY] = //Perhaps an ImGui call to return the bounds of the openGL surface?
if(xposIn < minX || xposIn > maxX || yposIn < minY || yposIn > maxY) {
return; //We're outside the relevant bounds; do not do anything
}
//I'm assuming setting these values should only happen if the mouse is inside the bounds.
//Move it above the first if-statement if it should happen regardless.
cur_mouse.x = xposIn;
cur_mouse.y = yposIn;
if (is_pressed)
{
camera.ProcessMouseMovement((static_cast<float>(yposIn) - prev_mouse.y) / 3.6f, (static_cast<float>(xposIn) - prev_mouse.x) / 3.6f);
prev_mouse.x = xposIn;
prev_mouse.y = yposIn;
}
}
https://stackoverflow.com/questions/71680516
复制相似问题