int Start = 0; int StartX, StartY; void Mouse(int button, int state, int x, int y) { Start = 0; if( state == GLUT_DOWN ) switch( button ) { case GLUT_LEFT_BUTTON: glColor3f( drand48(), drand48(), drand48() ); StartX = x; StartY = Height - y; Start = 1; break; case GLUT_MIDDLE_BUTTON: glutPostRedisplay(); break; } } void MouseDrag( int x, int y ) { DrawRect( StartX, StartY, x, Height - y ); }
The event handler Mouse simply establishes the corner of the rectangle when the button is pressed.
The event handler MouseDrag continually draws a rectangle until the button is released.
Note that default mouse y coordinates are inverted from window coordinates.
CS248: Introduction to Computer Graphics, Pat Hanrahan