JOGL and JNI

Hi, all.

I would like to achieve the following, and don’t know if it’s possible.

I want to write a native Mac OS X Cocoa application (the problem can be generalized to any platform).
The light-weight front-end would be implemented in Cocoa, possibly using NSOpenGLView for rendering.
The back-end would be implemented in Java, featuring rendering through JOGL.
The glue would be JNI.

Now, assume that one of the methods in Java, which would be exposed as C function through JNI would be named renderFrame(). Assume, that the C function would be called within -(void)drawRect:(NSRect) method of NSOpenGLView (or in general on current GL thread).

The question is: will following implementation of the Java method perform rendering on current GL thread in C, and as a consequence on NSOpenGLView?


void render() {
  GLContext glContext = GLContext.getCurrent();
  GL gl = glContext.getGL();

  /* ... perform rendering here on retrieved instance of GL ... */
}

Regards,
Michal.

Yes, but you need to use GLContext.makeCurrent(), not getCurrent(), for this. See for example demos.context.DualContext in the jogl-demos workspace. The only thread switching that JOGL does under the hood is associated with the GLEventListener mechanism. If you don’t use that, but do OpenGL context management manually, all of the operations occur on the thread they’re executed.

Hi, Ken.

From what I can see, the makeCurrent() is an instance method, so I need to have a reference to an existing instance of GLContext. The problem is, how can I get a reference of GLContext?

Maybe I’ll try to describe my problem from the other side. What I actually need in the renderFrame() method is an instance of GL, which would invoke OpenGL functions in C on current context on current thread. If you could, please, write a fragment of code responsible for getting a hold on GL instance, I’d be grateful. Note, that the initialization of OpenGL is done on the C (Cocoa) side.

Regards,
Michal.

I’ve driven over the JavaDocs, and found a method which is maybe what I was looking for: GLDrawableFactory.createExternalGLContext().

Now, assuming that the renderFrame() method will always be called through JNI on the same thread and the same GL context, it may look like below. I just need your confirmation, that I’m not doing something completely stupid.


void renderFrame() {
  if (gl == null) {
    glContext = GLDrawableFactory.createExternalGLContext();
    gl = glContext.getGL();
  }

  /* Perform rendering using GL instance */
}

Regards,
Michal

You should call makeCurrent() on the external GLContext even though this is largely a no-op – this allows it to interoperate with the rest of JOGL (GLContext.getCurrent(), in particular). Aside from that your code looks fine. Call release() on the context when you’re done with all of your rendering and are cleaning up (again, largely a no-op).

Thanks, Ken. Your support is extraordinary.

Michal.