Java to C++ to OpenGL

I working with a large control interface system written in Java running on Linux. What we want to do is to run some visualization + user interface controls written in C++ with OpenGL and compiled to a .so, and called from Java and displayed on a panel in the java program. We will also need user interface events past to the .so . We want to do this for a bunch of reasons that I don’t want to get into.

We are just starting this project and I am not too sure where to start? Has any one done this or something similar in the past? and where do you think we would be best to start?

1 Like

Do you have a good reason not to use Jogl (java opengl) ?

If there is an actual need for c++, JNI wrappers can be done quite easily to stitch java and c/c++ together. But how can java provide the surface to be rendered by c++/opengl ? This sounds like the hardest part.

If you don’t already know it, learn the Java Native Interface.

After that, you’re probably best looking at open source Java bindings to UI frameworks and use them for inspiration. You can get the ones for Eclipse’s SWT, there’s an article on their approach here, and for QtJava.

It might also be worth looking at some Java to OpenGL bindings such as LWJGL.

But how can java provide the surface to be rendered by c++/opengl ?

This I agree is the hardest part, maybe Jogl can provide something to help us do this?

we do have a good reason to try to keep this all in C++ on our end, for one thing we are using some pretty hefty algorithms that will struggle to run real-time in Java, and we already have the visualization code done in C++.

It depends on who is going to create your OpenGL context.
If it is going to be done in C, then you need to get the
window handle from AWT and pass it to the native layer.
Then you need to persuade JOGL/LWJGL to use your manually
created GL context.

If you are creating the GL context in Java, then you need
to grab the context pointer that is well-hidden with JOGL/
LWJGL and pass it to the native layer for rendering.

In any case, it is going to be a pretty ugly job. I’d
suggest you look into the possibility of either going fully
C or fully Java. The C approach will make your UI code
hard to port across platforms.

try Qt for cross-platform C GUI.

No worries of OpenGL/Java/C++ mixing.

Use Java for your UI.
Use JOGL to create OpenGL canvas.
Use your C++ (legacy) rendering code.

You don’t need to dig any context from AWT.
Just call your C++ method that renders you scene from Java render method.

Because Java and C++ threads are the same the OpenGL context is also current in your C++ code.

There is no magic. It is very simple.

You never need to do this. Just call C++ method after the JOGL is initialized. Then in C++ call wglGetCurrentContext(). That’s it.

The OP is working in Linux, so there will need to be a Linux
equivalent of wglGetCurrentContext() for this to work.

Then you call glXGetCurrentContext()