### Perspectives And Reoscopic Projections

**
I. O. Angell considers ways of producing perspective projections on screen.
**

A previous article in the February edition of Your Computer, I gave a method for drawing pictures of three dimensional wire¬objects on the screen of a BBC micro. I looked at the simplest type of projection which maps a 3-D scene Onto a 2-D screen. This orthographic projection has the property that parallel lines in 3-D space are projected into parallel lines on the screen. Although very useful, such views do look odd! Our brains are used to the perspective phenomenon of 3-D space, and so they attempt to interpret orthographic pictures as if they are perspective views and so they look distorted.

So it is essential to produce a projection which displays perspective phenomena ie;. parallel lines hould meet on the horizon: an object should appear smaller as it moves away from the observer. The drawing board methods devised by artists over the centuries are of no value to us. Three dimensional co¬ordinate geometry, however, furnishes us with a relatively straightforward technique.

### A Perspective View

To produce a perspective view we introduce a very simple definition what we mean by vision. We imagine every visible point in space sending out a ray which enters the eye. Naturally the eye cannot see all of space, it is limited to a cone of rays which fall on the retina, the so-called cone of vision, which is outlined by the dashed lines of figure 1. The axis of this cone is called the straight-ahead ray. We imagine that the eye at the origin and the straight ahead ray identified with the positive z-azis of our co-ordinate system.

We imagine the screen - which we call the perspective plane - perpendicular to the axis of the cone of vision at a distance from the eye. In order to form the perspective projection we mark the points of intersection of each ray with this plane. Since there are an infinity of such rays this appears to be an impossible task. Actually the problem is not that great because we need only consider the rays which emanate from the important points in the scene eg. the vertices at the ends of line segments. The final view is formed by drawing lines between the projected points on the screen in exactly the same way as the equivalent vertices are related in 3-D space.

Figure 1 shows a cube observed by an eye and projected onto two planes: and the whole scene is also drawn in perspective. Two example rays are shown: the first from the eye to A, one of the near corners of the cube relative to the eye, and the second to B one of the far corners of the cube. The perspective projections of these points onto the near plane are A^1 and B^1, and onto the far plane A^{11} and B^{11}. Note that the projections will have the same shape and orientation, but they will be of different sizes.

We let the perspective plane be a distance d from the eye - variable PPD in later programs. Consider a point P=(x,y,z) in space which sends a ray into the eye. We must calculate the point where this line curs the view plane - the z=d plane - suppose it is the point p^1=(x^l,y^1,d). Let us first consider the value of y^l by referring to figure 2. By similar triangles we see that y^l/d=y/z, that is y^l=y.d/z. Similarly x^l=x.d/z. Hence P^1 = (x.d/z,y.d/z,d), and we have the x- and y-co-ordinates of the projection on the screen.

Many of the routines needed to draw a perspective view of an object are the same as those required for the orthographic projection. We lump them all together in the library of routines given in listing 1. It contains the routines Start, Setorigin, Moveto, Lineto and Real-to-pixel functions necessary for manipulating the screen of pixels as though it is a window on real 2-D space.

It also contains routines Angle, Rot3, Tran3, Mult3 and IdR3 for manipulating the matrices that represent transformations of 3-D space as well as Genrot which creates a matrix that represents the rotational transformation of space about a general axis, and Look3 which enables the observer to look from any point (EX,EY,EZ) in space toward any other (DX,DY,DZ).

### Use Of Matrices

If you want to understand them fully you must refer back to my previous article or read *Advanced Graphics With the BBC Model B Microcomputer* by I. O. Angell and B. J. Jones - ISBN 0 333 35052 9.

In order to draw a perspective view we must now create a Scene3 routine which calls other routines to calculate a vertex/line data base describing objects in the scene - the setup positions - uses matrices to move each individual object into its actual position in space, and then again to move space into the observed position where rhe eye is at the origin looking along the positive z-axis, ready for the perspective projection achieved in the Drawit, routine.

The database will contain information on NOV vertices - their X,Y and Z values and their projected values XD and YD - and NOL lines. The I'th line joins vertices and indicies LIN(1,I) and LIN(2,1).

The only difference between the program which draws a perspective view and that of the orthographic view is in the calculation of the co-ordinates of the projected image on the screen. Unlike the orthographic, in the perspective projection the co-ordinates on the screen cannot be identified with the x- and y-values of the point in observed position. We need to store the perspective transformation of the vertices in the arrays XD and YD so as not to corrupt the database for future projections. The I'th vertex (X(I),Y(I),Z(I)) in observed position is projected to (XD(I),YD(I)). The values in arrays XD and YD are given by:

XD(I)=X(I)*PPD/Z(I), and

YD(I)=Y(I)*PPD/Z(I)

for I = 1,2 ... NOV

The value of PPD is set to 3*VERT in Scene3.

We still need to explain our value for PPD. You can see from figure 1 that different values of PPD produce pictures of different sizes - which one do we choose? Is there a correct value. If we consider the practical situation, we note that the observer is sitting in front of a television and the perspective plane is identified with the plane of the TV screen. Normally the observer is sitting at a distance which is about three times the height of the screen from the terminal.

In the scale of our mapping from the real-world to the graphics area of pixels, this is a distance 3*VERT. If we choose PPD greater than this value, it is as though we are creating a close up, and if PPD is less than 3*VERT we get the smaller image of a long shot.

