« Shifting gears to Ruby | Main | RubyCocoa progress »

February 18, 2005

OpenGL is turtles all the way down

I'm really starting to like OpenGL. It does a lot more than I expected, I was expecting graphics primitives at a lower level. It's great to be able to specify an interpolation method, and then just start drawing points on the canvas, knowing that OpenGL will connect them, fill in the shapes, add dotted lines, or whatever other decorations I need. And it feels a bit like LOGO too!

I mention LOGO because before you place points on the canvas, you tell OpenGL what decoration to apply (take my points, and turn them into a filled polygon, or take my points, and draw colored lines between them with a width of 2 pixels). LOGO comes to mind because you're setting the color manually before each graphics operation, and there is a begin and end for every series of points. If you want to draw a red line parallel to a blue line, you have to set your color to red and say you're going to draw a line, then add two points to the canvas, then end the draw operation, repeating the series of steps to draw the blue line. It ends up making the code very easy to understand, although a little verbose.

The pseudocode looks like this:

set color red

prepare to draw line
place point(10, 0)
place point(20, 0)
stop drawing line

set color blue

prepare to draw line
place point(10, 10)
place point(20, 10)
stop drawing line

So I should modify what I said above about OpenGL being higher level than I expected. It lets me perform complicated tasks easily, but the way you tell it to draw shapes is about the level I expected. I don't mind having some structure there, it makes it much easier to think about scene rendering and what operations to do in what order. Too much syntactic sugar at the operation ordering level would end up making the graphics code undecipherable, IMO. It has a small set of rules that are easy to remember, and if you want syntactic sugar, I imagine it would be fairly easy to write a compiler that takes higher level primitives and generates the OpenGL commands to implement them, taking care of setting colors, ordering draw methods, etc, etc.

I bought a nice OpenGL reference this afternoon, so I'm looking forward to working my way through it. I think in a week or two, I'll be able to start tackling the 3d time-series graph I want to implement.

BTW, the JOGL book I bought is a bit of a disappointment. I've found a few bugs in the sample code and the formatting is pretty bad, even for a technical book. The visual noise and formatting inconsistencies make it more difficult to absorb the lessons the author is trying to teach me. I'm not slamming the author, I know that writing a book is difficult and the editing/publishing cycle can have surprises in it. I guess the book was good from the perspective of understanding how JOGL wraps native OpenGL libraries on different platforms, and to learn some tips/tricks for gotchas you wouldn't know otherwise, but I feel the real jumps in knowledge and confidence are going to come from the OpenGL bible I bought today, even though all its examples are in C.

I've got JOGL added to my eclipse tree, so I don't need a reference for JOGL itself. I love how eclipse will load a jar, parse out javadoc if it's there, and then auto-complete method names and arguments for you when you're writing code. The pop-ups for method signatures (and the side window showing the doc for that method) make exploring unfamiliar frameworks very quick and painless. I shudder to think how frustrated and slow I'd be if I were trying to do java work with makefiles and vi or emacs. Did that before, and will never do that again.

p.s. Kudos if you caught the connection between my post title and LOGO. I have a soft spot for LOGO since I spent a lot of time playing with it when I was first getting into computers and software.

Posted by djb at February 18, 2005 02:25 PM

Comments

Post a comment




Remember Me?

(you may use HTML tags for style)