Jump to content
  • Advertisement
Sign in to follow this  
Kwark

Mouse picking (GL_SELECT) not registering

This topic is 3314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
I don't have a direct solution for your issue. However, it is worth noting that OpenGL's selection method is deprecated, and you should probably use an alternate approach, such as raycasting or colour picking.

Share this post


Link to post
Share on other sites
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!