Creating a simple menu in OpenGL GLUT

Introduction

The following program contains the use of menu and submenu items in opengl how to use them and how to apply menu in opengl projects .

The following program describes how we can effectively use menu item to create different types of structure and change their color using menu items.open_menu

The following program has menu enabled which fired when we hit the mouse right button and a menu pops which have some object to be selected and further selecting an object it provides a submenu for choosing the color of the object.

 

Function used in the Program

There are mainly four function used in the program namely: menu(), createMenu() , display() and finally main().

1.menu():

This function of the program is used to recall the display function using glutPostRedispaly(). This function has an integer argument which is sent by the glutAddMenuEntry() function defined in the createMenu() function.

This integer value is static which is used and initialized so that when calling the display function inside menu() function through glutPostRedisplay() a particular object with particular color can be drawn.

This menu() function also has a glutDestroyWindow() function which closes the window (graphic/opengl) when a certain condition is met here n==0.

The parameter to the glutDestroyWindow() is passed from the main() function where the id is passed when calling window=glutCreateWindow(); line.

 

2.createMenu():

This function is a important function where all the menu items are made. The function glutCreateMenu() is used to create a frame for the items of the menu.

The glutAddMenuEntry() function is used to provide the name and the value for the static integer value which was being passed to the menu() function before.

With the following static integer matching value from the display()

function will be used to draw a particular object with particular color.

Similar to glutAddMenuEntry() function there is glutAddSubMenu()

function which is used to create submenu inside a menu item.

The argument to the glutAddSubMenu() function is the

name of the submenu item and menu_id of the menu item of which this submenu item is going to be a part.

The main function of this createMenu() function is attaching this whole menu with a certain mouse key so it can be popped up whenever that key is pressed.

For this function used is glutAttachMenu() and the argument to the following function can be the keys mouse is having for eg. GLUT_RIGHT_BUTTON etc.

3.display():

          This function is used for making the object on the display window. This function contains a function named glClear()  which clears the window everytime its called from either main() function or menu() function.

This function has conditions on the static integer variable which is defined globally.

Every object which is displayed on the window screen is basically based on this variable value. For eg. in this program the first item of the menu and first color of the submenu so value of variable is 11(only in this program not always).

Drawing any object needs following functions named: glPushMatrix(), glPopMatrix(), glColor3d(), and type to object to be drawn for eg. for solid teapot we use glutSolidTeapot().

4.main():

          This function as already discussed is the main function for creating any c or c++ or any opengl program should be present.

The following function is responsible for the calling of the createMenu() and the display() function whenever any object is to be redisplayed.

Code Used:


