Search code examples
c++openglcameraglut

glutWarpPointer() not working while application is active


My problem is the screen will no longer rotate while my mouse is in screen. Only when the application is no longer active.

if I take out these warp pointers then it starts working again.

Here is my mouse update:

mouse.x = x;
mouse.y = y;

if (x > screen.x/2)
{
    player.angle -= 0.05f;
    player.lx = sin(player.angle);
    player.lz = -cos(player.angle);
    glutWarpPointer(screen.x/2,screen.y/2);
}
else if (x < screen.x/2)
{
    player.angle += 0.05f;
    //if (player.vertAngle < 0)
        //player.vertAngle = 0;
    player.lx = sin(player.angle);
    player.lz = -cos(player.angle);
    glutWarpPointer(screen.x/2,screen.y/2);
}
if (y < screen.y/2)
{
    player.vertAngle += 0.05f;
    player.ly = sin(player.vertAngle);
    glutWarpPointer(screen.x/2,screen.y/2);
}
else if (y > screen.y/2)
{
    player.vertAngle -= 0.05f;
    //if (player.vertAngle > 360)
        //player.vertAngle = 360;
    player.ly = sin(player.vertAngle);
    glutWarpPointer(screen.x/2,screen.y/2);
}

Solution

  • 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;
    }