Hi,
I’m a beginner with openFrameworks and I’m working on a small project that needs to register clicks on one of a number of spheres in 3D space. I looked around and I think that “picking” is what I’m after.
I’ve read more about it at http://glprogramming.com/red/chapter13.html and http://forum.openframeworks.cc/t/problem-with-picking-objects-in-opengl/2143/0 and I’ve tried to implement it, but I can’t seem to get any hits.
Here is the code in my mousePressed:
#define BUFSIZE 512
#define SELTOL 5.0
void testApp::mousePressed(int x, int y, int button){
GLuint selectBuf[BUFSIZE] = {0};
GLint hits;
GLint glViewport[4];
GLdouble projMatrix[16];
glGetIntegerv(GL_VIEWPORT, glViewport);
glGetDoublev(GL_PROJECTION, projMatrix);
glSelectBuffer(BUFSIZE, selectBuf);
glRenderMode(GL_SELECT);
glInitNames();
//Set up the region where we're going to look for hits
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix(x, ofGetHeight() - y, SELTOL, SELTOL, glViewport);
glMultMatrixd(projMatrix);
glMatrixMode(GL_MODELVIEW);
//Redraw everything while in selection mode
for (std::vector<int>::size_type i = 0; i != spheres.size(); i++) {
//Add name by calling glLoadName()
//Note: When you do a Load or Push, anything drawn until the next Load or Pop will be added to that name, thus allowing you to create hierarchies
glPushName(i);
spheres[i].draw();
glPopName();
}
//Get hits, return to render mode
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glFlush();
hits = glRenderMode(GL_RENDER);
printf("Got %d hits\n", hits);
glMatrixMode(GL_MODELVIEW);
}
Everything compiles fine, but I always get 0 hits.
There are a few areas I might have done something wrong:
-
Pushing names onto the stack. I tried using LoadName before and then tried Push/Pop to see if that was the issue
-
Multiplying the pick matrix by the projection matrix, is it correct to do this?
-
Could using EasyCam have anything to do with it? I tried a suggestion to apply the pick matrix but not turn GL_SELECT mode on so as to see what was in the pick region. However I didn’t see any difference to what was there before, so I thought maybe EasyCam might be interfering in some way?
Any help would be appreciated, and if it helps, I’m using version 007.
Cheers,
Aditya