Wednesday, 30 January 2008

Experience report: Common Lisp and OpenGL

I am working through the (in)famous OpenGL Red Book using cl-opengl(-thomas). Who is this Thomas guy anyway?

I just thought I would post my experience about it so far.

I considered just using C or C++ to work through the examples but the thought of the turnaround time before something was running was bugging me. So I decided to use Common Lisp.

First of all, using clbuild was a smart idea. I additionally set up Emacs+Slime to be able to use the clbuild core or using the default core. I currently have a couple of projects and don't use clbuild for the others. The .emacs is quite ugly as a result but does what I need it to ;-)

Secondly, cl-opengl uses generic functions instead of direct function callbacks. At first, I did not like this deviation from the way OpenGL normally works (via callbacks) but now I have come to appreciate it. One benefit is that there are (apparently!) no global variables. Secondly, there is a lot of nice stuff you can do with generic functions that you cannot do with function callbacks and cl-opengl takes full advantage of this fact.

Thirdly, I was afraid that it might not perform well enough but it turns out that I haven't reached any part of the book yet that requires absolute performance (or it is fast enough!) I'm told that choice of language should not generally be an issue if you have done things right so that is encouraging.

Last, but not least, Emacs + Slime for development of the examples is awesome. I can modify the definition of the display code at runtime and the changes show up. That is very, very useful for understanding (for example) how functions like gluLookAt work.

In general, I believe the experience has been better than it would have been had I used C or C++ for going through the book's examples.

5 comments:

Luís said...

There was a patch that made glut:display-func and friends accept regular functions.

I appreciate the dynamicity as well. It's very unfortunate that GLUT sometimes doesn't play well with that, though. They like to call exit() -- thereby killing the lisp process -- upon trivial errors, those bastards. :-)

So, implementing the GLUT functionality directly in Lisp would be nice at some point.

Sohail Somani said...

Not that you will actually check back but...

What was the reasoning behind the patch?

I *hate* when libraries call exit for you but I don't see any real benefit to having Glut in Lisp, given that using cl-opengl is fairly Lispy. This is of course after only a couple of days of use :-)

Luís said...

Heh, no, I wouldn't check back but Blogger has this nice feature wherein an email notification is sent whenever someone replies to one's comments.

The patch enabled users to write (glut:display-func #'some-callback) instead of (cffi:defcallback foo ...) (glut:display-func (cffi:callback foo)). I prefer the generic function version anyway.

If you load cl-glut without X11 running, (Free)GLUT will call exit(), for example. And there are some other cases that can confuse GLUT whose details I don't recall. Besides, a glut in Lisp would enable even more opportunities for dynamic exploration of OpenGL, I think.

Sohail Somani said...

Ah, I thought that Blogger only did that for the poster not the comments :-)

I might be underestimating the advantage of a GLUT in Lisp given that the exploratory nature of Lisp has been a benefit so far.

Slobodan Blazeski said...

So, implementing the GLUT functionality directly in Lisp would be nice at some point.

I couldn't agree more about this Luis. I've never crushed my image so many times like when interfacing with foreign dlls. Lispworks has a nice example of OpenGl interfacing without glut with their CAPI but I doubt they will release the source. Even if they do , we'll definately need a different windowing system.