Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Menuing, Introduction to Picking

Similar presentations


Presentation on theme: "Advanced Menuing, Introduction to Picking"— Presentation transcript:

1 Advanced Menuing, Introduction to Picking
Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, September 29, 2003

2 Review: Using GLUT Menus [1/3]
What we can do with GLUT menus: We can create (and destroy) menu data structures. Create a menu with glutCreateMenu. Creation  display. We can add entries (and submenus) to an existing menu. Add an entry with glutAddMenuEntry. We can attach a previously created menu to a mouse button (and we can also detach it). Attach a menu with glutAttachMenu. We can change the entries of a menu. Each menu has associated callback: the handler function. This is called when the user selects an entry in that menu. As is typical with GUI toolkits, under GLUT the application does not draw pop-up menus. GLUT does that. GLUT is entirely in control while a pop-up menu is visible. 29 Sep 2003 CS 381

3 Review: Using GLUT Menus [2/3]
The menu-creation function from creature.cpp. This is called in function init. void make_main_menu() { glutCreateMenu(handle_main_menu); glutAddMenuEntry("Toggle eye color", 1); glutAddMenuEntry("Toggle mouth motion", 2); glutAddMenuEntry("Quit", 3); glutAddMenuEntry(" ", 999); glutAddMenuEntry("for CS 381, fall 2003", 999); glutAttachMenu(GLUT_RIGHT_BUTTON); } 29 Sep 2003 CS 381

4 Review: Using GLUT Menus [3/3]
The menu handler function from creature.cpp. Must be declared before make_main_menu. As with all GLUT callbacks, you do not call this function; GLUT does. // handle_main_menu // Menu handling callback function for our menu void handle_main_menu(int value) { switch (value) case 1: // Toggle eye color blueeyes = !blueeyes; glutPostRedisplay(); break; case 3: // Quit exit(0); case 999: // Other menu items do nothing 29 Sep 2003 CS 381

5 (Review?) Advanced Menuing: The Current Menu
Some GLUT menuing commands (e.g., glutAddMenuEntry, glutAttachMenu) deal with the current menu. If you have just one menu, then it is the current menu. glutCreateMenu sets the current menu to the menu created. In a menu handler, the current menu is the one handled. Function glutCreateMenu returns an int identifying the menu created. If you have multiple menus, save this value. int menu_x = glutCreateMenu(handle_menu_x); You can set the current menu yourself using glutSetMenu. There is also a “current window”. It is dealt with similarly. 29 Sep 2003 CS 381

6 (Review?) Advanced Menuing: Submenus
A submenu is a menu that is an item in another menu. To make a submenu (in the “main” menu): Create the submenu (as an ordinary menu, but do not attach it). Create the main menu. Use glutAddSubMenu to make submenu an item in the main menu. int superduper_menu = glutCreateMenu(handle_superduper_menu); int rightmouse_menu = glutCreateMenu(handle_rightmouse_menu); glutAddSubMenu("Super-Duper Stuff", superduper_menu); The submenu does not need to be attached. See dumbsubmenus.cpp, on the web page, for sample code. 29 Sep 2003 CS 381

7 (Review?) Advanced Menuing: Changing Menu Entries
To change the text or return value of a menu entry: Set the the current menu, by passing the proper ID to glutSetMenu. If you are in the menu handler, this is unnecessary. glutSetMenu(superduper_menu); Use glutChangeToMenuEntry to set the new text & return value. This function takes 3 parameters: The entry number (int): count from the top of the menu, starting at 1. The new text (char*). The new return value (int). glutChangeToMenuEntry(1, the_entry.c_str(), 1); You can also change submenus, using glutChangeToSubMenu; see the GLUT documentation. See menuchange.cpp, on the web page, for sample code. 29 Sep 2003 CS 381

8 Picking: Overview With a pointing device we can point at things.
The program must determine what is being pointed at. This called picking. We will discuss: What picking is and what it is for. Issues involved in picking. Three picking methods. The “obvious” method, and two that depend on specialized commands. The first programming assignment after the test (#4) will involve picking. 29 Sep 2003 CS 381

9 Picking: Definitions In a CG scene, we often have a number of objects.
Objects are made up of graphics primitives. The extent of an object or primitive is the collection of pixels (screen positions) that are used to display it. When the user clicks the mouse, we generally want to know: Whether the click was in the extent of an object and, if so, which object. We use the term picking to encompass both the user’s action and the program’s determination of which object was chosen. Sometimes we consider the concept of extent in a looser fashion. We can allow clicks near an object, not necessarily right on it. Sometimes we allow clicks anywhere in the smallest screen-aligned rectangle that encloses a displayed object. This is called the bounding rectangle. 29 Sep 2003 CS 381

10 Picking: Uses When do we use picking?
When doing click-and-drag: Moving/resizing windows. Moving icons. When using widgets (controls). What kind of mouse-based input does not involve picking? Various game controls. Painting. Unless you count the in-drawing-area test. 29 Sep 2003 CS 381

11 Picking: Issues Recall the OpenGL geometry rendering pipeline:
We send geometric primitives into the pipeline. Images on the screen emerge. When we do picking, we want to reverse this. We begin with a screen position (the mouse location). We want to know what primitive, if any, appears there. Again, primitives are generally grouped into objects. But the geometry pipeline does not run backwards! Vertex Operations Rasterization Fragment Operations Vertex enters here To framebuffer Vertices (object coordinates) Vertices (window coordinates) Fragments Fragments 29 Sep 2003 CS 381

12 Picking: Method #1: Extent Testing
The “obvious” way to do picking: Figure out the extent of each object. For each, test whether the mouse position is inside it. In a 2½-D interface (e.g., overlapping windows), can test in front-to-back order. Disadvantage Can be tricky, if objects are not screen-aligned rectangles. (However, they very often are!) Next we look at two methods that take advantage of CG internals, and do not have the above problem. 29 Sep 2003 CS 381

13 Picking: Method #2: Buffer Reading [1/2]
If we can read the frame buffer: Draw each object in a different, solid color. Read the color of the pixel at the mouse position to determine what was clicked on. Double buffering makes this nicer. Draw to and read from the back buffer, without swapping. Then users don’t need to see the strangely colored version of the scene. 29 Sep 2003 CS 381

14 Picking: Method #2: Buffer Reading [2/2]
The OpenGL command glReadPixels reads a rectangular block of pixels from a color buffer. The block can be 11 (a single pixel). Watch out for precision! The color returned may not be exactly the color that was drawn. May need to call glDrawBuffer to set which buffer is drawn to. 1 parameter: generally GL_FRONT (default) or GL_BACK. When double buffering is enabled (GLUT_DOUBLE), GLUT does “glDrawBuffer(GL_BACK);”. So you probably do not need to call glDrawBuffer yourself. May need to call glReadBuffer to set which buffer is read. 29 Sep 2003 CS 381

15 Picking: Method #3: Selection Mode
OpenGL allows “names” to be given to primitives. A name is just an integer, as usual. In selection mode, the application can get a list of the names of primitives that made it all the way to rasterization. That is, those primitives that were not discarded during clipping. The Method Name each object in the scene. Assign a name to each primitive according to what object it is part of. Set the clipping region to be a very small rectangle at the mouse position. Set selection mode and draw the scene. As with the previous method, the user need not see the results. Get the list. It contains the names of objects that were clicked on. If there is more than one, take the front-most. OpenGL details in a forthcoming lecture … 29 Sep 2003 CS 381


Download ppt "Advanced Menuing, Introduction to Picking"

Similar presentations


Ads by Google