PDA

View Full Version : Java to C++ to OpenGL



d_a_heitbrink
08-27-2009, 04:52 PM
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?

ZbuffeR
08-28-2009, 04:06 AM
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.

davej
08-28-2009, 04:25 AM
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 (http://www.eclipse.org/articles/Article-SWT-Design-1/SWT-Design-1.html), and for QtJava (http://developer.kde.org/language-bindings/java/qtjava.html).

It might also be worth looking at some Java to OpenGL bindings such as LWJGL (http://www.lwjgl.org/index.php).

d_a_heitbrink
08-28-2009, 10:19 AM
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++.

rexguo
08-29-2009, 11:41 PM
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.

ZbuffeR
08-30-2009, 04:16 AM
try Qt for cross-platform C GUI.

mfort
08-30-2009, 10:53 AM
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.

mfort
08-30-2009, 10:57 AM
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.

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

rexguo
08-30-2009, 11:43 AM
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.

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.

mfort
08-30-2009, 12:07 PM
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()