Sign in to follow this  
Kwark

Mouse picking (GL_SELECT) not registering

Recommended Posts

Kwark    100
I'm creating a simple (for now) model viewer applet in Java using JOGL. What I would like to get working first is an universe with several simple objects (cubes etc), be able to rotate/zoom/pan on the universe and be able to select single objects. I've got the simple universe setup, only mouseclicks are not hitting any objects. Any pointers what I'm doing wrong? These are relevant (I think) snippets for my applet.
public void display(GLAutoDrawable drawable) {
  // ..
  drawCubes(GL.GL_RENDER);
  gl.glFlush();  // manual using GL_QUADS
}
// MouseMotionListener interface implemented 
public void mouseDragged(MouseEvent e) {
  // angle (float) and axis (int[3]) are calculated
  gl.glRotatef(angle, axis[0], axis[1], axis[2]);

public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  gl.glMatrixMode(GL.GL_PROJECTION);
  gl.glLoadIdentity();
  // Orthogonal projection, translated for better fitting onscreen
  gl.glOrtho(left, right, bottom, top, 0.01f, 1000.0f);
  gl.glTranslatef(0.0f, 0.0f, -500.0f);
}
And now the not-working part:
// Called on each mouseClicked() event.
private void pickRects(GL gl) {
  final int BUFSIZE = 512;

  int[] selectBuf = new int[BUFSIZE];
  IntBuffer selectBuffer = BufferUtil.newIntBuffer(BUFSIZE);
  int hits;
  int viewport[] = new int[4];

  gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
  gl.glSelectBuffer(BUFSIZE, selectBuffer);
  gl.glRenderMode(GL.GL_SELECT);

  gl.glInitNames();
  gl.glPushName(-1);

  gl.glMatrixMode(GL.GL_PROJECTION);
  gl.glPushMatrix();
  gl.glLoadIdentity();

  // pickPoint.xy (ints) is set correctly on each mouseclick
  glu.gluPickMatrix((double) pickPoint.x,
    (double) (viewport[3] - pickPoint.y),
    5.0, 5.0, viewport, 0);

  drawCubes(GL.GL_SELECT);  // now glLoadName(x) is used
  gl.glPopMatrix();
  gl.glFlush();

  hits = gl.glRenderMode(GL.GL_RENDER);  // hits is always 0 in my testcase, why?
  selectBuffer.get(selectBuf);

  processHits(hits, selectBuf);
}
Any ideas what's going wrong? The mouseClicked event is registering fine, the x,y mouse coordinates are correct too. It's just that I get no hits at all in GL_SELECT mode.

Share this post


Link to post
Share on other sites
gpu_fem    208
When you get 0 hits this usually means that your projection matrix is wrong. You can always test it by not switching to select mode - this will render the selected region to the screen.

It looks to me like you forgot to apply you perspective transformation after creating the pickmatrix. According to OpenGL, you have to do your perspective transformation after doing the pickmatrix transformation.

i.e.,

glLoadIdentity()
gluPickMatrix()
gluPerspective() / glOrtho

Share this post


Link to post
Share on other sites
Kwark    100
Okay, I've got it fixed.
It was indeed a (minor) problem with my projection matrix in SELECT mode.

I can parse the hit results alright, getting stackdepth + z1 + z2 + names alright.

I now have a different problem though, perhaps you can help me with that too:

When there are multiple objects in the hit results (in front of eachother during the click), I can sort those using the minimum (z1) and maximum (z2) depth values returned.
From the front (default projection) these depth values are correct. However, when I rotate behind the object (180 degree turn) and click again, the same hit results are returned (same names, different depth values ofc). But now when I do a depthsort, the object order is backwards.

I'm using a quaternion based camera, calculating a quaternion and using that to translate.

Any ideas why the depth order is wrong?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this