PDA

View Full Version : Mac Os X and glutWarpPointer



Charogne
08-01-2017, 02:55 AM
Hi everybody!

As I explained in my previous and first thread in this forum, I am currently working at a very basic "game engine" in OpenGL + C++, providing the drawing and tranformation of meshes, the movement of cameras and so on.
Specifically, to build the interface with the user I am using GLUT, and I am using glutPassiveMotionFunc() and glutMotionFunc() to detect the movement of the mouse, that drives the rotation of the active Camera.
In particular, this is the code I am using:



FirstMouseMovement = true;

glutMotionFunc(onMouseMovement);
glutPassiveMotionFunc(onMouseMovement);


void onMouseMovement(int x, int y)
{
int Dx, Dy;


//If it is the first movement of the mouse
if(FirstMouseMovement)
{
//Just set the initial value of the mouse position
StartingMousex = x;
StartingMousey = y;

glutWarpPointer(w/2, h/2);

FirstMouseMovement = false;
}
//Otherwise
else
{
//Get the variation of the position of the mouse
Dx = x - StartingMousex;
Dy = y - StartingMousey;


//Consequently rotate the view
Cameras[currentCamera]->RotateCameraX(delta * Dx);
Cameras[currentCamera]->RotateCameraY(delta * Dy);


//Reset the mouse position
StartingMousex = x;
StartingMousey = y;

glutPostRedisplay();
}
}


And it works perfectly fine and smooth except for the fact that, when the mouse pointer meets the boundaries of the window, it arrests its movement and therefore the rotation of the camera.
Instead, I would like it to continue the rotation independently of the position of the mouse on the windows, like in a fps game.

Googling around, I found out that the way to achieve this should be using the glutWarpPointer() function to displace the mouse back to the centre of the window everytime we move it.
So, I modified the onMouseMovement() function as follows:




void onMouseMovement(int x, int y)
{
int Dx, Dy;


//If it is the first movement of the mouse
if(FirstMouseMovement)
{
//Just set the initial value of the mouse position
glutWarpPointer(w/2, h/2);

FirstMouseMovement = false;
}
//Otherwise
else
{
//Get the variation of the position of the mouse
Dx = x - w/2;
Dy = y - h/2;


//Consequently rotate the view
Cameras[currentCamera]->RotateCameraX(delta * Dx);
Cameras[currentCamera]->RotateCameraY(delta * Dy);


//Reset the mouse position
glutWarpPointer(w/2, h/2);

glutPostRedisplay();
}
}


However, this variation of the function produces a completely unsmooth and weird movement of the camera.
I googled around, and it seems to be due to the fact that I am working on Mac Os X, that has some problems with glWarpPointer.

Do you guys know any workaround to this problem? :)

Charogne
08-05-2017, 09:12 AM
Anybody has an answeeeeer? :)

Silence
08-06-2017, 11:24 PM
Read carefully these (quickly found) articles:

https://linux.die.net/man/3/glutwarppointer
https://www.gamedev.net/forums/topic/612651-help-understanding-how-to-get-glutwarppointer-to-work-right/
https://www.opengl.org/discussion_boards/showthread.php/146444-glutWarpPointer-question

From a quick read of these articles, it seems that:

1) glutWarpPointer expects relative positions, not absolute ones. You're giving absolute position, which then looks wrong
2) glutWarpPointer will most probably generate mouse motion event, which will then call your mouse move function callback. Take this into account for your code design.