C++ GLFW摄像机和鼠标控制

C++ GLFW摄像机和鼠标控制,c++,camera,glfw,C++,Camera,Glfw,因此,我基本上是从第页的教程中学习OpenGL和GLFW库的: 我的问题是,这节课显示了用鼠标控制相机移动。 基本上,它使应用程序获得“FPS”一样的相机,禁用的光标被移动到屏幕中心的每一帧。但是当我们对窗口失去焦点,然后它又恢复时,相机会变得疯狂。例如,如果我们单击窗口以从视图中间恢复焦点,相机将被大量移动。我尝试通过添加窗口焦点回调来解决此问题: void window_focus_callback(GLFWwindow* window, int focused){ if (focused)

因此,我基本上是从第页的教程中学习OpenGL和GLFW库的:

我的问题是,这节课显示了用鼠标控制相机移动。 基本上,它使应用程序获得“FPS”一样的相机,禁用的光标被移动到屏幕中心的每一帧。但是当我们对窗口失去焦点,然后它又恢复时,相机会变得疯狂。例如,如果我们单击窗口以从视图中间恢复焦点,相机将被大量移动。我尝试通过添加窗口焦点回调来解决此问题:

void window_focus_callback(GLFWwindow* window, int focused){
if (focused)
{
    //center mouse on screen
    int width, height;
    glfwGetWindowSize(window, &width, &height);
    glfwSetCursorPos(window, 1024 / 2, 768 / 2);
    windowFocused = true;
}
else
{
    windowFocused = false;
}
在主应用程序循环中:

if(windowFocused) computeMatricesFromInputs();
但是由于某种原因,这个解决方案不起作用。
有没有办法用glfw解决这个问题?

这个问题有点老了,但我最近遇到了一个类似的问题。因此,只要分享,就会有更多的解决方案。 我使用GLFW_光标_禁用。在这种模式下,当您收到“打开”焦点事件时,鼠标位置不会(尚未)更新,因此对GetCursorPos的调用会传递以前的值。新的光标位置在“on”焦点事件之后到达MouseMove回调中。 我通过跟踪焦点的恢复来解决这个问题,并使用这个int-he-OnMouseMove回调来调度MouseInit(捕捉光标)或常规MouseMove

通过这种方式,我可以将ALT+TAB键移出窗口,并将光标返回到其他地方,而不会使相机发生剧烈的跳跃/旋转

void InputManagerGLFW::Callback_OnMouseMove(
            GLFWwindow* window,
            double      xpos,               //
            double      ypos)               //
{
    if (!mFocusRegained)
    {
        mMouseBuffer.Move(xpos, ypos);
    }
    else
    {
        mFocusRegained = false;
        mMouseBuffer.Init(xpos, ypos);
    }
}

void InputManagerGLFW::Callback_OnFocus(
            GLFWwindow* window,
            int         focused)
{
    if (focused)
    {
        // The window gained input focus
        // Note: the mouse position is not yet updated
        //       the new position is provided by the mousemove callback AFTER this callback
        Log::Info("focus");

        // use flag to indicate the OnMouseMove that we just regained focus,
        // so the first mouse move must be handled differently
        mFocusRegained = true;

        // this will NOT work!!!
        // double x,y;
        // glfwGetCursorPos(window,&x,&y);
        // mMouseBuffer.Init(x,y);
    }
    else
    {
        // The window lost input focus
        Log::Info("focus lost");
    }
}