Search Projects

Sunday, December 18, 2016

OpenGL Glut Create Menu Example

OpenGL Glut program can be mainly divided into 4 Parts. First part includes initialization with library and necessary files. Second Part include other necessary functions and third is the display functions which draw the object on screen. Final part is the main function. In this OpenGL Glut Create Menu Example, we will see these 4 parts separately.

This program will teach us how to add menu and submenu in opengl program. The menu can be created with click of right mouse button. In the OpenGL Glut Create Menu Example program menu and submenu in opengl id demonstration. The menu create have different types of structures whose color can be changed with selection of color in respective submenu.


In this program we have three function - main, display, menu and createmenu used. We all know what main function do, so no tension about it. The function - display, menu and createmenu need to be understand.

The display function have all the code abou the objects that is going to show on the screen. The various object drawn are - Teaport, Torus, Sphere and Cone. Each of these objects are coded separately and placed in the display function. A global variable declared which helps in determination color and the object combination. Hence we are able to screen the objects in different colors as per the user selection in submenu. All these has been done with nested if else statement.

The createmenu Function lets us create menu and submenu. In main function first createmenu() is called. In the createmenu function glutCreateMenu(menu) is called for creation of each of the menu. Each selected menu entry passes the value passed for the callback.

Next is to add menu.The glutAddMenuEntry() function  will add the menu which passed to createmenu() along with the name of menu. For each menu a separate glutAddMenuEntry() function is required along with the different integer callback. If name of menu is same there will be now error while if integer callback is same, compiler error will show up.

Then we have glutAddSubMenu() function which create the submenu inside a menu item. It works in same way as glutAddMenuEntry() function. Different color is set in each of menu as submenu so that object in different color can be displayed.


These are following functions used in the program along with the opengl functions.

  1. Teapot - glutSolidTeapot(r);
  2. Sphere - glutWireSphere(r, w, h);
  3. Torus - glutWireTorus(r, f, w, h);
  4. Cone - glutWireCone(r, f, w, h);
The color of each object used - Red,White,Yellow. Green and Blue attached with the menu in this OpenGL Glut Create Menu Example. Hence there are 5*4=20 objects can be drawn in display.

To trigger the menu and popup the menu, it can be attached to right or left mouse button with glutAttachMenu(). 

To attach it to right mouse button  - glutAttachMenu(GLUT_RIGHT_BUTTON);
To attach it to left mouse button - glutAttachMenu(GLUT_LEFT_BUTTON);

In our program we have used the GLUT_RIGHT_BUTTON so menu will appear on right click of mouse.

Download the source code.

No comments:

Post a Comment