/*================================================================= MenuExample Adapted from square.c by H. Masterman to illustrate the use of menus in glut. Version 1.0 February 2005 Version 2.0 September 2005 minor revisions Version 3.2 February 2009 Ported to Visual Studio 2005 Version 4.0 February 2011 minor revisions ===================================================================*/ #include #include #include using namespace std; /* globals */ GLsizei wh = 700, ww = 700; // initial window size GLfloat size = 6.0; // half side length of square int global_x_position, global_y_position; //stores where the figure is to be drawn float r=1.0,g=1.0,b=1.0; // to store the global color variables enum {square, triangle} current_shape; // stores the current selected shape //This function draws a square polygon of 2*size in the current color void drawSquare(int x, int y) { y=wh-y; //because the window coordinate system is flipped in y glBegin(GL_POLYGON); //drawing the polygon glVertex2f((float)x+size, (float)y+size); glVertex2f((float)x-size, (float)y+size); glVertex2f((float)x-size, (float)y-size); glVertex2f((float)x+size, (float)y-size); glEnd(); glFlush(); //insure that the commands clear the pipeline } //This function draws a trianglular polygon in the current color void drawTriangle(int x, int y) { y=wh-y; //because the window coordinate system is flipped in y glBegin(GL_POLYGON); glVertex2f((float)x, (float)y+size); glVertex2f((float)x-0.866f*size, (float)y-0.5f*size); glVertex2f((float)x+0.866f*size, (float)y-0.5f*size); glEnd(); glFlush(); //insure that the commands clear the pipeline } //this callback is called whenever the window is resized or moved //also called initially, so I included the view initialization here also void myReshape(GLsizei w, GLsizei h) { cout << "in reshape" << endl; // so we will know we are in the reshape callback //it is customary OpenGL style to set the viewing projection in the Reshape Function glMatrixMode(GL_PROJECTION); glLoadIdentity(); //set up an orthographic view with the same coordinate size as the current windo glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0,0,w,h); // set viewport to use the entire window ww = w; //reset the global width and height variables to current window size wh = h; } void accept_left_button_command(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) //this is only for left button events { global_x_position = x; //capture x mouse position in global global_y_position = y; //capture y mouse position in global glutPostRedisplay(); // post a display event } } // display callback void display(void) { cout << "in display" << endl; // just so you will know when display is called glColor3f(r,g,b); // set the color to the current color if (current_shape == triangle) drawTriangle(global_x_position, global_y_position); else if (current_shape == square) drawSquare(global_x_position, global_y_position); glFlush(); //insure that commands are executed } //shape select callback sets the global current_shape void shape_select(int shape) { if (shape == 0) current_shape = square; else if (shape == 1) current_shape = triangle; } //callback for the color selection menu sets globals, r,g,b void color_select (int color) { if(color == 1) {r = 1.0; g = 0.0; b = 0.0;} //red else if(color == 2) {r = 0.0; g = 1.0; b = 0.0;} //green else if(color == 3) {r = 0.0; g = 0.0; b = 1.0;} //blue else if(color == 4) {r = 0.0; g = 1.0; b = 1.0;} //cyan else if(color == 5) {r = 1.0; g = 0.0; b = 1.0;} //magenta else if(color == 6) {r = 1.0; g = 1.0; b = 0.0;} //yellow else if(color == 7) {r = 1.0; g = 1.0; b = 1.0;} //white else if(color == 8) {r = 0.0; g = 0.0; b = 0.0;} //black } void main_menu_callback(int selection) { if (selection == 0) exit(0); // exit the program if exit is selected else if (selection == 1) //clear screen { glClear(GL_COLOR_BUFFER_BIT); glFlush(); } else if (selection == 2) size = size*1.5f; //scale the size of global size multiplier } void set_up_menu(void) { int c_menu_id, shape_menu_id; // menu identifiers for submenus //create a color menu with callback color_select and assign to menu id c_menu_id c_menu_id = glutCreateMenu(color_select); glutAddMenuEntry("Red",1); glutAddMenuEntry("Green",2); glutAddMenuEntry("Blue",3); glutAddMenuEntry("Cyan",4); glutAddMenuEntry("Magenta",5); glutAddMenuEntry("Yellow",6); glutAddMenuEntry("White",7); glutAddMenuEntry("Black",8); //create a shape menu with callback shape_select and menu id shape_menu_id shape_menu_id = glutCreateMenu(shape_select); glutAddMenuEntry("Square", 0); glutAddMenuEntry("Triangle", 1); //create the main menu //we don't need the main menu id so don't save it glutCreateMenu(main_menu_callback); //attach the color and shape menus to the main menu as submenus glutAddSubMenu("Select Color", c_menu_id); glutAddSubMenu("Select Shape", shape_menu_id); glutAddMenuEntry("Exit",0); //this is not a submenu -> calls callback glutAddMenuEntry("Clear Screen",1); glutAddMenuEntry("Increase Size", 2); glutAttachMenu(GLUT_RIGHT_BUTTON); //attach menu to right button } int main(int argc, char** argv) { glutInit(&argc,argv); // this initializes glut glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // set up the frame buffer glutInitWindowSize(700,700); // this is the default window size glutCreateWindow("Menu Example"); // creates and names the window // here is where we register all of the callback functions for glut glutReshapeFunc (myReshape); glutMouseFunc (accept_left_button_command); glutDisplayFunc(display); set_up_menu(); // build pop up menu glutMainLoop(); // enter the event loop waiting for input }