Mondrian Example Program


The procedure Mouse is called called whenever a mouse button is pressed or released. The button state and (x,y) position are provided in the callback. In this example program, pressing the middle mouse button sets the seed for the random number generator to a new value, in effect causing a new image to be generated. Pressing the middle button toggles between filled and outlined rectangles.

Note that no drawing is done by this procedure. It calls glutPostRedisplay which schedules a display callback. (The display callback is initialized in main by the procedure glutDisplayFunc.)

void
Mouse(int button, int state, int x, int y)
{
    if( state == GLUT_DOWN ) {
        switch( button ) {
            case GLUT_LEFT_BUTTON:
                Seed = mrand48();
                break;
            case GLUT_MIDDLE_BUTTON:
                FillFlag = !FillFlag;
                break;
        }
        glutPostRedisplay();
    }
}


The procedure Keyboard is called whenever a key is pressed. In this case, when we press the <ESCAPE> key, the program exits. For convenience, the position of the mouse is also provided in the callback; but this information is not used in this simple program.

void
Keyboard( unsigned char key, int x, int y )
{
#define ESCAPE '\033'

    if( key == ESCAPE )
        exit(0);
}


CS248: Introduction to Computer Graphics, Pat Hanrahan, Winter 98