6.B] How are menus and submenus created in OpenGL? Illustrate with an example.
Answer:
Menus are an important feature of any application program. OpenGL provides a feature called “Pop-up-menus” using which sophisticated interactive applications can be created.
Creating Menus and Submenus in OpenGL
- Define the Menu Callback Function
- Create a callback function that will handle menu item selections. This function will be called whenever a menu item is selected by the user.
- Create Menu Items
- Use the
glutCreateMenu
function to create a menu and specify the callback function. Then, useglutAddMenuEntry
to add individual menu items.
- Use the
- Create Submenus
- Create a submenu using
glutCreateMenu
and add items to it usingglutAddMenuEntry
. Attach this submenu to a parent menu usingglutAddSubMenu
.
- Create a submenu using
- Attach the Menu to a Mouse Button
- Use
glutAttachMenu
to attach the menu to a mouse button (usually the right mouse button).
- Use
// Function to create the menus void createMenu() { int submenu; // Create a submenu submenu = glutCreateMenu(menu); glutAddMenuEntry("Submenu Item 1", 3); glutAddMenuEntry("Submenu Item 2", 4); // Create the main menu glutCreateMenu(menu); glutAddMenuEntry("Menu Item 1", 1); glutAddMenuEntry("Menu Item 2", 2); glutAddSubMenu("Submenu", submenu); glutAddMenuEntry("Exit", 5); // Attach the menu to the right mouse button glutAttachMenu(GLUT_RIGHT_BUTTON); }
// Function to handle menu item selections void menu(int item) { switch (item) { case 1: printf("Menu Item 1 selected\n"); break; case 2: printf("Menu Item 2 selected\n"); break; case 3: printf("Submenu Item 1 selected\n"); break; case 4: printf("Submenu Item 2 selected\n"); break; case 5: printf("Exit selected\n"); exit(0); break; } }
Full Code:-
#include <GL/glut.h> #include <stdio.h> // Function to handle menu item selections void menu(int item) { switch (item) { case 1: printf("Menu Item 1 selected\n"); break; case 2: printf("Menu Item 2 selected\n"); break; case 3: printf("Submenu Item 1 selected\n"); break; case 4: printf("Submenu Item 2 selected\n"); break; case 5: printf("Exit selected\n"); exit(0); break; } } // Function to create the menus void createMenu() { int submenu; // Create a submenu submenu = glutCreateMenu(menu); glutAddMenuEntry("Submenu Item 1", 3); glutAddMenuEntry("Submenu Item 2", 4); // Create the main menu glutCreateMenu(menu); glutAddMenuEntry("Menu Item 1", 1); glutAddMenuEntry("Menu Item 2", 2); glutAddSubMenu("Submenu", submenu); glutAddMenuEntry("Exit", 5); // Attach the menu to the right mouse button glutAttachMenu(GLUT_RIGHT_BUTTON); } // Display function (for rendering) void display() { glClear(GL_COLOR_BUFFER_BIT); glFlush(); } // Main function int main(int argc, char** argv) { // Initialize GLUT glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow("OpenGL Menu Example"); // Set up the display function glutDisplayFunc(display); // Create the menu createMenu(); // Start the GLUT event loop glutMainLoop(); return 0; }
Explanation
- Menu Callback Function (
menu
)- This function is called whenever a menu item is selected. It handles different cases based on the item selected.
- Creating Menus (
createMenu
)- A submenu is created first using
glutCreateMenu
and populated with items usingglutAddMenuEntry
. - The main menu is created and populated with items, including the submenu using
glutAddSubMenu
. - Finally, the menu is attached to the right mouse button using
glutAttachMenu
.
- A submenu is created first using
- Display Function (
display
)- This function clears the screen and is required by GLUT, though it doesn’t do much in this example.
- Main Function (
main
)- Initializes GLUT, sets up the window, and starts the event loop.