picking

Did anyone picking with JOGL ? I’ve tried to port the select-example from gl4java, but somehow it didn’t work. It renders correctly, but the selection routine returns always 0 hits and I can’t see a difference between the gl4java and JOGL version. Could someone provide example code ?

Thanks.

I have the same question. I tried to do picking but it did not work and gave me some exceptions. I used code from OpenGL Superbible.

My concern is that I have to issue some opengl commands within mouseClicked. I remember that due to multithread issue, opengl commands should not be used within mouse or key handler. Anybody know how to do picking?

Thanks.

wz

net.java.games.jogl.GLException: glGetError() returned the following error codes after a call to glRenderMode(): GL_INVALID_OPERATION
at net.java.games.jogl.DebugGL.checkGLGetError(DebugGL.java:9741)
at net.java.games.jogl.DebugGL.glRenderMode(DebugGL.java:6351)
at JoglTest.processSelection(JoglTest.java:183)
at JoglTest.mouseClicked(JoglTest.java:217)
at java.awt.Component.processMouseEvent(Component.java:5137)
at java.awt.Component.processEvent(Component.java:4931)
at java.awt.Component.dispatchEventImpl(Component.java:3639)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
net.java.games.jogl.GLException: glGetError() returned the following error codes after a call to glLoadName(): GL_INVALID_OPERATION
at net.java.games.jogl.DebugGL.checkGLGetError(DebugGL.java:9741)
at net.java.games.jogl.DebugGL.glLoadName(DebugGL.java:4970)
at JoglTest.display(JoglTest.java:134)
at net.java.games.jogl.impl.GLDrawableHelper.display(GLDrawableHelper.java:74)
at net.java.games.jogl.GLCanvas$DisplayAction.run(GLCanvas.java:208)
at net.java.games.jogl.impl.GLContext.invokeGL(GLContext.java:192)
at net.java.games.jogl.GLCanvas.displayImpl(GLCanvas.java:196)
at net.java.games.jogl.GLCanvas.display(GLCanvas.java:91)
at net.java.games.jogl.Animator$1.run(Animator.java:104)
at java.lang.Thread.run(Thread.java:536)

I also couldn’t get picking to work. Has anybody gotten picking to work?

I’m about to start implementing that right now. I’ll let you know in an hour or so.

Picking seems to work fine for me… This is a copy of the code I added to the end of my rendering method (the one that is called via GLListener):


gl.glDisable(GL.GL_DEPTH_TEST);

int BUFSIZE = 100;
int[] selectBuf = new int[BUFSIZE];
int hits;
int[] viewport = new int[4];

gl.glGetIntegerv(GL.GL_VIEWPORT, viewport);

gl.glSelectBuffer(BUFSIZE, selectBuf);
gl.glRenderMode(GL.GL_SELECT);

gl.glMatrixMode(GL.GL_PROJECTION);

gl.glInitNames();

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

gl.glLoadIdentity();

glu.gluPickMatrix(xMousePos, (viewport[3] - yMousePos), 5.0, 5.0, viewport);
glu.gluPerspective(70.0f, ((float)width) / ((float)height), 0.25f, 2048.0f);

gl.glMatrixMode(GL.GL_MODELVIEW);
meshRenderer.pick(playerPos, time, alpha, gl);
gl.glPopName();
gl.glPopName();
gl.glFlush();

hits = gl.glRenderMode(GL.GL_RENDER);
if (hits > 0)
{
  System.out.print("Hits (" + hits+"): ");
                  
  IntBuffer ib = IntBuffer.wrap(selectBuf);
            
  for (int i=0; i<hits; i++)
  {
    if (i>0) System.out.print(", ");
    int nameCount = ib.get();
    int minZ = ib.get();
    int maxZ = ib.get();
    int[] names = new int[nameCount];
    ib.get(names);
                        
    String name = "";
    for (int j=0; j<names.length; j++)
    {
      if (j>0) name+=",";
      name+=names[j];
    }
                        
    System.out.print("["+name+"]");
  }

  System.out.println();
}

Please forgive the messy code. :wink:

“meshRenderer.pick(playerPos, time, alpha, gl)” renders the closest trees around the player in “picking” mode, meaning no texture or lighting. It’s a poorly named method, I know. :wink:

When I run this, the game spits out similar output to this on System.out:

Hits (3): [1,0], [1,1], [1,4] Hits (3): [1,0], [1,1], [1,4] Hits (3): [1,0], [1,1], [1,4] Hits (3): [1,0], [1,1], [1,4] Hits (3): [1,0], [1,1], [1,4]

The first number (1) meaning it’s a tree (hardcoded), and the second number being the number i gl.glLoadName(…) in meshRenderer.pick(…).

Hi, Markus,

Thanks a lot. I messed up variable names that caused exception. it works now.

By the way, if the pick part is added at the end of the rendering method, another rendering method (your meshRenderer.pick) will be needed to be called at GL_SELECT mode. Can the picking be added within mouse handler? Then one display method will be reused in different modes. I am not sure about this since the multithread issue. Any suggestions?

wz

I don’t think that would work, as the only “proper” place to perform opengl commands is in methods called via the GLEventListener interface.

Hello !

I up this old topic as my problem does concern picking.

I got an GL_INVALID_OPERATION error while i’m trying to get to selection mode.
I used the code found here : http://www.java-tips.org/other-api-tips/jogl/another-picking-example-in-jogl.html
and adapted it to mine but i got this error :

glGetError() returned the following error codes after a call to glSelectBuffer(): GL_INVALID_OPERATION

There is a bit of my code :

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();
	    
	    /* create 2x2 pixel picking region near cursor location */
	    glu.gluPickMatrix((double) pickPoint.x,
	        (double) (viewport[3]+45 - pickPoint.y), //
	        2.0, 2.0, viewport, 0);
	    
	    glu.gluPerspective(40., 1.0, 0.1, 100.0);

            // draw the Scene
	    this.afficherScene(gl,GL.GL_SELECT);
	    
	    gl.glPopMatrix();
	    gl.glFlush();

	    hits = gl.glRenderMode(GL.GL_RENDER);
	    selectBuffer.get(selectBuf);
	    processHits(hits, selectBuf);

My problem is when the mode in the red line is GL_SELECT i got the error and my app crashes.
If I change the mode to GL_RENDER, the app loads fine, but i got no selection (for sure ;D)

The error doesn’t seem to be in the picking method but i can’t find it.
Does someone got any idea ?

Take a look at demos.misc.Picking in the jogl-demos workspace.