Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 994
Xplane Avatar asked Jan 22 '13 21:01

Xplane


2 Answers

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 );
like image 74
Marco Castorina Avatar answered Oct 04 '22 21:10

Marco Castorina


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);

       // ...
    }
like image 22
lihansey Avatar answered Oct 04 '22 20:10

lihansey