Jogl Picking: almost there...

Hi everyone.

I’m trying to get picking to work correctly with jogl.

I read tons of tutorials and forum post concerning the topic, but I couldn’t fix my problem.
My situation is as follows:
I’m drawing a hex tiled map that is scrollable with the mouse.
Now I want to use the left mouse button to select a single tile.

Picking works near the coordinate center, but the farther I move the mouse button from the center the farther the picking result is off.
Seems to me like some problem with the perspective, but the code seems alright. Maybe someone could give me some hints about what I’m missing.
If I plug the select Function instead of the draw function with glRenderMode(GL_RENDER) the displayed result is the same.
This is an excerpt from my code:


private void hitCheck(int x, int y) {
		GL gl = this.canvas.getGL();
		GLU glu = this.canvas.getGLU();
		int viewport[] = new int[4];
		IntBuffer selectBuf = BufferUtils.newIntBuffer(512);
		FloatBuffer pmatrix = BufferUtils.newFloatBuffer(32);
		int hits = 0;
		
		selectBuf.rewind();
		gl.glSelectBuffer(selectBuf.capacity(),selectBuf);
		gl.glRenderMode(GL.GL_SELECT);
		gl.glGetIntegerv(GL.GL_VIEWPORT,viewport);
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glGetFloatv(GL.GL_PROJECTION_MATRIX, pmatrix);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		glu.gluPickMatrix((double)x,(double)(viewport[3]-y),0.1d,0.1d,viewport);
		gl.glMultMatrixf(pmatrix);
		gl.glMatrixMode(GL.GL_MODELVIEW);
		
		gl.glInitNames();
		renderInSelectionMode();
			
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glPopMatrix();
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glFlush();
		hits = gl.glRenderMode(GL.GL_RENDER);
		
		if(hits != 0) processHits(hits,selectBuf);
}
private void renderInSelectionMode() {
		GL gl = this.canvas.getGL();
			
		gl.glPushMatrix();
		gl.glTranslatef(-xpos,-ypos,-16.5f);

		for(int i = 0; i<height; i++) {
			gl.glPushName(i);
			for(int j = 0;j<width;j++) {
				gl.glPushName(j);
				gl.glBegin(GL.GL_POLYGON);
                                    //DRAWING CODE
				gl.glEnd();
				gl.glPopName();
			}
			gl.glPopName();
		}
		gl.glPopMatrix();
}

Any help is greatly appreciated!

There are two other active threads in this forum on the same topic. Take a look at the code and discussions there and see if you can figure out what’s going on.

Well, I did that already (as stated in my post, I already consulted a lot of sources on picking).
Both topics did not solve my problem. (I wouldn’t have posted otherwise.)
I know the whole picking discussion is getting old, but I spent a lot of time looking over my code and just couldn’t find any clue to solve my problem.
It would just be nice if someone took a look at the code (or the symptoms) to give me a pointer to the right direction.

I never use glPopName(). I wonder if that has anything to do with it?

How do you distinguish between objects if you never pop names off the stack?

In my case I have to use glPopName().

I’m working with two names per tile here. So each tile has it’s x and y coordinate as names.
Before drawing I set the two names with glPushName (x and y coordinates of the tile are saved in i and j).
If drawing causes the object to intersect with the tiny selection viewing volume a hit record is created and saved in the select buffer.
After drawing the tile I don’t need the names anymore (in fact they are quite distracting because they appear in every following hit record and could eventually fill up and overflow the name stack), so i call glPopName.
Thanks for your effort, but unfortunately this was not the problem…
Maybe I’ll rewrite the whole thing tonight. Might help to get a clearer view on the topic…