New GLGraphics with OpenGL context sharing   4 comments

The latest version of GLGraphics (, available for download here) incorporate a new useful feature: the GLTextureWindow object. This object allows to display a GLTexture in a separate window. This window is undecorated (doesn’t have title or borders), its position in the screen is fixed, and cannot take any kind of input. So what is the purpose of this object? Basically, it can serve as an output window while the main sketch canvas servers as the input/drawing/compositing region. For instance, this is very useful in a VJ or live visual performance tool, where the main window contains the GUI and the previsualization of the images, while the secondary output window is used to show the resulting visuals (without the overlayed interface elements) on a secondary display (usually a projector). Take a look at this screenshot of modul8, for example:


Implementing this kind of functionality in Processing was a little tricky since Processing sketches are designed to have a single PApplet window. Also, having two windows that show the same OpenGL texture involves a technique called context sharing. Each window has its own OpenGL context where all the texture, vertex buffers and other objects IDs are defined, and sharing these resources between different contexts requires dealing with multi thread synchronization issues. Anyways, the current implementation of GLTextureWindow deals with these issues and it is fairly easy to use. All it is needed is to add the following code in the setup() function:

void setup() {
  size(300, 300, GLConstants.GLGRAPHICS);
  // Creates a window of (400, 400) pixels located at (0, 0).
  texWin = new GLTextureWindow(this, 0, 0, 400, 400);

  // The creation of the texture window should go before any
  // other opengl resource allocation, such as:
  tex = new GLTexture(this, 100, 100);
  // ... so doing new GLTextureWindow right after size() is a good choice.

  // Attaching a texture to the window.

  // A call to init() is required at the end of setup.

Some other functions are available in the GLTextureWindow class, such as show(), hide() and isVisible(), to further control its functionality. Please take a look at the example included with the library for more information about these functions.

The next image shows a Processing sketch using the GLTextureWindow object:


You can find more information about context sharing with OpenGL and thread synchronization in Java in the following links:


4 responses to “New GLGraphics with OpenGL context sharing

Subscribe to comments with RSS.

  1. Andres,

    I am trying to access my main thread’s GLContext from a class that implements Runnable. I only want to run GLTextureFilter.apply from within the class to perform some GPGPU calculations–I don’t need to draw anything. Can you think of a way for me to do that?

    • Hi,

      All JOGL classes/interfaces derived from GLCanvas inherit a method called getContext() that allows you to get the GLContext associated to the given drawable.

      Processing’s PGraphicsOpenGL and GLGraphics also have a getContext() method, which you can use from your main applet like this:

      GLContext mainContext = ((PGraphicsOpenGL)g).getContext();

      Does this answer your question?

      • I am not sure. What I’m looking for is a way to run execute a GLTextureFilter from my Runnable thread… I thought that my Runnable thread would need to grab my main thread’s GLContext and somehow call GLTextureFilter.apply() with it.

  2. I have little experience dealing with openGL contexts, but from what I saw it is a tricky business to properly handle and share contexts between threads.

    I think you can only associate a GLContext to a drawable, so this means that you would need to add a drawable interface to your Runnable.

    The GLTextureFilter class is designed to work within a GLGraphics renderer, to make it more general so it can accessed from any openGL surface some changes in the code would be required.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: