Articles

Creating a "Sketchpad" for jit.gl.sketch

Lately, I've been working on some "classic" OpenGL programming within Jitter, and I've been using jit.gl.sketch to do that work; it is very close to the OpenGL syntax that you find in most books, and is fairly forgiving in terms of incoming data type. However, I got very tired of editing message boxes once the programs got a little bigger, but I wanted replaceable parameters like you get with a message box.

Download the patch used in this tutorial.

The Basic Idea

In looking over options, I realized that the text object would give me most of what I desired: an easy-to-use editor, line-by-line output using the dump message and the ability to load and save the contents as a standard text file. However, I realized that I would not have access to replaceable parameters with the text object's content.

In order to get around this limitation, I build a little subpatcher that takes the individual lines sent out of the text object, puts them into a message object, then hits them with external data.

If the text object's output line contains a standard replaceable parameter (the symbols $1 through $9), the output will contain the replaced data. If, however, the output line does not have replaceable parameters, the result is the same as if I had sent a bang message to the object.

The basic framework for my resultant "sketchpad" is the following patch:

Click here to see a full-size version.

In addition to the text object support objects, I've also added all of the basic messages used with OpenGL Jitter programming, and also added a little section for generating generic contents (using counter objects).

Using JitterSketching

Let's use the JitterSketching patch to do a little bit of OpenGL programming. Double-click on the text object to open a text editor. Enter the following code:

reset
glortho -1. 1. -1. 1. 1. -1.
glclearcolor 0. 0. 0. 1.
glclear
moveto 0. 0. 0.
glcolor .5 0. 0.
sphere 0.2
glflush

Close the editor and run the renderer (by turning on the toggle checkbox at the top-right). You should see a red sphere drawn in the center of the display window. Click on the rendering options (the depth_enable, smooth_shading and lighting_enable) to see the effect on each.

Now, let's rotate the ball around a center point by adding a little processing. Double-click on the text object again and change the sketch information to:

reset
glortho -1. 1. -1. 1. 1. -1.
glclearcolor 0. 0. 0. 1.
glclear
moveto 0. 0. 1.
glrotate $1 0. 1. 0.
glcolor .5 0. 0.
sphere 0.2
glflush

We added only one new line - the glrotate line - while will cause a rotation around the Y axis. Close the text editor and restart the renderer. You will notice that the sphere moves, but isn't animated. That's because we haven't sent anything into the textmassage subpatcher to give it movement.

At the right of the patch is a section, driven by its own metro, that drives three counters. The first one will iterate from 0 to 360 - just what we need to feed the glrotate statement with "degrees of rotation". While the renderer is running, turn on this metro (by turning on the toggle connected to it). Now our sphere is rotating around the Y axis; you can verify this by turning on the "axes" message to jit.gl.sketch.

You can save the OpenGL program text file using the "write" message that is connected, or you can kill the patch and lose the work. While this patch is handy, it is much more useful when you modify the JitterSketching patch for a specific OpenGL program file. Let's look at an example of that.

Configuring JitterSketching it to a specific text file

Open the JSK-atom.maxpat patch. In this case, we've taken the JitterSketching.maxpat file and modified it to be specific to an OpenGL program file.

Click here to see a full-size version.

You will notice that the read and write messages have been changed to include the file name, and are driven by loadbang and closebang objects respectively. We've also used the loadmess object to preset the rendering options that are best for this file. This is a good example of taking a basic idea and making it specific - without losing the functionality and intent of the original.

Extending it with additional patching

Now open JSK-atom2.maxpat for a more extensive modification of our original patch.

Click here to see a full-size version.

Initially, you will recognize the main rendering engine from the previous patch. But now we've encapsulated the parameter generator in a subpatcher (the "parameters" subpatcher at the right of the patch), and we've made a bunch of textures to be used for the electrons of our atom display. The OpenGL text file (JSK-atom2.txt) is updated too, allowing for the camera position to change under programmatic control.

Conclusion

As you learn new techniques in Jitter, it is useful to have function-specific templates to use to get started. The JitterSketching patch is meant to be a playground for working with the jit.gl.sketch object, and for using standard OpenGL-like textual programming in your patching regimen.

Hopefully you will find this useful, and that it helps you explore new territory in your patching. If you have any questions or comments, you can feel free to contact me at ddg@cycling74.com. Thanks for listening!

by Darwin Grosse on August 31, 2009

okicomputer's icon

Hi does jit.gl.sketch object works in max for live?

Andrew Benson's icon

Yes, all Jitter objects work in Max for Live.

enricodetrizio's icon

Very nice! Is there any guide to the Open GL messages used by jit.gl.sketch?

Darwin Grosse's icon

The best place to look is in the jit.gl.sketch help patch. If you double-click on the subpatcher labeled "p gl_commands", you will get a lit of all the Open GL commands recognized by jit.gl.sketch. Most of these, by name, will relate to Open GL commands that you can find in the Red or Blue book.

jihannkki's icon

Excellent tutorial. Is there a way I can use more than the standard $1 to $9 replaceable arguements?

I'm building a kind of dynamic network graph and I'm trying to move 100 straight lines in two dimensions - acting as connections between nodes in the network. I'd like to be able to dynamically control each of their start and end points independently at the same time.

enki's icon

I only see a blank screen in myWindow for the JitterSketching.maxpat patch.

Never mind I realize i have to click read and choose JSK-atom* file.

max 8.6.1

OS 11.7.8