#include <windows.h>
#include <GL/glut.h>
static int window, returnmenu, returnsubmenu, returnsubmenucolor1, returnsubmenucolor2, returnsubmenucolor3, returnsubmenucolor4, value = 0;
void menu(int n){
if (n == 0){glutDestroyWindow(window);
exit(0);}
else{value = n;}
glutPostRedisplay();
}
void createMenu(void){

//Teapot
returnsubmenucolor1 = glutCreateMenu(menu);
glutAddMenuEntry("Red", 11);
glutAddMenuEntry("White", 12);
glutAddMenuEntry("Yellow", 13);
glutAddMenuEntry("Green", 14);
glutAddMenuEntry("Blue", 15);

//Sphere
returnsubmenucolor2 = glutCreateMenu(menu);
glutAddMenuEntry("Red", 21);
glutAddMenuEntry("White", 22);
glutAddMenuEntry("Yellow", 23);
glutAddMenuEntry("Green", 24);
glutAddMenuEntry("Blue", 25);

//Torus
returnsubmenucolor3 = glutCreateMenu(menu);
glutAddMenuEntry("Red", 31);
glutAddMenuEntry("White", 32);
glutAddMenuEntry("Yellow", 33);
glutAddMenuEntry("Green", 34);
glutAddMenuEntry("Blue", 35);

//Cone
returnsubmenucolor4 = glutCreateMenu(menu);
glutAddMenuEntry("Red", 41);
glutAddMenuEntry("White", 42);
glutAddMenuEntry("Yellow", 43);
glutAddMenuEntry("Green", 44);
glutAddMenuEntry("Blue", 45);

returnmenu = glutCreateMenu(menu); //function to call menu function and return value
glutAddMenuEntry("Clear", 1);
glutAddSubMenu("Teapot", returnsubmenucolor1);
glutAddSubMenu("Sphere", returnsubmenucolor2);
glutAddSubMenu("Torus", returnsubmenucolor3);
glutAddSubMenu("Cone", returnsubmenucolor4);
glutAddMenuEntry("Quit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
if (value == 1){
return;
}

else if (value == 11){
glPushMatrix();
glColor3d(1.0, 0.0, 0.0);
glutSolidTeapot(0.5);
glPopMatrix();
}
else if (value == 12){
glPushMatrix();
glColor3d(1.0, 1.0, 1.0);
glutSolidTeapot(0.5);
glPopMatrix();
}
else if (value == 13){
glPushMatrix();
glColor3d(1.0, 1.0, 0.0);
glutSolidTeapot(0.5);
glPopMatrix();
}
else if (value == 14){
glPushMatrix();
glColor3d(0.0, 1.0, 0.0);
glutSolidTeapot(0.5);
glPopMatrix();
}
else if (value == 15){
glPushMatrix();
glColor3d(0.0, 0.0, 1.0);
glutSolidTeapot(0.5);
glPopMatrix();
}
else if (value == 21){
glPushMatrix();
glColor3d(1.0, 0.0, 0.0);
glutWireSphere(0.5, 50, 50);
glPopMatrix();
}
else if (value == 22){
glPushMatrix();
glColor3d(1.0, 1.0, 1.0);
glutWireSphere(0.5, 50, 50);
glPopMatrix();
}
else if (value == 23){
glPushMatrix();
glColor3d(1.0, 1.0, 0.0);
glutWireSphere(0.5, 50, 50);
glPopMatrix();
}
else if (value == 24){
glPushMatrix();
glColor3d(0.0, 1.0, 0.0);
glutWireSphere(0.5, 50, 50);
glPopMatrix();
}
else if (value == 25){
glPushMatrix();
glColor3d(0.0, 0.0, 1.0);
glutWireSphere(0.5, 50, 50);
glPopMatrix();
}
else if (value == 31){
glPushMatrix();
glColor3d(1.0, 0.0, 0.0);
glutWireTorus(0.3, 0.6, 100, 100);
glPopMatrix();
}
else if (value == 32){
glPushMatrix();
glColor3d(1.0, 1.0, 1.0);
glutWireTorus(0.3, 0.6, 100, 100);
glPopMatrix();
}
else if (value == 33){
glPushMatrix();
glColor3d(1.0, 1.0, 0.0);
glutWireTorus(0.3, 0.6, 100, 100);
glPopMatrix();
}
else if (value == 34){
glPushMatrix();
glColor3d(0.0, 1.0, 0.0);
glutWireTorus(0.3, 0.6, 100, 100);
glPopMatrix();
}
else if (value == 35){
glPushMatrix();
glColor3d(0.0, 0.0, 1.0);
glutWireTorus(0.3, 0.6, 100, 100);
glPopMatrix();
}
else if (value == 41){
glPushMatrix();
glColor3d(1.0, 0.0, 0.0);
glRotated(65, -1.0, 0.0, 0.0);
glutWireCone(0.5, 1.0, 50, 50);
glPopMatrix();
}
else if (value == 42){
glPushMatrix();
glColor3d(1.0, 1.0, 1.0);
glRotated(65, -1.0, 0.0, 0.0);
glutWireCone(0.5, 1.0, 50, 50);
glPopMatrix();
}
else if (value == 43){
glPushMatrix();
glColor3d(1.0, 1.0, 0.0);
glRotated(65, -1.0, 0.0, 0.0);
glutWireCone(0.5, 1.0, 50, 50);
glPopMatrix();
}
else if (value == 44){
glPushMatrix();
glColor3d(0.0, 1.0, 0.0);
glRotated(65, -1.0, 0.0, 0.0);
glutWireCone(0.5, 1.0, 50, 50);
glPopMatrix();
}
else if (value == 45){
glPushMatrix();
glColor3d(0.0, 0.0, 1.0);
glRotated(65, -1.0, 0.0, 0.0);
glutWireCone(0.5, 1.0, 50, 50);
glPopMatrix();
}
glFlush();
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
window = glutCreateWindow("Menu");
createMenu();
glClearColor(0.0, 0.0, 0.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
return 1;
}

Leave a Reply

Your email address will not be published. Required fields are marked *