Picking seems to work fine for me… This is a copy of the code I added to the end of my rendering method (the one that is called via GLListener):
gl.glDisable(GL.GL_DEPTH_TEST);
int BUFSIZE = 100;
int[] selectBuf = new int[BUFSIZE];
int hits;
int[] viewport = new int[4];
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport);
gl.glSelectBuffer(BUFSIZE, selectBuf);
gl.glRenderMode(GL.GL_SELECT);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glInitNames();
gl.glPushName(1);
gl.glPushName(-1);
gl.glLoadIdentity();
glu.gluPickMatrix(xMousePos, (viewport[3] - yMousePos), 5.0, 5.0, viewport);
glu.gluPerspective(70.0f, ((float)width) / ((float)height), 0.25f, 2048.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
meshRenderer.pick(playerPos, time, alpha, gl);
gl.glPopName();
gl.glPopName();
gl.glFlush();
hits = gl.glRenderMode(GL.GL_RENDER);
if (hits > 0)
{
System.out.print("Hits (" + hits+"): ");
IntBuffer ib = IntBuffer.wrap(selectBuf);
for (int i=0; i<hits; i++)
{
if (i>0) System.out.print(", ");
int nameCount = ib.get();
int minZ = ib.get();
int maxZ = ib.get();
int[] names = new int[nameCount];
ib.get(names);
String name = "";
for (int j=0; j<names.length; j++)
{
if (j>0) name+=",";
name+=names[j];
}
System.out.print("["+name+"]");
}
System.out.println();
}
Please forgive the messy code.
“meshRenderer.pick(playerPos, time, alpha, gl)” renders the closest trees around the player in “picking” mode, meaning no texture or lighting. It’s a poorly named method, I know.
When I run this, the game spits out similar output to this on System.out:
Hits (3): [1,0], [1,1], [1,4] Hits (3): [1,0], [1,1], [1,4] Hits (3): [1,0], [1,1], [1,4] Hits (3): [1,0], [1,1], [1,4] Hits (3): [1,0], [1,1], [1,4]
The first number (1) meaning it’s a tree (hardcoded), and the second number being the number i gl.glLoadName(…) in meshRenderer.pick(…).