PDA

View Full Version : Anyone here know the pick/selection in OpenGL??



SAmhigh
01-06-2003, 04:36 AM
Hello Everyone:

First of all, I feel guilty to post this question again here -- I have post the question in the advance group, but got no reply. If this duplication offends you, I apologize. If you know anything, please do help me. My question is --

--------------------------------------
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.

Turbo_Pascal
01-07-2003, 08:31 AM
Hello,

Yes, picking method is a litle hard to make it works the first time.

You have posted just 2th part code for the picking method, the code that need to be executed when the user do click somewhere,

But inside the rutine (void) glRenderMode(GL_SELECT);

each "selectable" object to be render need to be marked with the command glloadname()

example:

glloadname(1); //mark with value 1
draw_objectA; //draw one object

glloadname(2); //mark with value 2
draw_objectB; //draw one object

glloadname(3); //mark with value 3
draw_objectC; //draw one object
.
.
.
more objetcs if you want

depending objects position and current camera position and orientation, when the user do click in the screen the mouse pointer could be over just 1, 2, 3 or all objects, if it was over just one object then only info for one object will be returned in the bufferselect, along with some other info you will found value "1" or "2" or "3" etc, if value "1" is found mean the user do click in objectA, i value "2" is found then ObjectB was clicked and so on.

If the mouse pointer was over all 3 objects then the bufferselect will return info for 3 objects, along with the mark value info for each object you will also get the minimun and maximum "Z" coordinate value used when the objects is draw, so you have to choose the most front object (so the picked) inspecting all minZ (or maxZ, depending) in the bufferselect.

good luck,

Turbo Pascal

SAmhigh
01-07-2003, 02:54 PM
Thanks for your reply. I appreciate.