Friday, 2 February 2007

Simple OpenGL demo

I recently added another OCaml demo to my company site. This time it is a simple OpenGL demo that reads vertex information from the file format used by the Stanford mesh database and renders the mesh using diffuse and specular lighting:

http://www.ffconsultancy.com/free/bunny/

Functional programming languages like OCaml often provide a means to "memoize" a function. This entails recording the results of previous function applications so that they can be looked up in the future, without needing to be recomputed.

The concept of memoization can actually be applied to OpenGL with good effect by memoizing rendering calls in an OpenGL display list. This simple OpenGL demo does exactly that. The resulting display list is heavily optimised by the OpenGL driver and performance is superb as a consequence.

Thanks to the simplicity of the OpenGL API and the incredible expressiveness of the OCaml programming language, the entire program (including loading the mesh, setting up the perspective projection and rendering) takes only 85 lines of code.

I have also written a more sophisticated implementation that incrementally computes the silhouette of a mesh in order to render sharp shadows by extruding the silhouette from the light. I'll post this ASAP and probably do a port to F#.

Cheers,
Jon.

No comments: