PDA

View Full Version : Picking In OpenGL



twoski
02-08-2014, 09:42 AM
I have followed several tuts about picking and i have gotten the basics figured out, however i have come across a strange issue... My picking seems to stop working randomly. I'll click directly on a vertex i want to select and it will come back with 0 hits.

I tried upping my picking tolerance to 30 which seemed rather high and it still has issues.

Here is the relevant snippet, if anyone could point out any obvious bugs i'd appreciate it!

pastebin.com/7tsuiBY0

Aleksandar
02-08-2014, 05:00 PM
You probably didn't specify a select buffer. Where did you say that your PickBuffer shoud be the actual GL SelectBuffe?

twoski
02-08-2014, 05:50 PM
Oh sorry, that is done in the initialize function.

Picking does work, just not all the time. Sometimes i click a vertex and it puts -1 and the number of the vertex in the buffer, other times i click a vertex dead on and it doesn't detect any hits. :/

Aleksandar
02-09-2014, 04:09 AM
Then the problem is somewhere else. Ensure that you are drawing the same scene in both GL_RENDER and GL_SELECT modes (it seems it is not the case). Also ensure GL context is active in the tread you where you call pick() function.

I suggest to use the following pattern:



PickObject(...,int x, int y)
{
GLint viewport[4];
GLuint selectBuf[512];

glSelectBuffer(512,selectBuf);
glRenderMode(GL_SELECT);

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

glGetIntegerv(GL_VIEWPORT, viewport);
gluPickMatrix(x, viewport[3]-y, 3, 3, viewport); // 3 pix is enough
gluPerspective(...); // the same as in reshape function or wherever set up

glMatrixMode(GL_MODELVIEW);
glInitNames();
glPushName(0); // useful only if you are using glLoadName()

Draw(GL_SELECT);

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

int hits = glRenderMode(GL_RENDER);
unsigned int retVal = 0;
if(hits != 0)
retVal = ProcessHits(hits,selectBuf);

return retVal;
}


For all details look at Legacy OpenGL Programming Guide Chapter 13 - Selection and Feedback (http://glprogramming.com/red/chapter13.html).