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!