I’m having a lot of problems trying to get picking to work in jogl in CentOS 5 (RedHat). I have a lot of pickable cubes on screen and I need to be able to pick one. Right now, glRenderMode(GL_RENDER) returns -1 or 0. When I comment out glRenderMode(GL_SELECT), I do sometimes get something drawn on screen, but it is usually not from where I clicked. It appears that, when I am not commenting out glRenderMode(GL_SELECT), the return value of -1 is when nothing is drawn when it is commented out and the return value of 0 is when something incorrect is drawn. In the place with the commented out gluUnproject, gluUnproject returns 0,0,0 on every click.
Here is my pick function:
public void pick(GL gl)
{
if(mouseLeftClicked)
{
System.out.println("you clicked your mouse and are attempting to pick something");
IntBuffer selectionBuffer = BufferUtil.newIntBuffer(BUFSIZE);
int hits = 0;
double[] worldCoords = new double[3];
IntBuffer viewport = BufferUtil.newIntBuffer(4);
FloatBuffer modelview = BufferUtil.newFloatBuffer(16);
FloatBuffer projection = BufferUtil.newFloatBuffer(16);
DoubleBuffer objectPosition = BufferUtil.newDoubleBuffer(3);
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport); //save viewport into buffer
gl.glGetFloatv(GL.GL_PROJECTION, projection); //save projection matrix into buffer
gl.glGetFloatv(GL.GL_PROJECTION, modelview); //save modelview matrix into buffer
viewport.rewind();
projection.rewind();
modelview.rewind();
objectPosition.rewind();
gl.glSelectBuffer(hits, selectionBuffer); //setup hits buffer
int realY = viewport.get(3) - winHeight -1;
gl.glRenderMode(GL.GL_SELECT); //put gl context into selection mode
gl.glInitNames(); //initialise name stack
gl.glPushName(0);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
glu.gluPickMatrix(mouseX, viewport.get(3) - mouseY, pickWidth, pickHeight, viewport);
glu.gluPerspective(45.0f, ((float)winWidth/(float)winHeight), 1.0, 50.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
//cube.drawCubes(gl);
//gl.glLoadName(1);
c.draw(gl);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glFlush();
hits = gl.glRenderMode(GL.GL_RENDER);
// glu.gluUnProject(winWidth, realY, 10.0f, modelview, projection, viewport, objectPosition);
processHits(hits, selectionBuffer);
mouseLeftClicked = false;
System.out.println("oh we got to the end of picking; selectionBuffer has " + selectionBuffer.capacity() + " elements");
// System.out.println("gluUnProject results: x: " + objectPosition.get(0) + " y: " + objectPosition.get(1) + " z: " + objectPosition.get(2));
}
}