JOGL TextRenderer in Processing 2.0 Alpha X

06 February 2012

In my short time using Processing it became evident quite quickly that the text display is fine when using the normal 2D mode but it didn’t work so well when using OpenGL. Since my aim for 2012 is to learn more OpenGL and often I need text to render nicely on screen - it was clear that I was not alone. This class seem to work well in Procesing 1.5 but the libs in 2 alpha x is out of date. So hopefully these steps will help to get it set up in a jiffy.

JOGamp

The JOGL TextRenderer is a class that allows text to be rendered in OpenGL in a way that you’d expect. There are some drawbacks like rendering a lot of text seems to be slow but this can be improved by not using Vertex Arrays when rendering the text.

Getting JOGL to work with Processing 2.0 Alpha x

To get it to work with Processing 2.0 Alpha x we need to add the latest JOGL lib (2.x as of writing this tutorial) to Processing.

  • Go to JOGAMP
  • On the right under “Useful Links” click on the Builds / Downloads link
  • Click on the zip link under Builds / Downloads
  • Open your Sketch root folder or “Playground” folder
  • In libraries create a new folder and call it jogl
  • In jogl create a new folder library
  • Extract the zip file to this location

Making sure the JAR file is in the right place

  • If you browse to the extracted JOGL library folder there is subfolder called jar.
  • Open it and copy the file named jogl.all.jar.
  • Now go back up to the root of the JOGL library folder and paste the JAR file.

Important here is that Processing doesn’t allow the naming convention used but renaming this will fix it. Rename the file jog.all.jar to jogl.jar. So the library JAR file and the folder name needs to match for it to come up in the Processing IDE.

That’s it. Now you are ready to use the JOGL TextRenderer in your sketches. It should be noted that you could replace the jog.all.jar which can be found in the processing opengl lib folder but I prefer to keep anything I add separate to what is shipped with Processing. This way I know what I’ve changed. Also, important to note that when you keep the JOGL lib separate and export your application, you must replace the jogl.all.jar file that is in your application.x export folder.

Using the TextRenderer in Processing

One of the things I like about the TextRenderer is the automatic switch between Orthographic and Perspective camera views. To use this feature simply either render using 3D or just normal. As mentioned earlier I have come across some performance issues when rendering lots of aliased text. To improve performance you can set the renderer not to use vertex arrays or just not use aliased text.

renderer.setUseVertexArrays( false );

In setup() we create our instance

...
renderer = new TextRenderer( new Font( "Bitstream Charter Bold", Font.BOLD, 150 ), true, true );
renderer.setColor( 1.0f, 1.0f, 1.0f, 1.0f );       
renderer.setSmoothing( true );
...

In draw() we simply render the text we want

...
if ( twoD )
{ 
        renderer.beginRendering( width, height ); 
        renderer.draw( "Text to draw", 0, 0 );
        renderer.endRendering();
}

if ( threeD )
{
        renderer.begin3DRendering(); 
        renderer.draw3D( "Hello World", (float)-renderer.getBounds( "Hello World" ).getWidth() * 0.5, 0.0, 0.0, 1.0 );
        renderer.end3DRendering();    
 }    
...

Processing Libraries

It looks like the guys are planning some nice updates for Processing 2.0 when it comes to library submissions and adding them to your project. I think the above will most likely be out of date once this is done as it will mean simply clicking on the menu and adding the library you want. Also it won’t be long until the Alphas and Beta include the latest JOGL lib. Understanding libraries and how to use them in your project is key part in my opinion to working in any language and this is a good start.

For more info on building libraries for Processing see the Wiki page.

Resources

Incorrect please try again