Live Lua Scripting of OpenGL

I’ve just invented a new phrase for myself, “Live Scripting”. That’s an environment in which you use some sort of language, probably late bound scripting, and you change the script, and the application changes in realtime.

I’m not saying I’ve invented a new technique here. There are probably plenty of examples of the technique already in existance. I’ve just given a name to it for myself so that I can easy describe what it is.

The picture here is of the current state of the HeadsUp program. By default, HeadsUp provides a window with an OpenGL context baked in. All that means is that as an executable, you can feed it a script file, and that script has ready access to the GL Context, without having to do any OS specific stuff to get it.

One of the cool things HeadsUp does is to reaload/compile/execute the script if you make any changes to it. HeadsUp itself does not provide any editor, so if you have your script file open in your favorite editor, you can make changes there, press “Save”, and HeadsUp will pick up the changes, and run your script again.

This makes for “Live” programming. You can try stuff out, and see how it will affect the outcome pretty immediately. If you’ve ever used OpenScad, it has a similar feature, where it will pick up changes to your model file, and re-render if anything has changed. Of course, if you change something that is deeper than the top file, it won’t pick that up, but that’s a little bit of work to make that happen.

So, what do you get for your troubles?

Well, first of all, trying stuff out suddenly becomes really fast and easy.  The example above was taken from typing in some code from “The Red Book” on OpenGL, page 24 of the  sixth edition.  I could type it in almost verbatim.  There are a couple of default global functions that HeadsUp looks for.  This is to match what the GLUT library normally does.  So, if you want, you can implement:

init() – This will be called first, and only once per compilation of your script

display() – This is called every time a frame is to be rendered

reshape() – This is called every time the window changes size

mouse() and keyboard() are coming, once I decide how I want to map the raw windows messages to something easier to handle.  Perhaps I’ll use the same as what GLUT does, or perhaps I’ll use what VNC does, or something in between.

This is a great boon to my OpenGL programming.  Most of the time, when you’re trying to do something new, you bump around a lot, trying out various API calls.  I can’t seem to get cameras setup properly, so I typically spend an inordinate amount of time fiddling about with various parameters.  Well, this “live scripting” allows me to mess around in realtime, without ever having to leave the comfort of my editor, or having to wait for a “compile”.

I think this is a great way to program in general.  I can do the same thing with the Kinect library, and the networking stuff.  Just mess around, script kiddying away, until things work the way I want them.

There’s another funky little oddity with this live scripting thing.  Since you can have multiple versions of HeadsUp running at the same time, looking at the same script, you could actually create several different views of the same thing, simply by having different windows up.  Your script would have to deal with some sort of UI to get the different views, but once you have that, you can alter the core script, and the different windows will show the changes in realtime as well.

Well, lots of fun and games, once you can throw off the chains of opression that have built up in our typical programming environments.

 

About these ads


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 44 other followers