Use a flag to ignore the extra motion event that glutWarpPointer()
generates:
#include <GL/glut.h>
#include <iostream>
using namespace std;
bool capture = false;
void keyboard( unsigned char key, int x, int y )
{
if( key == 'z' )
{
capture = !capture;
}
}
void passiveMotion( int x, int y )
{
static bool warped = false;
if( warped )
{
warped = false;
return;
}
if( capture )
{
warped = true;
int w = glutGet( GLUT_WINDOW_WIDTH );
int h = glutGet( GLUT_WINDOW_HEIGHT );
glutWarpPointer( w / 2, h / 2 );
int dx = ( w / 2 ) - x;
int dy = ( h / 2 ) - y;
cout << dx << " " << dy << endl;
}
else
{
cout << x << " " << y << endl;
}
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutKeyboardFunc( keyboard );
glutPassiveMotionFunc( passiveMotion );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}