   Object Selection using Java and OpenGL
  I have been using the Java-OpenGL combination for about 6 months and I have reached as far as the NeHe tutorials go with the Java tutorials, I was wondering if anyone out there knew where I could find a tutorial for object selection? I have found an example from JPot but the code is quite bundled together and not particularly commented :S Any help would be much appreciated  Thanks!! :D 
  I have ported a C sample to JOGL long time ago,hope that helpsturbotank  /**  * @param x  * @param y  * @param drawable  * @return  */  public int[] retrieveSelectionBuffer(int x, int y, GLDrawable drawable) {  GL gl = drawable.getGL();GLU glu = drawable.getGLU();selectBuffer[0] = 0;selectBuffer[1] = 0;selectBuffer[2] = 0;selectBuffer[3] = 0;int objectsFound = 0;// glSelectBuffer is what we register our selection buffer with.  The first parameter// is the size of our array.  The next parameter is the buffer to store the information found.// More information on the information that will be stored in selectBuffer is further below.gl.glSelectBuffer(32, selectBuffer);// Setup our selection buffer to accept object ID''s// This function returns information about many things in OpenGL.  We pass in GL_VIEWPORT// to get the view port coordinates.  It saves it like a RECT with {top, left, bottom, right}gl.glGetIntegerv(GL.GL_VIEWPORT, viewportCoords);// Get the current view port coordinatesgl.glMatrixMode(GL.GL_PROJECTION);gl.glPushMatrix();gl.glRenderMode(GL.GL_SELECT);gl.glLoadIdentity();// gluPickMatrix allows us to create a projection matrix that is around our// cursor.  This basically only allows rendering in the region that we specify.// If an object is rendered into that region, then it saves that objects ID for us (The magic).// The first 2 parameters are the X and Y position to start from, then the next 2// are the width and height of the region from the starting point.  The last parameter is// of course our view port coordinates.  You will notice we subtract "y" from the// BOTTOM view port coordinate.  We do this to flip the Y coordinates around.  The 0 y// coordinate starts from the bottom, which is opposite to window''s coordinates.// We also give a 2 by 2 region to look for an object in.  This can be changed to preference.glu.gluPickMatrix(x, viewportCoords[3] - y, 2, 2, viewportCoords);// Next, we just call our normal gluPerspective() function, exactly as we did on startup.// This is to multiply the perspective matrix by the pick matrix we created up above. glu.gluPerspective(45.0f,(float)1024/768, 4 , 4000.0f);gl.glMatrixMode(GL.GL_MODELVIEW);// Go back into our model view matrixdisplayable.render(drawable, true);objectsFound = gl.glRenderMode(GL.GL_RENDER);// Return to render mode and get the number of objects foundgl.glMatrixMode(GL.GL_PROJECTION);// Put our projection matrix back to normal.gl.glPopMatrix();// Stop effecting our projection matrixgl.glMatrixMode(GL.GL_MODELVIEW);// Go back to our normal model view matrix/*if (objectsFound > 0) {// 1 the number of names in the name stack at the time of the event// 2 the minimum and maximum depth values of all vertices that hit since the  previous event// If we found more than one object, we need to check the depth values// of all the objects found.  The object with the LEAST depth value is// the closest object that we clicked on.  Depending on what you are doing,// you might want ALL the objects that you clicked on (if some objects were// behind the closest one), but for this tutorial we just care about the one// in front.  So, how do we get the depth value?  Well, The selectionBuffer// holds it.  For every object there is 4 values.  The first value is// "the number of names in the name stack at the time of the event, followed // by the minimum and maximum depth values of all vertices that hit since the // previous event, then followed by the name stack contents, bottom name first." - MSDN// The only ones we care about are the minimum depth value (the second value) and// the object ID that was passed into glLoadName() (This is the fourth value).// So, [0 - 3] is the first object''s data, [4 - 7] is the second object''s data, etc...// Be carefull though, because if you are displaying 2D text in front, it will// always find that as the lowest object.  So make sure you disable text when// rendering the screen for the object test.  I use a flag for RenderScene().// So, lets get the object with the lowest depth!// Set the lowest depth to the first object to start it off.// 1 is the first object''s minimum Z value.// We use an unsigned int so we don''t get a warning with selectBuffer below.int lowestDepth = selectBuffer[1];// Set the selected object to the first object to start it off.// 3 is the first object''s object ID we passed into glLoadName().int selectedObject = selectBuffer[3];// Go through all of the objects found, but start at the second onefor(int i = 1; i < objectsFound; i++){// Check if the current objects depth is lower than the current lowest// Notice we times i by 4 (4 values for each object) and add 1 for the depth.if(selectBuffer[(i * 4) + 1] < lowestDepth)  {// Set the current lowest depthlowestDepth = selectBuffer[(i * 4) + 1];// Set the current object IDselectedObject = selectBuffer[(i * 4) + 3];}}}return objectsFound;*/return selectBuffer;  };  Im going to have a sit down with my dinner and read through but that seems like just what I needed!! :D Thanks a lot 