PDA

View Full Version : The selection buffer????



SAmhigh
01-05-2003, 03:46 AM
Hi, Everyone:

I use MFC6.0 and OpenGL to write a Win32 application
under Window XP (with SP1). For the pick/select problem,
I have the following code:

bool CView::PickObject(CPoint point)
{
const int BUFSIZE = 100;
GLuint selectBuf[BUFSIZE];
GLint hits;
int ID;
CString cts;

glSelectBuffer(BUFSIZE,selectBuf);

(void) glRenderMode(GL_SELECT);

glInitNames();
glPushName(0);

glGetIntegerv(GL_VIEWPORT,viewport);

glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
gluPickMatrix((GLdouble)point.x,
(GLdouble)(viewport[3]-point.y),
5.0,5.0,viewport);

glOrtho(Winleft, Winright, Winbottom, Wintop,Winnear, Winfar);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
SetModelView();

for (i=0;i<total_mesh;i++)
{
ID = get_mesh_id(i);
glPushName(ID);
Draw_each_mesh(i);
glPopName();
}

glPopMatrix(); // For ModelView Matrix

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glMatrixMode(GL_MODELVIEW);

glFlush();
hits = glRenderMode(GL_RENDER);

if (hits > 0) //something is picked
{
cts.Format("hits=%d b0=%d b1=%d b2=%d b3=%d b4=%d b5=%d b6=%d b7=%d",hits,
selectBuf[0],selectBuf[1],selectBuf[2],selectBuf[3],selectBuf[4],selectBuf[5],selectBuf[6],selectBuf[7]);
AfxMessageBox(cts);
return true;
}
return false;
}

I test the code, it seems work fine. Yet, I am confusing with the explanation of the content of the selectBuf
from the OpenGL FAQ 20.020. It says
---Begin quote ----
"Each hit record contains the following information stored as unsigned ints:
.Number of names in the name stack for this hit record
.Minimum depth value of primitives (range 0 to 2 power 32 -1)
.Maximum depth value of primitives (range 0 to 2 power 32 -1)
.Name stack contents (one name for each unsigned int)
---End quote ---

Exactly what does it try to explain? Can anyone explain it with more easy understanding way?
I print out the hits and selectBuf as follows:

hits=1 b0=2 b1=-2143505408 b2=-2143500032 b3=0 b4=1 b5=0 b6=0 b7=0
hits=1 b0=2 b1=-2144751104 b2=-2144735488 b3=0 b4=2 b5=0 b6=0 b7=0
hits=1 b0=2 b1=-2146665728 b2=-2146595840 b3=0 b4=4 b5=0 b6=0 b7=0
hits=3 b0=2 b1=2142254848 b2=-2142262784 b3=0 b4=1 b5=2 b6=2144132864 b7=2145358080

Will anyone tell me why the selectBuf[0] is always 2? After the pick,
how will I know which entities are picked? (I can tell selectBuf[4] seems
store the ID, but when the hits are 3, which are the three entities picked?

Thanks for help.

pbrown
01-06-2003, 08:40 PM
selectBuf[0] is always 2 because the name stack is 2 deep when you send your rendering commands. You call PushName(0) after InitNames(), which makes your stack 1-deep. The subsequent PushName(ID) before Draw_each_mesh(i) will make the stack 2-deep, containing (0,i). This is what you're seeing in the trace below. b3 will have the first stack entry, b4 will have the second.

Each select hit copied the entire name stack into the select buffer -- the first entry tells you how many stack entries are copied so you can parse the buffer.

The name stack is hierarchical to match a hierarchical object model -- you may have multiple instances of an object.

Hope this helps,
Pat

SAmhigh
01-07-2003, 01:56 PM
Thanks for your help. I appreciate.