/* Mouse Driven Squares Drawing Example Program VIZA 652 Fred Parke 11/04/97 */ #include /* define standard I/O routines */ #include /* define OpenGL graphics routines */ #include /* define OpenGL utility routines */ #include /* define GLUT window and device routines */ #include /* define math library routines */ int wh = 500; /* window dimensions */ int ww = 500; float size = 10.0; /* half size of the drawn sqaures */ void drawSquare(int x, int y) /* executed once for each sqaure */ { y = wh-y; /* invert y mouse coordinate */ /* pick a random number for this sqaure */ glColor3ub((char) random()%256, (char) random()%256, (char) random()%256); glBegin(GL_POLYGON); /* draw the sqaure */ glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); glFlush(); /* flush out the graphics commands */ } void myinit(void) /* intialize window coordiantes */ { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,ww,0,wh); glViewport(0,0,ww,wh); } void clearwindow(void) /* clear the window to black */ { glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); /* flush out the graphics commands */ } /* prodecure to handle mouse events */ void mouse(int btn, int state, int x, int y) { if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) exit(); if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) clearwindow(); /* previous line could be replaced with if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) glutPostRedisplay(); */ } /* Main program */ void main(int argc, char* argv[]) { glutInit(&argc, argv); /* intialize the glut package */ /* create a single buffer RGB window */ glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(ww,wh); glutCreateWindow("Squares"); myinit(); /* Initialize routine */ glutDisplayFunc(clearwindow); /* register display callback procedure */ glutMouseFunc(mouse); /* register mouse event callback */ glutMotionFunc(drawSquare); /* register mouse motion callback */ glutMainLoop(); /* enter main loop */ }