// 0. Compile and run the code. Left click in the window, and choose the menu options. // Are they functional? // 1. Complete the code so that the menus three options are functional: // (a) quit // (b) double the size of the box // (c) halve the size of the box // 2. Finally, modify the code so that a main menu provides the option of choosing // from two subMenus: // (a) a color menu, which changes the color of the box // (b) a size menu which functions as above #include #include #define WINDOWSIZE 400 GLint size = 100; void myInit() { glClearColor(1.0,1.0,1.0,0.0); glColor3f(0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,WINDOWSIZE,0.0,WINDOWSIZE); glMatrixMode(GL_MODELVIEW); } void display() { glClear(GL_COLOR_BUFFER_BIT); glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glBegin(GL_POLYGON); glVertex2f(0.0,0.0); glVertex2f(size,0.0); glVertex2f(size,size); glVertex2f(0.0,size); glEnd(); glFlush(); } void firstMenu (GLint id) { switch (id) { case 1: exit(0); break; case 2: size *= 2; break; case 3: size /= 2; break; } glutPostRedisplay(); } void sizeMenu (GLint id) { switch (id) { case 2: size *= 2; break; case 3: size /= 2; break; } glutPostRedisplay(); } void colorMenu (GLint id) { switch (id) { case 4: glColor3f(1.0,0.0,0.0); break; case 5: glColor3f(0.0,0.0,1.0); break; } glutPostRedisplay(); } void mainMenu (GLint id) { switch (id) { case 1: exit(0); break; } glutPostRedisplay(); } int main (int argc , char **argv) { GLint subMenu1, subMenu2; glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WINDOWSIZE,WINDOWSIZE); glutInitWindowPosition(0.0,0.0); glutCreateWindow("MenuDemo"); myInit(); glutDisplayFunc(display); /* // #1 glutCreateMenu(firstMenu); glutAddMenuEntry("quit",1); glutAddMenuEntry("bigger square",2); glutAddMenuEntry("smaller square",3); glutAttachMenu(GLUT_LEFT_BUTTON); */ // // #2 subMenu1=glutCreateMenu(sizeMenu); glutAddMenuEntry("bigger square",2); glutAddMenuEntry("smaller square",3); subMenu2=glutCreateMenu(colorMenu); glutAddMenuEntry("red",4); glutAddMenuEntry("blue",5); glutCreateMenu(mainMenu); glutAddMenuEntry("quit",1); glutAddSubMenu("size",subMenu1); glutAddSubMenu("color",subMenu2); glutAttachMenu(GLUT_LEFT_BUTTON); // glutMainLoop(); return 0; }