public void endPicking(final GL2 gl){
  int selectedIndex=PickingState.NONE;
  final int howManyObjects=gl.glRenderMode(GL2.GL_RENDER);
  gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
  gl.glPopMatrix();
  gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
  if (howManyObjects > 0) {
    selectedIndex=selectBuffer.get(3);
    int mindistance=Math.abs(selectBuffer.get(1));
    for (int i=0; i < howManyObjects; i++) {
      if (mindistance < Math.abs(selectBuffer.get(1 + i * 4))) {
        mindistance=Math.abs(selectBuffer.get(1 + i * 4));
        selectedIndex=selectBuffer.get(3 + i * 4);
      }
    }
  }
 else {
    selectedIndex=PickingState.WORLD;
  }
  pickingState.setPickedIndex(selectedIndex);
}

public void endPicking ( final GL2 gl ) { int selectedIndex = PickingState . NONE ; final int howManyObjects = gl . glRenderMode ( GL2 . GL_RENDER ) ; gl . glMatrixMode ( GLMatrixFunc . GL_PROJECTION ) ; gl . glPopMatrix ( ) ; gl . glMatrixMode ( GLMatrixFunc . GL_MODELVIEW ) ; if ( howManyObjects > 0 ) { selectedIndex = selectBuffer . get ( 3 ) ; int mindistance = Math . abs ( selectBuffer . get ( 1 ) ) ; for ( int i = 0 ; i < howManyObjects ; i ++ ) { if ( mindistance < Math . abs ( selectBuffer . get ( 1 + i * 4 ) ) ) { mindistance = Math . abs ( selectBuffer . get ( 1 + i * 4 ) ) ; selectedIndex = selectBuffer . get ( 3 + i * 4 ) ; } } } else { selectedIndex = PickingState . WORLD ; } pickingState . setPickedIndex ( selectedIndex ) ; }
after drawing we have to calculate which object was nearest screen and return its index
