Seeing through walls with OpenGL depth buffer

Tags

, , , , , , , , , , , , ,

‘Check out my X-ray specs,’ The Clown said, tearing the plastic glasses off the front page of a children’s comic.

‘Screw that,’ I replied, honking the horn on his lapel, ‘Try out my Z-ray specs.’

The Clown put on the Oculus Rift virtual reality headset and I cranked up my C++ Microsoft Visual Studio application (with OpenGL graphics library and the Oculus SDK for Windows).

Here he is, standing in the virtual room:

But when he presses up against the walls he can see through them:

The wallpaper is semi-transparent, letting him see the mountains and sea beyond the walls of the room.

It’s all to do with a bit of blending, as outlined in my post Transparency in OpenGL.

But the not-so-casual reader might ask, “How come the walls are only transparent when you press up against them?”

Good question, my not-so-casual reader.

Learn OpenGL has a top article on depth testing. The article explains how to access the depth buffer Z value in the fragment shader, for each pixel we render to the Rift headset.

So instead of the fragment shader rendering the virtual room with an alpha value of 1.0, which means no transparency…

FragColor = vec4(result, 1.0);

…it uses the depth buffer Z value for the alpha value, which means some transparency.

FragColor = vec4(result, gl_FragCoord.z);

And since the depth buffer Z value will only be small for the objects closest to The Clown, the walls will only be transparent when he presses up against them!

The Clown was so impressed he lit up a comedy cigar, which duly exploded on his face with the first puff.

Ditch the bondage of X-Ray Spex.

Ciao!

P.S.

Here’s a video of the walls becoming transparent as The Clown walks up to them: