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?