PDA

View Full Version : Mouse picking (GL_SELECT) not registering



Kwark
06-18-2009, 06:19 AM
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.

cprimenull
06-18-2009, 11:54 AM
Try changing the 'name' -1 to a positive value. glPushName excepts a GLuint - eg unsigned. Also should take into account any rotations or translations of the modelview or projection view when you do your picking. Lastly, not sure if its necessary, but you should call glPopName

Kwark
06-23-2009, 12:42 AM
Okay, I've got it fixed.
It was 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?

Prompt
06-24-2009, 02:19 AM
Remember, GL_SELECT is not hardware accelerated, only in workstation graphics cards like nVidia QUADRO.

Kwark
06-24-2009, 02:57 AM
But GL_SELECT won't use many resource if my picking region is small, right?

What would be a better way then? All my entities are fully shaded
so using a unique color for each entity is not really an option.

ZbuffeR
06-24-2009, 03:36 AM
But GL_SELECT won't use many resource if my picking region is small, right?
Small region will not reduce the resources needed to transform all your vertices with the CPU.


All my entities are fully shaded
so using a unique color for each entity is not really an option.
You are wrong ! Color picking is done in a separate pass, no shading, no texturing, only solid colors.

Kwark
06-24-2009, 04:09 AM
Oh ok, I'll definitely look into the color picking then.