Results 1 to 4 of 4

Thread: Picking, glRenderMode

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    4

    Picking, glRenderMode

    Hi,

    I'm using the code below to pick the object under a mouse click. If I use the same gluPerspective parameters that I set during initialization, I get no hits. If I vary them, I get some hits, but the min Z, max Z values for the buffer entries are 0 (or close to 0). On top of that, I've tried with a very simple case, displaying a cube, and clicking on the front face, so that I should get two hits (another one for the back face), and glRenderMode only returns a hit.

    Code :
    void selection(int x, int y)
    {
      GLuint buffer[512] = {0};
      GLint numberOfHits;
      GLint viewport[4];
     
      // Use buffer for selection
      glSelectBuffer(512, buffer);
      // Get viewport info
      glGetIntegerv(GL_VIEWPORT, viewport);
      // Switch to selection mode
      glRenderMode(GL_SELECT);
      // Clear the name's stack
      glInitNames();
      // Push one element onto the stack
      glPushName(NULL);
      glMatrixMode(GL_PROJECTION);
      glPushMatrix();
        glLoadIdentity();
        // Restrict the drawing to an area around the cursor
        gluPickMatrix(x, y, 1.0, 1.0, viewport);
        // zNear=1000, zFar=10000 during initialization
        //gluPerspective(fovy, ((GLdouble) windowWidth) / windowHeight, zNear, zFar);
        gluPerspective(fovy, ((GLdouble) windowWidth) / windowHeight, 0.001, 1000);
        // Draw the objects onto the screen
        glMatrixMode(GL_MODELVIEW);
        // Draw only the names in the stack, and fill the array
        glutSwapBuffers();
        display();
        glMatrixMode(GL_PROJECTION);
      glPopMatrix();
      glMatrixMode(GL_MODELVIEW);
      // Get the number of objects drawn in that area
      // Return to render mode
      numberOfHits = glRenderMode(GL_RENDER);
      list_hits(numberOfHits, buffer);
      if (numberOfHits > 0)
      {
        int objectMinZ = buffer[1];
        int objectName = buffer[3];
        for (unsigned int i = 1; i < numberOfHits; i++)
        {
          if (buffer[i*4+1] < GLuint(objectMinZ))
          {
            objectMinZ = buffer[i*4+1];
            objectName = buffer[i*4+3];
          }
        }
        Side::selectSide(objectName);
      }
    }

    Cheers,

  2. #2
    Senior Member Frequent Contributor scratt's Avatar
    Join Date
    May 2008
    Location
    Thailand
    Posts
    555

    Re: Picking, glRenderMode

    Personally I have had problems with some z values in gluUnProject.
    farz = 1.0 for example.

    For picking using gluUnProject twice and ray sphere intersection is much better IMO. This removes the need to run through rendering twice, or in a more complicated fashion, on the GPU.

    I have not looked at your code in detail btw.

    The one hit you are getting is (maybe) because you are looking at the depth buffer so you'll only get the closest hit. That is one drawback with ray / sphere, as you get multiple hits going through z, but that may be what you want then?

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    4

    Re: Picking, glRenderMode

    I think my problems come from (my not understanding correctly how to use) gluPerspective. If I don't use it at all within the code above, I get some hits. However:

    1) for an easy case, such as "I only see the front face", and I click it, I get two hits, the front and the back faces, but the min z corresponds to the back face.

    2) sometimes, they are not the hits I expect. For example, if I'm watching three sides of a cube, and I click on the left side, expecting to get left and bottom as hits, I get though left and back. It's as if the picking mechanism would be looking at the cube from a position where it could only see left and front faces.

    Does this ring a bell to anyone?

    Thanks,

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    4

    Re: Picking, glRenderMode

    OK, I got it. I was missing the call to gluLookAt after the call to gluPerspective. I had that in my graphics initialization function, and you need to replicate all the settings within the picking code.

Similar Threads

  1. glRenderMode(GL_RENDER) is slow with VBO.
    By vishalvaza in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 06-03-2014, 03:49 PM
  2. Geometry Selection using glRenderMode( GL_SELECT )
    By Rohit Pathak in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-12-2008, 01:41 PM
  3. glRenderMode(GL_SELECT) and Display Lists
    By devdept in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 08-08-2007, 05:23 PM
  4. Why does glRenderMode(GL_RENDER) fails sometimes?
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-17-2003, 07:50 AM
  5. glRenderMode(GL_SELECT) ... glEnable(GL_GO_FAST_PLEASE) ...
    By Andrew Jameson in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 07-08-2003, 01:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean