## OpenGL stereoscopic anaglyphs and patents

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:

• The red channel of the left image is used as the red channel of the output image.
• The green channel of the left image is used as the green channel of the output image.
• All three color channels of the right image are converted to grayscale as follows: $\left(r\ g\ b\right) \cdot \left(0.15\ 0.15\ 0.7\right)$, and are used as the blue channel of the output 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; void main() { vec3 left = texture2D(tex_left, gl_TexCoord[0].st).rgb; vec3 right = texture2D(tex_right, gl_TexCoord[0].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 of screenshots in both color-code and the more common red/cyan anaglyph variety (note that I don’t have red/cyan glasses, so I can only assume that the red/cyan rendering works correctly, I haven’t tested it).

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”.

References

### 7 Responses to “OpenGL stereoscopic anaglyphs and patents”

1. […] is, I couldn’t settle with my crummy anaglyph glasses see previous post. I had to try out proper shutter glasses and quad-buffer OpenGL […]

2. Chris Says:

I tried to get colorcode with my application, but it
doesn’t work. Have somebody a code example for me?

3. […] done and I’m sure multisampling will be sorted in a day or so. After that, I might give ColorCode3D (TM)(R)(C)(Blah) a go, as it seems to give a better colour representation whilst still allowing the […]

4. […] fun never stops with stereoscopic rendering. I posted previously about my earlier attempts with anaglyphs and shutter glasses, and all that was really fun, but not without drawbacks. Shutter glasses are […]

5. caramish Says:

hi, first, I find this very great! I would like to know what are the combinations in the frag shaders and wich colors should I take for the red/cyan, red/blue and red/green glasses. Thank you!