GLFW 鼠标事件延迟与窗口拖动

Key*_*ess 5 c opengl cursor-position glfw setwindowpos

我试图在 GLFW 中拖动一个未装饰的窗口,但遇到了一些事件滞后,即使我正在使用 glfwWaitEvents()

我有一个光标位置回调和一个简单的循环:

// register a cursor position callback
glfwSetCursorPosCallback(win, cursor_pos_callback);

// then loop..
while(!glfwWindowShouldClose(win)) {
  glfwWaitEvents();
  ... some rendering...
  glfwSwapBuffers(win);
}
Run Code Online (Sandbox Code Playgroud)

我的光标回调对增量和更新窗口位置进行了一些简单的跟踪。

cursor_pos_callback(GLFWwindow *win, double xpos, double ypos) {
  // figure out delta_x and delta_y based on cursor previous position
  int delta_x, delta_y;

  // update window position
  if (window_drag_active) {
     int x,y;
     glfwGetWindowPos(window, &x, &y);
     glfwSetWindowPos(window, x + delta_x, y + delta_y);
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我沿直线拖动时增量的样子

delta_x:  10    delta_y:   0    |    xpos:   649    ypos: 55
delta_x:   5    delta_y:  -1    |    xpos:   654    ypos: 54
delta_x:   5    delta_y:   3    |    xpos:   659    ypos: 57
delta_x:   5    delta_y:   2    |    xpos:   664    ypos: 59
delta_x:  -5    delta_y:  -2    |    xpos:   659    ypos: 57
delta_x:   4    delta_y:   0    |    xpos:   663    ypos: 57
delta_x:   2    delta_y:   0    |    xpos:   665    ypos: 57
delta_x:  -3    delta_y:  -3    |    xpos:   662    ypos: 54
delta_x:   2    delta_y:   1    |    xpos:   664    ypos: 55
delta_x:   2    delta_y:   0    |    xpos:   666    ypos: 55
delta_x:   3    delta_y:   2    |    xpos:   669    ypos: 57
delta_x:   1    delta_y:  -1    |    xpos:   670    ypos: 56
delta_x:   2    delta_y:  -1    |    xpos:   672    ypos: 55
delta_x:   7    delta_y:   3    |    xpos:   679    ypos: 58
delta_x:   2    delta_y:  -1    |    xpos:   681    ypos: 57
delta_x:  -2    delta_y:  -3    |    xpos:   679    ypos: 54
delta_x:   0    delta_y:  -2    |    xpos:   679    ypos: 52
delta_x:   3    delta_y:   3    |    xpos:   682    ypos: 55
delta_x:  -5    delta_y:  -3    |    xpos:   677    ypos: 52
Run Code Online (Sandbox Code Playgroud)

xpos增量,因为它应该,然后每隔一段时间,它会向后(陈旧的事件?)

也许我的窗口移动与光标不同步?

结果是当我拖动窗口时,它剧烈摇晃,我几乎无法将其移动到任何地方......


更新:我也尝试将glfwSetWindowPos逻辑移到主循环中,但没有成功——我仍然会出现同样的颤抖和口吃。


更新:当我注释掉glfwSetWindowPos()窗口不再移动(当然)但事件流现在是一致的。

这让我认为快速移动窗口会导致猛烈运动(即向前 2 步,向后 1 步)。

K. *_*uhr 5

我怀疑您的问题是接收相对于窗口的cursor_pos_callback光标位置并且移动窗口会立即影响该位置。

假设您以恒定速率沿对角线移动光标。如果经过一刻度,光标从相对位置 (100,100) 移动到 (105,105),则计算delta_x=5delta_y=5。然后你移动窗口。移动窗口的过程会立即将相对坐标从 (105,105) 更改回 (100,100),并且在下一个刻度上,即使您已移动到相对于原始窗口位置的位置 (110,110),您也仅位于相对于新窗口位置的相对delta_x=0位置 (105,105),因此您可以根据之前的位置(加上一些随机抖动噪声)计算和delta_y=0,即使您实际上已沿每个轴额外移动了 5 个单位。

相反,修改您的算法以保持恒定的相对光标位置。在拖动开始时,存储相对光标位置(例如(100,100))。现在,在每个刻度处,计算需要将窗口放置在哪里才能将光标移回到该固定的相对位置。因此,如果光标已移动到 (112,108),请将窗口移动 (+12,+8),以使光标回到 (100,100)。在稍后的刻度处,如果光标已移动到 (108,106),则不要尝试从 (112,108) 计算增量;相反,将其与原始 (100,100) 起点进行比较,并将窗口移动 (+8,+6)。它会像下面这样:

cursor_pos_callback(GLFWwindow *win, double xpos, double ypos) {

  // update window position
  if (window_drag_active) {
     int delta_x = xpos - window_drag_starting_xpos;
     int delta_y = ypos - window_drag_starting_ypos;
     int x,y;
     glfwGetWindowPos(window, &x, &y);
     glfwSetWindowPos(window, x + delta_x, y + delta_y);
  }
}
Run Code Online (Sandbox Code Playgroud)


Key*_*ess 5

我将另一个答案标记为可接受的解决方案,因为您确实需要记录初始起始光标位置(相对于窗口)并使用它来计算增量。

然而,这样做只提供了一半的解决方案。拖动时我仍然经历了一些重大的跳跃和跨屏幕射击!

最后我发现这个同步问题来自于我设置新窗口位置的地方。

要完全消除任何滞后,并确保窗口+光标同步,您必须在渲染循环内同时执行glfwGetWindowPos()和!glfwSetWindowPos()

在回调内部这样做会导致位置不同步而出现抖动。所以我相信解决方案的一部分还在于确保尽可能保持窗口+光标同步。

这是我遇到的一个最小的例子(非常感谢 KA Buhr!)

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <GLFW/glfw3.h>

static double cursor_pos_x = 0;
static double cursor_pos_y = 0;
static double delta_x = 0;
static double delta_y = 0;

static int window_drag_active = 0;

static void mouse_button_callback(GLFWwindow *window, int button, int action,
                              int mods) {
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
        window_drag_active = 1;
        double x, y;
        glfwGetCursorPos(window, &x, &y);
        cursor_pos_x = floor(x);
        cursor_pos_y = floor(y);
    }
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
        window_drag_active = 0;
    }
}

int main() {
    glfwInit();

    GLFWwindow *win = glfwCreateWindow(400, 500, "Drag Example", NULL, NULL);

    glfwSetMouseButtonCallback(win, mouse_button_callback);
    glfwMakeContextCurrent(win);

    while (!glfwWindowShouldClose(win)) {
    glfwWaitEvents();

    if (window_drag_active) {
        double xpos, ypos;
        glfwGetCursorPos(win, &xpos, &ypos);
        delta_x = xpos - cursor_pos_x;
        delta_y = ypos - cursor_pos_y;

        int x, y;
        glfwGetWindowPos(win, &x, &y);
        glfwSetWindowPos(win, x + delta_x, y + delta_y);
    }

    glfwSwapBuffers(win);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)