Getting no hits with OpenGL picking


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 and 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);  
    //Set up the region where we're going to look for hits  
    gluPickMatrix(x, ofGetHeight() - y, SELTOL, SELTOL, glViewport);  
    //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  
    //Get hits, return to render mode  
    hits = glRenderMode(GL_RENDER);  
    printf("Got %d hits\n", hits);  

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.



I had to set up the projection myself and not just use the projMatrix. Not exactly sure why that is, but it seems like what gluProject isn’t quite the same as just grabbing the resulting matrix and then multiplying it.

gluPickMatrix(x, ofGetHeight() - y, 2.0, 2.0, glViewport);    
        float width = ofGetWidth();  
        float height = ofGetHeight();  
        float viewW = ofGetViewportWidth();  
        float viewH = ofGetViewportHeight();  
        float fov = 45;  
        float eyeX = viewW / 2;  
        float eyeY = viewH / 2;  
        float halfFov = PI * fov / 360;  
        float theTan = tanf(halfFov);  
        float dist = eyeY / theTan;  
        float aspect = (float) viewW / viewH;  
        float nearDist = dist / 10.0f;  
        float farDist = dist * 10.0f;  
        gluPerspective(fov, aspect, nearDist, farDist);  

I’m not sure if working with ofEasyCam would mean that you need to grab it’s FOV and viewport info though as I’m still not super familiar with it.

also probably you need to store the mouse position in a variable and move the code you have in mousePressed() to draw()

Thanks Joshua, I’ll give that a go.

arturo, I’m not sure if that would help. I’m still drawing everything inside mousePressed(). Unless there’s some restriction on drawing from inside mousePressed()?

yes, usually you can’t draw inside mousePressed, i don’t know if drawing to the select buffer works in the mouse callbacks but it can be the problem

It seems to be working for me drawing to GL_SELECT within mousePesssed but its definitely better to do any drawing inside of draw().

Thanks, I’ll give both suggestions a go when I have a chance to work on it.