/* * 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 #include GLint Width = 512, Height = 512; int Times = 100; int FillFlag = 1; long Seed = 0; void DrawRect( float x1, float y1, float x2, float y2, int FillFlag ) { glBegin(FillFlag ? GL_QUADS : GL_LINE_LOOP); glVertex2f(x1, y1); glVertex2f(x2, y1); glVertex2f(x2, y2); glVertex2f(x1, y2); glEnd(); } void Display(void) { int i; float x1, y1, x2, y2; float r, g, b; srand48(Seed); glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); for( i = 0; i < Times; i++ ) { r = drand48(); g = drand48(); b = drand48(); glColor3f( r, g, b ); x1 = drand48() * Width; y1 = drand48() * Height; x2 = drand48() * Width; y2 = drand48() * Height; DrawRect( x1, y1, x2, y2, FillFlag ); } glFinish(); } void Reshape(GLint w, GLint h) { Width = w; Height = h; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, w, 0, h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } 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(); } } void Keyboard( unsigned char key, int x, int y ) { #define ESCAPE '\033' if( key == ESCAPE ) exit(0); } main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB); glutInitWindowSize(Width, Height); glutCreateWindow("Mondrian (RGB)"); glutDisplayFunc(Display); glutReshapeFunc(Reshape); glutKeyboardFunc(Keyboard); glutMouseFunc(Mouse); glutMainLoop(); }