Pages: [1]
Author Topic: Getting no hits with OpenGL picking  (Read 869 times)
adkeswani

Posts: 4

Gravatar


Getting no hits with OpenGL picking
« on: December 30, 2011, 07:37:18 AM »

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/index.php?topic=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:

Code:
#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
Logged

--

Aditya Keswani
joshua noble
Global Moderator
Cascadia

Posts: 1252

Gravatar

Let's make better mistakes tomorrow.


WWW
Re: Getting no hits with OpenGL picking
« Reply #1 on: December 30, 2011, 09:08:39 PM »


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.

Code:
gluPickMatrix(x, ofGetHeight() - y, 2.0, 2.0, glViewport);  
        //glMultMatrixf(projMatrix);
   
        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);

       
        glMatrixMode(GL_MODELVIEW);

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

arturo
Administrator
barcelona

Posts: 2395

Gravatar


WWW
Re: Getting no hits with OpenGL picking
« Reply #2 on: December 30, 2011, 09:15:55 PM »

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

Posts: 4

Gravatar


Re: Getting no hits with OpenGL picking
« Reply #3 on: December 31, 2011, 03:27:44 AM »

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()?
Logged

--

Aditya Keswani
arturo
Administrator
barcelona

Posts: 2395

Gravatar


WWW
Re: Getting no hits with OpenGL picking
« Reply #4 on: December 31, 2011, 04:33:38 AM »

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
Logged
joshua noble
Global Moderator
Cascadia

Posts: 1252

Gravatar

Let's make better mistakes tomorrow.


WWW
Re: Getting no hits with OpenGL picking
« Reply #5 on: December 31, 2011, 05:04:15 AM »

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

adkeswani

Posts: 4

Gravatar


Re: Getting no hits with OpenGL picking
« Reply #6 on: January 04, 2012, 03:42:19 AM »

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

--

Aditya Keswani
Pages: [1]
 
Jump to:  

Powered by SMF 1.1.15 | SMF © 2011, Simple Machines

viagra priser