Stereo graphics: making images with depth. (part 1) John D. Fowler Jr..
Remember 3-D movies? I still remember slinking down in my seat at the local theater, fearing that if those bats floating out over the audience didn't get me, the falling rocks would. And then there were 3-D comics with the funny red and green glasses. The pictures seemed to stand out from the page. How did they work?
Our ability to perceive depth in the world around us stems primarily from two factors: perspective and the relative angle of view between the viewer's eyes. An example of the former is the apparent convergence of parallel lines which extend to the horizon. We are conditioned to believe that the lines really don't get closer together but rather are extending away from us in depth. This use of perspective is what allows pictures of objects projected onto a flat page or screen to give the illusion of depth.
But another and more convincing sense of depth comes from the positioning of the eyes. If you must cross your eyes to focus on something, then you know it must be very close. This is why there can be no doubt in your mind when you see a 3-D movie that it is different from a regular movie, even though both are projected onto a flat screen.
The difference between perspective and depth perception in pictures is that perspective requires only one image, but it takes two separate images to create a really convincing sense of depth. This is directly related to the fact that you have two eyes in the front of your head. This ability to perceive how far away an object was proved to be a great headache-preventer for our tree-dwelling ancestors.
If you want to create an image, a mirage if you will, out of flat paper, then you must do two things: draw two images in perspective, and assure that each eye sees only the appropriate image. In 3-D movies this is accomplished by projecting both images onto the same screen, in more or less the same place.
For color movies, two projectors are used. The images are sorted out by the use of polarizing filters over the projector lenses and similarly-oriented filters in the glasses you must wear. The light coming from the right projector, for example, is horizontally polarized and the light from the left is polarized verically. Then the filter over your right eye, which is horizontally polarized, passes light only from the right projector and that eye sees only the image intended for it. The rest of the light on the screen shows up in your left eye and, voila, each eye receives the intended image. Your brain, working on these images. notices the similarities (and vital differences) and forces you to duck when those bats come fluttering out ovr the audience.
If you put the glasses on upside down, so that the images go to the wrong eyes, you can still see the picture, but the essential depth information no longer corresponds to experience. The picture looks strange in a way that is difficult to describe, and no longer gives the proper illusion of depth.
Non-color 3-D movies and comics work on the same principle, but use red and cyan (sort of greenish blue) colors and filters. Red and cyan are complementary colors. A red line can't be seen through a red filter (because it looks just like the red background), but a cyan line looks black. And viceversa for the cyan filter. So one yee gets the red image and the other eye the cyan.
The real problem in making your own stereo pictures with a computer comes in getting the proper impage to the proper eye. Several possible methods will be discussed.
If you have two slide projectors and the equipment to photograph your display, you can buy some polarizing filters and do it as in the movies: properly oriented filters over each lens with corresponding filters over the eyes. Or, if you are satisfied with red and cyan images and want to experiment with different colored pens and filters, you can do it that way. But beware: light blus pens are hard to find. Colored and polarizing filters can be purchased at a photographic supply store.
Another approach is to use lenses. You have probably seen the stereo viewers on the market. They have two lenses through which you can view images from disks containing small pieces of film which are inserted into the viewer. You must make the center-to-center distance of the images less than the distance between your eyes. This means that the images must be rather small. A viewer can be made by placing a small magnifying glass in front of each eye.
There is another method which requires no extra equipment at all. The drawback to this method is tht it takes a degree of voluntary control over the eye muscles which some people lack. If you can makeyour eyes diverge slightly (the opposite of crossing them), you can try pointing your left eye toward the left member of an image pair and the right eye towards the right image. If your can still focus on the images, you've got it made. You also have a rere talent because very few people can do this.
A variation of this method is to cut out the images and reverse them, so that the image intended for the left eye goes on the right. The trick now is to cross your eyes slightly so that the left eye sees the right image, and viceversa. One aid in doing this is to cup your hands a few inches in front of your face and look through the hole. Or you can cut a rectangular hole in a piece of cardboard and view the images through it.
The problem that most people encounter with these methods is that there exists a semi-voluntary coupling mechanism between the interocular muscles (the ones that control the relative angles of your eyes) and the muscles which focus the eyes. You must focus on an image (really two images) which are farther away than your interocular muscles are telling your brain they should be. Some people can do this and some can't. If all this leaves you dizzy, then go on to the next method, which works for almost everyone.
Perhaps the best way to view stereo images is through an inexpensive viewer made from cardboard and mirrors. To construct the viewer, you will need four small rectangular mirrors (approximately 1 1/2 X 2 ), some sturdy cardboard, double-stick tape, and blue. The pocketbook mirrors, which can be found at a variety store, will do nicely. You may have to remove the plastic cases by boiling them to loosen the glue. Heavy-duty photographic mounting board works well for building the frame. In addition, you will need some small strips of wood or plastic for sturdiness and to form the necessary 90 degree angles. I used decorative molding strips for this.
Construct the viewer, as shown in Figure 1, by first cementing the rear short sides of two of the mirrors to a piece of molding strip to form a rightangle V. Next glue a piece of molding strip to the back side along one of the long edges of each of the other two mirrors. While the glue is drying, you can cut two pieces of cardboard to the dimensions shown in Figure 1(a). Transfer the lines, as shown in the figure, to one of the pieces, then put down a layer of double-stick tape.
After the glue has dried, take the middle pair of mirrors, and align them with the V you have drawn on the cardboard base. Place the outer two mirrors along the other two lines on the base, with the reflecting sides facing each other. The molding strips you glued to the bases of these outer mirrors should be against the tape, holding them in place.
Now you can pick up the viewr and look at the stereo figures which accompany this article. You will probably have to make slight adjustments in the mirror positions to get the images to line up and appear as one. This is the reason for using the double-stick tape. Take your time and get the mirrors just right, so that you comfortably see a single 3-D image. (You will also be able to see other images from unwanted reflections, but we will take dare of them shortly.)
When everything is lined up, put on the top piece of cardboard and glue the mirrors in place. Finally, as shown in Figure 1(b), you should apply cardboard wherever possible to the four sides of the viewer to keep out extraneous light (without, of course, obstructing the view of the images you want to see).
In what follows, we will go through the steps required to write your own programs and make stereo images like those shown here. I will give you the subroutines and main programs you will need, except for the plotting foutine.
The statement GOSUBPLOT probably won't work on your system. In place of PLOT you must insert the line number for your plotting subroutine. In most cases, the plotting subroutine will be quite simple: a command to draw a straight line from one point to another, followed by a RETURN statment. Variables used in the listings are defined in Table. 1.
We will be considering three objects: the viewer, the object to be displayed, and the display itself (monitor, plotter, or printer). To establish a fame of reference, we will create a coordinate system with its origin in the center of the display. The X and Y axes are in the plane of the display, with X increasing to the right and Y increasing upwards. The Z axis comex out of the display toward the viewer.
As shown in Figure 2, the axes are labeled with capital letters, while the coordinates of objects use lower-case letters. (Only capital letters will be used in the listings.) Viewer coordinates are (x, y, z), coordinates of points in the object to be plotted are (a, b. c), and points on the display surface are labeled (m, n, o) or just (m, n) since the Z coordinate always equals zero on the display surface.
For the time being, let's content ourselves with producing a single image on the display. (After this has been accomplished, it will be easy to produce two images, one for each eye.)
Pretend that you are holding a pencil in front of your display and that when you remove it, you want an exact image of the pencil as you saw it to remain on the display. To do so, we must draw an image on the display, corresponding to the original "real' image of the pencil, which was displayed on the backs of your eyeballs. When the pencil is removed and only the display remains, we want the image on the back of your eye to remain unchanged.
This can be done by thinking of the pencil as being made up of a bunch of points each of which is transferred, one at a time. For each point on the pencil, we want to set the corresponding point which is directly behind it on the display. This point on the display can be found by extending a straight line from the viewer's eye, through the point on the pencil, to the display. Do this for each point on the pencil, and when you remove it your eye will see the identical image on the display. (Note in what follows that th pencil could just as well be behind the display as in front of it.)
What we have to know to get the computer to do this, is a mathematical relation between the point at which the viewer exists, the points whih make up the pencil, and the corresponding points on the display. Figure 2 shows a stereo view of just one point and the projection from the eye through the point to the screen. Looking down from above, as in Figure 3, we can see that for X coordinates the needed relation between viewer, object, and display is expressed by the equation:
(1a) m@+(a-x) /(z-c).
Likewise, if we were to view the figure from the side, it would be apparent that for Y coordinates the equation is:
(1b) n=b+(b-y) /(z-c).
And that's all there is to drawing perspective images. You put in a coordinate for the viewer (x, y, z) and a series of coordinates for the object (a, b, c) (or let the computer calculate them), and then plot the resulting m and n (horizontal and vertical) numbers on the display.
That was so easy, let's try something else before getting to the stereo part. How about rotating the object before plotting it? Once again, since objects can be thought of as sets of points, we need only consider how to rotate a single point about each of the axes.
In Figure 4, we see the result of rotating a point about the Z axis. Using a little trigonometry, and calling the rotated point (al, bl, cl) we get:
a1 = a* cos - b* sin ,
b1 = a* sin + b* cos , and
c1 = c.
If we now use a1, b1, and c1 in equations 1(a) and 1(b), instead of a, b, and c, the projection of the object onto the display will be rotated by the angle .
To rotate about the X and Y axes, you do the same things to the b and c coordinates or to a and c. Putting all of this together, we arrive at the Master Rotaion-Perspective Subroutine which starts at line 1000 in Listing 1.
Since this subroutine will be executed a great many times, it is desirable to make it run as fast as possible. Thus, the since and cosines of the rotational angles are calculated only once, near the beginning of the program.
There are several other neat things you can do with this subroutine. Notice the variables Q and W in Listing 1. These can be used to displace the image on the display. For instance, if the origin of coordinates on your system is in a corner instead of the middle, you can insert appropriate values for Q and Q to move the display point (0, 0) back to the center.
The TRS-80 Color Computer has the coordinate origin in the upper left corner of the display, with a range of m from 0 to 255 and n from 0 to 192 in the highest resolution mode. Setting Q=128 and W=96 causes the point (0, 0) to be plotted in the center of the screen. So you can use Q and W to position the image on the display. On some systems, the Y coordinate on the display is positive downward instead of upward. To keep things from coming out upside down, you can change line 1080 to:
1080 N=-(BB+(BB-Y)) Z+W.
Another operation, similar to moving the projection of the object around the display, but not quite the same, it transalation of the object itself before the projections (m, n) are computed. This corresponds to actually moving the object to a new position. to do this you must add the statement:
to Listing 1. Then a GOSUB 1000 will calculate the rotated coordinates of the object only. You can then add or subtract displacement coordinates to the rotated coordinates (aa, bb, cc) and
Now that you know how to plot images of points (a, b, c) in perspective, as seen from a view position (x, y, z), you are ready to make stereo parirs of images. The only thing left is to plot the same object from two view points, one for each eye.
Usually, if you are positioned directly in front of the display, the (x, y, z) coordinates of each of your eyes will be given by (es/2, 0, zd) for the right eye and (-es/2, 0, zd) for the left. The variable es is the distance between your eyes, and zd is your distance from the display. The numerical values for these variables will depend on the plotting units you use. We define one plotting unit as the distance between two adjacent points (pixels) on your display, separated by the minimum distance of resolution. As an example, my plotter has a resolution of 0.005 inch, so this is one plotting unit. Since I normally view these plots at a distance of about one foot,
My eyes are about 2 1/2" apart, so:
In practice, I find a value of about 200 to be more pleasing. Making es larger enhances the stereo effect, but if it gets too big, the illusion no longer looks real. Making zd smaller corresponds to getting closer to the display.
The lines in Listing 2 can be added to those of Listing 1 to make stereo pairs of images, the image for the left eye occupying the left half of the display. To reverse the images, change
line 15 to:
15 X=XRES/20: Y=0:
The variables XRES and YRES are the number of plotting units in the X and Y directions, respectively. These will vary from system to system. I have used units appropriate to my system. Some of the other variables are defined in terms of these two and you may have to experiment to get them just right for your system. In many systems, the X plotting unit is of a different size than the Y plotting unit. To make them the same, you will have to scale (multiply) one of them by the ratio of the two. Doing this will make circles come out round rather than like ellipses.
Listings 1 and 2 combined enable us to plot stereo images of anything which can be described as a set of (a, b, c) points. These points can be calculated by the computer or input as data.
Now that we have both ends of a program, let's fill in the middle with some listings to make the drawings shown here. By the time we get through these, I am sure you will have some good ideas of your own.
If your plotting device is a video display, the resolution will probably be inadequate to show all the detail in these drawings. You may have to modify the listings to draw fewer lines. You can, of course, plot them from any viewpoint and rotation. With a video recorder, you could probably even make an animated sequence, one frame at a time.
Remember that GOSUB PLOT refers to your plotting subroutine, which simply draws a line from wherever it is now, to the point (m, n), using the color number of the PEN variable. PEN=0 means to move to the new point without drawing. The drawings shown here use up to seven different colors.
The listings which follow must be combined with Listings 1 and 2 to make an executable Basic program.
Listing 3 contains the program lines to make a sphere (Figure 5). The listing is for the high resolution sphere of Drawing 1a. Lines of longitude (from pole to pole) are drawn by lines 120-220, and latitudes are made by lines 230-330. Increasing the STEP size in lines 130 and 250 causes the spheres to have coarser resolution. Eventually they don't look like spheres at all. If the high resolution sphere looks ellipsoidal on your display, you need to scale the plotting coordinates, as described previously.
A mobius strip is a figure with just one side and a single edge. You can make one by cutting out a strip of paper, rotating one end by 180~, and taping it to the other end. To demonstrate that it has just one side, draw a line down the middle of the strip. Keep drawing until you end up where you started. You will be able to see that the line you have drawn covers both "sides' of the strip. Since you didn't go over an edge, the strip really must have only one side.
Listing 4 contains the lines for the mobius strip shown in Figure 6. Lines 100-230 draw in the colored bars which form the surface of the strip. The edge is drawn by lines 250-320. Notice that the rotation of the strip, generated by the second term of the variable A and by C, contains the factor I/2. This causes the 180~ twist in the strip.
"String Art' was part of the pop art fad of a couple of decades ago. A string art creation is made by nailing a set of pegs in a board along a predetermined set of lines and then running a length of string from peg to peg in a pattern. Listing 5 generates 3-D stereo string art (Figure 7). These patterns are also known to electronics engineers as Lissajous figures, which are created by using harmonics of sine waves to generate the a and b object coordinates. Of course, we will throw in a component in the Z direction for depth.
To make different figures, try varying the STEP size in line 100 and the arguments of the trig functions in lines 110-130.
Figure 8 illustrates the possibility of creating symmetrical patterns by drawing a segment and then rotating it sufficient times about the Z axis to make a circular pattern. Listing 6 creates the figures shown in Figure 8. The original figure is drawn in a 60~ angular segment. This is rotated by 60~ five times to complete the drawing.
The dodecahedron of Figure 9 extends this concept to translation as well as rotation. It consists of a single pentagon which is rotated and translated five times to make the top half of the figure. The bottom six sides are not drawn, giving the illusion of hidden line removal. Part 2 of this series will include a detailed consideration of hidden line removal.
As with most string art, the object appears to contain curved lines, even though only straight lines are used in drawing it. Because the generation of the dodecahedron requires rotation, translation, and then another rotation, extensive modification of Listing 1 is necessary. Listing 7 contains the entire program for generating the dodecahedron.
Finally, I leave you will the blown dandelion of Figure 10. The RND (random) function was used in generating the coordinates for the fuzz, so this one comes out different each time it is drawn. The one you see here will probably never be duplicated. If you want a listing for this program, you must first blow off the rest of the fuzz, then send me an SASE for the listing. So go ahead. Make a wish and blow. Then make some nice pictures of your own. And watch out for those bats at the movies. See you next month when we will discuss the creation of mesh plots of functions of two variables and hidden line removal.
Table: Index of variables used in listings.
Table: Listing 1. Rotation-perspective subroutine begins at line 1000.
Table: Listing 2. Additional lines which should be added to Listing 1 to produce stereo pairs of images. Any variable which depends on XRES or YRES may need slight changes of different systems. In determining Q and W it is assumed that the display origin is in the lower left corner.
Table: Listing 3. Add these lines to Listing 1 and 2 to draw the sphere of Figure 5.
Table: Listing 4. These lines should be added to Listings 1 and 2 to draw the mobius strip shown in Figure 6.
Table: Listing 5. These lines, along with Listings 1 and 2, create the pop art figure shown in Figure 7.
Table: Listing 6. These lines were used to create Figure 8. Values for loop indices I and J may have to be altered to fit your display.
Table: Listing 7. Lines for dodecahedron of Figure 9.
Photo: Figure 1a. Viewer template. Lines at 45~ angles should be one mirror length.
Photo: Figure 1b. Photograph of partially assembled viewer.
Photo: Figure 1c. Fully assembled viewer with additional strips of cardboard on sides to exclude light.
Photo: Figure 2. Stereo projection of viewer, object, and display, illustrating projection of object image from viewer to display.
Photo: Figure 3. Top view of viewer-object-display relationship. Notice that the legs of the two triangles have the same ratio.
Photo: Figure 4. Rotation of object by angle about Z axis.
Photo: Figure 5. Sphere.
Photo: Figure 6. Mobius strip.
Photo: Figure 7.3-D string art.
Photo: Figure 8.
Photo: Figure 9. This dodecahedron was created using rotation and translation of a single pentagon.
Photo: Figure 10. Dandelion with airborne fuzz.