Anaglyph 3D scene

The Processing P3D library is excellent for creating and navigating 3D scenes, but for a really immersive virtual reality experience you may want to try using specialist goggles. However, that can be expensive and you can get almost as good a result using anaglyph glasses: the ones with a red filter for your left eye and a cyan one for your right eye. The question is, how do you go about creating an anaglyph image in Processing?

I've been playing with creating anaglyph images using Processing, on and off, for around a year now, with varying success. The method I've been adopting is basically to use the Processing camera function to change the view position in a 3D scene. Having created an image for the left and right eye views I then used them to create the anaglyph image by combining red pixels from one view, with blue and green pixels from the other view. The two eye views are drawn to offscreen images, and only the anaglyph is rendered onto the Processing window. An example is shown in the applet below.

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

The code (which you can download by clicking here) is relatively simple, although you may not think so on first reading it! If you look for a while you'll see that it's very similar to a normal P3D program, except that the drawing commands are being done on offscreen images. The draw() routine just calls the scene() routine for left and right view images, combines them, and draws the resulting anaglyph image. If you don't like the amount of offset between the red and cyan images, you can use the left and right arrow buttons on your keyboard to change it (you may need to click the applet with your mouse to give it keyboard focus first). The code isn't perfect, but if you improve on it why not tweet @geocomputer and let the world know about your anaglyph creations?