opengl+delphi 鼠标拾取
AME) shl 1), 2, 2, @viewportCoords);
// Next, we just call our normal gluPerspective() function, exactly as we did on startup.
// This is to multiply the perspective matrix by the pick matrix we created up above.
gluPerspective(45.0, viewportCoords[2]/viewportCoords[3], 0.5, 150.0);
glMatrixMode(GL_MODELVIEW);// Go back into our model view matrix
glDraw();// Now we render into our selective mode to pinpoint clicked objects
// If we return to our normal render mode from select mode, glRenderMode returns
// the number of objects that were found in our specified region (specified in gluPickMatrix())
objectsFound := glRenderMode(GL_RENDER);// Return to render mode and get the number of objects found
glMatrixMode(GL_PROJECTION);// Put our projection matrix back to normal.
glPopMatrix();// Stop effecting our projection matrix
glMatrixMode(GL_MODELVIEW);// Go back to our normal model view matrix
// PHEW! That was some stuff confusing stuff. Now we are out of the clear and should have
// an ID of the object we clicked on. objectsFound should be at least 1 if we found an object.
if (objectsFound > 0) then
begin
// If we found more than one object, we need to check the depth values
// of all the objects found. The object with the LEAST depth value is
// the closest object that we clicked on. Depending on what you are doing,
// you might want ALL the objects that you clicked on (if some objects were
// behind the closest one), but for this tutorial we just care about the one
// in front. So, how do we get the depth value? Well, The selectionBuffer
// holds it. For every object there is 4 values. The first value is
// "the number of names in the name stack at the time of the event, followed
// by the minimum and maximum depth values of all vertices that hit since the
// previous event, then followed by the name stack contents, bottom name first." - MSDN
// The only ones we care about are the minimum depth value (the second value) and
// the object ID that was passed into glLoadName() (This is the fourth value).
// So, [0 - 3] is the first object's data, [4 - 7] is the second object's data, etc...
// Be carefull though, because if you are displaying 2D text in front, it will
// always find that as the lowest object. So make sure you disable text when
// rendering the screen for the object test. I use a flag for RenderScene().
// So, lets get the object with the lowest depth!
// Set the lowest depth to the first object to start it off.
// 1 is the first object's minimum Z value.
// We use an unsigned int so we don't get a warning with selectBuffer below.
lowestDepth := selectBuffer[1];
// Set the selected object to the first object
to start it off.
// 3 is the first object's object ID we passed into glLoadName().
selectedObject := selectBuffer[3];
// Go through all of