PDA

View Full Version : picking woes...



wenjamin
11-29-2002, 10:44 PM
Hi all,

i'm fairly new to opengl and this is my first time using the selection features so i'm kinda confused and please bear with me... i followed the example from the big red book but nothing seems to be happening in my program...

here's what i'm trying to do...

i have a 3d object (triangle mesh) and when i click on this object i need to find the closest point on its surface to the mouse position. btw i'm using fltk as the GUI constructor, if relevant...

so here's what i'm thinking of doing:

for each triangle in the mesh, assign a name - so in my draw function, i did a glLoadName(triangle_num) for each triangle drawn (before glBegin())... i didn't check if i'm in selection mode since i read somewhere that if mode != GL_SELECT the call to glLoadName will simply be ignored - not sure if this is true...

then, in my mouse handle function i did the following:

//if the right mouse button is clicked...
int viewport[4];
GLint hits;
GLuint selectBuf[BUFSIZE];

glGetIntegerv( GL_VIEWPORT, viewport );
glSelectBuffer(BUFSIZE, selectBuf);
glRenderMode(GL_SELECT);

glInitNames();
glPushName(0);

glMatrixMode(GL_PROJECTION);
glPushMatrix();

glLoadIdentity();
gluPickMatrix((GLdouble) m_lastMouseX, (GLdouble) (h() - m_lastMouseY),
1.0, 1.0, viewport);
gluOrtho2D(.0, w(), .0, h());
redraw();

glPopMatrix();
glFlush();

hits = glRenderMode(GL_RENDER);
cerr << "hits = " << hits << endl;

i figure that if i can first identify the triangle i clicked on then it should be possible to determine the actual position inside of this triangle. however, it seems that i'm never getting any hits so something must be wrong. someone suggests to comment out the line glRenderMode(GL_SELECT), as it should cause the display of only objects inside the area specified by glPickMatrix, but again it is not happening in my program.

any ideas on what could be wrong? any help would be very very appreciated.

[This message has been edited by wenjamin (edited 11-29-2002).]

wenjamin
11-30-2002, 09:24 PM
nobody cares about me...

anyway, i made some changes and now there's some progress. except when i pick, the number of hits = the number of pickable polygons in viewport. it doesn't matter where i click, the number of hits is always equal to the number of polygons that made up my object, but as soon as i move my object so it's partially visible then the number of hits changes (decreases, as one would expect). can anyone please take a look and see if there is a problem with the following?

//if the desired mouse button is double clicked...

int viewport[4];
GLint hits;
GLuint selectBuf[BUFSIZE];

glGetIntegerv( GL_VIEWPORT, viewport );
glSelectBuffer(BUFSIZE, selectBuf);

glRenderMode(GL_SELECT);

glInitNames();
glPushName(0);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

gluPickMatrix((GLdouble) m_lastMouseX, (GLdouble) (viewport[3] - m_lastMouseY),
1.0, 1.0, viewport);

gluOrtho2D(.0, w(), .0, h());
glMatrixMode(GL_MODELVIEW);

drawSelection();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();

hits = glRenderMode(GL_RENDER);

cerr << "hits = " << hits << endl;

any suggestions are appreciated... thanks.


[This message has been edited by wenjamin (edited 11-30-2002).]

Bill123
12-02-2002, 08:37 AM
This can be involved ... your drawSelection() routine should be your main draw routine, with an 'if' block (or something) to enable the picking operation, like this ....

for(i=0; i < num; i++) // draw num objects
{


if(doing_picking)
{
glPopName();
glPushName((GLuint)i);
}


// gl rendering calls here

}


hopefully, the will result in a correct hit count.

zed
12-02-2002, 06:12 PM
also check glut there are selectioon examples there (see where it differe from youres)
btw try making the pick matrix larger than 1,1 say 2,2

wenjamin
12-02-2002, 06:17 PM
thanks all for the reply. i finally fixed my problems... there's one more thing i need to do that maybe you guys can help:

is there a way to figure out the exact point on the surface of polygon that i picked using this selection buffer method? i need to know where exactly (x, y, z) that my mouseclick intersects the object.