防止光标移动到GLFW窗口边界之外?

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

我使用 OpenGL 和 C,窗口使用 GLFW。我已启用

glfwSetInputMode(window.self, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

但是光标没有锁定,并且移动到窗口之外,限制了 360 度+相机的移动。这是我处理鼠标输入的相机代码供参考:

void cameraMouseCallback(double xposIn, double yposIn) {
    float xpos = xposIn;
    float ypos = yposIn;

    /*printf("%f %f\n", window.width/2 - xpos, window.width/2 - ypos);
    glfwSetCursorPos(window.self, window.width/2, window.height/2);*/

    float xoffset = xpos - lastX;
    float yoffset = lastY - ypos; // Reversed since y-coordinates go from bottom to top
    lastX = xpos;
    lastY = ypos;

    xoffset *= camera.sensitivity;
    yoffset *= camera.sensitivity;

    yaw += xoffset;
    pitch += yoffset;

    // Make sure that when pitch is out of bounds, screen doesn't get flipped
    if (pitch > 89.0f)
        pitch = 89.0f;
    if (pitch < -89.0f)
        pitch = -89.0f;

    vec3 front;
    front[0] = cos(glm_rad(yaw)) * cos(glm_rad(pitch));
    front[1] = sin(glm_rad(pitch));
    front[2] = sin(glm_rad(yaw)) * cos(glm_rad(pitch));

    glm_vec3_normalize(front);

    glm_vec3_copy(front, cameraFront);
}

我尝试使用

glfwSetCursorPos(window.self, window.width/2, window.height/2);
将光标锁定到屏幕中心,并使用中心位置减去鼠标位置来获取增量,然后递增它。然而,这也行不通。我该如何解决这个问题,以便鼠标不会移出窗口,并且还允许自由移动相机/鼠标?

c linux glfw glm-math
1个回答
0
投票

我想通了。就像添加一样简单

    glfwSetCursorPosCallback(window.self, _mouseCallback);

    glfwSetInputMode(window.self, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

after调用光标回调。

© www.soinside.com 2019 - 2024. All rights reserved.