Anaglyphic 3D in GLSL

I’ve been playing around with getting some red/cyan stereoscopic 3D working of late, and it’s all turned out rather well – take a look… (Red/Blue or Red/Cyan glasses are required for the effect to work):

Anaglyphic 3D in GLSL
Click for bigger version.

If you’ve got suitable glasses you should definitely see a 3D effect, although I don’t think me rescaling the image has done it any favours – so click the image to see the full sized version for the full effect.

The trick to this has been to render the scene twice to two separate FBO textures, then sample from the left and right textures to draw a fullscreen quad with a combined version as follows:

// Fragment shader to perform Analyphic 3D conversion of two textures from the left and right eyes
#version 330
uniform sampler2D leftEyeTexture;
uniform sampler2D rightEyeTexture;
in vec2 vTexCoord;
out vec4 vFragColour;
void main(void)
	vec4 leftFrag = texture(leftEyeTexture, vTexCoord);
	leftFrag = vec4(1.0, leftFrag.g, leftFrag.b, 1.0); // Left eye is full red and actual green and blue
	vec4 rightFrag = texture(rightEyeTexture, vTexCoord);
	rightFrag = vec4(rightFrag.r, 1.0, 1.0, 1.0); // Right eye is full green and blue and actual red
	// Multiply left and right components for final ourput colour
	vFragColour = vec4(leftFrag.rgb * rightFrag.rgb, 1.0); 

In the code itself, the torus’ spin around on the spot and look pretty good, although there’s no anti-aliasing as yet as I need to create some multisample buffers instead of straight/normal buffers for the FBO, but it’s not decided to play ball just yet – not to worry though, the hard part’s 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 same amount of depth as traditional anaglyphic techniques. Also, I’ve got to start using asymmetric frustums for the projection to minimise the likelihood of eye-strain, but I don’t see that as being too much of a problem.

Good times! =D

14 thoughts on “Anaglyphic 3D in GLSL”

  1. After trying out various anaglyph glasses, I believe red-cyan are the best. Colorcode try to retain more color, but I find the extreme difference in brightness levels between the left and right eyes very tiring.

    1. I never had any ColorCode glasses to try it out with, although I did try with Green/Magenta TrioScopic glasses – and, well, it works – but I wouldn’t have been able to call one over the other, they were just different. Which reminds me that I should prolly post up a final multisampling buffer version with anaglyph cycling options… But regardless, I guess colour reproduction is always going to be dreadful with any anaglyphic method.

      Thanks for your anaglyph post, btw – I found it really useful in getting my own stuff up and running. Much appreciated.

    1. The non-anti-aliased code works fine so I could post that up, but the multisample buffer version complains at one point (even though it works and runs as expected) – lemme take a look this evening and I’ll post something up, even if it’s just the non-AA version.

      1. Alright – try this:

        It’s a Code::Blocks project and I’ve fixed it so that it doesn’t moan about allocating multisampled buffer storage – keys are W/A/S/D and mouse to move around and F1 to maximise/restore window size.You’ll also need red/cyan 3D glasses ;)

        This project is in desperate need of refactoring into classes – as it stands it’s all just mashed together so I could get something up and running in the quickest time possible. Unfortunately, I just don’t have the time to refactor it at the moment, but when/if I do I’ll post an updated version. Also, it doesn’t use asymmetric frustums so it’s quite likely to cause some eye-strain if used for long periods – so use at your own risk!

        The best thing to play around with is probably the eyeSeparation value defined on line 54 of main.cpp – larger values increase the separation to increase the 3D effect and make objects appear infront of the screen (up to a certain point where your brain just won’t gel the two images together anymore) , but even small values where the “front” is at the physical screen’s location look pretty good!

        Have fun…

  2. hey friend! I have a question… what are the combinations for the left and right frags if i want to make this work for:
    as I can see, the one in your code works fine for red/cyan… but what about this other 2?
    sorry for my english, thak you.

    1. I’ve used the following for trioscopic 3D (green/magenta):

      // Final colour output
      vFragColour = vec4(rightFrag.r, leftFrag.g, rightFrag.b, 1.0);

      And I believe the following will work for ColorCode 3D:

      vec3 coefficient = vec3(0.15, 0.15, 0.7);
      // Take the dot product of the right-eye colour components and specified coefficients
      float mixedRightFrag = dot(rightFrag.rgb, coefficient);
      // Final colour output
      vFragColour = vec4(leftFrag.r, leftFrag.g, mixedRightFrag, 1.0);

      There’s a good article which shows how do do all this and also remove/minimise the ghosting effect here:

  3. Nice work. Has there been any improvements to the code since originally posted? If so, is updated source code available for download?

    I am rendering a snowfall scene in monoscopic view. Any suggestion on extending it to stereo? Not sure if FBO will give a realistic animation of a particle system. Any feedback is much appreciated.

    1. Further, extending your snow scene to stereo should look great, and twin FBOs would be the only way to really give that effect if you’re not aiming for VR.

      Ping me a link to it sometime if you get it going!

    2. I just looked back at my linked code and it’s appalling.

      I’ll aim to put together something saner within the next week. No promises, but I’ll do my best.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.