Skip navigation
The Hemingway Version

In my last blog entry, Easy 2D/3D Mixing in Swing, my goal was to show Swing developers how easy it can be to include 3D content in a desktop application via JOGL and OpenGL. I think we've done a decent job of making things easier for that class of developers, but what if we look at the problem from the other end of the barrel? There are lots of JOGL/OpenGL developers that want to include Java 2D elements in their applications. Historically it has been possible to include Java 2D content in a JOGL application, but there haven't been any good standard APIs to make this sort of integration convenient for developers. Until now.

Let's get the demo out of the way before getting into any of the details. Here's a screenshot (click to enlarge), for the truly impatient reader:



Better yet, run the demo yourself (the app itself is sandboxed and only requires JDK 5; here's the source code, with NetBeans project included):

The Faulkner Version

Ken Russell (the mastermind behind JOGL) promises me that he'll soon have a blog of his own, which means I'll have to stop stealing all of his thunder. In the meantime, let's look at some of the new convenience classes that he's cooked up in the past few weeks (with some prodding from James Gosling, and inspired by some of the reader comments from my last blog).

Texture and TextureIO
These aren't new (they were added to Sun's JOGL implementation over 18 months ago), but it helps to mention them in the context of the newer convenience classes. The Texture class is a really nice abstraction layer around an OpenGL texture object. It makes it very easy for developers to use non-power-of-two sized images in OpenGL (historically this has been tricky for OpenGL developers) and provides simple methods for enabling and binding texture state. The TextureIO class offers an extremely convenient API for loading image data from disk, the network, or any other source into OpenGL texture memory. (If you're into SAT-style analogies: TextureIO is toTexture as ImageIO is toBufferedImage.) It even supports special compressed texture formats, mipmapping, and other complex things that are usually a nightmare for OpenGL developers.

The first of the three new convenience APIs found in thecom.sun.opengl.util.j2d package. This class builds on top of the Texture class described above and provides a convenient way for developers to dynamically render into an OpenGL texture using Java 2D. From the following code snippet, it should be clear that you can use a TextureRendererinstance just as you would a BufferedImage:

    // Create the TextureRenderer and render into it using Java 2D
    TextureRenderer renderer = TextureRenderer(w, h, true);
    Graphics2D g2 = renderer.createGraphics();
    // and so on...
    renderer.sync(0, 0, w, h);

    // Now use it as you would any other OpenGL texture
    Texture tex = renderer.getTexture();


Note that currently the TextureRenderer class uses a BufferedImage behind the scenes as backing store (but that's just an implementation detail). This means that when you're rendering into a TextureRenderer, it currently all goes through software routines. However, Ken and I have some tricks up our sleeves: if all goes well in the next few weeks, we may have an alternate codepath that leverages the JOGL/Java 2D bridge, which will mean that all rendering into theTextureRenderer will be accelerated in hardware via the OpenGL-based Java 2D pipeline, with no costlyBufferedImage-Texture copy steps required. This could be an exciting development, so stay tuned.

Since the beginning of time, rendering text in OpenGL applications has been a huge hassle for developers. There were some really hokey GLUT routines that used ugly bitmap fonts with no support for antialiasing or non-Latin text. In the Java world, crafty developers using JOGL and LWJGL have figured out custom ways to leverage Java 2D's high-quality font rendering in their applications. But until this new TextRenderer class came along, (as far as I know) there was no easy/standard way to get access to the high quality, antialiased text offered by Java 2D. It includes full Unicode support, a really smart string/glyph caching algorithm, and clean APIs that allow for both 2D and 3D rendering of text in a JOGL application (as you saw in my demo above). Under the hood, it takes advantage of the aforementionedTextureRenderer and Texture classes. Ken has been bold enough to declare it "the last word in text rendering" for JOGL apps, and I think he's right. I can even imagine existing C-based OpenGL apps being ported to Java and JOGL for this feature alone...

I didn't actually use this one in the above demo, but it's a really useful and simple way to add a "heads-up display" to your JOGL application. Imagine a flight simulator application: you could render the terrain and all of your 3D content using JOGL, then you could use the new Overlay class as a way to render the airplane's control panel using the high-quality Java 2D APIs. It's really just an even more convenient wrapper around the already convenientTextureRenderer APIs.

To help explain how these new APIs fit together and how they layer on top of OpenGL, I put together a pancake diagram showing how it all comes together. (I don't have my charting software handy, so the following, might I add beautiful, ASCII art diagram will have to suffice.)

       | TextRenderer  |   Overlay  |
       |      TextureRenderer   - - | - - ->  BufferedImage, Graphics2D
       |          Texture       - - | - - ->  TextureIO, ImageIO
       |      OpenGL textures       |


That's all for now. In my next entry I'll have some exciting performance numbers to share in the context of the OpenGL-based Java 2D pipeline (hint: hardware accelerated BufferedImageOps).

In my ears: Sloan, "Never Hear The End Of It" *
In my eyes: William Faulkner, "Absalom, Absalom!"**

* It's a bit long and wordy, but I'm still just as much of a slobbering fan of theirs as I was in high school...
**... But sometimes "long and wordy" can be a virtue.

Well, Richard Bair was kind enough to tag me, but I'm not sure if it was out of pity, or simply out of convenience (we work about 3.5 feet from each other). I've been shocked by how prevalent this whole tag thing has been recently; I mean, doesn't it remind you of those lame chain emails circa 1996? I told myself, "Self, if this tagging thing ever reaches you, you'd better not play along! Those other bloggers are being so vain! Remember your principles!"

But it's a new year (a time for resolutions), and I should probably resolve to stop resisting social phenomena just for the sake of resistance. Then again, I usually resist making new year's resolutions as well, for all the same reasons. (Late last year, Chet and I summed up this philosophy as: "I'm sticking it to the man, but the man ain't listening.")

Anyway, that's it. I give in. Here are five things you may not know about me:

  • My two favorite things in the whole wide world: sheep that wear coats, and inverted umbrellas on windy/rainy day in Manhattan.

  • I'm the sole member (a "non-musician" of sorts [but to compare myself to Eno would be ludicrous]) of a number of bands, some real (La Bonne Soupe), some less real (Shimmy, Burt Rido and the Texas Nachos). The second La Bonne Soupe album, "Literary Gentlemen," was released in November 2006 to worldwide accla... fanfa... umm, something. You can listen to it in full here(not gapless, unfortunately) on the not-so-great-but-at-least-it-aint-myspace

  • When I was 5, I wrote my first BASIC program on our Commodore 64. It was something like a New York State information guide (yes, that was my patriotic year), with lots of menus and very little content. When I was 14, I led a group of random folks in building a virtual reality amusement park (anyone remember REND386?). Yes, that was one of many dorky years, but as you can see graphics programming is in my blood. More on "dzzyland" here; it still runs, if you can track down all the pieces.

  • I'm afflicted by some really silly, non-debilitating medical conditions; for example:

    • Reynaud's syndrome -- my left middle finger goes white when the temperature drops below 55 F; my right ring finger will follow suit below 50 F
    • Sleep paralysis -- it comes and goes, but is most common when my sleep schedule is confused; this condition has been an endless source of amusement for my close friends

  • If I could have lunch with anyone, it would probably be Woody Allen. If I could choose our waiter: Ricky Gervais.


Okay, that's that. I still feel dirty about keeping this phenomenon alive, but I hope these colleagues of mine have more willpower than I do: Shannon, Alex, Artem, and Jasper.

In my ears: Cluster, "Cluster '71"
In my eyes: William Faulkner, "Absalom, Absalom"