Menus

OpenGL does not provide support for menus (or any other menu system function). The GLUT library, however, does provide rudimentary popup menus.

The popup menus can be arrainged in a hierarchy of menus, sub-menus, sub-sub-menus, and so on. Since a sub-menu identifier must be passed as an argument to the GLUT functions that create its parent menu, the menu hierarchy must be defined from the bottom up.

The following fragments of code in your main function will create a main popup menu with two submenus.

#define MENU_A    1                  /* menu option identifiers */
#define MENU_B    2
#define MENU_C    3
#define MENU_D    4
#define MENU_QUIT 9

  int sub1, sub2;                    /* submenu identifiers */
  ...

  glutCreateWindow("OpenGL Example 10 - Menus");

  ...

  sub2 = glutCreateMenu(menuCB);             /* submenu 2 */
  glutAddMenuEntry("Option D", MENU_D);      /* submenu option D */

  sub1 = glutCreateMenu(menuCB);             /* submenu 1 */
  glutAddMenuEntry("Option B", MENU_B);      /* submenu option B */
  glutAddMenuEntry("Option C", MENU_C);      /* submenu option C */
  glutAddSubMenu("Submenu 2",  sub2);        /* submenu 2 */

  glutCreateMenu(menuCB);                    /* main popup menu */
  glutAddMenuEntry("Option A", MENU_A);      /* menu option A */
  glutAddSubMenu("Submenu 1",  sub1);        /* submenu 1 */
  glutAddMenuEntry("Quit",     MENU_QUIT);   /* quit option */

  glutAttachMenu(GLUT_RIGHT_BUTTON); /* tie popup menu to right mouse button */

  ...

The menu callback function menuCB, passed as an argument to the glutCreateMenu routine, will look something like this:

void menuCB(int item)
{
  switch (item) {                     /* process the popup menu commands */
  case MENU_A:
    /* process menu option A... */
    break;
  case MENU_B:
    /* process menu option B... */
    break;
  case MENU_C:
    /* process menu option C... */
    break;
  case MENU_D:
    /* process menu option D... */
    break;
  case MENU_QUIT:
    exit(0);                          /* quit the program */
    break;
  }
}

A complete working example is available as example10 in the Athena course locker, /mit/13.016/graphics/example10-menu.c.


On to multiple windows...
Back to the 13.016 overview...