We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › GL Context Invalid if accesed from external thread
Page Index Toggle Pages: 1
GL Context Invalid if accesed from external thread (Read 559 times)
GL Context Invalid if accesed from external thread
Dec 15th, 2007, 5:43am
 
Hey Guys, hope you can help me out.
Im using the openGL renderer to draw a scene in 3D and im using JohnG´s method to unproject and get the world coordinates from the mouse coordinates for picking objects.

But since the program will be used for a multitouch screen, that sends touch Messages using the TUIO protocol, im now trying to get the World coordinates from the TUIO input.
But now the function that translates the coordinates gives wrong results (always returns (0,0,0)).
My guess is that the openGL stuff is only valid during or right after processings draw() function but since the TUIO Client runs in a different thread it calls the unprojection method at arbitrary times when the GL context is invald.

So heres the unprojection method that returns the unprojected world coordinates for a screen X,Y.
It gets called when the TUIO client thread recieves a message.

Quote:
public static Vector3D getWorldForScreenCoordss(PApplet applet, float screenX, float screenY ){
int viewport[] = new int[4];
double[] proj  = new double[16];
double[] model = new double[16];
double[] worldPosArr = new double[4];

GL gl;
GLU glu;

gl  =((PGraphicsOpenGL)applet.g).gl;
glu =((PGraphicsOpenGL)applet.g).glu;

 ((PGraphicsOpenGL)applet.g).beginGL();
 gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
 gl.glGetDoublev(GL.GL_PROJECTION_MATRIX,proj,0);
 gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX,model,0);
 FloatBuffer fb =    ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asFloatBuffer();
 gl.glReadPixels((int)screenX, applet.height - (int)screenY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, fb);
 fb.rewind();
 glu.gluUnProject((double)screenX, applet.height - (double)screenY, (double)fb.get(0), model,0,proj,0,viewport,0,worldPosArr,0);
 ((PGraphicsOpenGL)applet.g).endGL();

 return new Vector3D((float)worldPosArr[0], (float)worldPosArr[1], (float)worldPosArr[2]);
}


Sometimes, not always, it get this error while getting TUIO input:
Quote:
javax.media.opengl.GLException: May not call this between glBegin and glEnd
at com.sun.opengl.impl.GLImpl.checkBufferObject(GLImpl.java:28006)
at com.sun.opengl.impl.GLImpl.checkPackPBODisabled(GLImpl.java:28046)
at com.sun.opengl.impl.GLImpl.glReadPixels(GLImpl.java:15943)
at util.WorldCoordinateTool.getWorldForScreenCoords(WorldCoordinateTool.java:55)
at tangibleObjects.objectContainers.ObjectContainer3D.isObjectAt(ObjectContainer3D.
java:80)
at test.controlInput.inputAnalyzers.dragAnalyzer.DragGestureAnalyzer.fingerDown(Dra
gGestureAnalyzer.java:65)
at test.controlInput.inputSources.AbstractInputSource.fireFingerDownEvent(AbstractI
nputSource.java:39)
at test.controlInput.inputSources.TuioInputSource.updateTuioCur(TuioInputSource.jav
a:81)
at tuio.TuioClient.acceptMessage(TuioClient.java:169)
at com.illposed.osc.utility.OSCPacketDispatcher.dispatchMessage(OSCPacketDispatcher
.java:74)
at com.illposed.osc.utility.OSCPacketDispatcher.dispatchPacket(OSCPacketDispatcher.
java:50)
at com.illposed.osc.utility.OSCPacketDispatcher.dispatchBundle(OSCPacketDispatcher.
java:57)
at com.illposed.osc.utility.OSCPacketDispatcher.dispatchPacket(OSCPacketDispatcher.
java:41)
at com.illposed.osc.OSCPortIn.run(OSCPortIn.java:64)
at java.lang.Thread.run(Unknown Source)


So, any ideas how to best solve this problem? Any help would be appreciated =)


Page Index Toggle Pages: 1