// OpenGL program // Illustrates interaction #include #include #include //global variables GLsizei wh = 500, ww = 500; /* initial window size */ GLfloat size = 3.0; /* half side length of square */ //prototypes void myinit(); // OpenGL init void display(); // display callback void myReshape(GLsizei, GLsizei); // reshape callback void mouse(int btn, int state, int x, int y); // callback void keyboard(unsigned char key, int x, int y); //callback void drawSquare(int x, int y); // motion call back void demo_menu(int id); //menu callback /****************/ /***** MAIN *****/ /****************/ void main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutCreateWindow("square"); myinit(); glutReshapeFunc(myReshape); glutKeyboardFunc(keyboard); glutMotionFunc(drawSquare); glutCreateMenu(demo_menu); glutAddMenuEntry("quit", 1); glutAddMenuEntry("increase square size", 2); glutAddMenuEntry("decrease square size", 3); glutAttachMenu(GLUT_RIGHT_BUTTON); glutDisplayFunc(display); glutMainLoop(); } /********************************/ /***** FUNCTION DEFINITIONS *****/ /********************************/ /*****************************************************************/ // init steps /*****************************************************************/ void myinit() { /* set viewing conditions */ glViewport(0,0,ww,wh); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww, 0.0, (GLdouble) wh, -1, 1); glMatrixMode(GL_MODELVIEW); /* clear color buffer stuff */ glClearColor(0.0, 0.0, 0.0, 1.0); //black glClear(GL_COLOR_BUFFER_BIT); glFlush(); } /*****************************************************************/ // Keyboard callback, keyboard events: // Quit the program on press of letter q or Q /*****************************************************************/ void keyboard(unsigned char key, int x, int y) { if (key=='q' || key=='Q') exit(0); } /*****************************************************************/ // Reshape callback, window events: // Ajust projection according to new winow parameters /*****************************************************************/ void myReshape(GLsizei w, GLsizei h) { /* adjust clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* adjust viewport and clear */ glViewport(0,0,w,h); glClearColor (0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); /* set global size for use by drawing routine */ ww = w; wh = h; } /*****************************************************************/ // Motion callback: // Draws a square of random color at x,y screen position /*****************************************************************/ void drawSquare(int x, int y) { y=wh-y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); glFlush(); } /*****************************************************************/ // display callback /*****************************************************************/ void display() { } /*****************************************************************/ // menu callback /*****************************************************************/ void demo_menu(int id) { if (id==1) exit (0); else if (id==2) size = 2* size; else if (size > 1) size = size/2; glClearColor (0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); glutPostRedisplay( ); }