Charles,
There is also an alternative (unofficial) picking technique that might work considerably faster than the official
glLoadName() method.
The official method implies looping through all the objects in the scene to find out the one whose coordinates are the closest to the viewer's cursor in the given state of the frame's z-buffer. The looping is relatively slow, and especially so for heavily populated scenes with a few hundred thousand polies and more in view.
The unofficial method also presupposes an extra render invisible to the viewer because it is executed without the usual
SwapBuffers() call (the hungriest FPS eater) at the end of frame render. The scene is rendered untextured and unlit, which makes the render yet faster. Instead, the objects are rendered simply in shades of red, green, or blue where each shade corresponds directly to the respective object's index in the array of scene objects.
For simplicity, let the number of objects in the scene be not more than 256. Then object 0 could be rendered in integer color RGB(0,0,0), object 1, in color RGB(0,0,1) (blue), object 2, in color RGB(0,0,2) (lighter blue), etc. etc. etc. All we have to do now is just poll the pixel color under the cursor's hotspot with
glReadPixels() during this invisible render -- et voila the desired index of the object the cursor is pointing to, without unnecessary looping!
Below please find a screenshot of XANEngine's pick render forced on-screen for demonstration purposes. It uses shades of blue but in a 32-bit float color space because there may be up to 1K objects in the current field of view with some 200K+ visible (unculled) polies, some of which may also be alpha transparent.
glLoadName() would've been unbearably slow in this environment.
The second screenshot is a practical use case of this unofficial color-coded method in the XANEngine Editor where it powers, amongst other things, the tooltip used to optionally display some data about the object that the cursor is currently pointing at within the famous 3D model of Sponza Palace in Aurel's beloved Croatia.
.