3D ROTATOR
by Richard Taylor
from Big K #7 (Oct'84)

RICHARD TAYLOR's PAGE
The Six Million Viewpoint Graphics Routine

RICHARD TAYLOR delves into the innermost depths of the Z80 chip and delivers
a stunning rotatable 3D wire-frame package.
For 48k Spectrum owners only.


THIS MONTH'S program, in stark contrast to the last three months, has nothing
to do with 2D animation. Instead the program works in 3 dimensions and is
called a 3D Rotator.

Basically it allows you to design a 3D object on paper; tap a few numbers
into the old Spectrum and end up with a computer representation of the object
on the TV screen - rather like user definable graphics but in three
dimensions. Well, you might think, that's all very clever but what's the
point of the whole exercise? The point is that at the touch of a key you can
rotate the object around on the TV screen and look at it from another angle.
Not only does this give a very nice demo of micro-graphics but also such
animation can be very useful for integrating into home-brewed games. The
program's been designed so that it's easy to put in your own programs and is
able to draw a "wire frame" representation of any object bounded by straight
lines.

The rotator is written in 100% machine code but even so, because of the sheer
complexity of the mathematical formulas used to work out exactly what an
object looks like from numerous different positions, the program isn't
instant. I've used all the speeding up techniques I can think of in writing
the program to make it run as quickly as possible. The result is that the
program is fast enough to make a turning object look as if it is really
rotating, rather than just 'jumping' from one position to the next.

The amount of time the Spectrum takes to draw the object really depends on
how complicated it is; ie. how many lines it is composed of. Now, before I
continue, I feel I should warn all 16K owners that this program is for its
larger memory sized brother only. Hard luck.

The machine code is a bit on the lengthy side - it runs to about 1.25K. The
machine code loader is shown in listing one. All (!) you have to do is tap it
in, RUN it and wait for a couple of minutes. You should then be greeted with
a message advising you to SAVE the machine code to tape a couple of times
using:

SAVE "3D ROTATOR" CODE 58084,1308

If you're unfortunate enough to get a message informing you about an error in
one of the DATA lines then I'm afraid that you'll have to correct it and try
again.

 So as not to compound your finger ache with another gigantic listing, the
BASIC program which gives you the facility to actually enter the data for
your own objects will not be published until next month. However, the actual
rotating bit is complete so, meanwhile, to act as a demonstration of its
capabilities, there's listing 2. This program sets up the data for a simple
representation of an aeroplane which you can rotate about and so generally
get the feel of the rotation functions. After typing it in and running it,
you'll have to set your tape recorder to the start of the machine code
recording. After that's loaded you'll be presented with a front, horizontal
view of the plane.

You'll no doubt be aware of how points in 2D space are represented using x
and y co-ordinates. Well, 3D co-ordinates are just as simple, the only
difference being that you have to tag a z co-ordinate on the end of the other
two. The third axis is at right angles to the first two; ie. if you draw x
and y axes on a piece of paper then the z axis would pass through the paper.
In this program, positive z values go into the page and negative ones out of
the page. Listing 2 allows you to rotate the plane around the x, y and z axes
using, not surprisingly, the x, y and z keys. Pressing CAPS-shift and one of
the rotation keys (ie. the capital of the letter) results in negative
rotation in the appropriate axis. Notice how all rotation takes place about
the point of origin, where all the axes intersect. The program itself is able
to rotate an object with a resolution of just 2 degrees. Therefore, in each
axis, you can view the plane from some 780 different views. In total there
ere 5,832,000 (180^3) possible aspects on each designed object!

It's all very well having a resolution of 2 degrees, but if you want to turn
an object over quickly then it would take ages because of the number of times
the object has to be re-drawn in its journey. To remedy this, you can change
how many (multiples of 2) degrees the object rotates each time you press a
rotation key. By just pressing the appropriate digit key, the program will go
into a demonstration mode if you press 'd', where the plane is automatically
rotated around by the computer. Demo mode can be terminated with key 'e'.
Again, the speed of rotation can be varied with the digit keys.
