Use JOGL to render swing UIs in an external OpenGL Context

Hello, I asked some time ago if it was possible to use an external context (for example created in an external C app) in JOGL. It is of course possible using GLDrawableFactory.createExternalContext().

I now want to paint swing UIs (living inside GLJPanels) inside this external context. This would for example allow to draw swing widgets in a 3D world created by external code. I don’t want here to simply use java2D inside OpenGL, but an entire GLJPanel and all its contained widgets. I assume that is is possible by:

  1. creating a JOGL context using the current external OpenGL context (by GLDrawableFactory.createExternalContext)
  2. create a new GLJPanel sharing its context with the previously created JOGL context
  3. simply add children widgets in the GLJPanel

Alternatively, another way may be to treat swing widgets as regular java2D objects, and paint them like the various demos using the Java2D / JOGL interoperability.

Any hints ?

Herve

I believe I tried doing what you are doing a year or two ago and I wasn’t successful. JOGL is resposible for rendering the 3D scene only, the UI manager handles painting the window. If I’m remembering correctly you would have to create your own UI that used opengl to draw the windows. Or somehow set java2d to use opengl and then pipe those calls into your external context. Either way it might be a little harder to accomplish than you think it is. I’ve got too much on my plate right now but I’ve always wanted to come back and try to get this too work. It would be awesome to get this working, hopefully you can succeed where I failed.

I’m pretty sure that the GLJPanel and context sharing do not work the way you think they do. Unless the java2d-opengl pipeline is enabled, jogl rendering into a GLJPanel is read into an image, which is then used with the rest of swing rendering (this I’m a little vague on the details).

For sure, though, context sharing does not mean that rendering in one context causes renderings to show up in others. Context sharing allows any objects created for opengl (textures, vbos, shaders) to be accessible to any of the shared contexts; the objects will not be destroyed until all contexts using them are destroyed, or if you explicitly destroy them.

I should have known that it would not be so simple :wink:

However, it seems judging by your anwers that the “non direct” approach of piping Java2D calls to OpenGL would be the one that could (maybe) work. I will try, and post if it works at the end…

should work. Add your components to the GLJPanel and override the paint function to paint to an image instead. Copy to texture and draw a quad at the same position and you are done.
As long you stay away from controls with overlays like combo boxes it should give you same results as before.

the same scenario with a GLCanvas is a bit trickier :wink:

bienator’s solution seems to be a good one. I’d give that a try first.

[quote]bienator’s solution seems to be a good one. I’d give that a try first.
[/quote]
Thanks, I will try that !!