Results 1 to 7 of 7

Thread: Mouse picking (GL_SELECT) not registering

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    5

    Mouse picking (GL_SELECT) not registering

    I'm creating a simple (for now) model viewer applet in Java using JOGL.
    What I would like to get working first is an universe with several simple objects (cubes etc), be able to rotate/zoom/pan on the universe and be able to select single objects.

    I've got the simple universe setup, only mouseclicks are not hitting any objects. Any pointers what I'm doing wrong?
    These are relevant (I think) snippets for my applet.

    Code :
    public void display(GLAutoDrawable drawable) {
      // ..
      drawCubes(GL.GL_RENDER);
      gl.glFlush();  // manual using GL_QUADS
    }
    Code :
    // MouseMotionListener interface implemented 
    public void mouseDragged(MouseEvent e) {
      // angle (float) and axis (int[3]) are calculated
      gl.glRotatef(angle, axis[0], axis[1], axis[2]);
    Code :
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      // Orthogonal projection, translated for better fitting onscreen
      gl.glOrtho(left, right, bottom, top, 0.01f, 1000.0f);
      gl.glTranslatef(0.0f, 0.0f, -500.0f);
    }

    And now the not-working part:
    Code :
    // Called on each mouseClicked() event.
    private void pickRects(GL gl) {
      final int BUFSIZE = 512;
     
      int[] selectBuf = new int[BUFSIZE];
      IntBuffer selectBuffer = BufferUtil.newIntBuffer(BUFSIZE);
      int hits;
      int viewport[] = new int[4];
     
      gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
      gl.glSelectBuffer(BUFSIZE, selectBuffer);
      gl.glRenderMode(GL.GL_SELECT);
     
      gl.glInitNames();
      gl.glPushName(-1);
     
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glPushMatrix();
      gl.glLoadIdentity();
     
      // pickPoint.xy (ints) is set correctly on each mouseclick
      glu.gluPickMatrix((double) pickPoint.x,
        (double) (viewport[3] - pickPoint.y),
        5.0, 5.0, viewport, 0);
     
      drawCubes(GL.GL_SELECT);  // now glLoadName(x) is used
      gl.glPopMatrix();
      gl.glFlush();
     
      hits = gl.glRenderMode(GL.GL_RENDER);  // hits is always 0 in my testcase, why?
      selectBuffer.get(selectBuf);
     
      proces****s(hits, selectBuf);
    }

    Any ideas what's going wrong?
    The mouseClicked event is registering fine, the x,y mouse coordinates are correct too.
    It's just that I get no hits at all in GL_SELECT mode.

  2. #2
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    3

    Re: Mouse picking (GL_SELECT) not registering

    Try changing the 'name' -1 to a positive value. glPushName excepts a GLuint - eg unsigned. Also should take into account any rotations or translations of the modelview or projection view when you do your picking. Lastly, not sure if its necessary, but you should call glPopName

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    5

    Re: Mouse picking (GL_SELECT) not registering

    Okay, I've got it fixed.
    It was a (minor) problem with my projection matrix in SELECT mode.

    I can parse the hit results alright, getting stackdepth + z1 + z2 + names alright.

    I now have a different problem though, perhaps you can help me with that too:

    When there are multiple objects in the hit results (in front of eachother during the click), I can sort those using the minimum (z1) and maximum (z2) depth values returned.
    From the front (default projection) these depth values are correct. However, when I rotate behind the object (180 degree turn) and click again, the same hit results are returned (same names, different depth values ofc). But now when I do a depthsort, the object order is backwards.

    I'm using a quaternion based camera, calculating a quaternion and using that to translate.

    Any ideas why the depth order is wrong?

  4. #4
    Junior Member Newbie
    Join Date
    Aug 2010
    Posts
    11

    Re: Mouse picking (GL_SELECT) not registering

    Remember, GL_SELECT is not hardware accelerated, only in workstation graphics cards like nVidia QUADRO.

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    5

    Re: Mouse picking (GL_SELECT) not registering

    But GL_SELECT won't use many resource if my picking region is small, right?

    What would be a better way then? All my entities are fully shaded
    so using a unique color for each entity is not really an option.

  6. #6
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: Mouse picking (GL_SELECT) not registering

    But GL_SELECT won't use many resource if my picking region is small, right?
    Small region will not reduce the resources needed to transform all your vertices with the CPU.

    All my entities are fully shaded
    so using a unique color for each entity is not really an option.
    You are wrong ! Color picking is done in a separate pass, no shading, no texturing, only solid colors.

  7. #7
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    5

    Re: Mouse picking (GL_SELECT) not registering

    Oh ok, I'll definitely look into the color picking then.

Similar Threads

  1. mouse picking
    By Richard145 in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 01-03-2007, 09:13 AM
  2. Which is the best way of doing mouse picking?
    By doodlemunch in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 06-22-2005, 12:34 PM
  3. Mouse Picking
    By asaslow in forum OpenGL ES
    Replies: 1
    Last Post: 04-07-2005, 07:16 PM
  4. Picking with GL_SELECT
    By in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 07-12-2002, 07:04 AM
  5. picking without mouse coordinates
    By grodslukaren in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-12-2001, 12:37 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