GLFW - glfwSetMousePos Bug on Mac OS X 10.7 with OpenGL camera
Asked Answered
D

4

5

I have been following the tutorials at http://opengl-tutorials.org and they are brilliant so far (I'm on a Mac so I am having to use OpenGL 3.2 and GLSL 1.50 rather than OpenGL 3.3 and GLSL 3.30). The only problem with the tutorials so far, is that with the 3D camera tutorial (Tutorial 6: Keyboard and Mouse), when I move my mouse, I do not get any rotation what so ever and if I do, it will only be slowly in the down direction; even if I move my mouse in any direction.

I have compiled the code given (OpenGL 2.1 and 3.x) as well as write it by hand, and this still presents this bug. I have not idea why this would happen. Could this be a bug with GLFW, Mac OS X or something else?

Disinfectant answered 22/1, 2013 at 21:9 Comment(1)
What about posting this to the GLFW bug tracker?Ciceronian
B
4

I know it's quite an old question, but I had the same issue so it might be of help. I have downloaded the code from the website and in common/controls.cpp this line was commented:

glfwSetMousePos(1024/2, 768/2);

Apparently there is a bug in GLFW with MacOS for which this instruction doesn't work properly. Hopefully they fixed it in the newer versions, but I haven't tested it yet.

On a side note, commenting this line will make the tutorial work, but you might experience some issues when clamping the vertical camera angle: if you move the mouse past the clamping point (say going up), the mouse position will keep updating and when you move the mouse down you will have to wait until it reaches the clamping point before the camera moves again.

[EDIT] Here's the full modified code

// Reset mouse position for next frame
// EDIT : Doesn't work on Mac OS, hence the code below is a bit different from the website's
//glfwSetMousePos(1024/2, 768/2);

// Compute new orientation
horizontalAngle = 3.14f + mouseSpeed * float( 1024/2 - xpos );
verticalAngle   = mouseSpeed * float( 768/2 - ypos );
Baerman answered 11/9, 2013 at 10:5 Comment(1)
Thank you for the reply and answer. GLFW 3 seems to fix it but still not in GLFW 2. It seems to be a bug but it's a bug that has really 'bugged' me per se.Disinfectant
D
3

As GLFW3’s doc mention :

"The window must have input focus.”

So you should call glfwSetCursorPos until window have input focus ! It is not a bug in GLFW with MacOS !

The code should like that:


    bool firstMouse = true;

    float centerX = (float)mWidth / 2, centerY = (float)mHeight / 2;

    GLFWwindow* mWindow;

    void mouse_callback(GLFWwindow *window, double xPos, double yPos) {
      if (firstMouse) {
        // why here?
        // As
        // https://www.glfw.org/docs/3.3/group__input.html#ga04b03af936d906ca123c8f4ee08b39e7
        // mention :
        // "The window must have input focus."
        glfwSetCursorPos(mWindow, centerX, centerY);
        firstMouse = false;
      }

      // ...
    }

    void main() {
       // ...

       glfwSetCursorPosCallback(mWindow, mouse_callback);

       // ...
    }
Dews answered 16/3, 2020 at 8:41 Comment(0)
T
1

There's still the same problem with GLFW 3 on OS X 10.9.

To disable the cursor and thus trap it inside the window I added this after the window is created.

glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

Then I commented out //glfwSetCursorPos(window, 1024 / 2, 768 / 2); in controls.cpp

Now the mouse movement works. However, it seems like when viewing the cube from certain angle, the view is rotated when moving the mouse left and right while from other angle the view moves left and right as it should. Strange.

Typhoid answered 17/5, 2014 at 20:2 Comment(0)
G
1

For GLFW2 on OSX add this line in your tutorial-XX.cpp after glfwCreateWindow( ...,...,)

glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

and then create two global variables in common/controls.cpp

double xpos_old, ypos_old;

finally comment out the glfwSetCursorPos(window, 1024/2, 768/2); and change the calculation of the angles to the new setup, the idea is to get the difference between the old mousepos and the new. The final code should look like this:

// Reset mouse position for next frame
// glfwSetCursorPos(window, 1024/2, 768/2);
// Compute new orientation

horizontalAngle += mouseSpeed * float( xpos_old - xpos );
verticalAngle   += mouseSpeed * float( ypos_old - ypos );
xpos_old = xpos;
ypos_old = epos;

This works great on my mac! cheers!

Giralda answered 23/5, 2014 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.