/* drawrects.cpp * This program demonstrates the drawing of a set of randomly * generated aligned rectangles * * Click the right button of the mouse to exit the program */ #include << X11/Xlib.h> #include << fstream.h> #include << math.h> #include << GLUT/glut.h> #include << OpenGL/gl.h> #include << stdlib.h> #include << stdio.h> const int screenWidth = 640; const int screenHeight = 480; GLdouble A, B, C, D; void myInit(void) { glClearColor (1.0, 1.0, 1.0, 0.0); // set black background color glColor3f (0.0f, 0.0f, 0.0f); // set the drawing color glPointSize (2.0); // set dot size 2 x 2 glMatrixMode (GL_PROJECTION); // set "camera shape" glLoadIdentity (); // clear the matrix // viewing transformation gluOrtho2D (0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight); // set the Worlk Window } int random(int m) { return rand() % m; } void drawFlurry(int num, int numColors, int Width, int Height) // draw num random rectangles in a Width by Height rectangle { for (int i = 0; i < num; i++) { GLint x1 = random(Width); // place corner randomly GLint y1 = random(Height); GLint x2 = random(Width); // pick the size so it fits GLint y2 = random(Height); GLfloat lev = random(10)/10.0; // random value, in range 0 to 1 glColor3f(lev,lev,lev); // set the gray level glRecti(x1, y1, x2, y2); // draw the rectangle } glFlush(); } void myDisplay(void) { glClear (GL_COLOR_BUFFER_BIT); // clear the screen GLint numRects = 7, numColors=7; drawFlurry( numRects, numColors, screenWidth, screenHeight); } void myMouse(int button, int state, int x, int y) { switch (button) { case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) exit (-1); break; default: break; } } int main(int argc, char** argv) { glutInit(&argc, argv); // initialize the toolkit glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // set display mode glutInitWindowSize (screenWidth, screenHeight); // set screen window size glutInitWindowPosition (100, 150); // set window position on screen glutCreateWindow (argv[0]); // open the screen window myInit (); glutDisplayFunc(myDisplay); // register redraw function glutMouseFunc(myMouse); // register the mouse action function glutMainLoop(); // go into a perpetual loop return 0; }