John Tsiombikas email@example.com
30 October 2009
An anaglyph is a combination of two images into one, in such a way that they can later be separated by viewing the image through appropriately colored transparent filters. The objective is to present slightly shifted views of the same 3D environment to each eye, in order to achieve depth perception (i.e. really perceive the 3rd dimension).
I've never dealt with anaglyphs in the past, but during my recent week-old obsession with stereoscopy, I've stumbled upon a pair of free anaglyph viewing glasses (made out of cardboard and cellophane of course). So I couldn't help but try to find out how I can use them with my own programs.
I dug up a small dungeon editor I've written some time ago, and hacked stereoscopic rendering into it. I implemented various stereo rendering modes including cross-eyed (side-by side images), quad-buffered (the native OpenGL stereo mode), and anaglyphs.
Quad-buffering is of course the ideal method to present stereo views in OpenGL, but it requires support from the graphics driver and expensive hardware such as shutter glasses or a pair of polarized projectors and polarized glasses.
The problem with the glasses that I've got is that all articles regarding anaglyphs I've found on the internet talked about either red/blue or red/cyan glasses, while mine was obviously neither. It turns out my glasses are for a new variety of anaglyphs which go by the name ColorCode3D and are supposed to be able to keep more color information than previous red/blue and red/cyan methods (which generally only work for grayscale images).
This color-code thing is apparently patented and there's no mention anywhere on how to combine stereo images for viewing with color-code glasses. The company who owns the patent has released a program for converting stereo pairs of images to colorcode anaglyphs, which is of course proprietary and doesn't come with source code, ergo useless.
Then I remembered that there's an upside to a patented algorithm if you choose to completely disregard patent law, which I definitely do. In order for an algorithm to become patented, it must be described in detail in a patent application which must be filed at the patent office. All such patent documents are publicly available at the web site of the US patent office. So I just got the patent document, and implemented the algorithm from that description.
It turns out the process is extremely simple, although just outside the reach of the OpenGL fixed function pipeline, which can be used to handle red/blue and red/cyan anaglyphs easily by employing additive blending and color masks. To combine the left/right stereo pair into a single color-code compatible image:
Here's the small GLSL program I used to combine the two left and right images, previously rendered into two textures:
uniform sampler2D tex_left, tex_right;
vec3 left = texture2D(tex_left, gl_TexCoord.st).rgb;
vec3 right = texture2D(tex_right, gl_TexCoord.st).rgb;
vec3 col, coeff = vec3(0.15, 0.15, 0.7);
col.r = left.r;
col.g = left.g;
col.b = dot(right, coeff);
gl_FragColor = vec4(col, 1.0);
The result is fantastic, I'm using the space navigator to navigate in the 3D dungeon without taking my eyes off the screen, and the stereoscopic effect is very evident indeed, creating the illusion of depth quite effectively.
Here are a few screenshots in both color-code and the more common red/cyan anaglyph variety.
The second one with the jutting pillar is particularly nice. The geometry crosses the zero-parallax plane into the negative subspace, making the pillar seem to "come out of the screen".
This was initially posted in my old wordpress blog. Visit the original version to see any comments.
Discuss this post
Back to my blog