Problem with Selection/Picking

I have been unable to get Selection to return a non-zero number of hits. Here is the way the code is laid out.

  1. I draw the scene as normal (no selection code)
  2. I have a mouse listener that listens for a mouseClicked
  3. On receiving the mouse click, I set a flag (actually the point clicked) and call repaint on my GLJPanel.
  4. In the GLPanel’s display() method I check the selection flag. If it is set I call glSelectBuffer, glRenderMode, etc.
  5. At the end of the display() method, I call glRenderMode( GL.GL_RENDER ) and print out the returned numHits. This is always 0.

Here is the code snippet within the display() method:

        // Draw DSO's
        IntBuffer selectBuffer = null;
        try
        {
            if ( clickedPt != null )
            {
                selectBuffer = BufferUtil.newIntBuffer( objects.size( ) * 8 );
                gl.glRenderMode( GL.GL_SELECT );
                gl.glSelectBuffer( 0, selectBuffer );
                gl.glInitNames( );
                gl.glPushName( 0 );
            }

            // This method calls   gl.glLoadName( i ); for each object drawn
            drawDSOs( gl );
        }
        finally
        {
            if ( selectBuffer != null )
            {
                int numHits = gl.glRenderMode( GL.GL_RENDER );
                processHits( numHits, selectBuffer );
            }
            clickedPt = null;
        }

My numHits is always zero. Can anyone see anything wrong with this code? Am I going about this in the right way?

This is on Mac OS X 10.4.7 by the way…

I only just got this working myself about an hour ago. Here is my version of the select code:



			IntBuffer selectBuffer=BufferUtil.newIntBuffer(objects.length*4);
			gl.glSelectBuffer(selectBuffer.capacity(),selectBuffer);
			gl.glRenderMode(GL.GL_SELECT);
	
			gl.glMatrixMode(GL.GL_PROJECTION);
			gl.glPushMatrix();
	
			gl.glLoadIdentity();
			IntBuffer viewport=BufferUtil.newIntBuffer(4);
			gl.glGetIntegerv(GL.GL_VIEWPORT,viewport);
			glu.gluPickMatrix(mouseX,viewport.get(3)-mouseY,2,2,viewport);
			glu.gluPerspective(45,((double)viewport.get(2))/((double)viewport.get(3)),.1,1000);
	
			gl.glInitNames();
			gl.glPushName(0);
	
			gl.glMatrixMode(GL.GL_MODELVIEW);
			gl.glLoadIdentity();
			positionCamera(gl);
			drawTargets(gl);
	
			gl.glMatrixMode(GL.GL_PROJECTION);
			gl.glPopMatrix();
			gl.glMatrixMode(GL.GL_MODELVIEW);

			

That basically seems like what I’m doing although currently I’m not setting a gluPickMatrix(). I’m just hoping to see a count of all objects drawn (instead of the zero I get). Because of the way my code is structured, I do call the glRenderMode() after setting up the projection matrix. Is there some reason that’s not valid?

Well one problem (I think) is that you tell OpenGL that the size of your selection buffer is zero

gl.glSelectBuffer( 0, selectBuffer );

try this instead:

gl.glSelectBuffer(selectBuffer.capacity(),selectBuffer);

You’re absolutely right!! I had misinterpreted that argument and somehow assumed it was the offset into the buffer to start at. Thanks so much for catching that. I’m now getting back the correct number of hits.