How to fix GLFW cursor position value keep on getting bigger under GLFW_CURSOR_DISABLE?
Asked Answered
Q

1

7

I am using opengl as my window. I wanted to implement a camera class and found that the regular cursor (GLFW_CURSOR_NORMAL) was inadequate for a camera. Thus I ran the command glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_DISABLE) to have opengl recenter the mouse for me and gives back the cursor position through a call back method which I set using the method glfwSetCursorCallback(window,callbackPos)

The callback function (callbackPos) takes in a window and two doubles for x position and y positions as stated on the glfw website. http://www.glfw.org/docs/latest/input_guide.html

However, these two doubles keep growing and growing every time I shifted my mouse in one direction. My problem comes from the fact that through the whole usage of the window and movement of the mouse in one direction, the whole double will be too small to even hold the value being give since it is constantly growing. Is there a solution to this problem or this is not something that I should be worrying about?

Qianaqibla answered 29/7, 2017 at 18:50 Comment(4)
Could you be more specific about "constantly growing", this is only while moving the mouse correct? Because while the cursor is disabled, GLFW uses a virtual cursor position, that as far as I recall has no limit.Dimenhydrinate
Hi, what I mean by it has no limit is as follow if you move the mouse, the mouse to the left, the double will become more negative so if it is -100 then it becomes something like -400. If the user keep going to the left, eventually it will reach a point where the double will overflow right? Then calculating the delta x that is required for my camera is no longer accurate and will causes problem.Qianaqibla
Yes and that's the effect of the virtual mouse position. However you're probably not likely to reach a precision problem that significant, unless you have the program open for weeks always moving the mouse manually in one direction.Dimenhydrinate
Thanks for the answer. So it is something I shouldn't be worrying about.Qianaqibla
M
7

So I'm not actually sure if this Truly Correct (TM), but I had the same concern as you and found your question with no answers, so I might as well post my solution (which works for me).

Inside the cursor position callback you can simply call glfwSetCursorPos(window, 0, 0) and it will reset the virtual cursor's position back to (0, 0); the next invocation of your cursor position callback will now be relative to that! Note that, at least in my tests, setting the cursor position inside the callback function does not cause the callback to be called again (i.e. it only gets called when the user actually moves the cursor with their hand).

In other words, the code is something like this:

static void cursor_position_callback(GLFWwindow *window, double x, double y)
{
    // (x, y) are relative!
    ...

    glfwSetCursorPos(window, 0, 0);        
}

int main(...)
{
    GLFWwindow *window = ...;
    ...
    glfwSetCursorPosCallback(window, cursor_position_callback);
    ...
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
    ...
}
Mugwump answered 2/3, 2019 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.