opengl+delphi 鼠标拾取
ctedObject: cardinal;
i: integer;
begin
objectsFound := 0;// This will hold the amount of objects clicked
ZeroMemory(@viewportCoords, sizeof(viewportCoords));// We need an array to hold our view port coordinates
ZeroMemory(@selectBuffer, sizeof(selectBuffer));
// This will hold the ID's of the objects we click on.
// We make it an arbitrary number of 32 because openGL also stores other information
// that we don't care about. There is about 4 slots of info for every object ID taken up.
// glSelectBuffer is what we register our selection buffer with. The first parameter
// is the size of our array. The next parameter is the buffer to store the information found.
// More information on the information that will be stored in selectBuffer is further below.
glSelectBuffer(32, @selectBuffer);// Setup our selection buffer to accept object ID's
// This function returns information about many things in OpenGL. We pass in GL_VIEWPORT
// to get the view port coordinates. It saves it like a RECT with {top, left, bottom, right}
glGetIntegerv(GL_VIEWPORT, @viewportCoords);// Get the current view port coordinates
// Now we want to get out of our GL_MODELVIEW matrix and start effecting our
// GL_PROJECTION matrix. This allows us to check our X and Y coords against 3D space.
glMatrixMode(GL_PROJECTION);// We want to now effect our projection matrix
glPushMatrix();// We push on a new matrix so we don't effect our 3D projection
// This makes it so it doesn't change the frame buffer if we render into it, instead,
// a record of the names of primitives that would have been drawn if the render mode was
// GL_RENDER are now stored in the selection array (selectBuffer).
glRenderMode(GL_SELECT);// Allows us to render the objects, but not change the frame buffer
glLoadIdentity();// Reset our projection matrix
// gluPickMatrix allows us to create a projection matrix that is around our
// cursor. This basically only allows rendering in the region that we specify.
// If an object is rendered into that region, then it saves that objects ID for us (The magic).
// The first 2 parameters are the X and Y position to start from, then the next 2
// are the width and height of the region from the starting point. The last parameter is
// of course our view port coordinates. You will notice we subtract "y" from the
// BOTTOM view port coordinate. We do this to flip the Y coordinates around. The 0 y
// coordinate starts from the bottom, which is opposite to window's coordinates.
// We also give a 2 by 2 region to look for an object in. This can be changed to preference.
//-Integer(not g_Fullscreen)*(GetSystemMetrics(SM_CYCAPTION)+ GetSystemMetrics(SM_C
YSIZEFRAME) shl 1)
gluPickMatrix(x, viewportCoords[3]-y-Integer(not g_Fullscreen)*(GetSystemMetrics(SM_CYCAPTION)+ GetSystemMetrics(SM_CYSIZEFR