If you merge listing 2 with listing 1 you get program 1 which, when run in Mode 4, draws a perspective view of a cube. Try HORIZ=1, (EX,EY,EZ)=(10,0,0) and (DX,DY,DZ)= (0,0,0). Here we are viewing the wire cube from the front but note that, unlike the ortho¬graphic projection, the back face of the cube is smaller in projection than the front face and that lines parallel on the 3-D cube need no longer be parallel in projection: in fact they meet at a vanishing point.

Now choose (EX,EY,EZ)=(20,0,0) i.e., the observer is still looking at the cube from the same direction, but from further away: see how the projection is the same shape and orientation as previously but it has diminished in size, as you would expect with perspective! If you now return program 1 with the same values of (EX,EY,EZ) and (DX,DY,DZ) but with HORIZ=100 you will see that a change in the size of HORIZ makes no difference to the perspective projection! So you can keep a value of HORIZ=1 for all perspective projections.

Now run the program with (EX,EY,EZ) =(1,0,0). It will fail. The eye of the observer lies in one edge of the cube, and the perspective transformation leads to a division by zero. Theoretically, objects may be positioned throughout space, even behind the eye, however to avoid problems we only consider points with positive z-co-ordinates in the observer position.

Now run program 1 with general values of (EX,EY,EZ) and (DX,DY,DZ) e.g., (10,20,30) to (0,0,0) : (5,10,15) to (0,0,0) : (5,10,15) to (1,1,1) etc.

Listing 3 should be merged with listing 1 and the Object and Drawit routines of listing 2, to form program 2 which runs in Mode 4. It gives a general perspective view of a scene consisting of two cubes that can be rotated by an angle Gamma about an axis of rotation which joins point (PX,PY,PZ) to (QX,QY,QZ).

Program 3 is formed by merging listing 4 with listing 1 and the Drawit routine of listing 2. When run in mode 4 it draws a perspective view of a jet. Try HORIZ = 1, (PX,YP,PZ)=(1,1,1), (QX,QY,QZ) = (0,0,0), GAMMA=1 with (EX,EY,EZ)= (200,300,400) and (DX,DY,DZ)=(0,0,0). Then try (EX,EY,EZ)=(300,400,500) etc.

Perspective views are all very well but we have two eyes. Each eye should have its own perspective view, which will differ slightly from that of the other eye. This is the means by which we appreciate the three-dimensional quality of our world.

This leads to a problem; we cannot simply draw two such projections because the left eye will not only see the view created for it, but also that made for the right eye, and vice versa. To stop this confusion we must ensure that each eye sees its own view, but only its view. This is achieved by using a pair of stereoscopic spectacles: a pair of transparent plastic sheets, one red - left eye - and one blue - right eye. In this way the left eye cannot see red lines because they appear the same colour as the white background.

Similarly for the right eye which cannot see blue lines, but red lines look black. So the computer must make two line drawings of a scene: one in blue for the left eye, and one in red for the right eye: hence we have to run using Mode I logically Anding the colours - GCOL 2 - onto a white background.

So we wish to devise a method of producing the stereoscopic projection of a general point P=(x,y,z), that is two points PL=(x_{1},y_{1}) for the left eye and PR=(x_{r},y_{r}) for the right eye, in the co-ordinate system of the perspective plane - see figure 3. Naturally the perspective plane is the same for both eyes. We will assume that the origin is between the eyes, that space is in observed position with the direction of view for each eye - the straight ahead ray - is parallel to the z-axis.

The eyes have co-ordinates (-e,0,0), left, and (e,0,0), right: in the program that follows, e is given by variable ED, which is normally about 0.2*VERT. Again the perspective view plane is a distance d - variable PPD - from the origin. In order to find PL, we move space by (e,0,0) so that P becomes (x + e,y,z) and the perspective transform of this point for the left eye is ((x + e).d/z,y.d/z), which when we return space to its original position becomes ((x + e).d/z - e,y.d/z). Similarly, the right eye transformation produces PR = ((x - e).d/z + e,y.d/z).

Listing 5 is a Drawit routine which, when used as a replacement in our first two programs, draws a stereoscopic view of the cube - program I - or pairs of cubes - program 2.

For the best stereoscopic views it its best to make the perspective plane cut on the object being viewed ie., if (DX,DY,DX)=(0,0,0) then make the square root of V(EX^{2} + EY^{2} + EZ^{2}) = PPD(=3 * VERT). Therefore in the case of stereoscopic views, we cannot keep HORIZ - and VERT - fixed, since for the best projections HORIZ and VERT depends on (EX,EY,EZ). For program 4, formed in the above-mentioned way from program I, try HORIZ= 10, (EX,EY,EZ) = (8,12,16) and (DX,DY,DZ) = (0,0,0).

If you replace the Scene3 of program 3 - the jet - with listing 6, and also use Drawit - listing 5 - you get program 5 for drawing a stereoscopic picture of the jet. There is not enough memory to run the program in mode 1, so instead we run in mode 7, and the program *SPOOLs the picture onto file PICCY in backing store. Then, if you clear the old program and *EXEC PICCY onto a white mode 1 screen you will get your stereoscopic picture. A good result is achieved with HORIZ = 230, (EX,EY,EX) = (250,300,350) and (DX,DY,DZ) = (0,0,0). Type:

NEW

MODE 1:VDU 23,1,0;0;0;0;:GCOL 0,135:CLG

*EXEC PICCY