Barrel Distortion

This Generator implements the Barrel Distortion algorithm, potentially for use with an Oculus Rift or Google Cardboard.

I do not have an Oculus Rift but I did test Camera3D and Processing using Google Cardboard on my Samsung S6 phone. Unfortunately it is ridiculously slow. Camera3D works by copying images back forth between the GPU and the CPU, and for whatever reason that is very slow on my phone. Your experience might be different. Other approaches combining Processing and Google Cardboard may work significantly better. If the speed issues are not solved then this should be used for demo purposes only.

Here's a great video on what barrel distortion is and why it is important.

Start with the same right and left images:

four cubes with black edges. Going clockwise from the upper left, the cube faces are red, green, gray, and blue.four cubes with black edges. Going clockwise from the upper left, the cube faces are red, green, gray, and blue.

Distort each image. Here is the left component:

partial view of the four cubes on the left side of the image.

and the right component:

partial view of the four cubes on the right side of the image.

Notice that it does not squish each image to make all of it fit in the frame. Only the middle section is visible. Typically you would want to make the size of your sketch a wide rectangle, not a square as we are doing here. Keep the part you want people to look at in the center because the left and right 25% will be gone.

Add the two images together, yielding:

two partial views of the four cubes on the right and left sides of the image.

Adding a label to the result with the postDraw method doesn't make sense for this, so skip that part.

By default the Barrel Distortion Generator is configured to the distortion coefficients for an Oculus Rift. You can easily change this to something else if you like using the setBarrelDistortionCoefficients method, as demonstrated in the example code.