In Part 1 I discussed an simple shader which generated autostereograms from depth maps. The next step was to make the application interactive, so that instead of generating an autostereogram from a static depth-map, an interactive 3D model could be used to generate dynamic autostereograms.
To create a depth-map from a model, I added a pixel shader to return the depth value. A similar example can be found in How To: Create a Depth Texture. In order to obtain a sensible range of depth values, I need to calculate the bounds of the model using bounding spheres. I can then set the near and far planes in my projection matrix to correspond to the closest and furthest points in the model respectively. The depth texture can be extracted by rendering to a render target. It can then be passed as a shader parameter to allow autostereograms to be rendered in real time as the model is manipulated.
One of the main issues with animating an autostereogram is that manipulating the model results in changes across areas of the image which do not correspond to the location of the model itself.1 One way around this distracting side-effect is to use a SIRDS, or Single Image Random Dot Stereogram, and alter the pattern each frame in which the model is manipulated.
Instead of passing a pre-defined pattern texture, I generate a random texture on the CPU as follows:
private Texture2D CreateStaticMap(int resolution)
Random random = new Random();
Color colors = new Color[resolution * resolution];
for (int x = 0; x < resolution; x++)
for (int y = 0; y < resolution; y++)
colors[x + y * resolution] = new Color(new Vector3((float)random.NextDouble()));
Texture2D texture = new Texture2D(GraphicsDevice, resolution, resolution, 1, TextureUsage.None, SurfaceFormat.Color);
The resultant grayscale image is shown below in Figure 1.
Figure 1. SIRDS for animation