tags:

views:

95

answers:

2

I'm trying to get SWT working with the newest JOGL (2.0).

I tried SWT snippet 209, but it doesn't work. The problem is caused by the following line:

final GLContext context = GLDrawableFactory.getFactory().createExternalGLContext();

...but when I change getFactory() to getFactory(GLProfile.getDefault()) (to make it compatible with the new API), it crashes on createExternalGLContext() with javax.media.opengl.GLException: Error: current context null. I don't understand this message: I'm trying to create a new context, and it complains that the current is null. Why?

Maybe there is some other way to create a GLContext?

Or maybe it is possible to use GLEventListener with SWT GLCanvas?

PS. I placed my modified version of the snippet here.


Edit: now I understand the error message. Documentation says:

The underlying OpenGL context must be current on the current thread at the time this method is called.

How to create the first GLContext?

+2  A: 

Easy way: Have your rendering class implement GLEventListener and move your GLContext creation code into the .init(...) callback (seems like the right location for the example you posted). Then add that listener to whatever GLCanvas or the like you're using as your display widget.

They cleaned up JOGL a decent bit recently so most examples won't even compile without a decent bit of massaging. If you're new to it, you might want to roll back to an older version with higher code example compatibility while you get spun up.

j flemm
Searching in whole SWT, I didn't find any mention of `GLEventListener`. How to "attach" it to the canvas (`org.eclipse.swt.opengl.GLCanvas`)?
m01
@m01: Ah. For some reason the SWT library has removed a lot a stuff from JOGL. `GLCanvas` usually implements `GLAutoDrawable` which accepts a `GLEventListener`. Is there any restrictions preventing you from using the vanilla JOGL lib?
j flemm
No restrictions at all; I just don't know how to do this. (And I need it to be placed inside an SWT GUI)
m01
@m01: It'll be similar to adding in an AWT component, which is possible on most systems with a relatively up-to-date JDK. I'm actually working on an Eclipse/SWT project right now where we use a JOGL canvas for a visualization and it hasn't bit us yet. Just add the JOGL jar to your project, point it to the native libraries, then import your GL classes from the vanilla JOGL packages instead of the SWT ones.
j flemm
Ah, I just realized that you can [embed AWT in SWT](http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet135.java) :)
m01
A: 

The tutorial at http://wadeawalker.wordpress.com/2010/10/09/tutorial-a-cross-platform-workbench-program-using-java-opengl-and-eclipse/ shows how to do exactly this. I modified the SWT snippet 209 starting from the JOGL2 version at http://github.com/sgothel/jogl-demos/blob/master/src/demos/swt/Snippet209.java.

My tutorial shows how to do it as an Eclipse RCP app (using only SWT, no AWT bridge). To do it as a standalone SWT app, there's a tutorial at https://sites.google.com/site/justinscsstuff/jogl-tutorials.

Wade Walker