opengl+delphi 鼠标拾取(4)

2021-04-06 05:22

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


opengl+delphi 鼠标拾取(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:神奇的滚筒

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: