7.0 Introduction
7.1 Drawing the object
7.2 Giving the object perspective
7.3 Rotating the object
7.4 The complete program
7.5 Activities
7.6 Two techniques for hidden line removal
7.7 Activities
You can use the graphics facilities of the BBC Microcomputer to rotate and to add perspective to objects drawn to appear in three dimensions. Screen Displays 7.1a,b,c are examples. This chapter provides and develops a suitable program (see Listing 7.1) and it explains how each part of the program works, so that you can modify it to your own requirements. In particular, the program has to take care of drawing the object, giving it perspective and rotating it. We discuss these in the next three sections.
In Section 1.6, we explained one way of drawing pictures on the screen. PROCbox, for drawing rectangles, utilised co-ordinates in two-dimensions, X and Y. For the purpose of adding perspective and rotating, however, you have to work with an additional Z co-ordinate to specify how far any point is away from the screen. Only when the computer has this information can it estimate the foreshortening that perspective and rotation would produce. So, when you design your object, you have to specify three co-ordinates for the principal points. These are X, Y and Z co-ordinates. The orientations are such that the x axis is positive towards the right, the y axis is positive in the upwards direction and the z axis is positive in the direction coming out from the screen or page. Clockwise rotations are positive, looking towards the origin from a positive position along an axis. By way of illustration, Screen Displays 7.1a,b,c respectively show the same cube rotated 25 degrees about the y axis; 25 degrees about both the x and y axes; and 25 degrees about ail three axes.
It is best to have the origin of co-ordinates at the centre of the screen (at addressable points 640,512) and to have the centre of the object at the origin. This is the case for the cube of Screen Displays 7.1a,b,c.
You specify the co-ordinates in any convenient units and supply a conversion factor to turn them into screen co-ordinates.
Once you have specified these, the next step is to specify which of the points have to be joined up and which not. In the program of Listing 7.1, we do this with two codes: 5 for a PLOT5,X,Y to draw to join the points and 4 to move between the two points without joining them. We use the codes after the co-ordinates of each point, 'm DA T A statements (see lines 310 to 330). They eventually arrive as M in the PLOT statement in line 280.
Parallel lines seem to get closer together as they get further away from the eye. This is an example of perspective, and this is the effect that our program has to achieve. It is actually very simple. The perspective scaling depends on the distance of the point from the observer, and must be such that the size of parts of the object appear smaller if they are farther away from the observer. Any part of the object sufficiently far away should tend to a zero size. If the screen co-ordinates X and Y represent projections of the image's X and Y co-ordinates, then the perspective must only depend on the Z co-ordinate. The perspective scaling can be produced by multiplying each X and Y co-ordinate by the following, where Z is the Z co-ordinate of the point and P is a length which determines the amount of perspective in the image:
P/(P-Z)
P represents the distance of the viewer's eye from the origin, i.e, from the centre of the object. It is measured in the same units as those for the size of the object. Good perspective seems to be obtained by viewing the object at a distance of about 3 times its height. This gives a perspective corresponding to viewing a book at arm's length. In Listing 7 .I the height of the cube is 2. Since we want it viewed at distance of 3 times this value, P becomes 6 (see line 140).
When an object is rotated, the amount of foreshortening changes. In principle it is not very difficult to work this out in terms of the sine or cosine of the angle through which the object is rotated. However, for objects in three dimensions, the mathematics becomes very complex indeed. The best way of approaching it is via matrix algebra. We have worked out three suitable expressions for you to use: one each for lengths originally lying in the x, y and z directions. We express our results in terms of variables rather than sines and cosines. This is because it would be too time consuming for the program to keep having to evaluate the same trigonometric ratios. So the program evaluates them once only and then calls on them, as they keep being needed. These variables as are as follows:
SX = sine of angle of rotation around x axis
CX = cosine of angle of rotation around x axis
SY = sine of angle of rotation around y axis
CY = cosine of angle of rotation around y axis
SZ = sine of angle of rotation around z axis
CZ = cosine of angle of rotation around z axis
Our expressions for the screen co-ordinates for any point in the image are given in terms of these variables in lines 250, 260 and 2 70, which also includes the perspective factor. If you are familiar with matrix algebra, you should have no difficulty in deriving these expressions for yourself. If not, you will probably be prepared to accept them as we give them.
The program starts by asking for the angles of rotation around the three axes. The next line then calculates all the sines and cosines that are required. This takes a relatively long time and so is done only once in the program. Once the mode is set in line l0, the graphics origin is fixed at the centre of the screen by the VDU29 statement in line 120. For simplicity the corners of the cube are taken to lie I unit along each axis, and the data for their co-ordinates are stored in the OAT A statements at the end. As this means that the co-ordinates are too small for plotting directly on the screen, line 180 scales them up, according to the scaling factor K.
For programming convenience each point is plotted using a procedure called in line 190, together with the current co-ordinates and M. The procedure which does the rotation and perspective scaling is in lines 230 to 280. Line 250 calculates the Z co-ordinate first. This is then used in the next two lines to calculate the two-dimensional picture co-ordinates. The first part of the calculation of the screen co-ordinates involves the perspective scaling factor referred to in Section 7.2.
Screen Display 7.1a
Screen Display 7.1b
Screen Display 7.1c
i. Enter and run the program in Listing 7.1. Try rotating around the x axis by small angles, say between 1 and 20 degrees.
ii. Repeat for rotations around the y and the z axes.
iii. Now enter the following three lines and run the program:
85 AY=25 :AZ=25
90 FOR AX=1 TO 30 STEP 4
95 RESTORE
205 NEXT AX
Does the cube rotate?
In Section 6.4, we touched on hidden line removal. We now illustrate two methods for it - but they have to be rather simplistic because the whole subject is so complex.
Screen Displays 7.2a,b,c illustrate the simplest of the two. It shows a house and garden - and we only draw the parts of the house that can be seen from the front! In other words, we only give the co-ordinates for two sides of the complete figure! The perspective view is still perfectly acceptable, provided the house is not rotated too far around any of the axes. Even though the acceptability breaks down with larger angles, the technique is still valuable, because it gives a picture which is more pleasing and less obstructed than a wire-frame one showing all the details at the back.
You may like to experiment with another way of achieving hidden line removal (see Screen Display 7.3 and Listing 7.3). For this, you make each face of the figure a solid block of colour. The figure is then drawn starting with the areas furthest from the viewing point and working to the nearest areas. Then if any of the nearer areas, which are drawn later, cover up those aireadiy drawn, hidden line removal is automatically achieved. The technique requires rewriting the data into blocks, each representing an area. The program then needs to search through these areas and make sure that it draws them in the order of their distance from the viewing point. Our program makes no attempt at such a search but merely draws the faces of the cube in a fixed order. This means that if the cube is rotated too far the illusion is spoilt.
Screen Display 7.2a
Screen Display 7.2b
Screen Display 7.2c
i. Enter the house and garden program of Listing 7.2 and experiment with the rotations to examine the limitations of such a partial model.
ii. Run the program of Listing 7.2. How do you feel about the way it removes hidden lines?
iii Run the program of Listing 7.3. How do you feel about the way it removes hidden lines?
Screen Display 7.3