You can join the our mailing list by entering your e-mail address below. A reply will be required for verification.

Back to White Papers

Creating Anaglyph Images in Java

by Kevin Neelands, April 2006

This document explains the algorithms used to create the anaglyph graphics displayed in the 3D Cartesian coordinates demo and the molecules applets.

Two programs that utilize these techniques are Conic Sections and 3D Tempest. You'll need red/blue glasses for the 3D effect.

Anaglyph images are graphics that utilize the red/blue glasses to appear 3 dimensional, similar to cheesy 1950's horror flicks. One image is created for one eye and colored red while a slightly different image of the same object is colored blue for the other eye. When the brain combines the two images they appear 3D. This is useful in educational programs for 2 reasons... "

  1. Demonstrating and explaining 3 dimensional concepts difficult to illustrate in 2 dimensions.
  2. The 'Gee-whiz' effect that may help maintain and pique a students interest in the remainder of a lesson.
As an example, compare the diagram of nicotene on the left, and the 3D applet on the right.

The images on the left are what you may find in a chemistry book. Many students would not be able to glean the true structure of the molecule from these diagrams, and many would find them just plain boring. The applet on the right, however, gives an excellent representation of the chemical.

To create two different images of the same object, the method that first comes to mind is to mimic natural eyesight and chose a common focal point for both eyes, as shown in this diagram.:

The top view shows two eyes looking at a pyramid, the front view shows the image that results when the two views are merged.

Another way to get the same effect is to have the viewpoints created with the as if the eyes are looking parallel with each other and no common focal point. When the component images are created in this fashion, the horizontal offset between them can be altered when they are merged with the effect ofthe object moving out of the screen towards the viewer or into the screen away from the viewer. This is called HIT, Horizontal Image Translation.

Initially it would seem that both approaches require twice the amount of rendering and twice the amount of data as a simple 'flat' 2D rendering. However, the latter method can let us create anaglyph images in a mathematically simple and elegant way. Converting 3 dimensional points to 2 dimensional points for on screen display involves scaling and translating points from the 'data' realm to the 'display' realm. At first glance it appears each method requires calculating 2 sets of data in the data realm and converting to the display realm, involving a good bit of calculation and trigonometry. We'll see how the conversion to anaglyph display can be done after the points are in the display domain, and how easy it is.

First, let's look at simple single point perspective.

Run the 3D Cartesian coordinate system and turn perspective on, but leave 3D off. Move the Perspective slider left to right.

The math here is probably obvious - an arbitrary distance along the Z axis is chosen to represent the vanishing point, and each X and Y coordinate is moved toward the center of the display area somehow proportional to that points Z coordinate. Note that a screen Z coordinate had to be calculated, even though it is not used directly in the display. These next two images display the measurements used to perform these calculations.

In the above image we are looking 'down' at the bounding box of the display area with a pyramid in it. Looking at the point that is the far right corner of the base, its X coordinate is MidX+dX1. B is the distance along the Z axis from the vanishing point to our reference plane, and C is the distance from the vanishing point to our point of interest. Calculating the new X coordinate uses the C/B ratio to move X and Y coordinates towards the midpoint of the display, mimicing one-point perspective.

This image shows the base of the pyramid altered after perspective is applied.

 
	The original X coordinate is 
	X1 = midX + dX1 
since we are moving towards the center an amount directly proportional to the distance from the vanishing point along the Z axis,
	dX2/dX1 = C/B = R
	dX2 = R * dX1

	X2 = midX + R*dX1 

converting these equations to pseudo-code; For each point,

	   R = C/B  
	   X2 = MidX + R*(X1-midX)
	   Y2 = MidY + R*(Y2-midY) 
Note that if a point lies on the reference plane the ratio R becomes 1 and the new coordinates are the same as the old.

The neat thing is, the coordinates for the red and blue portions of the image can be calculated in an equally unsophisticated manner.
But before we get into that, let's take a look at the application of HIT - Horizontal Image Translation. Turn Perspective off and 3D on, and move the Perspective slider from side to side. Notice how the two component images remain the same, the only thing that differs is their horizontal offset from each other. But this has the net effect of seemingly moving the object into and out of the screen. Those locations where the points from the red and blue components are the same will appear to lie directly on the screen.

The display coordinates for the left and right images differ from the original coordinates only in that the X value is shifted to the left or right some number of pixels. Turning 3D on and off will demonstrate this. Similar to the one point perspective calculations, this amount is proportional to the ratio of the distance from the points Z coordinate to a reference plane to the distance from the vanishing point to that reference plane.
for each point,

	c = k * a/b 
    leftX = oldX - c 
    rightX = oldX + c 

Note that if a points lies on the reference plane the ratio a/b becomes 0, and the new coordinates are the same as the old. where k is an arbitrary value that scales this ratio to an aesthetically pleasing distance. Generally if the left and right X coordinates differ by more than 30 pixels the eyes will have difficulty reconciling the component images. To see the effect of altering this k value, turn perspective off, 3D on, and move the L/R separation slider.

It is worthwhile to note that points that lie on the reference plane will have no perspective applied and the point for the left and right images will coincide, so that plane will appear to lie directly on the screen. This, combined with the fact that the two views were created with parallel points-of-view, allows a plane perpendicular to one of the X,Y, or Z axis to be viewed undistorted - handy for those cases where it is necessary to view a 'slice' of a 3D view, such as when you need to examine what happens when X = 2;     For real fun turn both Perspective and 3D on, click both Spin buttons, and play with the sliders.

I hope this discourse has shown that often, if you don't go with the methods that seem obvious, you can do things simply, elegantly and easily. And cheesily.

A more thorough explanation of these concepts is available in a white paper written by Lenny Lipton, founder of stereographics, Stereo3D Handbook is downloadable from Stereographics.com.

Back to White Papers

About the Orlando Java Users Group Minute by minute of past meetings resources Java Breaking News WhitePapers Calendar contact us! start at the top!