More Processing - Colorcube

More messing around with primitive graphics in Processing, this time using OpenGL which I haven't touched since 3rd year at University. It's a little frightening to think that was five years ago! I thought it would be cool to see what it would look like you were to use each of the Red, Green and Blue components of a pixel's colour as it's co-ordinates across the X, Y and Z axes and plot it inside a cube.

I'll spare you the details, but this was another little one-evening job - after manhandling horrible legacy VB code all day I've rarely felt like doing anything particularly brain intensive when I get home at night. Anyway my favourite is the image comprised of a number of individual images to make a little spectrum. You can see little tendrils of alike colours arcing from one corner of the cube (0,0,0 - Black) to the other (255, 255, 255 - White):

Annoyingly something's up with OpenGL for Applets on recent-ish JVM versions so I can only embed the Applet using Processing's slightly lame P3D software renderer, which mangles the colours beyond belief. It's so ugly I'm not going to bother uploading it and embedding it on this page. You can view the video further down the page or download the program itself at the bottom of the page (to see it in all its technicolour OpenGL glory), or if you're sadistic you can download it and use the P3D renderer. Wash your eyes afterwards though. Anyway if you do download it, here are the keys to manipulate what's on screen:

Key(s) Function
←, ↑, →, or ↓ change the cube's angle
=/- reduce/increase the amount of points on display (if your frame rate is too low)
n cycle through the images

increase/decrease amount of points per "shape"

The last function is a little odd, when I was having trouble with performance I started looking for ways to improve speed. After a couple of straight forward ones (no plotting duplicate points, precompute the co-ordinates rather than calling red(), blue() and green() on each point each frame) I noticed that I was creating a new shape for each vertex (i.e. beginShape(POINTS); /* add vertex */ endShape();). After a bit of playing around it turns out that the sweet spot is somewhere between 100 - 1000 points vertices per shape (this is a little vague as I've not been particulary scientific about it).

Here are some videos of a few of the images together with their rotating colorcubes. They're a tad jerky due to the capture software, it's a lot smoother when you run the actual software.

You can download the source code here, just load the PDE in Processing and run (the images used in the video demo are contained within so you don't need to fiddle around with anything).