GLUT Tutorial – Creating Menus and Submenus in GLUT

GLUT supports simple cascading pop-up menus. They are designed to let a user select various modes within a program. The functionality is simple and minimalistic and is meant to be that way. Do not mistake GLUT’s pop-up menu facility with an attempt to create a full-featured user interface. glutCreateMenu creates a Menu in GLUT. The syntax of glutCreateMenu is

 

This function defines the callback that has to be called when a menu item was selected. This callback function has one parameter, the value. This function returns anint, this is the menu identifier. This identifier is needed when you would want to attach this menu as a submenu. This is illustrated in sample example later.

 

This adds an entry to the menu with the label defined by name and the second parameter is the value that will be passed to the callback function. The menu is being added to the current menu. Each menu entry that is being added is added at the bottom of the current menu.

 

This adds the menu identified by the menu identifier as submenu with a given name to the current menu. The program won’t work if it contains an infinite loop of menus.

 

This attaches the current menu to a certain (mouse) event, you can let a menu listen to a specified mouse button, button can be one of the following:GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, and GLUT_RIGHT_BUTTON. You can also detach the menu using void glutDetachMenu(int button);.
Source code of sample example
SHARE GLUT Tutorial – Creating Menus and Submenus in GLUT

You may also like...

10 Responses

  1. Anonymous says:

    Hello
    i copied your code and complie ,but when running ,right click ,i recvied error Segmentation fault why?

  2. Morais says:

    You should replace "GlutPostRedisplay();" in the display function for "return;", otherwise you will enter a infinite loop.

  3. Anonymous says:

    I'm a newbie here, and I've tried to run this test case. It builds under my environment (gcc under SuSE Linux 12.2), but I simply can't get any response from the menu – it just doesn't appear. With no error messages, it's hard to know where to begin to look. Has anybody out there any ideas?

  4. Anonymous says:

    Thankyou so much this code helped me alot to understand functioning of menus …

    zak

  5. Renan says:

    I second Anonymous. Thanks for this.

  6. Anonymous says:

    This works for me in Windows 10. If I right-click in the created window, I get the following context menus and submenus displayed:
    Clear
    Draw
    Sphere
    Cone
    Torus
    Teapot
    Quit
    However, the "Clear" menu item doesn't seem to do anything.

  7. hello. i need to combine my project with my others friends. So how to I want to combine 4 project file in one ? can you help me ?

  8. where I have to put this void glutAddMenuEntry(char *name, int value);

  9. Anonymous says:

    Uncomment glutpostredisplay function and remove return statement in display function

Leave a Reply

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

Share