Search Projects

Saturday, March 2, 2013

How to add menu to your OpenGL Projects

In OpenGL programming there many way to make the user interactive programs. One of the things that is the use of menu, which is like selecting something (off-course with mouse) to change the state of some variable to achieve the desire result with that change. In this tutorial we are going to see how to add menu in OpenGL.



Adding menu in you OpenGL Projects not only make the program more user interactive but it make it smother and more usefull. So let get started with it.

For adding menu in our OpenGL Projects we use the glutCreateMenu, which create the popup menu and return  identifier (small integral value). The code looks like -

glutCreateMenu(menu); // it can be given to variable like mainmenu=glutCreateMenu(menu)

Firstly we create sub-menus then we go for the main menu which became the current menu.

glutAddSubMenu("Operating System", os);

The string "Operating System" represent the name of sub-menu that is to be displayed, while second argument 'os' is the sub-menu that has been created.

To add the entries in the sub menu list we use glutAddMenuEntry, which a menu entry to the bottom of the current menu.

glutAddMenuEntry("Windows", 1);

Here "Windows", is the string or the name that is to be displayed and the number is for the purpose of menu's callback function, when it is selected.
Since menu are mouse driven events in OpenGL, so we can add the interaction to either the left or right mouse button or on both (separate menu's). This is done by calling glutAttachMenu with right or left mouse button as argument.

 glutAttachMenu(GLUT_RIGHT_BUTTON); //for right mouse click
 glutAttachMenu(GLUT_LEFT_BUTTON);  //for left mouse click

This is all! So easy to add menu's to the projects in OpenGL.

Here is sample program that has menu demonstrated.

2 comments:

  1. Do you mind if I quote a couple of your posts as long as I provide credit and sources
    back to your blog? My blog is in the very same area of interest as yours and
    my visitors would genuinely benefit from some of the information
    you present here. Please let me know if this alright with you.
    Cheers!

    my web page new baby advice, ,

    ReplyDelete