很好的一篇文章,看完后有豁然开朗的感觉。
The OpenGL API provides a mechanism for picking objects in a 3D scene. This tutorial will show you how to detect which objects are bellow the mouse or in a square region of the OpenGL window. The steps involved in detecting which objects are at the location where the mouse was clicked are:
Using the OpenGL terminology, a hit occurs whenever a primitive is rendered in selection mode. Hit records are stored in the selection buffer. Upon exiting the selection mode OpenGL returns the selection buffer with the set of hit records. Since OpenGL provides depth information for each hit the application can then easily detect which object is closer to the user.
Introducing the Name Stack
As the title su ggests, the names you assign to objects are st or ed in a stack. Actually you don't give names to objects, as in a text string. Instead you number objects. Nevertheless, since in OpenGL the term name is used, the tutorial will also use the term name instead of number.
When an object is rendered, if it intersects the new viewing volume, a hit record is created. The hit record contains the names currently on the name stack plus the minimum and maximum depth for the object. Note that a hit record is created even if the name stack is empty, in which case it only contains depth information. If more objects are rendered before the name stack is altered or the application leaves the selection mode, then the depth values stored on the hit record are altered accor d ingly .
A hit record is stored on the selection buffer only when the current contents of the name stack are altered or when the application leaves the selectio n mode.
The rendering function for the selection mode therefore is responsible for the contents of the name stack as well as the rendering of primitives.
OpenGL provides the following functions to manipulate the Name Stack:
glPopName();
glPushName(name);
Note: Calls to the above functions are ignored when not in selection mode. This means that you may have a single rendering function with all the name stack functions inside it. When in the normal rendering mode the functions are ignored and when in selection mode the hit records will be collected.
Note: You can't place these functions inside a glBegin glEnd construction, which is kind of annoying since that will require a new rendering function for the selection mode in some cases. For instance if you have a set of points inside a glBegin glEnd and you want to name them in such a way that you can tell them apart, then you must create one glBegin glEnd block for each name.
Rendering for the Selection Mode
An example of a rendering function is now presented.
OK, lets go line by line.
1 glInitNames(); - This function creates an empty stack. This is required before any other operation on the stack such as Load, Push or Pop.
Note: lines 4 and 5 could have been replaced by glLoadName(HEAD);
Note that you can push a dummy name (some unused value) right at the start, and afterwards just use glLoadName, instead of Pushing and Popping names. However it is faster to disregard objects that don't have a name than to check if the name is the dummy name. On the other hand it is probably faster to call glLoadName than it is to Pop followed by Push.
Using multiple names for an object
There is no rule that says that an object must have a single name. You can give multiple names to an object. Suppose you have a number of snowmen disposed on a grid. Instead of naming them as 1,2,3,... you could name each of them with the row and column where they are placed. In this case each snowman will have two names describing its position: the row and column of the grid.
The following two functions show the two different approaches. First using a single name for each snowman.
The following function gives two names to each snowman. In this latter function if a hit occurs the hit record will have two names.
Hierarchical Naming
This is a natural extension to multiple naming. It may be of interest to find out where in a particular object you have clicked. For instance you may want to know not only in which snowman you clicked but also if you clicked on the head or the body. The following function provides this information.
In this case you'll have three names when you click on either the body or head of a snowman: the row, the column and the value BODY or HEAD respectively.
A Note about Rendering on Selection Mode
As mentioned before, all calls to stack functions are ignored when not in selection mode, i.e. when rendering to the frame buffer. This means that you can have a single rendering function for both modes. However this can result in a serious waste of time. An application may have only a small number of pickable objects. Using the same function for both modes will require to render a lot of non-pickable objects when in selection mode. Not only the rendering time will be longer than required as will the time required to process the hit records.
Therefore it makes sense to consider writing a special rendering function for the selection mode in these cases. However you should take extra care when doing so. Consider for instance an application where you have several rooms, each room with a potential set of pickable objects. You must prevent the user from selecting objects through walls, i.e. objects that are in other rooms and are not visible. If you use the same rendering function then the walls will cause a hit, and therefore you can use depth information to disregard objects that are behind the wall. However if you decide to build a function just with the pickable objects then there is no way to tell if an object is in the current room, or behind a wall.
Therefore, when building a special rendering function for the selection mode you should include not only the pickable objects, but also all the objects that can cause occlusions, such as walls. For highly interactive real time applications it is probably a good option to build simplified representations of the objects that may cause occlusions. For instance a single polygon may replace a complex wall, or a box may replace a table.
【转】http://www.lighthouse3d.com/opengl/picking/
有一点,需要注意,函数glLoadName()不可置于glBegin()与glEnd()之间。下面的代码就是犯了这种错误:
glBegin( GL_TRIANGLES );
for( i = 0; i < NTRIS; i++ )
{
glLoadName( i );
glVertex3f( Tris[i].x0, Tris[i],y0, Tris[i].z0 );
glVertex3f( Tris[i].x1, Tris[i],y1, Tris[i].z1 );
glVertex3f( Tris[i].x2, Tris[i],y2, Tris[i].z2 );
}
glEnd();
应改作:
for( i = 0; i < NTRIS; i++ )
{
glLoadName( i );
glBegin( GL_TRIANGLES );
glVertex3f( Tris[i].x0, Tris[i],y0, Tris[i].z0 );
glVertex3f( Tris[i].x1, Tris[i],y1, Tris[i].z1 );
glVertex3f( Tris[i].x2, Tris[i],y2, Tris[i].z2 );
glEnd();
}