Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

opengl

glfw

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?

like image 749
Wowzer Avatar asked Jul 29 '17 18:07

Wowzer


1 Answers

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);
    ...
}
like image 148
Vegard Avatar answered Nov 18 '22 14:11

Vegard