Selection


Selection applications allow the user to identify objects on the screen and then to move, modify, delete, or otherwise manipulate these objects. OpenGL provides a selection mechanism that tells you which objects are drawn inside a specified region of the window. To use the selection mechanism you create a stack that holds each object selected and process each selection in turn.

To begin the selection process, you must define an array to be used for the returned selection data. This is done by using the command: glSelectBuffer(), where the parameters are the size of the array and a pointer to an array where the data is put.

When you are ready to start the selection process, you must switch to selection mode by issuing the glRenderMode() command, specifying GL_SELECT. The application then remains in selection mode until glRenderMode() is called again with GL_RENDER mode. The command returns an integer value that is the number of hits accumulated when the application switches back to render mode.

Name Stacks

The name stack holds the information that is returned to the program after the user selects a series of objects. To create a name stack, first initialize it with glInitNames(), which simply clears the stack, and then adds integer names to it while issuing corresponding drawing commands. To add a name to the top of the stack, you use the glPushName() command and to remove a name from the top of the stack, you use the glPopName() command. To replace the name at the top of the stack with a different one, you use the glLoadName() command.

For example to draw a scene with three objects:

Calls to glPushName(), glPopName(), and glLoadName() are ignored if you're not in selection mode. So you might want to simplify your code by using these calls throughout your drawing code, and then use the same drawing code for both selection and normal rendering modes.

Picking

You can use the selection process to select specific objects for further manipulation. To do this, you use a special picking matrix in conjuction with the projection matrix to restrict drawing to a small region of the viewport, typically near the cursor. Then you allow for some form of input, such as clicking a mouse buttonm to initiate selection mode. With selection mode established and with the special picking matrix used, objects that are drawn near the cursor cause selection hits. Thus, during picking you're typically determining which objects are drawn near the cursor.

Before calling the standard projection matrix (such as glOrtho() or glFrustum()), we need to use the utility routine gluPickMatrix() to multiply the specified projection matrix by a special picking matrix. The center of the picking region is passed as x and y values (in window coordinates), typically the cursor location. A width and height are also specified which define the pickingn region in screen coordinates. You can think of the width and height values as the sensitivity of the picking device. You should also specify a viewport. Use glGetIntegerv(GL_VIEWPORT, viewport); to get the current values for the viewport array. For example, to create a 5x5 pixel picking region near the cursor location, use

You will probably want to save the contents of the current projection matrix before any manipulation, so the sequence of operations may look like this:

Hits

In selection mode, we can use the name stack and mouse interaction to keep track of which objects are selected and process them accordingly. When you enter selection mode, OpenGL initalizes a pointer to the beginning of the selection array. We can then use mouse input values, such as GL_DOWN and GL_LEFT_BUTTON, to determine if a hit should be recorded and update the hit record with each mouse input. Each time a hit record is written into the array, the pointer is updated accordingly. The hit records are accumulated in the array until glRendermode(GL_RENDER) is called. As noted above, the return value for this command is the number of hits in the array. This can then be passed to another procedure, along with the array itself to act on the selected pieces.


This program creates a crane-like object that has a ball swinging from the end of a rope and will take the users object selection via mouse interaction. The objects will each react differently. For example, the base will rotate around, the ball will swing back and forth, and the crane top will dip up and down.

	

	Source Code

Main Menu
Back to Top