Mondrian Example Program


/*
 * mondrian.c
 * 
 * Draw a bunch of colored rectangles in the style of the painter Mondrain.
 *
 *   Left button: randomizes the pattern
 *   Middle button: toggles between filled and outlined rectangles.
 *
 * Pat Hanrahan 1/97
 */

#include <stdlib.h>
#include <glut.h>

main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(512, 512);
    glutCreateWindow("Mondrian (RGB)");

    glutDisplayFunc(DrawMondrian);
    glutReshapeFunc(Reshape);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(Mouse);

    glutMainLoop();
}

The main program uses the glut library to create a window. The main program is also responsible for setting up callbacks to handle window system events (redraw and reshape) and input from the keyboard and mouse.

glutInit must be the first procedure called and initializes the system. For convenience, it accepts command line arguments as input, and parses them according to the standard X conventions.

glutInitDisplayMode(GLUT_RGB) instructs the system to create an RGB window; that is, true color image, not a color index image.

glutInitWindowSize(512, 512) instructs the system to try to create a 512x512 window.

glutCreateWindow("Mondrian (RGB)") actually creates the window according to the previous instructions and hints. The window manager is invoked, and in this example, since only the size and not the position of the window was specified, the user is asked to move the window into the desired position. The string passed to glutCreateWindow appears in the window's title bar.

The next four procedure define callbacks for handing window redraws, window reshapes, keyboard input and mouse input.

Finally, glutMainLoop() is called. At this point control is relinquished to the glut system, and all user code is only executed in response to some system event.


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