0% found this document useful (0 votes)
191 views

Atari ST-3D Graphics Programming Text

Uploaded by

ratapiojosa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views

Atari ST-3D Graphics Programming Text

Uploaded by

ratapiojosa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 364

ATARI

8
O
PROGRAMMING
Concepts and Techniques

w
3
c
3
/?////* / /11 jjp

A Data Becker book published by


m ■ ;ii **>♦♦♦♦« r*
v#u c,n cm"‘ ob ffffffiium
Abacus mm Software
3-D Graphic
programming
By Uwe Braun

A Data Becker Book

Published by
A I IlMWSIIlj) r c-
Abacus iiiiiiisnl Software
First Printing, Aug. 1986
Printed in U.S.A.
Copyright © 1985 Data Becker GmbH
Merowingerstr.30
4000 Dusseldorf, West Germany
Copyright © 1985 Abacus Software, Inc.
P.O.Box 7219
Grand Rapids, MI 49510

This book is copyrighted. No part of this book may be reproduced, stored


in a retrieval system, or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording or otherwise without the
prior written permission of Abacus Software or Data Becker, GmbH.

Every effort has been made to insure complete and accurate information
concerning the material presented in this book. However Abacus Software
can neither guarantee nor be held legally responsible for any mistakes in
printing or faulty instructions contained in this book. The authors will
always appreciate receiving notice of subsequent mistakes.

ATARI, 520ST, ST, TOS, ST BASIC and ST LOGO are trademarks or


registered trademarks of Atari Corp.

GEM is a registered trademark of Digital Research Inc.

ISBN 0 916439 69-0


- -
Table of Contents

1. Introduction 3

2. Mathematical Basis of Graphic Programming 7

2.1 Moving the Coordinate Base 10

2.1.1 Scaling the Axis 12

2.1.2 Rotation around one point 16

2.2 Plane conversion with matrix operations 19

2.3 Clipping 26

2.4 Transformations in three dimensional space 40

2.4.1 Rotation about any desired axis 36

2.5 Projection from space to a two dimensional plane 43

2.6 Perspective transformation 48

2.7 Hidden lines and hidden surfaces 56


2.8 Rembrandt and hidden surfaces 65

3. Machine language fundamentals for graphic


programming 75

3.1 Speed Advantages from tables 76

3.2 Assembler routines for screen manipulation 79

3.2.1 Drawing lines 80

3.3 Operating system functions 87


3.3.1 Starting a program 88
4. Graphics using assembly language routines 107

4.1 Definition of a data structure of an object in


space 108

4.1.1 Explanation of subroutines used 152

4.1.2 Description of the Subroutines of the first


Main program 165

4.2 Generation techniques for creating rotating


objects 169

4.1.2 New subroutines in this program 188

4.3 Hidden line algorithm for convex bodies 191

4.3.1 Explanation of the newly added subroutines 214

4.3.1.1 Errors with non-convex bodies 215

4.4 The painter algorithm 216

4.4.1 New things in the main program rotl.s 246

4.4.2 Sort algorithm 247

4.5 Entering rotation lines with the mouse 248

4.5.1 Description of the new subroutines 296

4.6 Handling several objects 298

5. Suggestions for additional development 327

5.1 Light and Shadow 329

5.2 Animated Cartoons 330


Appendices

A Number systems 333

B Analytical geometry of the planes and space 335

B.l Scalar product 342

B.2 Cross product 342

C Matrix calculations 344

C.l Adding matrices 345

C.2 Multiplying matrices 346

D Bibliography 348

Index 349
L_
Abacus Software ST 3D Graphics

1. Introduction

The possibilities of computer graphics are some of the most challenging


reasons for working with a computer today. Dazzling computer-generated
images are showing up almost everywhere--in medicine, engineering,
motion pictures, music videos, television advertising, and even in
newspapers like USA Today. The public is fascinated by the unlimited
forms that computer graphics are taking. Some of the more sophisticated
of these works are the three-dimensional, computer-animated videos used
in television advertisements.

One major application of computer graphics in industry is for CAD


(Computer-Aided Design) systems. The integration of CAD systems into
the manufacturing process is of increasing importance. Known as CAD-
CAM (Computer-Aided Design - Computer-Aided Manufacturing), these
systems are making significant inroads in automating many of the
manufactured, assembled, and processed goods such as machine tools,
automobiles, electronics, and agricultural products. Without advanced
graphic data processing, the latest medical processes such as CAT scans
would be difficult, if not impossible. Furthermore, three-dimensional
graphic data processing has made it possible to visually represent
complicated scientific relationships and to make them comprehensible
(like atomic and molecular models and the DNA helix). Eventually these
graphics will be integrated with advanced teaching and simulation
methods, and are bound to have a profound impact on the way we think
and learn.

The enormous strides made in the production of integrated circuits and


the increase in processing speeds of relatively new microprocessors such
as the Motorola MC68000 has made it possible for the home and personal
computers to enter application areas that were formerly the domain of
large mainframe computers costing several hundred thousand dollars.
Even now, an affordable 32-bit personal computer is just around the
comer. The traditional distinctions between microcomputers,
minicomputers, and mainframes are becoming increasingly blurred.

Of course, even the largest mainframes are getting faster as well. The
fastest computer at this time, the Cray II, has a throughput capacity of
2000 megaflops (200 million floating-point operations per second). Such
high computing speeds are needed to closely simulate natural world
processes with computer models. Examples are the simulation of

3
ST 3D Graphics Abacus Software

ecological problems (acid rain), simulation of the human physiology,


weather prediction, nuclear fusion and fission, origin of the solar system,
simulation of star systems, space travel, etc.

This book is intended to explore some of the possibilities of creating two-


and three-dimensional computer graphics on the Atari ST computer
series. To obtain a good understanding of the program sections, you
should have some fundamental knowledge of MC68000 machine
language.

Machine language represents the lowest level of communication with the


computer and contains a small number of rather simple instructions that
are consequently easy to learn. For the hobbyist, knowing machine
language programming makes it easier to understand the data structure of
higher-level languages such as Pascal and C. However, most problems
and algorithms are easier to program in a higher-level language than in
machine language.

For the problem of depicting and representing the 3-D wire models
presented here, maximum processor speed is crucial. Machine language is
clearly superior to any higher-level language in fulfilling this
requirement. With these applications for the Atari ST, real-time three-
dimensional graphics can be achieved. The removal of hidden lines and
the shading of areas requires a considerable amount of processor time.
The Cray II requires 8 minutes to create a single picture with a resolution
of 2000 by 3000 pixels, with up to 30 bits of color information per picture
point. In contrast, the ST manages only 640 by 400 pixels and only one
bit of color information. Of course, it is possible to increase the
computational capabilities of the ST with programming tricks, fast mass
storage (hard disk) and large amounts of memory to solve more complex
graphic problems.

This book provides you with help in solving the complex programming
problems of three-dimensional graphics. While the sample programs are
directly tailored for the Atari ST, the techniques can be used without too
much difficulty on other computers. Only the routines for hardware
communication and display control (keyboard input, line drawing, surface
shading (if possible) and switching between two screens) need to be
tailored to another computer using an MC68000 CPU (i.e., the Apple
Macintosh and Commodore’s Amiga). The subroutines for generating
and handling three-dimensional graphic objects can be run on any
computer with an MC68000 microprocessor.

4
Abacus Software ST 3D Graphics

2. Mathematical Basis of Graphic Programming

This chapter serves as the mathematical foundation of computer¬


generated, three-dimensional graphics. As a result, the explanations are
very extensive. For this reason we ask readers who are already familiar
with these topics for a little patience and understanding.

All computer graphic problems can ultimately be reduced to the problem


of drawing points on a graphic output device (monitor screen, plotter, or
printer) and to connect these points with lines. There may also be the task
of shading the area delineated by the lines. For a demonstration, we will
use a two-dimensional plane with one Cartesian coordinate system,
familiar to everybody, whose origin lies in the lower left hand comer of
the screen.

Cartesian coordinate system

T-1-1-1-1-r—i r r i i r»
I ST display coordinate system

Fig. 2.1: coordinate system and ST display coordinate system

In Figure 2.1, the first problem of representing graphics becomes clear.


The Cartesian coordinate system and the display coordinate system used
by the ST’s software and hardware are not the same. The directions of the
y-axis are opposite, and the coordinate origin is displaced. Consequently,

7
ST 3D Graphics Abacus Software

an object defined in the first system is inverted in the system on the right,
and is also displaced on the y-axis.

At first, you might be tempted to define objects to be represented using


the ST’s coordinate system. But doing this does not solve the second
problem-that the display surface of every computer is limited. The ST
can display only 640x400 points at its highest resolution. So, to avoid
defining objects with these limitations of 640x400 points, we must be
able to define an object in any desired coordinate system before
displaying it on the monitor screen. In other words, we must be able to
scale the object in any of the coordinate systems, i.e., change its size. All
points of the defined object can then be transformed using graphics
operations.

This operation is called windowing. We now introduce three coordinate


systems. They are:

1. world coordinate system

2. view coordinate system

3. picture coordinate system

Individual objects are defined in the world coordinate system, where the
calibration of the coordinate axis may be any desired unit of
measurement—for example, millimeters, kilometers, years, etc.

The view coordinate system accepts a portion of the world coordinate


system. This is similar to an observation window in the world coordinate
system.

Finally, the picture coordinate system represents the physical screen


display of the computer, A single point in this system corresponds to an
individual pixel on the screen.

This concept can be explained very simply with an example. Two objects
are defined in a world coordinate system, the outlines of a house and of a
church. The two outlines represent all objects that can be depicted on a
plane. For example, an architect would use the outline of the house in a
world coordinate system to define individual rooms and furniture.
Abacus Software ST 3D Graphics

Our task is to transform the observation window, together with the house
that fills its surface, to the specific picture window for display on the ST’s
screen.

Here’s the preferred solution to the problem, using the view coordinate
system: The origin of the world coordinate system is moved to the lower
left comer of the observation window and scaled by a suitable factor. It
now represents all points in the picture coordinate system. If the points
are in the field of picture coordinates, they can be drawn and connected
with lines.

Cartesian coordinate system Screen system for Atan ST

Fig. 2.2: Transformation of world coordinates to picture coordinates

9
ST 3D Graphics Abacus Software

2.1 Moving the coordinate base

Scaling and (as we shall see later) rotation are both related to the
coordinate base. To scale an object in relation to another point, or to
rotate it around an arbitrary point, the coordinate origin must first be
moved to the relative origin. We can illustrate this again using the house
example.

nx

Fig. 2.1.1

One way to describe the house is to list the coordinates of the end points
and to list the points which are connected with lines. For this example,
the two lists are as follows:

10
Abacus Software ST 3D Graphics

End point list:

Point X-coordinate Y-coordinate


PI 100 50
P2 100 90
P3 120 130
P4 140 90
P5 140 50

Connection list:
Line from Point A to Point B
LI: PI P2
L2 : P2 P3
L3: P3 P4
L4 : P4 P5
L5: P5 PI

This description of a polygon, consists of a sequence of closed lines. It


contains all the information necessary for representing it on the display
screen. To draw the polygon, the lines’ endpoints are passed to a
subroutine for drawing.

As we shall see later, the polygon is also perfectly acceptable for the
description of complex, three-dimensional objects. Any physical object
can be closely approximated by chaining various polygons. Also, natural
asymmetrical bodies such as mountains, forests, lakes and animals can be
represented in a realistic manner with polygons created through fractional
geometry, i.e. fractals. In addition, most man-made objects are
constructed in a symmetrical manner and are easier to represent
graphically.

In Figure 2.1.1 the coordinate origin of the world system is moved to


point P1 [ 10 0,5 0 ] . The new world coordinates (view coordinates) are
obtained by subtracting the coordinates of point P1— the new origin—from
the points that define the object. In general, the new world coordinates
are equal to the old world coordinates, minus the coordinates of the new
origin (in world coordinates). If we describe the old world coordinate axis
with x and y, the new world coordinate axis with x' and y', the new
origin point with NU[nx,ny3 and the point to be moved with
P1 [ x, y ], we can write:

Pl[xl',yl'] = Pl[xl,yl] - NU[nx,ny]

11
ST 3D Graphics Abacus Software

For example, for point 5—the new origin is located at


PI (100,50) = NU (100,50). The coordinates of the point to be
moved P 5 (14 0,5 0) become in the new world coordinate system
P5x' =140-100 = 40, P5y' =50-50 = 0. The point
P5 (140,50) becomes point P5' (40,0) . This translation must be
performed for every point of the object. It is possible to move the origin
of the world coordinate system to any point.

2.1.1 Scaling the Axis

As previously mentioned, scaling the axis refers to the coordinate origin.


This can be readily seen in Figure 2.1.2. The points of the house, i.e. the
X and Y coordinates, are scaled by the factor one half in the X and Y
axes. The result is the halving of the length of the edges, but also a
translation in the direction of the origin. If we want to avoid displacing
the direction of the origin, then before scaling the origin must be moved
to a point not affected by the scaling itself. The Figure 2.1.3 is an
example. If we want to leave the left lower comer of the house (the point
PI) in its place. The origin is moved to point PI. The picture is scaled by
multiplying the X and Y values by one half and finally moving the origin
to its original location. In this example this means:

1. Subtract 100 from the X-values of points P1-P5

Subtract 50 from the Y-values of points P1-P5

2. Multiply all X- and Y-values of points P1 -P 5

with the factor one half.

3. Add 100 to all X-values of points P1-P5

Add 50 to all Y-values of points P1-P5

12
Abacus Software ST 3D Graphics

13
ST 3D Graphics Abacus Software

Figure 2.1.3

Scaling with factors greater than one enlarges the object. If we select
different scaling factors for the X and Y axes, a distorted picture of the
object results.

At this point let’s briefly return to the example, at Figure 2.1, and alter
the scaling factors for converting to view coordinates. With the maximum
coordinates of the observation:
[wxmin,wymin]; [wxmax,wymax],
and the display window
[vxmin,vymin]; [vxmax,vymax]
one can give differing scaling factors for the two axes, Sx and Sy. In our
example:

Sx=(vxmax-vxmin)/(wxmax-wxmin)

Sy=(vymax-vymin)/(wymax-wymin)

14
Abacus Software ST 3D Graphics

Before scaling, the origin of the world system is moved to the left lower
comer of the observation window [wxmin, wymin], since this point is
the data point of the scaling. The result of the conversions is therefore:

1. Move the origin to the point W1 [wxmin,wymin] by


subtracting wxmin from all of the X coordinates and
wymin from all of the Y coordinates.

2. Multiply all X and Y values of the points with the factor j


Sx. If the relationship of height to width is equal for hpth c <»•/*'' ‘oa«UL
windows, then Sx=Sy. A*}*^ xV r°°

3. Convert to the display system by multiplying the Y values


by -I and adding of the maximum Y value to these Y
values (for the monochrome ST this is 399). This moves
the origin to the upper left comer of the screen.

The third step of converting the Y values to the screen display of the ST
is always the same. During the description we shall limit ourselves to the
view system. If during subsequent discussions no special reference is
made to this step, you should remember that if it is not performed, all
objects appear inverted on the screen after the drawing is completed.

The location of the picture window in the view system is not fixed to the
origin, but is movable in the total view system. However, the three
conversions must be followed by another conversion-moving the
window to point VI [vxmin,vymin] . Basically the conversion of an
object is the opposite of the conversion of a coordinate system. Therefore,
when moving the picture window and the object to the point
VI [vxmin, vymin] , the coordinates of this point (vxmin and
vymin) must be added to all object coordinates.

Summarizing the conversion of the world system into the view system:

1. Move the origin to the point W1 [wxmin,wymin] by


subtracting wxmin from all X coordinates, and wymin
from all Y coordinates.

2. Multiply all X values of the points by the factor


Sx=(vxmax-vxmin)/(wxmax-wxmin), the Y
values with the factor Sy= (vymax-vymin) / (wymax-
wymin) .

15
ST 3D Graphics Abacus Software

3. Move the window and the object to the point


VI [vxmin, vymin] by adding vxmin to all X values,
and vymin to all Y values.

4. Convert to the display system by multiplying the Y values


by -1 and adding the maximum Y-value to these Y values
(for the highest resolution this value is always 399).

2.1.2 Rotation around one point

The rotation of an object is related to a single point, just as we found out


in the previous section on scaling. To start the conversion, a single point
is rotated around the origin. Since the rotation occurs around the single
origin point, the data point of the rotation angle is the connecting line
between coordinate source and the point to be rotated. See Figure 2.1.4.

The point PI (xl,yl) is moved by rotation around the angle p of the


origin to the point P2 (x2,y2). We must define the sign of the angles a
and p as + or Following the conventions of mathematics, we designate

16
Abacus Software ST 3D Graphics

the angles as positive when the rotation moves the positive X axis to the
positive Y axis. Expressed differently, positive angles are measured in the
counterclockwise direction. For the angle between the connecting line
from 0,0 to P1 and the X-axis, the relationships are:

1) SIN(alpha) = Yl/C

2) COS(alpha) = Xl/C

3) SIN (alpha+beta)=Y2/C

4) COS(alpha+beta)=X2/C

with C=V (X12+Y12) W (X25+Y22) . The addition theorems for the


angle functions SIN and COS are as follow (we won’t derive them here):

5) SIN(Alpha+Beta)=SIN(Alpha)*COS
(Beta)+COS(Alpha)*SIN(Beta)

6) COS(Alpha+Beta)=COS(Alpha)*COS(Beta)-
SIN(Alpha)*SIN(Beta)

By combining these equations, X2 and Y2 can be calculated quite easily:

7) X2/C=COS(Alpha)*C0S(Beta)-
SIN(Alpha)*SIN(Beta)

gives us

8) X2= COS(Alpha)*C* COS (Beta) -


SIN(Alpha)*C* SIN(Beta)

from 1) follows

9) X2=Xl*COS(Beta)-Y1*SIN(Beta)

10) Y2=Yl*COS(Beta)+X1*SIN (Beta)

17
ST 3D Graphics Abacus Software

As an example of rotation, we will rotate the house in Figure 2.1.5 by an


angle of 30 degrees around the origin. The points P1-P5 become points
R1-R5, as can be seen on the example at Point PI.

R1X=P1X*C0S (30)-P1Y*SIN(30)

R1Y=P1Y*COS(30)+P1X*SIN (30)

From PI (100,50) follows R1 (61.6,93.3). According to the same


principle, the remaining points are likewise converted.

Figure 2.1.5

18
Abacus Software ST 3D Graphics

2.2 Plane conversion with matrix operations

After learning about the conversions, translations, scaling and rotations


described in the previous chapter, we are now able to draw on the screen
any object previously defined in a two dimensional coordinate system, in
any selected size and viewing angle. One drawback to this method is that
several arithmetic operations are required for each and every point of the
object.

Right now we’ll combine these conversion operations into a single matrix
operation. (Explanations of matrix operations are found in the Appendix).
Therefore it becomes possible to apply the conversions to the array and
then to multiply the resulting array with every point of the object. To
make the array operations usable for the point coordinates of the plane,
the point coordinates are first converted to array form.

There are basically two ways to convert these: with column vectors (2,1),
or with line vector (1,2) arrays. A conversion array (2,2) is used to
multiply a line vector with the transformation array, where the
transformation array must be multiplied with the column vector, (number
of columns A = number of rows B).

In this book we shall write the point coordinates as line vectors P and the
multiply this line vector with the transformation array. This sequence of
multiplication simplifies, purely subjectively, the creation of the
transformation matrices. If you multiply a line vector (1,2) with a
quadratic array (2,2), you will obtain as a result another line vector (1,2),
which represents point coordinates. The individual point operations can
be expressed by a suitable transformation matrix T. For scaling the X axis
by the factor 2, the array S1 is valid. It is also possible to quadruple the Y
values using transformation array S2. The two scaling steps can be by
multiplying SI and S2 with array S3.

Si 2 0 S2 = 1 0
0 1 0 4

S3 = S1 * S2 = 2 0 ★ 1 0
0 1 0 4

s3 0
4

19
ST 3D Graphics Abacus Software

For rotation, Rx is valid for one counter clockwise rotation; from


trigonometry, a clockwise rotation occurs with r2. From Figure 2.1.5, the
movement of point P1 [ x 1, y 1 ] to point P2[x2,y2], results from
multiplying P1 with R.

Rl = cos(b) sin(b)
-sin(b) cos(b)

R-2 = cos(-b) sin(-b) = cos(b) -sin(b)


-sin {-b cos(-b) sin(b) cos(b)

P2[X2,Y2] = [XI, Yl] * cos (30) -sin(30)


sin(30) cos (30)

Several rotations in succession can be carried out by multiplying the


rotation matrices. Unfortunately, this array form does not permit
translation (origin relocation). For this you can add a dimension to the
vectors. Every n-dimensional object can be represented in a (n+1) space
in innumerable many ways.

In a three dimensional space there are infinite possibilities for laying out
the X-Y plane we have just observed. The additional dimension is known
as Z coordinate of the X-Y plane. For two dimensional objects, its value
is always one. The X and Y coordinates remain unchanged: the line
vector [x,y] becomes the line vector [x,y,l]. The array for the translation
of the source at point D is as follows:

110
T = 0 10
-DX -DY 1

Every point of the object must be multiplied with this array to move the
origin of the world coordinate system to the point (DX, DY). For the point
P [ x, y, 1 ] the result is: new point in world coordinates p' =p*T

10 0
P'[x',y',l] = [x,y,1] *010= [x-dx,y-dy,1]
-DX -DY 1

20
Abacus Software ST 3D Graphics

You can combine two displacements by using array multiplications. First


the origin is moved to the point [DX,DY, 1 ] and then to the point
[AX, AY, 1 ] of the new coordinate system. The two translation matrices
Tl and T2 are as follows:

10 0 10 0
T1=010 T2=010
DX -DY 1 -AX -AY 1

Multiplication of the matrices results in T3:

10 0 10 0
t3=t1*t2= 0 1 0 * 0 1 0
-DX -DY 1 -AX -AY 1

10 0
T3 = 0 1 0
-DX-AX -DY-AY 1

P'[x',y',l]= P[x,y,1]*T3 = [x-DX-AX,Y-DY-AY,1]

The scaling array S can be defined in the new system:

SX 0 0
S = 0 SY 0 and P' = P * S
0 0 1

and finally the rotation array R

cos (a) sin (a) 0


R(a) = -sin(a) cos (a) 0
0 0 1

21
ST 3D Graphics Abacus Software

Scaling as well as rotation, viewed individually, may be carried out in a


series through array multiplications. The array multiplication is normally
not commutative, i.e. Ti * T2 is not necessarily identical with T2 * Ti.
However, the multiplication of the following array types is commutative:

1) Translation * Translation
2) Scaling * Scaling
3) Rotation * Rotation around
the same axis
4) Scaling •k
Rotating

Type 4 (scaling and rotating) is only valid when both scale factors
(Sx, Sy) are identical.

These fundamentals enable us, through a combination of several array


operations, to rotate an object around a selected point V[vx,vy,l]
using a series of several array operations. The various operations are:

1. Shifting the origin to point V

2. Rotation around point V by an angle of alpha

3. Shifting of the origin to the original point

Three matrices Tx, Rx and T2 are required:

l 0 0 co3(a) sin(a) 0
Ti - 0 1 0 Rl = -sin (a) cos(a) 0
-vx -vy 1 0 0 1

1 0 0
t2 = 0 1 0
vx vy 1

For the multiplication array M1# the result is:

Mi = Tx * Ri * T2 and for every point follows:


P' = P * Mi

22
Abacus Software ST 3D Graphics

The sequence of matrices is decisive in these operations and must occur


from left to right. It is possible however, to first calculate intermediate
results, but these must be used in the "right" sequence. In this example,
there are two possible ways to proceed:

1. First calculate from Zi=T1*R1 and then M1=Z1*T2

2. First calculate from Z2=Ri*T2 and then M2=T1*Z2

The first case is explained in detail. Z1=T1*R1:

0 0 cos(a) sin(a) 0
1 0 * -sin(a) cos (a) 0
-vy 1 0 0 1

cos (a) sin(a) 0


-sin(a) cos(a) 0
cos(a)+vy*sin(a) -vx*sin(a)-vy*cos(a) 1

and now Mi=z1*T2:

cos(a) sin(a) 0
Mx = -sin(a) cos(a) 0 *

-vx*cos(a)+vy*sin(a) -vx*sin(a)-vy*cos(a) 1

10 0
0 1 0 =

vx vy 1

cos(a) sin(a) 0
-sin(a) cos(a) 0
-vx*cos(a)+vy*sin(a)+vx vx*sin(a)-vy*cos(a)+vy 1

23
ST 3D Graphics Abacus Software

If point PI [x, y, 1 ] is multiplied with this array, the result is point


PI' [x' , y' , 1], the point PI which was rotated around the angle
alpha at point VI [vx, vy, 1]. This connection can be recognized in
Figure 2.2.1 and should be performed as example for point PI.
PI[x, y, 1 ] * Ml =

cos (a) sin (a) 0


[x,y,l] * -sin (a) cos (a) 0
—vx*cos(a)+vy*sin(a)+vx -vx*sin(a)-vy*cos(a)+vy 1

Pl[x,y,z]= f[x*cos(a)-y*sin(a) -vx*cos (a) +vy*sin (a) +vx] ,


[x*sin(a)+y*cos(a)-vx*sin(a)-vy*cos(a) +vy], [1] ]

You can see that when the rotation point and the point to be rotated are
identical, therefore x=vx and y=vy, the expression for the line vector of
the point at [ vx, vy, 1 ] = [ x, y, 1 ] degenerates. That means that the
point coordinates do not change.

24
Abacus Software ST 3D Graphics

The house already familiar in Figure 2.2.1 shall be rotated by the angle
alpha=30 degrees around the point VI [ vx, vy, 1 ] = [ 12 0,8 0, 1 ]. As
an example this is carried out on point P2[100,90,l].

P2x'=100*cos(30)-90*sin(30)-120*cos(30)+80*sin(a)+120

P2y'=100*sin(30)+90*cos(30)-120*sin(30)-80*cos (30)+80

P2'=[97.68,78.66,l] and finally for the remaining points P1-P5.

PI'=[117.68,44.02,1]
P2'=[97.68,78.66,1]
P3'=[95,123.30,1]
P4'=[132,32,98,66,l]
P5'=[143.66,59.02,1]

This procedure also permits you to change the point for scaling to any
location in the coordinate system. In the following, you can see the
buildup of the transformation array. First the coordinate origin is moved
to point K1 [ kx,ky,1] with translation array T1? then scaling with array
Si, using scaling factor Sx and Sy, and finally moving the origin to its
original location using translation array T2. For every single point this
means p' [x' ,y', 1] = p [x,y, 1] *T1*S1*T2.

0 0 Sx 0 0 1 0 0
1 0 Si- 0 Sy 0 t2= 0 1 0
-kx -ky 1 0 0 1 kx ky 1

Sx 0 0
T-l* Si* T2 = 0 Sy 0
kx*(l-Sx) ky*(1-Sy) 1

P'[x,y,1] =P'[x*Sx+kx(1-Sx),y*Sy+ky(1-Sy),1]

In this example Sx=Sy=0.5.

25
ST 3D Graphics Abacus Software

2.3 Clipping

As we transformed the object coordinates to the display coordinate


system, we assumed that all points in the object can be represented in the
picture coordinate system. When we define a window in the world
system, some objects may be completely pushed out of the view of the
window, or objects are cut in half by the window. This means that one or
several connecting lines of the points cut the comers of the observation
window.

Figure 2.3.1

To avoid these incomplete objects, we can test the coordinates to make


sure they lie within the borders of the window. This method slows down
the drawing procedures considerably. Therefore it is better to determine
before drawing a line if the line is completely visible, partially visible, or
not visible at all. The window is surrounded by eight equally large
surfaces to determine the exact position of the line to the window. Now
the exact location of a line can be determined by comparing its

26
Abacus Software ST 3D Graphics

coordinates to the window borders. A code containing four bits can be


used to represent the relative position of a line outside of the window.

Bit number 3 210

Figure 2.3.2:CIip-Window

In the Figure 2.3.2 the position of a point outside a window is repeated by


a set bit as follows:

bit position

0 = Point is left of the window

1 = Point is right of the window

2 = Point is below the window

3 = Point is above the window

27
ST 3D Graphics Abacus Software

The code [0,1,0,1] means the following: the point is to the left and below
the window. With this information, it is possible to calculate the points of
intersection of the lines with the window edges by including them in the
equation. This leads to a quadratic equation system whose solution
requires several multiplications and divisions. For our purposes, we want
to limit the number of multiplications and to replace them when possible
with other mathematical operations. We do this for two reasons. The first
is for speed since multiplication requires about eight to ten times the
calculation time of addition. The second is the fact that the result of
multiplication, with the same number of significant positions of the
operands, has a larger relative error.

To get an optimal solution of the line-clipping problem requires a


programming language which permits bit manipulation. This was
developed by Cohen and Sutherland. Since the efficiency of the Cohen-
Sutherland clipping algorithm is so great, it is sometimes implemented in
the hardware of some graphic terminals.

The starting point of the algorithm is to divide the plane into the nine
areas previously illustrated. For every line which is to be "clipped”, you
must determine a center point and on the basis of its position relative to
the window.

The calculation of the center point of a line AB is simple. Just add the X
and Y coordinates of the end points and divide them by two.
Mx= (ax+bx) /2, My= (ay+by) /2. Division by two is performed by
microcomputers easily by a single right shift and this explains the speed
of the algorithm.

The 8 different positions of the end points relative to the window are
illustrated in Figure 2.3.2. Before calling the clip-routine, you must first
test to see if the two end points are visible. If any of the bits are set, then
some portion of the line is not visible. In Figure 2.3.2 both A and B are
above the upper window edge, and therefore the line AB is not visible and
no longer needs to be considered. You can calculate the position of the
points by "ANDing" their codes and then testing for a "not zero”
condition. For lines which have no common position parameter, for
example the line CD, positions are determined with two separate
procedures. First the right and then the left intersecting points with the
clip-window.

28
Abacus Software ST 3D Graphics

First calculate the midpoint Ml of line CD. After determining the position
code of the point Ml, it is compared with the code of the right endpoint D.
If a single bit of these codes is the same, then the partial line MID does
not have to be considered further, and the right endpoint D is replaced
with the point Ml which was just determined. Now the midpoint of line
CM1 (M2), is calculated and tested again with the right endpoint, this
time Ml. If both points are not on one side, M2 becomes the new left
endpoint and the right endpoint remains Ml. Next search the midpoint of
the line M2M1. This procedure is continued until a new calculated
midpoint is equal to one of the two end points used for calculation.

After completing the algorithm, the last left endpoint is the desired
intersecting point with the window. The intersecting point is stored and
the two starting points C and D are interchanged. With the same
procedure the intersection with the left window edge is determined. At
the start of the routine, if you find that an endpoint is already inside the
window, this endpoint must be stored. The line ST causes a problem. The
two end points S and T are not on the same window side and the line does
not intersect the window. A comparison of the first center point T1 shows
it matching both end points. The points T1 and T are both to the right of
the window and point S below the window. You can thus define a new
ending criteria--# a new midpoint lies outside of the window and matches
both end points of the line, then the line is not visible.

29
ST 3D Graphics Abacus Software

2.4 Transformations in three dimensional space

A small warning before we start: Thinking in three dimensions requires a


period of adjustment for the non-mathematically oriented reader. It may
be necessary to read this chapter several times before the concepts can be
fully understood.

Starting with the two dimensional X-Y-coordinate system, there are two
ways to introduce a right angle coordinate system to describe three
dimensional space. They are the right-hand and the left-hand coordinate
system which differ only in the orientation of the negative Z axis.

A coordinate system is called a right-hand coordinate system when a


screw with a right-handed thread (a normal wood screw) moves in the
direction of the positive Z axis when it is turned from the positive X axis
in the direction of the positive Y axis. See Figure 2.4.2. The right-hand
coordinate system is used extensively in mathematics while some
computer graphic books select the left-hand coordinate system.

30
Abacus Software ST 3D Graphics

Mathematical problems can be solved in either system and one system


can easily be turned into the other. We shall use both systems. The
transformations in three dimensional space will be explained on a right-
hand coordinate system, the perspective transformations on a left-hand
coordinate system.

z z

Right-hand Left-hand

Figure 2.4.2

All operations in a two dimensional space are special cases of


corresponding operations in three dimensional space. In the extended
coordinate system, the line vector of a point is expressed as:
P [x,y, z, 1]. To move the origin to the point V[vx,vy, vz, 1], use
the matrix T1:

1 0 0 0
Ti = 0 1 0 0
0 0 1 0
•vx ■vy -vz 1

So for every point: [x,y,z,l] * Ti = [x-vx,y-vy,z-vz,1]

31
ST 3D Graphics Abacus Software

The scaling matrix is similar. A scaling factor for the Z axis (Sz) is
added:

Sx 0 0 0
S1 = 0 Sy 0 0
0 0 Sz 0
0 0 0 1

For every point: [x, y, z, 1] * = [x*Sx, y*Sy, z*Sz, 1 ]

Rotation is limited to the three rotation axis: X,Y, and Z. We are already
familiar with rotation about the Z axis from the earlier 2D description.
The 3D description is derived by assuming that the positive Z axis
projects from the drawing surface. The coordinates of the axis about
which rotation takes place, does not change, in this case the Z coordinates
retain their values.

cos(zw) sin(zw) 0 0
Rz = -sin(zw) cos (zw) 0 0
0 0 10
0 0 0 1

32
Abacus Software ST 3D Graphics

Figure 2.4.3

We must also allow for setting a positive turning angle for the rotation
about the X and Y axes. A solution which can be applied to both the left-
hand and right-hand coordinate systems uses the following definitions:

Rotation axis positive angles are measured from

Z-axis X- to Y-axis

Y-axis Z- to X-axis

X-axis Y- to Z-axis

From this follow the matrices for rotation around the X and Y axis Rx and
Ry.

33
ST 3D Graphics Abacus Software

10 0 0
Rx= 0 cos(xw) sin(xw) 0
0 -sin(xw) cos(xw) 0
0 0 0 1

cos(yw) 0 -sin(yw) 0
Ry= 0 10 0
sin(yw) 0 cos(yw) 0
0 0 0 1

For the coordinate system this means that if you look from a positive axis
in the direction of the coordinate origin, a positive angle describes a
counterclockwise rotation. In a left-hand coordinate system a positive
angle describes a rotation in the clockwise direction. This definition
applies to a fixed coordinate system in which the objects are rotated. The
other type of representation would be the fixed placement of the object
and the rotation of the coordinate system. The two types differ only in the
sign of the rotation angles. This means that if the object is rotated about
the angle alpha, or the coordinate system is rotated about angle alpha,
the result in both cases will be the same. In three dimensional space the
point of the rotation, as in the two dimensional plane, is the origin. If you
want to rotate an object around another point, it is first necessary to move
the origin to that point. The required steps are:

1. Change the origin to the point B[bx,by,bz, 1] using


translation matrix Ti.

2. Rotate around the Z axis with rotation matrix Rx.

3. Retranslate the origin using translation matrix T2

10 00 cos(a) -sin(a) 0 0
T]_= 0 1 0 0 Rl= -sin (a) cos (a) 0 0
0010 0 0 10
-bx -by -bz 1 0 0 01

10 0 0
T2 = 0 1 0 0
0 0 10
bx by bz 1

34
Abacus Software ST 3D Graphics

Figure 2.4.4

Let’s assume that you want to rotate an object about around all three
axes. It is then possible to combine the rotation matrices Rx, Ry and Rz
by multiplying with Rg. In contrast with the combination of rotations
about the same axis in this example the sequence of multiplications is
important, i.e. Rx*Ry*Rz yields a result different from Rz*Ry*Rx. A
point with a positive Z value is rotated 90 degrees around both the Z and
X axes. If the rotation is first made about the Z axis, the coordinates do
not change, X- and Y-coordinates are equal to zero, and the subsequent
rotation about the X axis rotates the point to the Z=0 level; which is the
X-Y plane.

If the first rotation is about the X axis, the point is transferred to the Z=0
level and the subsequent rotation about the Z axis rotates the point into
the Y=0 level, which is the level between the X and Z axes. This example
shows why it is necessary to follow the sequence of rotations during
program generation.

35
ST 3D Graphics Abacus Software

2.4.1 Rotation about any desired axis

Up to now we have only considered rotation about one of the coordinate


axes; with suitable combinations of various transformations we can turn
an object around any desired line in space. Two points Pl[xl,yl,zl]
and P2 [x2,y2, z2] are sufficient to describe a point in space. The
equation through these two points:

x = xl + t*(x2-xl)
y = yl + t*(y2-yl) with t elements from R
z = zl + t*(z2-zl)

36
Abacus Software ST 3D Graphics

Since the problem for rotation about one coordinate axis has already been
solved, we want to transform a rotation axis in such a way that it will
coincide with the negative Z axis. The sequence of the transformation
looks like this:

Displacement of the coordinate origin to the point


PI [xl, yl, zl] on the line.

Rotation about the angle xw on the X axis, so that the


rotation axis lies in the X-Z plane.

Rotation of the angle y w about the Y-axis until the rotation


axis coincides with the negative Z axis.

It is now possible to rotate the desired angle zw about the Z axis since it
matches the rotation axis. If one looks from PI to P2 a positive angle
will rotate an object in a counterclockwise direction.

37
ST 3D Graphics Abacus Software

To transform back to the original we need:

Rotation of the angle -yw around the Y axis

Rotation of the angle -xw around the X axis

Displacement of the coordinate origin at the starting point.

The only problem is the determination of the angles xw, yw, which can be
derived from the equation. As in Figure 2.4.7 we imagine that the
coordinate origin is already moved to point P1. Then the coordinates of
the point P2' [x2-xl,y2-yl, z2-zl] represent the direction vector
of the lines. This vector is now projected on the Y-Z plane, whereby the
term projection should be taken literally. In addition you should imagine
the vector G [gx, gy, gz] = G [x2-xl, y2-yl, z2-zl] illuminated
by light rays, parallel to the X axis and originating from the positive X
axis. The shadow created in the Y-Z plane is the vector L [0, gy, gz]
and the angle alpha between vector L and the positive axis Z is the
desired angle xw.

In a rotation about the X axis, a positive angle describes the rotation of a


point from the positive Y axis in the direction of the positive Z axis. The
angle alpha is positive and the rotation matrix is as follows:

10 0 0
Rx 0 cos(a) sin (a) 0
0 -sin(a) cos (a) 0
0 0 0 1

38
Abacus Software ST 3D Graphics

sin(a) = gy/1 and cos (a) = gz/1

For the rotation matrix Rx this means:

1 0 0 0
rx — 0 gz/l gy/i 0
0 -gy/l gz/1 0
0 0 0 1

After this transformation, the vector G (P IP2) lies in the plane located
between the positive Y and positive X axis. The angle gamma, which we
defined to be positive, is the desired angle (yw), which rotates the vector
G with one rotation about the Y axis on the negative Z axis. The rotation
matrix Ry:

39
ST 3D Graphics Abacus Software

cos (g) 0 -sin(g) 0


0 1 0 0
sin(g) 0 cos (g) 0
0 0 0 1

It is possible to divide the angle gamma into the partial angles beta and
the right angle alpha’ (90 degrees), between the positive X and negative Z
axes. Through rotation about the X axis the X coordinate of the point P2
has not changed, whereas the Y coordinate has become zero. The sum of
the vector G [gx, gy, gz ] g =V~(gx2+gy2+gz^) is therefore identical
to g =V"(gx^+zlz). From this follows z' =V”[gz-gx2) and from
1 =V (gy2+gz2) = V (g2-gx2) results in z' : z' = 1.

For the angle beta the following relationships result:

sin(b) = 1/g and cos (b) = gx/g

The rotation angle gamma is composed of beta plus 90 degrees,


ga - b + 90

40
Abacus Software ST 3D Graphics

From tlffe addition theorems for sine and cosine we get:

sin <ga)=sin(b+90)=sin(b)*cos(90))+sin(90)*cos(b)

sin(ga) = sin (b+90) = cos(b)

cos(ga)=cos(b+90)=cos(b)*cos(90)-sin(90)*sin(b)

cos (ga) = cos (b+90) = -sin(b)

Since the rotation angle is measured positive, it is possible to include the


information just acquired directly into the rotation matrix.

-sin(b) 0 -cos(b) 0
0 1 0 0
cos (b) 0 -sin(b) 0
0 0 0 1

with the references to the angle functions:

-1/g 0 -gx/g 0
0 10 0
gx/g 0 -1/g 0
0 0 0 1

After these preparatory transformations, the rotation takes place about the
desired angle za about the rotation axis, which is the connecting line
between PI to P2. The matrix for this is:

cos(zw) sin(zw) 0 0
Rz =- sin(zw) cos(zw) 0 0
0 0 10
0 0 0 1

41
ST 3D Graphics Abacus Software

The inverse transformation matrices:

The transformations for one point

-1/g 0 gx/g 0
Ry-1 =0 1 0 0
-gx/g 0 -1/g 0
0 0 0 1

10 0 0
Rx 1 = 0 gz/1 -gy/1 0
0 gy/l gz/1 0
0 0 0 1

10 0 0
T-1 =0100
0 0 10
xl yl zl 1

P'[x',y', z',l]=[x,y,z,1]*T*Rx*Ry*Rz*Ry-1*Rx-1*T~1

In these cases the rotation matrices Rx etc. are combined through


multiplication. The translations are performed separately.

42
Abacus Software ST 3D Graphics

2.5 Projections from space to a two dimensional plane

A window can be made for observation in 3D space just as it can on a


2-dimensional plane. The position of the window and its orientation
relative to the world system is purely arbitrary. For definition of this
observation window you should imagine a second coordinate system, the
view system inside the world system. Its origin lies in the left comer of
the observation window.

As a position parameter which describes the position of the view system


relative to the world system, the two points ORP (Observation reference
point) and ODP (Observation direction point) are sufficient, both of
which are defined in the world coordinate system, as well as perhaps an
inclination angle between positive Y and positive V axis (za), which
describes a rotation of the U-V plane about the Z axis. The view system,
as illustrated in Figure 2.5.1 is a left system. The orientation of the
positive Z axis is opposite to the world coordinate system.

43
ST 3D Graphics Abacus Software

For clarification: Every scene defined in the world coordinate system,


such as an airport for a flight simulator, can be viewed from any point
inside this scene. The only parameters required are the observation
reference point (ORP), which in comparison with a camera, would
represent the film, and the observation direction point (ODP), which
determines the direction in which the observer (the camera) is looking.
The additional angle used (za) between positive Y and positive V axes
describes a rotation of the camera about the longitudinal axis of the
objective. The focal point of the lens at which all light rays passing the
objective meet, would in this example be on the negative Z axis. Keeping
to the example of the camera, exposing a picture must transform the
entire scene into the view system (U-V-Z’).

This transformation, which appears complicated at first glance, has


already been solved: it is the rotation about an arbitrary axis. The points
PI and P2 of the axis of rotation are replaced by the points ORP and ODP
and the angle za describes the inclination of the V axis to the Y axis. All
operations relate to the observation reference point
(ORP [orx, ory, orz]), the positive axis of the observation coordinate
system (view-system) points to the observation direction point
(ODP [odx, ody, odz]). Both points are described in world coordinates
and the rotation matrix rotates the vector G[odx-orx, ody-ory, odz-
orz] to the negative Z axis of the world coordinate system. After fitting
the V axis, the object, which was subjected to the same operations, is
available in the view coordinates. Not quite, though, since the two
coordinate systems still differ in the orientation of the Z axis. Therefore
after fitting the V axis, all Z values must be multiplied by the factor -1
which corrects the orientation of the Z axis. The last step is a
mathematical cosmetic which is required only because of the starting
model of the positive Z axis of the left-hand coordinate system. If one
views the result of the transformation as a right-hand system, the last step
can be omitted.

Let us combine the steps again, considering the steps necessary for
rotation around any desired axis.

44
ST 3D Graphics Abacus Software

1. Shifting the origin to the observation reference point ORP


via the translation matrix Tj.

1 0 0 0
0 1 0 0
0 0 10
-orx -ory -orz 1

2. Rotation around the X axis until the vector G[odx-


orx, ody-ory, odz-orz] = [gx/gy,gz] lies in the
Y-Z-plane.

10 0 0
Rx= 0 gz/i gy/i 0
0 -gy/i gz/i 0
0 0 0 1

with 1 = V(gy2+gz2)

3. Rotation about the Y axis until the vector G[gx, 0, z']


meets with the Z axis:

-1/g 0 -gx/g 0
0 10 0
gx/g 0 -1/g 0
0 0 0 1

with g = V (gx2+gy2+gz2)

l = V7gy2+gz2)

z' = 1

4. Rotation of the Z axis around the za angle for adaptation


of the inclination of the V axis:

cos(zw) sin(zw) 0 0
Rz -sin(zw) cos(zw) 0 0
0 0 10
0 0 0 1

46
Abacus Software ST 3D Graphics

5. Multiplication of the Z coordinates with -1 to convert from


the right-hand to the left-hand coordinate system.

10 0 0
Mi 0 10 0
0 0-10
0 0 0 1

The object now lies in the left-hand coordinate system U-V-Z’ and can be
projected on the display, the plane suspended between the U and V axis
via a suitable perspective transformation.

47
ST 3D Graphics Abacus Software

2.6 Perspective transformation

Since the representation of objects on the screen is limited to two


dimensions, we have to simulate the third dimension, the Z coordinate, in
the two-dimensional plane. The method we used, the central projection,
defines a point in space (the focal point of a lens) at which visual rays
emanating from the object meet. The size of the objects represented on
the display screen is directly proportional to their distance from this focal
point. Equal size objects which are farther away are shown smaller than
objects which are closer to the observer.

l i

*z

Figure 2.6.1: Perspective

The coordinate system from Figure 2.6.1 is, as already indicated, another
coordinate system and the plane suspended between the positive U and
the positive V axis at point z’=0 will represent the screen. The center of
the projection (focal point) is located on the negative Z axis at point
PROZ[prozx, prozy, prozz' ] = [0, 0, prozz' ]. The position of the
point tc be viewed P[pu,pv,pz], appears to be located behind the

48
Abacus Software ST 3D Graphics

observation plane. The line through these two points is described by the
following equation:

u = plu + (prozu-plu)*t

v = plv + (prozv-plv)*t

z' = plz' + (prozz' -plz') *t = 0 , the plane


lies at z'=0 =>

t “ -plz'/(prozz'-plz')

u = plu - (prou-plu)*plz'/(prozz'-plz')

v = plv - (prozv-plv)*plz'/(prozz'-plz')

z' =0

with prozu=prozv=0:

u = plu + plu*plz'/(prozz'-plz')

v = plv + plv*plz'/(prozz'-plz')

z = 0

Since prozz' is negative and plz' is positive, the denominator


(prozz' -plz') becomes negative, and with larger distances between
focal point PROZ and point PI, the point coordinates (in the projection
plane) plu' or plv' become smaller. We are now in the position to
project a three-dimensional representation of the object on the screen and
the distance of the projection-center object is comparable to the focal
length of a camera lens. A short length corresponds to a wide-angle lens
and a larger distance to a telephoto lens. The projections described are
valid for the special case of the projection plane at the point z' = 0. The
project plane can be moved freely on the z’ axis and can be behind the
object or also behind the eye.

49
ST 3D Graphics Abacus Software

In this illustration the projection center is at the point PROZ, while the
object to be projected is the connecting line between the points PI P2.
d designates the location of the projection plane on the Z’-axis, which
can be moved arbitrarily in either direction. If the projection center and
projection plane (d=PROZ) match, all objects degenerate to a single
point, the center of the projection. The size of the projection can be
changed by moving the projection plane. For the line between projection
center PROZ and object point P1 the two point equation holds:

u = plu + (prozu-plu)*t

v = plv + (prozv-plv)*t

z' = plz' + (prozz'-plz')*t = d

50
Abacus Software ST 3D Graphics

The Z’-coordinate of the projection plane is d, and from the equation for
the Z’-coordinate it follows:

t = (d-plz' ) / (prozz' -plz' ) inserted into the linear equation


results in the projection coordinates:

u' = plx + [(prozu-plu) * (d-plz')] / (prozz'-plz')

v' = p1v + [ (prozv-plv) * (d-plz')] / (prozz'-plz')

z' = d

Every point P[u,v,z',l] is transformed into the display coordinates P


' [u',v',d,l]. The coordinates u' and v' represent a point on the
screen.

The equation derived from Figure 2.6.1 comes from the special case
where the projection center lies on the Z axis prozu=prozv=0 and
when the projection plane is on the z'=0 plane, d=0. The following
illustrations show how the selection of the various observation parameters
(ORP, PROZ, d) influence the appearance of the projection. The
coordinate origin of the display is in the lower left comer of the screen.

51
ST 3D Graphics Abacus Software

• projection center

Figure 2.6.3

52
Abacus Software ST 3D Graphics

+Z
resulting projection
Figure 2.6.5

54
Figure 2.6.6
ST 3D Graphics Abacus Software

2.7 Hidden lines and hidden surfaces

Up to now we have been in the position to project wire models of objects


on the screen. The action sequence of most any computer animation is set
up with the help of 3-D wire models. Wire models can be handled in real¬
time and thus shorten the development of the animation sequence
considerably. Once the sequence is set, the computer calculates the
visible surface and color nuances and light reflections of the objects for
every intermediate point of the movement, according to the illumination.
Generally the scan line algorithm is used. Seen from the eye, the vision
rays are tracked through each pixel of the display (= projection plane) to
the individual objects. The visual ray is either reflected, absorbed, or
wholly or partially transmitted by various objects with differing surface
characteristics. Under certain conditions the visual ray splits, such as on a
glass surface, into a reflected and a second visual ray which passes
through the object, naturally both must be tracked. This explains the
computation time of about 10 minutes which even super-computers like
the Cray II require for a picture.

Since by conservative estimate the throughput of the Cray II is superior to


that of the Atari ST by a factor of about 10,000 to 15,000, it should be
clear that the ST is somewhat “under powered" for such calculations.
Therefore we will limit ourselves to the “surface algorithms" and will not
determine the visibility of every point, but just for each surface of the
object. These algorithms are fast. To be accurate, they are valid only tor
convex bodies, and in the version presented here the surfaces of the
bodies must also be convex.

56
Abacus Software ST 3D Graphics

Figure 2.7.1: Convex and Concave Surfaces

With convex polygons the line connecting two points on the polygon lies
within the polygon, whereas in convex bodies the connecting line
between two points on the surface passes through the body or runs along
the surface. Formulated differently, convex polygons have at least one
inner angle which is larger than 180 degrees.

For these surface algorithms we must expand the object definition, which
up to now consisted of the point and line list, to include a surface list. The
surface list contains a description of each surface by the lines which
border the surface.

57
ST 3D Graphics Abacus Software

The two surfaces I and II would be described in the surface list as


follows:

Surface Line from point to point

I Pl,P4 P4,P3 P3,P2 P2, Pi


II P5,P6 P6,P7 P7,P8 P8, PI

You probably noticed that the line direction is reversed in the description
of the surfaces. The line vectors of surface I describe the surface as seen
from the negative Z axis in a clockwise direction, while surface 11 is
described in a counterclockwise direction. This small difference contains
the solution to the hidden-line-problem. If you imagine the surfaces I and
II as outer surfaces of a block, then SI is the front surface and SI I the
rear surface of the block. The observation point is still on the negative Z
axis. SI I is not visible from the observation point since it is hidden by
the other surfaces.

58
Abacus Software ST 3D Graphics

You can see that the description of the surface is always done in the
clockwise direction from outside the cube and looking toward the current
surface center. For the definition of the surface one wanders around the
object to be described and determines the direction of the connection
lines of the points belonging to the surface. As one can see in the next
illustration, the visibility of the surfaces can be determined through the
direction of the connection lines with a little vector algebra.

To do this, start from any point on the surface and form the vector to the
next point

P= [px,py,pz] = [p2x-plx,p2y-ply,p2z-plz] ,

and the vector to the next point

Q [qx, qy, qz] = [p3x-plx, p3y-ply, p3z-plz ],

as well as the projection vector from a point on the surface to observation


point A. An appropriate selection is the point

PI,S[sx,sy,sz] = [ax-plx,ay-ply,az-plz].

As explained in the appendix, the product of two vectors (a\b) (see


App. B) forms a vertical vector

R= [rx, ry, rz ] =[py*qz-pz*qy,pz*qx-px*qz,px*qy-


py*qx].

The direction of this vector results from the system in which the vector
product was performed. In the left coordinate system used here, the
vector d points in the same direction in which a screw with a left-handed
thread would move from P to Q when turned, that is, it points with
surface I in the direction of the positive Z axis and with surface II in the
direction of the negative Z axis.

Now we can say this about the visibility of surface I: if the vectors S and
R are pointing in the same direction, the surface is visible from the
observation point. If the vectors S and R point in different directions, the
surface is not visible. As mentioned earlier, this process is limited to
closed convex bodies, but the error is not very large with concave bodies.

59
ST 3D Graphics Abacus Software

Figure 2.7.3-4: Hardcopy of bodies before and after


Hidden-Line-Algorithm

60
Abacus Software ST 3D Graphics

Figure 2.7.5
61
ST 3D Graphics Abacus Software

Figure 2.7.6

Figure 2.7.7
62
Abacus Software ST 3D Graphics

The error with concave bodies is that surfaces which are visible from the
observation point are hidden by other surfaces but are not recognized.
Now only the "direction comparison criterium" between two vectors is
missing. This is accomplished by the scalar product of two vectors
(S *R) which is defined as follows:

c = |SI * IRI*cos(Phi) = sx*rx+sy*ry+sz*rz

cos(alpha) > 0

Figure 2.7.8

c is a real number and phi is the angle enclosed by S and R. From


Figure 2.7.8 we can see that the vectors a and b point in the same
direction when cos (phi) is positive. The recognition of hidden
surfaces can be summarized as follows.

1. Creation of a surface list in which the points are listed in a


clockwise direction.

2. Finding the vectors P and Q from three successive points


for each surface.

63
ST 3D Graphics Abacus Software

3. Determination of the vector S [sx, sy, sz ] from a point


on the surface to the observation point.

4. Determination of the vector perpendicular to P and Q


R [rx, ry, rz ] through the vector product (P\Q).

5. Comparison of the direction of the vectors S and R by


checking the sign of the scalar product (S*R) through
multiplication of the single components from S and R
(Scalar product = sx*rx+sy*ry+sz*rz)

6. Marking of surfaces which have positive scalar products as


visible surfaces. (Applies to left coordinate systems. In
right coordinate systems the surfaces with negative scalar
products are visible surfaces.)

7. Drawing the visible surfaces.

64
Abacus Software ST 3D Graphics

2.8 Rembrandt and hidden surfaces

You probably want to know what computer graphics and a painter who
died in 1669 have in common. An oil painting is created from back to the
front, that is to say, the painter first draws the background and then
objects are placed further to the front simply by covering the background
with oil paint. This method, carried over to the computer, is another
solution of the hidden surface problem. A middle Z coordinate is
calculated for each surface and, as an example, all Z coordinates of the
comer points can be added and divided by the number of comer points
which are stored for the surface. Then the surfaces are sorted according to
size and drawn from the largest to the smallest Z coordinates.

To insure that the surfaces which are painted over have really been
covered, we can’t just to draw the outer lines of the surface. It is
necessary to fill the surfaces with color. The surface construction from
the back to the front is shown in the following illustrations.

Figures 2.8.1-5: Hardcopy of the surface construction

Figure 2.8.1
65
ST 3D Graphics Abacus Software

Figure 2.8.2

Figure 2.8.3

66
Abacus Software ST 3D Graphics
ST 3D Graphics Abacus Software

Figure 2.8.6

Figure 2.8.7

68
Abacus Software ST 3D Graphics

Figure 2.8.8

Of course, the two methods for the removal of hidden surfaces can be
combined. First the visible surfaces can be determined through scalar
products. Followed by sorting the surfaces according to descending Z
coordinates, and then drawing them.

69
ST 3D Graphics Abacus Software

2.8.1 Light and Shadow

In general, there are two types of illumination, direct and indirect. With
indirect illumination the intensity of the light is equal on all places in
space. The indirect light is created through diffuse reflection from other
objects, such as walls and ceilings. The appearance of an object in space
under this illumination is dependent only on the reflection coefficient of
the object. This reflection coefficient is the relationship of reflected light
rays to the total striking the surface. Its value runs from zero for a black
body (all light rays which strike are absorbed) and one for a white body
(all light rays which strike are reflected). A body whose reflection
coefficients are between zero and one is designated as a gray body. A
reflection coefficient R can be given for every surface which determines
the intensity of the surface.

Intensity = R * IL with IL = Intensity of available indirect


light.

A more realistic representation results from the definition of one or more


point light sources in the space. These point light sources, for example
lamp, candle, or sunshine, have a certain position in the space and shine
in the direction of the object. In this case, the orientation of the
illuminated surface to the light source is of great importance. More light
rays fall on a surface which is perpendicular to the light source than an
equally large surface which is not perpendicular to the light source.

The orientation of the surface to the light source can be determined by


comparing the normal vector of the surface (the vector perpendicular to
it) with the vector to surface from the light source. If L and N are two
vectors of length 1, the relation for the angle between L and N is:

L*N = lx*nx+ly*ny+lz*nz = cos(w)

For the gray value of the surface the result is then:

Intensity = R*IL + R* (L*N) *DL

with the reflection coefficient R and the intensity of the direct light source
DL, which is between zero and one.

70
Abacus Software ST 3D Graphics

Figure 2.8.9: Surfaces with Light Rays

71
r
Machine Language Fundamentals
for Graphic Programming
Abacus Software ST 3D Graphics

3. Machine Language Fundamentals for Graphic Programming

All programs described in this book may be run on various ST


computer/monitor combinations. To simplify the compatibility, all
drawing functions for the 3-D graphics project were done with operating
systems functions (line-A). To introduce you to machine language
programming on the ST, we first have an explanation of some of the basic
principles (sine) and then a small program for drawing random lines. This
program illustrates the program interface to the operating system and a
simple line-drawing algorithm which writes directly to the screen. The
line-drawing algorithm is not necessary for the 3-D project coming later
and is intended only as an example. The use of the algorithm is limited to
monochrome monitors. Owners of color monitors can replace the call
drawl with ddrawl (indicated in the listing) if they want to run the
program mainl. s.

75
ST 3D Graphics Abacus Software

3.1 Speed Advantages from tables

Before starting a project in machine language, you should think about the
number format to be used. For all the following applications we can
perform all calculations with 16-bit integers. Another problem is the sine
function, whose function values can range from -1 and +1. The function
values can be approximated on computers using the Taylor series, which
approximates the exact function value through repeated summation of the
terms of a sequence. In practice, the summation can be terminated after 3
or 4 terms. As an example, we have here the Taylor series for the sine
function.

sin(x) = x - xV3! + x5/5! - x7/7! + ...

The angle x is given in radians, and 3! means 3 factorial = 1*2*3 = 6.


This method is not suitable for quick calculation of sine and cosine values
because several multiplications must be performed for each function
value. A rather unelegant but simple and common solution is to store all
the necessary function values in a table in memory, which can then be
accessed very quickly.

The accuracy can be set as desired since the function values are
calculated before the actual program application and the time factor does
not play a role. In our example, all sine values between 0 and 360 degrees
are entered in steps of one degree. This is quite adequate for almost all
applications which require trigonometric functions. Should an
intermediate value be required, it can be interpolated from the table.
Since the cosine function is the same as the sine function shifted by 90
degrees, the cosine functions can also be taken from the sine table.

The function values of the angle functions are real numbers which are
floating point numbers with several places after the decimal point. Since
all our calculations involve only integers, it is necessary to transform the
values of the sine function. This is done by multiplying by a sufficiently
large number-in our example with 214 = 16384.

76
Abacus Software ST 3D Graphics

The length of the line c and the angle alpha are already known, and we
want to find the length of b. According to the definition of the angle
function, the length of the distance =c* sin (a) = 20*sin(45). The
sine of 45 degrees is 0.707106781 with nine-place precision. In our table
we have the value 0.707106781 * 16384 = 11585 for 45 degrees. After
multiplying by 20 we got the number 231700 as a result. We don’t have
to worry that this number will exceed the value range of 16-bit integer
arithmetic because the processor always produces a 32-bit product as the
result of a 16-bit multiplication. This 32-bit result, the number 231700,
can now be adapted to the original value range by dividing by 16384, and
we get 14 as the result.

You may ask yourself why 16384 was used for the multiplication: first of
all the number is large enough to extend the range of the sine function.
Numbers between -1 and 1 become numbers between -16384 and
+ 16384. Second, the multiplication can be performed with two very fast
commands of the processor. Multiplications by a multiple of two can be
replaced in all microprocessors with shift commands which don’t take
much more time than an addition.

77
ST 3D Graphics Abacus Software

At this point I would like to briefly discuss the possibilities of the table
representation in the computer. The sine table is the simplest form of a
table, a linear list. The individual table values are stored sequentially in
memory. Our sine table for the first values looks like this:

sintab: .dc.w 0,286,572,857,1143,1428,1713,1997,2280


.dc.w 2563,2845,3126,3406,3686,3964,4240,4516
.dc.w 4790,5063,5334,5604,5872,6138,6402,6664

Since the gradations of the angles are in 1 degree steps, the first table
value gives the sine of 0 degrees, the second the sine of one degree, the
third the sine of two degrees, etc. The 91st table value is the sine of 90.
table value and the sine of 360 degrees is represented by the 361st value.
Zero is chosen as the start to match the table numbers to the
corresponding angle. This means that table value zero represents the sine
of zero degrees. Value number 90 corresponds to 90 degrees and 180 to
180 degrees. The 68000 computer makes access to this table very easy
through its excellent addressing capabilities. The initial address of the
table is loaded into the address register. This is the address where the zero
element is stored. With the number of the desired table value in a data
register it is possible to access the location using the addressing mode
"address register indirect with index." In this table format it is absolutely
necessary to pay attention to the data length of individual entries. The
address of the zero value is equal to the beginning address of the table
plus zero, but the address of the first value is the beginning address of the
table plus two, since each value occupies two bytes. This means that the
index number in the data register must be multiplied by the number of
bytes for one entry. In this case it is two bytes. This multiplication by two
is very fast with one left shift of the bits in the index number.

78
Abacus Software ST 3D Graphics

3.2 Assembler routines for screen manipulation

The screen of the Atari ST is organized using what is called bit-mapped


graphics. This means that bits which are set in the screen storage
correspond directly to dots on the monitor and therefore there is no
difference between text and graphics. Since the screen memory is part of
the main memory of the CPU, it can be manipulated quickly, i.e. without
waiting cycles. For monochrome display the resolution is 640*400 points,
which are represented by 400 times 640 bits in RAM.

Address: $78000_$78001_$78002_■■■$7804F X 0>*= X >- 639 ^

$78000 76543210 76543210

$78050 Bil number

$780A0

$780F0 $780F1

Y 0 >- Y >- 399


$7FCB0

1 r

Figure 3.2.1

The only routines required for screen manipulation are those for
displaying a point and for drawing and erasing lines. A line of the video
picture is formed from 80 bytes and the total picture is made up of 400
lines. The address of a picture point can be calculated as follows:

address = screen start + Y*80 + INT(X/8)

79
ST 3D Graphics Abacus Software

The bit number of the byte can be obtained with the following formula:

number = 7 - (X MOD 8)

The function I NT truncates the positions after the decimal point of a real
number, while the function MOD returns the remainder of the operand by
the second. For example, 9 MOD 2 returns 1 as the result Screen start is
the starting address of the screen memory, which is $78000 on the 520 ST
and 8F8000 on the 1040 ST

It may appear to be somewhat unusual to have the coordinate origin in the


upper left comer, but it is easy to change to the lower left comer and this
is accomplished by negating the Y values and adding 399. The X
coordinates remain unchanged of course , since the zero point is already
in the left comer of the display. The Y coordinate 370 in a normal left
system becomes (-370+399) = 29 in the screen system. This conversion
need be made only immediately before points are drawn. Some
calculations are required to draw a single point. The speed advantage of
tables for the calculation of the address of a point should also be
considered here. This table holds the RAM address for every possible Y
coordinate. This saves a multiplication for every calculation of the screen
address. Since the plot-point routine is used very often for drawing lines,
the speed advantage gained by using this table is correspondingly great.

3.2.1 Drawing lines

Since the size of a point on the screen is dependent on the resolution of


the computer, it is not possible to represent a line in the mathematical
sense. A line which connects two points PI and P3, actually takes a more
or less jagged path.

80
Abacus Software ST 3D Graphics

Xi

Figure 3.2.2

Starting from point PI, you have the problem of deciding which points
must be set, in order to reach point P3. Note that it is possible to set the
points only at the intersections of the raster lines. The line is formed when
either the X coordinate is retained and a point drawn with an incremented
Y coordinate or you can increment the X coordinate while the Y
coordinate retains its value.

In mathematics, a line which connects two points is described through its


slope m. m is a measure of the "steepness" of the line and the larger m
becomes, the steeper the line becomes. With a positive m, the line rises
from left to right, while with a negative m it slopes down from left to
right For a line parallel to the Y axis, the slope is infinite. The expression
for the slope:

m = dy / dx

81
ST 3D Graphics Abacus Software

Figure 3.2.3

See Figure 3.2.4 for an explanation of the algorithm for drawing of lines.

82
Abacus Software ST 3D Graphics

Figure 3.2.4

Let us assume that in drawing the line from PI to P3 that we have


already reached the point P2 already and now have to decide the direction
in which to draw. In our example, the point P2 is ’’over" the ideal line
from PI to P3. Expressed mathematically, the slope of the connecting
line from Point PI to P2 ml=(p2y-ply)/(p2x-plx) = wy/wxis
greater than the rise of the line which connects the points PI and P3
m2= (p3y-ply) / (p3x-plx) =dy/dx. As the illustration shows, the
next step in drawing must be made in the X direction.

With the comparison of the two slopes, we have found a decision


criterion for the direction of drawing: If the slope of the connecting line
between the starting point of the drawing PI and an intermediate point
P 2 is greater than the slope of the line between the beginning and end
points (PI, P3), a drawing step should be made in the X direction . If
the slope is smaller, the next point should be drawn in the Y direction.
For the purpose of programming this criterion we shall define a decision
variable D, which is assigned the difference between the desired and the
actual slope.

83
ST 3D Graphics Abacus Software

D = (dy/dx)-(wy/wx)

If D is larger than zero ==> Step in Y direction

If D is smaller than zero ==> Step in X direction

After a small conversion we get:

D*dx*wx = (wx*dy)-(wy*dx)

Multiplications slow down calculations, so we should try to eliminate


them from the calculation. The exact value of D is of no interest. It is only
important to know how D changes with a step in the X or Y direction so
that an eventual change in the sign of D can be recognized. For this
reason it is also possible to replace the expression D*dx*dy with D
again.

D = (wx*dy) - (wy*dx)

During a step in the X direction, wx is increased by one while we retain


the old value of wx. For our D which we call new D or ND to distinguish it
from D, the following results:

ND = (wx+l)*dy - wy*dx

ND = wx*dy + dy - wy*dx

The last expression is equal to old D + dy, where old D corresponds to


the value of D before the step in the X direction. Analogous to this for a
step in the Y direction:

ND = wx*dy - (wy+l)*dx

ND = wx*dy - wy*dx - dx

As you can see, D is reduced by dx with a step in the Y direction. For ND


can be written:

Step in Y direction ND = D - dx
Step in X direction ND = D + dy

84
Abacus Software ST 3D Graphics

The multiplications have been replaced according to our desires by


additions. To formulate the algorithm, we must still decide in what
direction we will draw if D is zero. This can be decided at random and in
our example ND=0 results in a step in the Y direction. Another special
case which has not been mentioned is when dy is zero. In this case, steps
can be made only in the X direction since the resulting line must be a
parallel to the X axis. This case can only be determined with a test at the
beginning of the routine.

Furthermore, we have only considered lines with a positive slope, that is,
those where py3 is smaller than pyl. To retain the decision method in
this form, it is necessary to make negative dx and dy values positive
through multiplication with -1, and to decrease the X and Y coordinates
by one instead of increasing them for every step in the X or Y direction.
The algorithm for drawing a line between the points PI [xl, yl] and
P 3[x3,y3] appears like this in a structogram:

85
ST 3D Graphics Abacus Software

Figure 3.2.5: Structogram Draw line

86
Abacus Software ST 3D Graphics

3.3 Operating system functions

Since we will use only operating system functions for the 3-D graphics
programming, some should be explained before they are used. One of
these functions is the routine for switching the beginning address for the
video controller. All computers which which can display animated
graphics quickly and flicker free have the ability to work with two logical
screen pages internally.

Fast drawing and erasing of objects on the screen and the rapid accesses
to the screen RAM by the computer and the video controller, causes the
monitor picture to be unstable and to flicker. If the hardware has the
ability to tell the video controller where in RAM the screen memory
starts, the strategy for the creation of flicker free graphic is very simple.

We define two logical screen pages. We will use the Atatri ST as a


concrete example: in the Atari ST with 512K RAM the standard screen
page is stored between $78000 to $7FFFF and it is possible to define a
second screen page from $70000 to $77FFF. In the initial state, both
screen pages are erased and the video controller shows the page starting
at $78000. Now the first picture can be drawn in the RAM starting at
address $70000. After drawing the picture, the video controller is
informed at a suitable time of the new beginning address for the screen
RAM ($70000). A suitable time for switching is the time period in which
the electronic beam which draws the video picture returns, without being
seen, from the lower right comer of the screen to the upper left corner.

This moment is even recognized by the operating system and the


switching of the screen pages can be solved without any major
programming effort. If the page starting at $70000 is being displayed by
the video controller, the CPU can draw another picture, such as the object
in another position, in the page starting at $78000 without disturbing the
picture construction. After the new picture is completed, pages are
switched again and you can erase the old picture in the storage area which
is not being displayed. In general, the page which is being displayed is
considered to be the physical page in which the drawing is taking place is
the logical page. Only when both are identical do you see the progress of
the drawing on the screen.

87
ST 3D Graphics Abacus Software

3.3.1 Starting a Program

To start a machine language program on the Atari ST you have to know


what happens when a program icon is clicked with the mouse. The
operating system loads the appropriate program and passes control to the
program once it is loaded. After loading a program, the operating system
declares the entire memory as occupied so that it is not possible to move
data or program sections. To avoid this disadvantage, the called program
must determine its actual memory requirements, declare this area as
occupied, and leave the rest of the memory free. The Atari operating
system simplifies this task by passing a pointer on the stack to the called
program indicating the memory area occupied by the program and data.

The called program can calculate the memory actually required and
declare the unused area as vacant to the operating system. Note:
sufficient space must be reserved for the processor stack. From the
Digital Research documentation, it is not clear how much stack space is
required for the GEM functions, but the 4K bytes reserved for this
purpose in the example should be sufficient for all purposes. To make it
possible to use all GEM functions, it is recommended that the program
call the functions Application-Init and then Open-Virtual-
Workstation when it starts. After these two calls, GEM-DOS, the
BIOS, Extended-BIOS and the AES and VDI functions are available to
the program. An overview of these functions are available in the two
Abacus Software books Atari ST Internals and the large Atari ST GEM
Programmer’s Reference.

All programs in this book were written using the assembler from Digital
Research. For users of other home computers the assembler is probably
new, and so I want to discuss it briefly. The assembler is completely disk
oriented, i.e. all input and output comes from and goes to the diskette.
First you create the source text of the program with an editor, store it on a
diskette and call the assembler with name of the source text The
assembler processes the source text by creating several auxiliary files on
the diskette. Finally it writes the desired object file on the diskette.

The object file which was created, recognizable by the extension . o, is


not executable since it was assembled at the absolute address zero. To
generate an executable program the absolute addresses must be replaced
with relative addresses to make it possible to load the program into any
memory area. For this purpose, you call the program RELMOD.PRG

88
Abacus Software ST 3D Graphics

which then creates the desired run-time program file. In this you can
write manner machine language programs whose length is limited only by
the storage capacity of the computer and the floppy disk. It is impossible
to combine two programs which are already object files with this method,
however.

For this reason, one usually adds an intermediate step, as is also done
with higher level languages, called linking. The linker permits several
separately-assembled object files to be combined into one single file.

Large assembly language programs quickly become difficult to


understand and it is recommended that they be divided into at least two
modules. The first module initializes the program and contains all of the
error-free and tested subroutines, while the second module contains the
latest main program. This can reduce the assembly time considerably
since the large basic module must be assembled only once and afterwards
only linked to the main program. The use of the linker also permits the
use of assembler directives which would otherwise not be possible. The
assembler in conjunction with the linker can manage three separate
program areas: text, data, bss. The text area contains the actual program,
i.e. the program text, and the data area contains the initialized data. These
are variables to which values were assigned already before the start of the
program. In the bss area there is storage space reserved for the data which
has not been initialized.

Each of the programs; assembler, linker and relocator require parameters,


which are passed during the start. To assemble the basic module, first
select AS68.PRG and then INSTALL APPLICATION from the
OPTIONS menu as TOS-takes parameters. Then enter the following line
into the dialog box which appears:

-p -I -u basicl.s > basicl.lst

where basic 1. s is the name of the text file to be assembled. The -p


and > basicl.lst statements create a listing to the disk of the
assembly process which can later be printed for examination. The
assembler creates a file with the name basicl.o. This object file
contains the tested subroutines and will be linked to the current main
program.

89
ST 3D Graphics Abacus Software

To assemble and link the main program, it is best to create a batch file,
which contains the individual command sequences. The batch file could
look like this:

as68 -1 -u %2. s
wait.prg
link68 [u] %2.68k=%l.o,%2.o
relmod %2.68k %2.prg
rm %2.68k
rm % 2.o
wait.prg

This batch file might be stored under the name aslink.bat on the
diskette. The batch file is made very flexible through the use of two place
holders, %1 and %2. To assemble the main program with the name
mainl. s and the subsequent linking with the basic module basicl. o
You call the program batch.ttp and pass the command sequence in
the dialog box:

aslink basicl mainl

After the assembly process the desired program file mainl.prg is


finally on the diskette. This creation of modules makes working with the
disk drive more bearable and the coffee breaks during assembly shorter.

As a practical test of all this, we have here the first version of the
basicl. s program and the first demo program. The basic program
contains only the initialization of the program and the basic routines for
screen manipulation such as screen erasing, and drawing of points and
lines. Assembly is done with:

as68 -1 -u basicl.s

The first main program demonstrates the speed of the computer by


drawing random lines and demonstrates how to call the operating system.
The steps for the creation of the ready-to-run program file mainl .prg,
without using a batch file are as follows:

1. Assemble MAIN1.S with the AS68.PRG.

90
Abacus Software ST 3D Graphics

2. Link the two object files with the


Linker.
Iink68 [u] mainl.68k = basicl.o,mainl.o

3. Create a relocatable program with


relmod mainl.68k mainl.prg

The file mainl. prg can be started by clicking with the mouse after the
file Relmod, the two files mainl. o and mainl. 68k, which are no
longer needed, are erased with the program RM.

The listing should be self-explanatory with all of its comments. It should


offer an easy introduction to graphics programming in machine language.
More detailed explanations of the routines used can be found with the
explanation of the link files grlinkl.s in section 4.1. Starting with
Chapter 4 we will really start to program.

91
ST 3D Graphics Abacus Software

***********************************************************************
* Link file basicl.s, is linked with the main program whose entry *
* routine must have the name main. *
* U.B. 11.85 *
***********************************************************************

.globl wait,wait1,drawl,ddrawl,inlinea
.globl grafhand
•globl grhandle
.globl global,contrl,intin,intout,ptsin,ptsout,addrin,addrout
.globl apinit,openwork,clwork,aes,vdi
.globl inkey
.globl mouse_on,mouse_off,printf

.text

*******************
* Entry to the program, initialization of all operating system *
* functions and creation of the Y-tables (For computers with color *
* monitors, replace "jsr startl" with "jsr start2". *
* Furthermore when using a color monitor/ replace all *
* "jsr drawl" calls in the main program with "jsr ddrawl".

**********************

art: ★ initialize the program

movei1 a7, a5 ★ Base page address is on the stack

move.1 A (a5),a5 * base page address = program start - $100

move.1 $c(a5),dO * Program length

add. 1 $14(a5),dO ★ Length of initialized data area


add. 1 $lc(a5),dO ★ Length of data area not initialized
add. 1 #$1100,dO * 4 K-Byte user stack=sufficient space

move.1 a5,dl ★ Starting address of the program

add. 1 dO, dl * Plus number of reserved bytes = space required

and. 1 #-2,dl * even address for stack

move.1 dl, a7 * User stackpointer to last 4K- byte

move.1 dO, -(sp) * Length of reserved area

move.1 a5, -(sp) * Beginning address of reserved area

move.w dO,-(sp) ★ Dummy-Word

move.w #$4a,-(sp) ★ GEM DOS function SETBLOCK

92
Abacus Software ST 3D Graphics

trap #1
add. 1 #12,sp ★ old stack address restored again

jsr start1 * Create Y-table

jsr main * Jump to main program. ( User-created


move.1 #0,- (a7) * Terminate program running

trap #1 ★ Back to Gem-Desktop

********************************************************************
* Call a AES-Routine, where the parameters are passed to *
* to the various arrays (contrl,etc.) *
********************************************************************

aes: move.1 #aespb,dl * call the AES routines


move.w #$c8,d0
trap #2
rts

************************************************************************
* Call a VDI-Routine *
ft********************************************************************

vdi: move.l #vdipb,dl * call the VDI routines


move.w #$73,dO
trap #2
rts

**********************************************************************
* Announce the program *
**********************************************************************

clr. 1 dO * Announce the program as


move.1 dO,aplresv * Application
move.1 dO,ap2resv
move.1 dO,ap3resv
move.1 dO,ap4resv
move.w #10,opcode
move.w #0,sintin
move.w #1,sintout
move. w #0,saddrout
move.w #0,saddrin
jsr aes
rts

93
ST 3D Graphics Abacus Software

***********************************************************************
* Check on screen handler and store for other functions *
***********************************************************************

grafhand: move.w #77,contrl * Get the screen handler


move.w #0, contrl+2 * and store it in the global
move.w #5,contrl+4 * Variable grhandle
move.w #0,contrl+6
move.w #0,contrl+8
jsr aes
move.w intout,grhandle
rts

***********************************************************************
* Open a Virtual Screen Work Station where all GEM drawing functions *
* will occur. *
***********************************************************************

openwork: move.w #100,opcode * open a workstation


move.w #1, d0

move.w #0,contrl+2

move.w #11,contrl+6

move.w grhandle,contrl+12 * screen handler


move.w dO,intin

move.w dO,intin+2

move.w dO,intin+4

move.w dO,intin+6

move. w dO,intin+8

move.w dO,intin+10

move. w dO,intin+12

move.w dO,intin+14

move.w dO,intin+16

move.w dO,intin+18

move.w #2,intin+20

jsr vdi

rts

94
Abacus Software ST 3D Graphics

*'*******★★*******★***★**★★★**’***★***★*★★**★**★***★**★★**★★★★★*******★★*
* Clear the workstation *
***********************************************************************

clwork: move.w #3,contrl * Clear workstation

move.w #0,contrl+2 * clear the screen

move.w #1,contrl+6

move.w grhandle,contrl+12

jsr vdi

rt s

* Turn on the mouse and its control. *

move. w #122,contrl * turn on the mouse

move.w #0,contrl+2 * its control

move.w #1,contrl+6

move.w grhandle,contrl+12

move.w #0,intin

jsr vdi

rts

***********************************************************************
* Turn off the mouse and control. *
***********************************************************************

mouse off: move.w #123,contrl * turn off the mouse

move.w #0,contrl+2 * its control

move.w #0,contrl+6

move.w grhandle,contrl+12

jsr vdi

rts

95
ST 3D Graphics Abacus Software

********************************* *************************************
*
* Write a string on the screen
********************************* *************************************

printf: move. 1 aO,- (a7) * write the string, whose

move. w #9,-(a7) * beginning address is in

trap #1 * register AO, on the screen.

addq. 1 #6,a7 * String must terminate with

rts * zero.

dbra dO,waitl * Time loop, counts the dO-Register


wait 1

rts * down to -1

wait: move.w #1, -(a7) * wait for a key stroke

trap #1 * GEM-DOS-Call

addq.1 #2,a7

rts

* Sense keyboard status (does not wait for keypress) and return key *

* code and also the scan code.

inkey: move.w #2,-(a7) ★ Sense keyboard, does not wait for key

move.w #1,-(a7) * activation and return an ASCII-code

#13 ★ of an activated key in the lower half


trap

addq.1 #4,a7 ★ of the long word of DO, and the scan code

tst. w dO * in the upper half of the long word of

endkey ★ DO.
bpl

move.w #7,-(a7)

trap #1

addq.1 #2,a7

endkey: rts

96
Abacus Software ST 3D Graphics

e********************************************************************
* Draw-line-routine, draws directly into the screen storage and is *

* used only for the high resolution mode {640*400 Points ). For color *

* monitor use ddrawl *

************************************ **********************************

drawl: move. 1 d7,-(a7) * Save register

move.1 #ytab,aO * Address of the Y-table

clr. 1 d4

move. w #1, a4 * X step = +1

move. w a4, a5 * Y step = +1

move. w a2,d6

sub. w d2,d6 * DX in d6 = X2 - XI

bge dxispos

neg. w d6 * If DX is negative, then

move. w #-l,a4 * make positive through negation

dxispos: move .w a3,d7

sub.w d3,d7 * DY in d7

bgt plotit * If DY is larger than zero draw then

beq dyis 0 * first point

neg. w d7 * DY is negative, make positive

move .w #-l,a5 * Y-Step is then -1

bra plotit

dyis_0: not. w d4 * If DY = 0 then parallel to X-Axis

plotit: tst .W d2 * Test if drawing area was

bmi draw_it * exceeded

tst. w d3

bmi draw_it

cmp. w #639,d2

bhi draw_it

cmp. w #399,d3

bhi draw_it

move.w d3,d0 * Y-value times two for access to

lsl. w #2, dO * Plot table

move.1 0(aO,dO.w),al * Screen address

move.w d2,dl * X-value

lsr .w #3, dl * INT (X/8)

move.w d2,d0 * X-value

not. w dO * -X

97
ST 3D Graphics Abacus Software

**********************************
* Here the point is drawn *
**********************************
bset dO, 0(al,dl.w) * 7-(X MOD 8) with the bset-command

cmp. w d2,a2 * End X reached?


draw it:

bne notend * no

cmp. w d3, a3 * End Y reached?

beq endit * no
★ D > 0 => Y step
notend: tst .w d4

bge ystep

add. w a4,d2 ★ else X step X=X+-1


xstep:

add. w d7,d4 ★ ND = D + DY

bra plotit

add. w a5, d3 * Y=Y +- 1


ystep:

sub. w d6, d4 * ND = D - DX

bra plotit

drawend:
* restore register
endit: movem. 1 (a7)+,d7

rts ★ Return to calling program

**********************************************************************
* This Draw-line-routine is universal for all monitor types and *

* can be used with all resolutions. *


**********************************************************************

move.1 d7,-(a7)

move.1 tlineavar,aO

move.w d2, 38 (aO) * XI

move.w d3,40(aO) * Yl

move.w a2,42 (aO) * X2

move.w a3,44(aO) * Y2

-dc. w $a003 * draw line

move.1 (a7)+,d7

rts

98
Abacus Software ST 3D Graphics

**********************************************************************
* Initialize the Line-A variables and store the address of the *

* Variable block in lineavar. *


**********************************************************************

inlinea: .dc. w $a000 ★ initialize the Line

move.1 aO,lineavar

move.w #0,32 (aO)

#$ffff,34 (aO) * Sample of the line


move.w

move.w #0,36(aO) * Writing mode


* drawing color
move.w #1,24(aO)

rts

* Creation of the Y table for the highest graphic mode (640*400) *

startl:

move. w #2,-(a7) * checks the screen address of the

trap #14 * System, recognizes which computer

addq. 1 #2,a7

move.1 d0,physbase * Display start minus 32 K-Byte

move. 1 #399,dl * Number of lines minus one


* Physical address
move. 1 #ytab, aO

stloopl: move.1 dO,(aO)+ * New address equals old address

add. 1 #80,dO * plus 80

dbra dl,stloopl

rts

* Line-A initialization *

start2: jsr inlinea * Initialize line A

rts

99
ST 3D Graphics Abacus Software

********************************************************************
* Variables of the basic program *
********************************************************************

.even
.bss
lineavar: .ds. 1 1 * Storage for address of Line-A variable
physbase: .ds. 1 1 * Storage for screen address.

ytab: .ds. 1 400 * Storage for the Y table


contrl: * Arrays for AES and VDI functions
opcode: .ds .w 1
sintin: .ds.w 1
sintout: .ds. w 1
saddrin: .ds.w 1
saddrout: .ds.w 1
.ds.w 6

global:
apversion: .ds.w 1
apcount: .ds.w 1
apid: -ds.w 1
apprivate: .ds. 1 1
apptree: .ds. 1 1
aplresv: .ds.l 1
ap2resv: .ds. 1 1
ap3resv: .ds.l 1
ap4resv: .ds.l 1

intin: .ds.w 128


ptsin: .ds.w 256
intout: • ds.w 128
ptsout: .ds.w 128
addrin: .ds.w 128
addrout: .ds.w 128
grhandle: .ds.w 1

.data
vdipb: .dc. 1 contrl,intin,ptsin,intout,ptsout
aespb: .dc. 1 contrl,global,intin,intout,addrin,addrout

.end

100

0
Abacus Software ST 3D Graphics

***********************************************************************
* Main program for link file basicl.o , runs only in connection with *
* this link file . U.B. 11.85
* Draws random line in coordinate area 0-255. The value area

* is valid for both axis


***********************************************************************

.globl main
.text

*
* Entry point from the linkfile

main: jsr apinit * Announce application


*
jsr grafhand
jsr openwork * Open screen work station

jsr mouse_off * Hide the Mouse

jsr clwork * Clear Display


W jsr inlinea * Color version only

loopl: jsr clwork


move. 1 ttextl,aO * Address of text after AO

jsr printf * Write text

move.1 loopc,d7

loop2: jsr random * Generate random number

and. w border,dO * bring to area 0-255

move.w dO, xO * through masking out of the upper

jsr random * 8 Bits of the lower word in DO

and. w border,dO
move. w dO, yO
jsr random
and. w border,dO

move.w dO, xl
jsr random
and.w border,dO
move.w d0,yl

move.w x0,d2 * transfer the two points to the

move.w xl,a2 * "right" registers

move.w y0,d3

101
ST 3D Graphics Abacus Software

move.w yl,a3
jsr drawl * Draw line from XO,YO to XI,Y1 sketch

dbra d7,loop2 * Repeat loopc

jsr inkey * Sense keyboard, do not wait for key

swap dO * activation, scancode in DO

cmp. w #$44,dO * compare with code in F10

bne loopl * If not : loop again

rts * otherwise terminate program

***********************************************************************
* Call the operating system function for creation of a 4-byte integer*
* random number, the number is returned to DO. *
***********************************************************************

random: move.w #17,-(a7) * generate a 4-Byte Integer-


trap #14 * Random Number in DO. Use only
addq.l #2,a7 * the lower 2-Bytes
rts

.data
.even
**********************************************************************
* Variables for the Main program *
* *

**********************************************************************

**********************************************************************
* Text for the printf function, 27 Y 34 96 positions the cursor *
* Sequence is column, line, both with an offset of 32 *
**********************************************************************

text 1: . dc -b 27, ' Y' ,40,42, ' Random lines ',0

loopc: . dc. 1 60 * Number of lines

border: . dc. w $ff * 255 as display limit, with the high'


★ * resolution B-W monitor the $ff
* * can be replaced with $lff = 511

102
Abacus Software ST 3D Graphics

**********************************************************************

.bss
.even

x0: .ds .w 1 * Temporary storage for the two

Y0: .ds.w 1 * Points, the program runs with small

xl: • ds .w 1 * changes even without the intermediate

yl: .ds.w 1 * storage; what changes ?

.end

103
Abacus Software ST 3D Graphics

4. Graphics using assembly-language routines

The programs presented in the following part of the book can be used
with monochrome as well as color monitors, since the line drawing is
performed by the operating system, or to be more accurate, by the LINE-
A-routines. Of course it would be possible to convert the draw-line-
algorithm from the first link file for the various picture formats, but this
process has the disadvantage of requiring a subroutine for every picture
format. The programs described here can be executed on all kinds of
computer-monitor combinations. During program start, the main program
recognizes what type of monitor is attached and what resolution is desired
and on the basis of this information provides some variables with the
required data. For example, the coordinate origin of the picture system is
placed in the middle of the display. The larger memory capacity of the ST
permits it to handle significantly larger quantities of data. Once the
operating system of the smaller models is placed in ROM, the area
released in RAM will be sufficient even for the largest applications.
When calling the Metacombco Editor for input of the larger source files
(grlinkl, menul, rotatel, paintl) you have to specify
more memory space for listing to be entered along with the filename. To
do this, enter grlinkl. s 23000 in the dialog box that appears. This
reserves about 80k for the source text. If you enter source text without
comments the space reserved in the basic version of the editor should be
sufficient.

107
ST 3D Graphics Abacus Software

4.1 Definition of a data structure for an object in space

The program modules presented here have the ability to represent on the
screen any object in a user defined world in any position, as seen from
various positions. The single disadvantage is the limitation of the valid
value range to ±32000; this means that for the definition of the world a
right angle three dimensional Cartesian coordinate system (right system)
is available whose three coordinate axes (X-Y-Z) are labeled with values
between +32000 and -32000. Whether these values are in meters,
kilometers or the number of corrupt politicians in the Senate depends on
the individual user and the application. For example, using the number of
corrupt politicians is a questionable value, since it changes from moment
to moment, and is far from constant.

Joking aside, a very simple object should suffice to describe the data
structure. We will use simple house as in Figure 4.1.1.

Figure 4.1.1: House as Wire Model

108
Abacus Software ST 3D Graphics

Every object in the coordinate system is described through a finite


number of points and the lines which connect these points. To represent
the object, these points in the world system must be specified by
declaring of their coordinates. It has proved to be useful to define the
object, in this case the house, in its own coordinate system and to
transform it during the construction of the world coordinate system. To
gain an advantage, the coordinate origin of the object system is located
inside the house, if possible at a "rotationally neutral” point, i.e. during a
rotation of the object around this point, the maximum changes of the
individual points resulting from the rotation should be minimized. The
object should not be distorted.

Figure 4.1.2: House with coordinate system included

The individual steps during the "construction” of the house therefore are:

1. Draw a total view of the object (on a piece of paper) and


arbitrarily number of the individual points.

109
ST 3D Graphics Abacus Software

Figure 4.1.3: House with numbered points

2. Draw the object in the various possible aspects with the current
coordinate axis for accurate specification of the points.

110
Abacus Software ST 3D Graphics

Figure 4.1.4 - Figure 4.1.9: six views of the house

ill
ST 3D Graphics Abacus Software

Figure 4.1.6

Figure 4.1.7

112
Abacus Software ST 3D Graphics

Figure 4.1.9

3. Set up a coordinate list of the individual points.

4. Create a line list, i.e. state which points are connected by


lines.

5. Indicate the number of points and lines in the object.

113
ST 3D Graphics Abacus Software

Coordinate list of the house:

Point No. X-coord. Y-coord. Z-coord.

1. -30 30 60
2. 30 30 60
3. 30 -30 60
4. -30 -30 60
5. 30 30 -60
6. -30 30 -60
7. -30 -30 -60
8. 30 -30 -60
9. 0 70 60
10. 0 70 -60
11. -10 -30 60
12. -10 0 60
13. 10 0 60
14. 10 -30 60
15. 30 20 40
16. 30 20 10
17. 30 0 10
18. 30 0 40
19. 30 20 -10
20. 30 20 -40
21. 30 0 -40
22. 30 0 -10
23. 30 -10 0
24. 30 -10 -20
25. 30 -30 -20
26. 30 -30 0

Total of 26 points.

114
Abacus Software ST 3D Graphics

Line list:

Line No. from point to point

1. 1 2
2. 2 3
3. 3 4
4. 4 1
5. 2 5
6. 5 8
7. 8 3
8. 8 7
9. 7 6
10. 6 5
11. 6 1
12. 7 4
13. 9 10
14. 1 9
15. 9 2
16. 5 10
17. 6 10
18. 11 12
19. 12 13
20. 13 14
21. 15 16
22. 16 17
23. 17 18
24. 18 15
25. 19 20
26. 20 21
27. 21 22
28. 22 19
29. 23 24
30. 24 25
31. 25 26
32. 26 23

Total of 32 lines.

115
ST 3D Graphics Abacus Software

Additional information on the object is required for the transformation of


the house into the world coorinate system: the angles housxw, housyw,
houszw, which describe a rotation of the house about one of the three
axes in regard to the coordinate origin, and the location of the house in
the world coordinate system. The location is the point to which the
coordinate origin (rotationally neutral point) of the house system is
displaced in the world system, housxO, housyO, houszO. In our first
example program the coordinate origin of the house system is moved to
the coordinate origin of the world system, housxO etc. and therefore
zero.

For further information, we need an observation point and a projection


center, where both points naturally are described in world coordinates. In
the simplest case the observation point is the coordinate origin point of
the world system, and the projection center [prox, proy, proz ] is
located on the positive Z axis of the world system. The system of the
observer (view system) is a right system in our programs and it is not
necessary to transform to a left system, to multiply all Z values by -1. For
our case this means that after transformation the view system a point with
the coordinates [10,10,-300] is farther from the observer than a
point with the coordinates [10,10,-200].

Furthermore, we need the normal vector (direction vector) of the


projection plane. For simplification we assume that it is pointed from the
projection center toward the coordinate origin of the world system and
points toward the negative Z axis. The projection center lies on the Z axis
and therefore has the coordinates [0,0, proz ], since the normal vector
of the projection plane points in the direction of the negative Z axis, the
rotation of the observation direction vector to the negative Z axis is not
necessary.

To help explain the coordinate systems and viewing points, we have here
Figure 4.1.10 with the world system and the observation factors defined
in it.

116
Abacus Software ST 3D Graphics

Figure 4.1.10

Summary:

To represent the house on the screen we need a total of four coordinate


systems, where the various coordinate systems exist only in theory and all
transformations occur in a single system. The defined points are stored in
arrays in which the various coordinate systems are then reflected so they
do not disappear after a transformation. The following coordinate systems
are used:

1. The data system (housdatx, housdaty,


housdatz), in which the house is defined at
construction.

2. The world system (wrldx, wrldy, wrldz), in which,


for example, a village is represented by several houses,
where the houses are all created by transformation at
various places in the world system from the one single
house defined in the data system.

117
ST 3D Graphics Abacus Software

3. The view system (viewx, viewy, viewz), which is


used for the description of the view transformation. The
view transformation is the transformation into the observer
system, which is described through the observation
reference point and projection center as well as the vectors
from projection center to the observation reference. The
vector from the projection center to the observation
reference point is therefore the normal vector of the
projection plane.

4. The screen system has only two dimensions. The only


transformation which occurs in this system is shifting the
coordinate origin to any desired location with reversal of
the Y axis. Something we also used in our example is the
displacement to the screen center but other locations are
also possible depending on the application.

After this simplified observation situation, now an example for the


general view-transformation of a more complicated model. As a fictional
example we will use a world system which represents an airplane
standing on a runway. The observation point of the system should be in
the middle of the cockpit window, which is therefore the projection plane,
and the eye of the pilot should be the projection center. Let us also
imagine a tanker truck and an airplane hangar at some distance from the
airplane. Two types of transformations are possible.

1. A transformation of the object, which might mean that the


tanker truck moves and approaches the airplane, for
example. In this case the movement must occur in the
world system and only the coordinates of the tanker truck
need be recalculated in the world system.

118
Abacus Software ST 3D Graphics

2. A movement of the observer, in this example the aiiplane.


Let’s go back to the starting position and assume that the
tanker truck remains in its original position. Now the
airplane should move, and for simulation of this
movement all objects in the world system, the tanker truck
and the hangar, must be transformed. The entire world
system would be rotated about the center of the cockpit
window. For a left rotation of the airplane, everything
must be rotated to the right. This connection can be easily
verified: If you move your head to the left, the observed
objects move to the right out of our field of view. When
the airplane is moved without rotation the observer gets
the impression of movement through the displacement of
the coordinate origin of the world system before the
projection.

3. A movement of the observer and the object, which means


first a transformation of the truck in the world system and
subsequent transformation of the total world system into
the view system.

Only after completion of these various cases do we get to the perspective


transformation, i.e the projection from space to the projection plane, or
more precisely into the screen. This was an example of a more complex
observation model.

You will probably ask why things have to be complicated by using an


additional coordinate system, the view system, when we could do
everything in the world system. This is true, but the addition of the view
system improves the accuracy and provides for a better overview of the
total system. Because of the rounding errors from the many
transformations, our world of the tanker truck and the hangar, would
according to the law of increasing entropy, degenerate to a chaotic mess
after a few hundred transformations. The aspect of the better overview is
at least as significant as the accuracy and I want to try to demonstrate this
fact again.

As you will see, all transformations can be carried out with a single
routine. Our application combines almost all of the rotations with matrix
multiplication and performs displacements before and after these
multiplications. The displacements are not included in the matrix
multiplications and our point coordinates are therefore not extended

119
ST 3D Graphics Abacus Software

coordinates but consist only of the three coordinates [x,y,z] of the


current point. The only routine used is the rotation around any selected
point. As a reminder, during the rotation around any point, the coordinate
origin must first be moved to this point, then rotated by the desired angle
and finally the coordinate origin moved inverse to its original place by
back transformation. For the sequence of our routine this means that the
point about which rotation should occur is passed, also the rotation angles
around the corresponding axes (xw,yw,zw). The rotation routine first
calculates a multiplication matrix through multiplication of the rotation
matrixes belonging to the various angles. Then all points belonging to the
desired object [ x, y, z ] are manipulated in the following sequence:

1. The point [ x, y, z ] is moved to the rotation point. This is


achieved through subtraction of the coordinates of the
rotation point from the object coordinates. The result of
this operation is the point [ x', y', z' ].

2. The point [x', y' , z' ] is multiplied by the previously


calculated total rotation matrix.

Result: [x'' , y'', z'' ].

3. The point is transformed back to the


"old" coordinate system by adding of the coordinates of
the rotation point

In this model the axes are not scaled. The size manipulation of the
objects, i.e. their pictured size on the screen, is performed during the
projection through movement of the projection plane. If different values
are selected for the subtraction occurring at the beginning and the
concluding addition, the movement of an observer in the world system is
simulated. If the angles of the normal surface vector in relation to the
world system are provided (in section 2.5 we calculated the angles
through projection on the various surfaces) the position of the observer
can be determined in space through one point and three angles.

Let us assume that a person is moving our world system, where the house
discussed in the first example is located at the coordinate origin. The eye
of this person, or actually the retina of the eye, is the projection plane. It
is irrelevant that the projection center in the human eye is in front of the
projection plane, since the reversed picture resulting from this is turned
around by the brain. For the simulation of this moving observer the

120
Abacus Software ST 3D Graphics

coordinate origin of the world system must be moved to the center of the
retina, but we are limiting ourselves to a single eye. The coordinates of
the eye in the world system must be known; furthermore the head of the
observer can be moved through three different axis. You can easily
determine the axis yourself. The rotation about the first axis in our
coordinate system corresponds to the X axis, described by the observer
nodding his head up and down. The Y axis rotation is shaking his head.
The head rotates on the Z axis when the observer attempts to touch his ear
to his shoulder. If the three rotaion angles are known, the coordinate
origin will be rotated about this angle and the observed object lies in the
coordinate system of the observer. It is not necessary to reverse the
movement of the coordinate origin which is similar to the example of the
airplane.

In principle, an unlimited number of displacements, rotations, and


observer situations are possible: rotation of the house, rotation of the total
system around one point, or any axis, and also the displacement with
rotation into the observer system. To bring some order into this flood of
rotations, our programming examples are limited to one fixed observer
location point. This is no limitation on the observed effects on the screen
however, i.e. in principle it is the same whether one assumes that an
object rotated around a point, or the observer moved his head, provided
the size relationships are suitably adjusted. Finally, the programmer must
know the desired effect. There are many ways to achieve the same
effects.

And now, the description of the transformations of our data structure for
the first, fairly simple transformation program. The concrete object
(house) is defined in a coordinate system (housdatx, housdaty,
housdatz). During the initialization of the program, the subroutine
makewrld moves the house to any desired location in the world system
(wrldx, wrldy, wrldz), with possible rotation. In the first program it
is moved to the point [0,0,0] without rotation.

All further operations concerning the house relate only to the world
system. For example, the house can be rotated around any point of this
world system, or only the position of the house can be changed by
displacement. But now the initial scenario of our model changes through
these transformations, so we store the data for the rotation of the world
system in a new coordinate system (viewx, viewy, viewz), where
the initial scene (in wrldx, wrldy, wrldz) is available at any time
and can be reproduced at any time.

121
ST 3D Graphics Abacus Software

After each operation in this world system, the coordinates of the


displaced house are stored in the view system. The object should also be
displayed on the screen. To do this, it must be adapted to the perspective
of the viewer situation. In our example, the projection center is at the
coordinates [0, 0, 1500]“therefore on the positive z axis of the right
handed coordinate system. Through the perspective transformation, the
coordinates of the view storage are transformed into screen coordinates
(screenx, screeny) whereby the desired location of the coordinate
origin and the orientation of the Y axis are considered during the
calculations. The screen coordinates are transferred with the aid of the
line list of the drawing routine, which, through the built-in "Cohen-
Sutherland clipper” draws only the desired screen area using the border
points clipule and cliplri (clip upper left, clip lower right). To
create some movement in this house, the rotation origin point or its
rotation angle can be changed slightly after each drawing and the whole
process can be programmed into a large loop for repeated execution.

In case you did not understand a few details, you can relax while typing
in the following program listings. You should consider that the material
discussed here corresponds to about a half a semester of lectures for
upper-class computer science students and therefore requires intense
consideration, even with the aid of the additional literature cited in the
beginning.

Just as in the first small program (random lines) this program is also
divided into a link file and main program. The new link file has the name
grlinkl. s and was enhanced with the sine and cosine routines, the
clip algorithm, the screen switch routine, the matrix operations and the
perspective transform routine. The main program housel.s contains
the data of the house and the main loop in which the rotation angles of the
house are changed in cycle and can be altered by the user. The steps for
creating a ready-to-run program are the same as in the third chapter. You
need only to replace basicl. s with grlinkl. s and mainl. s with
house 1. s in the command sequences. You should start typing in the
first program since the following programs build on the first two files.
That way you only have to type in the additional subroutines and data
sections. The link file grlinkl . s is the same for all following main
programs and does not have to be changed.

122
Abacus Software ST 3D Graphics

***********************************************************************
* grlinkl.s Graphic Driver Version 4.0 *

* The main program must begin with the label " main *
***********************************************************************

***********************************************************************

* Global variables in the link files *

.globl drawl,sin,sincos,physbase

.globl logbase

.globl sinx,siny,sinz,cosx,cosy,cosz,wait

.globl waitl,drawnl

.globl pers,grafhand

•globl nummark,xangle,yangle,zangle,numline,datx,daty,datz

.globl pointx,pointy

.globl pointz,xplot,yplot,xO,yO,zO,zl,linxy,sincos

.globl grhandle,global,contrl,intin,intout,ptsin,ptsout

.globl addrin,addrout

.globl apinit,openwork,clwork,aes,vdi

.globl rotate,dist,zobs

.globl matrixll,matrixl2,matrixl3

.globl matrix21,matrix22,matrix23

.globl matrix31,matrix32,matrix33

.globl xrotate,yrotate,zrotate,matinit,inkey

.globl mouse_on,mouse_off,printf

.globl clipxule,clipyule,clipxlri,clipylri

.globl filstyle,filindex,filform,filcolor,filmode,yrot

.globl lineavar,pageup,pagedown,plotpt

123
ST 3D Graphics Abacus Software

********************************************************************
* Program initialization and storage requirement calculations *
********************************************************************

.text

sstart:

move.1 a7, a5 * Base page address on the stack

move.1 4 (a5),a5 * basepage address = program start - $100

move.1 $c(a5),dO * Program length

add. 1 $14(a5),dO * Length of initialized data area

add. 1 $lc{a5),d0 * Data area not initialized

add. 1 #$1100,dO * 4 K-byte user stack

move.1 a5, dl * Start address of the program

add. 1 dO, dl * Plus number of occupied bytes = space requirement

and. 1 #-2,dl * Even address for stack

move.1 dl, a 7 * User stack pointer to last 4K- byte

move.1 dO,-(sp) * Length of reserved area

move.1 a5,-(sp) * Beginning address of reserved area

move.w dO, -(sp) * Dummy-word

move.w #$4a,-(sp) * GEM DOS function SETBLOCK

trap #1

add. 1 #12,sp * Restore old stack address

jsr startl * Check on display address

jsr inlinea * Initialize Line-A routines

jsr main * Jump to main program (user-created)

move.1 #0,- (a7) * End current program

trap #1 * Back to Gem desktop

***********************************************************************
* Pass upper screen page to video controller *

* while drawing the other *


***********************************************************************

pageup: move.w #-l,-(a7)

move.1 physbase,-(a7) * Page displayed

move.1 logbase,- (a7) * Draw on this page

move.w #5,- (a7)

trap

add. 1 #12,a7

rt s

124
Abacus Software ST 3D Graphics

***********************************************************************
* Display screen page at lower address, while all drawing *

* operations after the call go to the higher display *


***********************************************************************

pagedown: move.w #-l,-(a7)

move.1 logbase,- (a7) * display logical page

move.1 physbase,-<a7) * draw in the other one

move. w #5,-(a7)

trap Pb
add. 1 #12,a7

rts

***********************************************************************
* This subroutine calls AES functions, the user must *

* save the Registers D0-D2 and A0-A2 before the aes call, *

* which are used by VDI and AES *


***********************************************************************

aes: move.1 #aespb,dl * call the AES functions

move.w #$c8,dO

trap

rts
dp
* call the VDI functions *

vdi: move.l #vdipb,dl * call the VDI functions

move.w #$73,dO

trap (^#2^?

rts

125
ST 3D Graphics Abacus Software

***********************************************************************
* initialize the Line-A functions, pass the address of *

* Line-A variable area in AO, which is then stored *

* in lineavar *
***********************************************************************

.dc.w $a000

move.1 aO,lineavar

move. w #0,32(aO)

move.w #$ffff,34(aO)

move.w #0,36 (aO)

move.w #1,24 <a0)

rts

* announces application *

apinit: clr. 1 dO * announces an application

move. 1 dO,aplresv

move.1 dO,ap2resv

move.1 dO,ap3resv

move.1 dO,ap4resv

move.w #10,opcode

move.w #0,sintin

move.w #1,sintout

move.w #0,saddrout

move.w #0,saddrin

jsr aes

rts

A**********************************************************************
* Transfers desktop screen handler to caller *
***********************************************************************

grafhand: move.w #77,contrl * Transfer screen handler

move.w #0,contrl+2

move.w #5,contrl+4

move.w #0,contrl+6

move.w #0,contrl+8

126
Abacus Software ST 3D Graphics

jsr aes

move.w intout,grhandle

rts

* open a workstation *

openwork: move.w #100,opcode * opens a workstation

move.w #1, dO

move.w #0,contrl+2

move.w #11,contrl+6

move.w grhandle,contrl+12

move.w dO,intin

move.w dO,intin+2

move.w dO,intin+4

move.w dO,intin+6

move.w dO,intin+8

move.w dO,intin+10

move.w dO,intin+12

move.w dO,intin+14

move.w dO,intin+16

move.w dO,intin+18

move„w #2,intin+20

jsr vdi

rts

* Clear the screen

clwork: move.w #3,contrl * clear screen VDI function

move.w #0,contrl+2

move.w #1,contrl+6

move.w grhandle,contrl+12

jsr vdi

rts

127
ST 3D Graphics Abacus Software

* Enable mouse *

mouse on: move.w #122,contrl * enable mouse

move. w #0,contrl+2 * and control with

move. w #1,contrl+6 * operating system

move.w grhandle,contrl+12

move.w #0,intin

jsr vdi

rts

* Disable mouse *

mouse off: move.w #123,contrl * Disable mouse

move. w #0,contrl+2 * and control

move.w #0,contrl+6

move.w grhandle,contrl+12

jsr vdi

rts

* write string on screen *

printf: move.1 aO,-(a7) * write a string

move.w #9,-(a7) * whose starting

trap #1 * is in AO, on the

addq.1 #6,a7 * screen. String

rt s * must end with a zero.

* Determine screen address *

start 1:

move.w #2,-{a7) * Determine the screen

trap #14 * address of the system

addq.1 #2,a7 * which computer ?

128
Abacus Software ST 3D Graphics

move. 1 dO,physbase * screen start minus 32 K-byte

sub. 1 #$8000,dO

move.1 dO,logbase * equals logical display page

rts

************************************************************************
* Plot routine x-coordinate in d2, y-coordinate in d3 *
************************************************************************

plotpt: movem.1 dO-d2/aO-a2, - (a7)

t st. w d2 * X-value less than zero =>

bmi stop2

tst. w d3 * Y-value less zero

bmi stop2

cmp.w #639,d2 * X-value greater than 639?

bhi stop2 * Display limit

cmp.w #399,d3 * Y-value greater than 399?

bhi stop2

move .w d2,ptsin

move. w d3,ptsin+2

move. w #1,intin

.dc. w $a001

movem. 1 (a7) +,d0-d2/a0-a2

stop2: rts

**********************************************************************
* draw-line routine with Cohen-Sutherland clipping. The points are *

* passed in d2, d3 (start point) and a2, a3 (end point)


**********************************************************************

drawl: movem.1 d0-d7/a0-a6,-(a7) * Save registers

move.w d2,d6 * Determine position

move.w d3,d7 * of start point and

jsr rel_pos * store

move.w dl,codel

move.w a2,d6 * Position of second

move.w a3,d7 * point and store

jsr rel_pos

move.w dl,code2

tst. w dl * if points are not in

bne testwl * drawing area continue

129
ST 3D Graphics Abacus Software

tst. w codel * test. Otherwise test


beq drawit2 * first point. When visible.
* * draw both points

testwl: move.w dl, dO * If both points on the same


and. w codel,dO * 'page' outside the viewing
bne drawend * window, then do not draw.
move.w d2,a0 * else store starting points and
move.w d3, al * calculate intersecting points
move.w a2, a4

move.w a3, a5

tst.w code2 * is point 2 visible ?


bne testw2 * if not, find intersection point
move.w a2,rightx * if yes, store
move. w a3,righty

bra testw3 * find left intersect point

testw2:, move.w codel,plcode * right intersect point


move.w code2,p2code

jsr fndpoint * find intersect point


tst. w plcode * if 'intersect point'not
bne drawend * visible, then end

move.w d2,rightx * if visible, then store


move.w d3,righty

testw3: move.w a4,d2 * and the left intersect point


move. w a5, d3 * with switched points
move.w aO, a2 * determine with the same routine
move.w al, a3

move.w code2,plcode

move.w codel,p2code

tst. w p2code ★ Point visible?


bne testw4 * if not, continue test
move.w a2,leftx ★ if yes, store and
move. w a3,lefty * connect both visible
bra drawitl * points with a line

130
Abacus Software ST 3D Graphics

★ Find intersect point


testw4: jsr fndpoint

d2,leftx ★ and store.


move. w

move. w d3,lefty

leftx,d2 * connect both points with


drawitl: move. w
* a line
move. w lefty,d3

clr. 1 a2

clr. 1 a3

move. w rightx,a2

move.w righty,a3

drawit2: move.1 lineavar,aO

d2,38 <a0) * XI
move. w

d3,40 (aO) ★ Y1
move.w

a2,42(aO) * X2
move.w
* Y2
move.w a3,44(aO)

Sa003 * Draw line


.dc .w

drawend:

movem.1 (a7)+,d0-d7/a0-a6 ★ Restore registers


endit:

rts * Return to calling program

**********************************************************************
* recognizes the position of a point passed in D6 and D7 relative *

* to the clip window defined in the variables clipoli and clipure *


**********************************************************************

dl * determines the position


clr. 1

move. w d7,dl * of the point passed in

sub. w clipyule,dl . * d6 and d7 relative to

#1, dl * the drawing window


lsl.l
d7, dl * defined by clipure
move.w

sub. w clipylri,dl * and clipoli

neg. w dl

lsl.l #1, dl

move.w d6, dl

sub. w clipxlri,dl

neg. w dl

lsl.l #1, dl

move.w d6, dl

sub. w clipxule,dl

lsl.l #1, dl

131
ST 3D Graphics Abacus Software

swap dl

rts

**********************************************************************
* Finds the intersect point, if present, *

* of the the connecting line from Pi to P2 with the clip window *

* the points are passed in D2, D3 and A2, A3 as in drawl *


**********************************************************************

fndpoint: move.w d2,d4 * Find the center point of

move. w d3,d5 * the line PI P2

add. w a2,d4 * {XI + X2) / 2

ext .1 d4

lsr. 1 #l,d4

add. w a3,d5 * (Y1 + Y2) / 2

ext .1 d5 * = center point of line PI P2

lsr. 1 #l,d5
move.w d4,d6 * Store center point coord.

move.w d5,d7 * Y middle

jsr rel_pos * where is the intersect point ?

move.w p2code,d6 * Code of center pt. to D6

and.w dl, d6 * are the points on the same

bne fother * page outside the screen

cmp. w d4,d2 * points coincide ?

bne findwl

cmp.w d5,d3

beq fendit * if yes => stop

findwl: cmp. w d4, a2 * Do middle point and second

bne findw2 * point match ?

cmp. w d5,a3

bne findw2

bra fendit * if yes = stop

132
Abacus Software ST 3D Graphics

move. w d4,d2 * else exchange middle and


f indw2:

move. w d5,d3 * first point and start again

move. w dl,plcode

bra fndpoint

fother: cmp - w d4,a2 * middle point and P2 match ?

bne fotherl

cmp. w d5, a3

fendit * if yes, then end


beq

cmp. w d4,d2 * middle point and PI match ?


fotherl:

bne fother2

cmp. w d5,d3
* if yes, then end
beq fendit

tst. w plcode ★ is Pl in clip window


fother2:

beq fother3

move.w dl,d7 * if not, and Pl and P2 lie

and. w plcode,d7 * both on one side of the

bne fexit * Clip-window then none of line is visible

move.w d4,a2 * otherwise take middle point


fother3:

move.w d5,a3 * as new P2 and start again

move.w dl,p2code * until the intersect point

fndpoint * is found
bra

#1,plcode * Inform calling prog, of termination.


fexit: move.w

fendit: rts * either in d2,d3 middle point. or

* * in plcode termination notice

******************************************************************
* sine and cosine Function, angle is passed in DO and

* the sine and cosine are returned in Dl and D2 *


******************************************************************

tst. w dO * Angle negative, add 360 degrees

bpl noaddi

add.w #360,dO

#sintab,al ★ Beginning address of sine £able


move.1

move. 1 d0,d2 * Angle in dO and d2

#1, dO * Angle times two as index for access


lsl. w
0 (al,dO.w),dl * sine to dl
move.w

133
ST 3D Graphics Abacus Software

cmp. w #270,d2 * Calculate cosine through


bit plus9 * displacement of sine values

sub. w #270,d2 * by 90 degrees

bra sendsin

plus9: add.w #90,d2

sendsin: lsl. w #l,d2

move.w 0 (al,d2.w) ,d2 * cosine to d2

*
rt s * and back to calling program

* sine function *

* Angle is passed in dO and the sine returned in dl *

sin: move. 1 #sintab,al

t st • w dO

bpl sinl

add.w #360,dO

sinl: lsl. w #1, dO

move.w 0(al,dO.w)
rts

************************************************************************
* Initialize the main diagnonal of the result matrix with *

* ones which were multiplied by 2A14. This subroutine must *

* be called at least once before the call by rotate, or the *

* result matrix will only consist of zeros. *


★★★★A*******************************************************************

move.w #0, dl

move.w #16384,d2 * The initial value for

move.w d2,matrixll * the main diagonal of

move.w dl,matrixl2 * the result matrix

move.w dl,matrixl3 * all other elements

move.w dl,matrix21 ★ at zero

move. w d2,matrix22

move.w dl,matrix23

move.w dl,matrix31

move . w dl,matrix32

134
Abacus Software ST 3D Graphics

move.w d2,matrix33

rts

************************************************************************
* Multiplication of the rotation matrix by the rotation
* matrix for rotation about the X-axis

************************************************************************

xangle,dO * multiply matrixll-matrix33


xrotate: move. w
sincos * with the rotation matrix for a
jsr
dl,sinx * rotation about the X-axis
move.w

move. w d2,cosx

move.w dl, d3

move.w d2,d4

matrixll,rotxll * The first column of the matrix


move.w
matrix21,rotx21 * does not change with X rotation
move.w

move.w matrix31,rotx31

muls matrixl2,d2

mu Is matrixl3,dl

sub. 1 dl, d2

lsl.l #2,d2

swap d2

move. w d2,rotxl2

move. w d3, dl

move. w d4,d2

muls matrix22,d2

muls matrix23,dl

sub. 1 dl, d2

lsl.l '#2,d2

swap d2

move.w d2,rotx22

move.w d3,dl

move.w d4,d2

muls matrix32,d2

muls matrix33,dl

sub. 1 dl, d2

lsl.l #2,d2

swap d2

move.w d2,rotx32

move.w d3, dl

135
ST 3D Graphics Abacus Software

move. w d4,d2

mu Is matrixl2,dl

mu Is matrixl3,d2

add. 1 dl, d2

lsl.l #2,d2

swap d2

move.w d2,rotxl3

move. w d3, dl

move. w d4,d2

mu Is matrix22,dl

mu Is matrix23,d2

add. 1 dl, d2

lsl.l #2,d2

swap d2

move.w d2,rotx23

muls matrix32,d3

mu Is matrix33,d4

add. 1 d3,d4

lsl.l #2,d4

swap d4

move.w d4,rotx33

move. 1 irotxll,al

move.1 #matrixll,a2

move.1 #9,d7 * Number of matrix elements

subq.1 #1, d7

rotxlopl’: move. w (al)+, (a2) + * Copy result matrix. which

dbra d7,rotxlopl * is still in ROTXnn, to MATRIXnn

rts

***********************************************************************
* multiply the general rotation matrix by the Y-axis *

* rotation matrix. Results are stored in the general *

* rotation matrix *
***********************************************************************

move. w yangle,dO * Angle around which

jsr sincos

move .w dl,siny

move.w d2,cosy

move.w dl, d3 * Sine of Y-angle

move.w d2,d4 * Cosine of Y-angle

136
Abacus Software ST 3D Graphics

mu Is matrixll,d2

mu Is matrixl3,dl

add. 1 dl, d2

lsl.l #2,d2

swap d2

move.w d2,rotxll

move.w d3, dl

move.w d4, d2

mu Is matrix21,d2

mu Is matrix23,dl

add. 1 dl,d2

lsl.l #2,d2

swap d2

move.w d2,rotx21

move.w d3,dl

move.w d4, d2

muls matrix31,d2

muls matrix33,dl

add. 1 dl, d2

lsl.l #2,d2

swap d2

move.w d2,rotx31

neg.w d3

move. w d3, dl * -siny in the rotation matrix

move .w d4,d2

move. w matrixl2,rotxl2

move. w matrix22,rotx22 * The second column

move. w matrix32,rotx32 * of the starting

muls matrixll,dl * matrix does not

muls matrixl3,d2 * change

add. 1 dl, d2

lsl.l #2,d2

swap d2

move.w d2,rotxl3

move.w d3,dl

move.w d4,d2

muls matrix21,dl

muls matrix23,d2

add. 1 dl,d2

lsl.l #2,d2

swap d2

move.w d2,rotx23

137
ST 3D Graphics Abacus Software

muls matrix31,d3

muls matrix33,d4

add. 1 d3,d4
lsl.l #2,d4

swap d4

move.w d4,rotx33

move.1 #8, d7

move.1 #rotxll,al * Address of result matrix


move.1 #matrixll,a2 * Address of original matrix
yrotlopl: move.w (al)+, <a2)+ * Copy result matrix
dbra d7,yrotlopl * to the original matrix
rts

* 2-axis - Rotation matrix multiplications *

move.w zangle,dO
jsr sincos
move.w dl,sinz

move.w d2,cosz

move.w dl,d3

move.w d2, d4

muls matrixll,d2

muls matrixl2,dl

sub. 1 dl, d2

lsl.l #2,d2
swap d2

move.w d2,rotxll
move.w d3, dl

move.w d4,d2
muls matrix21,d2
muls matrix22,dl
sub. 1 dl, d2

lsl.l #2,d2

swap d2

move. w d2,rotx21

move. w d3, dl

move.w d4,d2
muls matrix31,d2

muls matrix32,dl
sub. 1 dl, d2

138
Abacus Software ST 3D Graphics

lsl.1 #2,d2

swap d2

move.w d2,rotx31

move.w d3, dl

move.w d4, d2

mu Is matrixll,dl

mu Is matrixl2,d2

add. 1 dl,d2

lsl.l #2, d2

swap d2

move.w d2,rotxl2

move.w d3, dl

move. w d4,d2

mu Is matrix21,dl

mu Is matrix22,d2

add. 1 dl, d2

lsl.l #2, d2

swap 42
move. w d2,rotx22

mu Is matrix31,d3

mu Is matrix32,d4

add. 1 d3,d4

lsl.l #2,d4

swap d4

move.w d4,rotx32

matrixl3,rotxl3 * the third column


move. w
matrix23,rotx23 * remains
move.w
matrix33,rotx33 * unchanged
move.w

move . 1 #8,d7

move.1 trotxll,al

move.1 #matrixll, a2

(al)+,(a2)+ * copy to general


zrotlopl : move.w
d7, zrotlopl * rotation matrix
dbra

rts

139
ST 3D Graphics Abacus Software

* Multiply every point whose Array address is in datx etc. *

* by previous translation of the coordinate source to *

* point [offx,offy,offz], with the general rotation matrix. *

* The coordinate source of the result coordinates is then *

* moved to point [xoffs,yoffs,zoffs] *

rotate: move.w nummark,dO * Number of points to be


ext. 1 dO * transformed as counter
subq.1 #1, dO

move.1 datx,al

move.1 daty,a2

move.1 datz,a3

move.1 pointx,a4

move.1 pointy,a5

move.1 pointz,a6
rotatel: move.w (al)+,dl * X-coordinate
add.w offx,dl

move.w dl, d4

move.w (a2)+,d2 * Y-coordinate


add.w offy,d2 * Translation to point [offx,offy,offz]
move.w d2,d5

move.w (a3)+,d3 * Z-coordinate


add.w offz,d3

move.w d3,d6

muls matrixll,dl

mu Is matrix21,d2

muls matrix31,d3

add. 1 dl, d2

add. 1 d2,d3

lsl.l #2,d3

swap d3

add.w xoffs,d3

move. w d3,(a4)+ * rotated X-coordinate


move. w d4, dl

move.w d5,d2

move.w d6,d3

muls matrixl2,dl
Abacus Software ST 3D Graphics

mu Is matrix22,d2

mu Is matrix32,d3

add. 1 dl,d2

add. 1 d2,d3

lsl.l #2,d3

swap d3

add. w yoffs,d3

move . w d3,(a5)+ * rotated Y-coordinate

mu Is matrixl3,d4

mu Is matrix23,d5

mu Is matrix33,d6

add. 1 d4,d5

add. 1 d5, d6

lsl.l #2,d6

swap d6

add. w zoffs,d6

move. w d6,(a6)+ * rotated Z-coordinate

dbra dO,rotatel

rts

************************************************************************
* Perspective, calculated from the transformed points in the arrays *

* pointx, pointy and pointz the screen coordinates, which *

* are then stored in the arrays xplot and^plot . *


************************************************************************

move. 1 pointx,al * Beginning address of

move. 1 pointy,a2 * Point arrays

move.1 pointz,a3

move.1 xplot,a4 * xplot contains start address of the

move.1 yplot,a5 * display coordinate array

move.w nummark,dO * Number of points to be transformed

ext .1 dO * as counter

subq.1 #1, dO

move.w (a3)+, d5 ★ z-coordinate of object

move.w d5,d6

move. w dist,d4 * Enlargement factor

sub. w d5,d4 ★ dist minus Z-coordinate of Obj.coord

141
ST 3D Graphics Abacus Software

ext. 1 d4

lsl.l #8,d4 * times 256 for value fitting

move. w zobs,d3 * Projection center Z-coordinates

ext. 1 d3

sub. 1 d6,d3 * minus Z-coordinate of object

bne persl

move. w #0, dl * Catch division by zero

addq.1 #2,al * Not really required since

addq.1 #2,a2 * computer catches this

move.w dl,(a4)+ * with an interrupt

move.w dl,(a5)+

bra perendl

persl: divs d3,d4

move.w d4,d3

move.w (al)+,dl * X-coordinate of object

move.w dl,d2

neg. w dl

muls dl,d3 * multiplied by perspective factor

lsr.l #8,d3 ★ /256 save value range fitting

add.w d3,d2 * add to X-coordinate

add.w x0,d2 * add screen offset (center point)

move. w d2,(a4)+ * Display X-coordinate

move.w (a2)+,dl * Y-coordinates of object

move.w dl, d2

neg. w dl

muls dl, d4

lsr. 1 #8,d4 * /256

add. w d4,d2

neg. w d2 * Display offset, mirror of Y-axis

add.w y0,d2 * Source at [XO,YO]

move.w d2,(a5)+ * Display Y-coordinate

perendl: dbra dO, perlop * All points transformed ?

rt s ★ If yes, return

142
Abacus Software ST 3D Graphics

•dr*****************************************************************
* Draw number of lines from array from lines in linxy *
******************************************************************

drawnl: move.1 xplot,a4 * Display X-coordinate

move.1 yplot,a5 * " Y-coordinate

move. w numline,dO * Number of lines

ext .1 dO

subq.1 #1, dO * as counter

move.1 linxy,a6 * Address of line array

drlop: move.1 (a6) +,dl * first line , (PI,P2)

subq.w #l,dl * fit to list structure

lsl.w #1, dl * times list element length (2)

move.w * X-coordinate of second point


0(a4,dl.w),d2

move.w 0(a5,dl.w),d3 * Y-coordinate of second point

swap dl * same procedure for first point

subq.w #1, dl

lsl.w #1, dl

move.w 0(a4,dl.w),a2 * X-coordinate of first point

move.w 0(a5,dl.w),a3 * Y-coordinate of first point

jsr drawl * draw line from P2 to P2

dbra dO,drlop * All lines drawn ?

rts

***********************************************************************
* simple counting loop *
***********************************************************************

waitl dbra dO,waitl * delay loop, counts dO register

rts * down to -1

***********************************************************************
* wait for key press, for Test and Error detection *
***********************************************************************

wait: move.w #l,-(a7) * wait for key activation

trap #1 * GEM DOS call

addq.l #2,a7

rts

143
ST 3D Graphics Abacus Software

***********************************************************************
* Key sensing, ASCII code returned in lower byte word of DO *

* Scan code in upper sord lower byte of DO *

* Returns zero if no input *


***********************************************************************

move.w #2,-(a7) * Key sensing, does not

move.w #1,- (a7) * wait for a key

trap #13 * press

addq.1 #4, a7

tst. w dO

bpl endkey

move.w #7,-{a7)

trap #1

addq.1 #2,a7

endkey: rts

** The six following subroutines are only required **

** for the second main program and do not have to be **

** entered for linking to the first main program **

filstyle: move. w #23,contrl * VDI function, set

move.w #0,contrl+2 * fill style passed

move. w #1,contrl+6 ★ in DO

move. w grhandle,contrl+12

move. w dO,intin

jsr vdi

rts

filindex: movem.1 dO-d2/aO-a2,- (a7) * set fill pattern

move. w #24,contrl * also passed in DO

move.w #0,contrl+2

move.w #1,contrl+6

move -w grhandle,contrl+12

move.w dO,intin

144
Abacus Software ST 3D Graphics

jsr vdi

raovera.1 {a 7)+ ,d0-d2/a0-a2

rts

filcolor: move. w #25,contrl * set fill color to

move. w #0,contrl+2

move. w #1,contrl+6

move.w grhandle,contrl+12

move.w #1,intin * one

jsr vdi

rts

filmode: move.w #32,contrl * set write mode

move.w #0,contrl+2

move.w #1,contrl+6

move.w grhandle,contrl+12 * passed in DO

move.w dO,intin

jsr vdi

rts

filform: move. w #104, contrl * switch on border

move.w #0, contrl + 2 * around area

move.w #1,contrl+6

move.w grhandle,contrl+12

move.w #1,intin

jsr vdi

rts

*********************************************************************
* Rotation of a number of points (nummark) in array datx etc. around*

* angle yangle around Y-axis to array pointx = address of array *


*********************************************************************

move.w yangle,dO * rotate the definition line

jsr sincos * of a rotation body nummark

move. w dl,siny * times about the Y-axis

move.w d2,cosy * Rotation is done without

move.1 datx,al * matrix multiplication.

move.1 daty,a2 * but directly, from arrays datx

move.1 datz,a3 * in which the address of the definition

move.1 pointx,a4 * line was stored into the array

145
ST 3D Graphics Abacus Software

move.1 pointy,a5 * whose address is stored

move.1 pointz,a6 * in pointx etc.

move . w nummark,dO

ext. 1 dO * the rotation is about

subq.1 #1, dO * angle -y, i.e. from direct:

move.w (al)+,dl * positive Y-axis

move.w dl,d3 * counterclockwise

move.w (a3)+,d2

move.w d2,d4 * z' = x*siny + z*cosy

mu Is cosy,d2

lsl.l #2, d2 * retract area extension

swap d2 * sine values

mu Is siny,dl

lsl.l #2, dl

swap dl

add. w dl,d2

move. w d2,(a6)+ * store z'

mu Is siny,d4 * calculate x'

lsl.l #2,d4 * x' = x*cosy - z*siny

swap d4

neg. w d4

mu Is cosy,d3

lsl.l #2,d3

swap d3

add. w d3,d4

move.w d4, (a4) + * store x'

move.w (a2) +, (aS) + * y' = y, since rotation is

dbra dO,ylop * around Y-axis

rts

* Variables for the basic program *

• even
.data * Sine table starts here

sintab: .dc.w 0,286,572,857,1143,1428,1713,1997,2280

.dc. w 2563,2845,3126,3406,3686,3964,4240,4516

. dc. w 4790,5063,5334,5604,5872,6138,6402,6664

146
Abacus Software ST 3D Graphics

.dc.w 6924,7182,7438,7692,7943,8192,8438,8682
. dc. w 8923,9162,9397,9630,9860,10087,10311,10531
.dc.w 10749,10963,11174,11381,11585,11786,11982,12176
.dc.w 12365,12551,12733,12911,13085,13255,13421,13583
.dc.w 13741,13894,14044,14189,14330,14466,14598,14726
.dc.w 14849,14962,15082,15191,15296,15396,15491,15582
.dc.w 15668,15749,15826,15897,15964,16026,16083,16135
• dc.w 16182,16225,16262,16294,16322,16344,16362,16374

.dc.w 16382,16384

.dc.w 16382,16374,16362,16344,16322,16294,16262,16225
.dc.w 16182

.dc.w 16135,16083,16026,15964,15897,15826,15749,15668
.dc.w 15582,15491,15396,15296,15191,15082,14962,14849
-dc.w 14726,14598,14466,14330,14189,14044,13894,13741

• dc.w 13583,13421,13255,13085,12911,12733,12551,12365
• dc.w 12176,11982,11786,11585,11381,11174,10963,10749
.dc.w 10531,10311,10087,9860,9630,9397,9162,8923

.dc.w 8682,8438,8192,7943,7692,7438,7182,6924

.dc.w 6664,6402,6138,5872,5604,5334,5063,4790
.dc.w 4516,4240,3964,3686,3406,3126,2845,2563
.dc.w 2280,1997,1713,1428,1143,857,572,286,0

.dc.w -286,-572,-857,-1143,-1428,-1713,-1997,-2280
.dc.w -2563,-2845,-3126,-3406,-3686,-3964,-4240,-4516
.dc.w -4790,-5063,-5334,-5604,-5872,-6138,-6402,-6664
.dc.w -6924,-7182,-7438,-7692,-7943,-8192,-8438,-8682
.dc.w -8923,-9162,-9397,-9630,-9860,-10087,-10311,-10531
.dc.w -10749,-10963,-11174,-11381,-11585,-11786,-11982
.dc.w -12176

.dc.w -12365,-12551,-12733,-12911,-13085,-13255,-13421
.dc. w -13583

.dc.w -13741,-13894,-14044,-14189,-14330,-14466,-14598
.dc.w -14726

.dc.w -14849,-14962,-15082,-15191,-15296,-15396,-15491

.dc.w -15582

• dc.w -15668,-15749,-15826,-15897,-15964,-16026,-16083
.dc.w -16135

.dc.w -16182,-16225,-16262,-16294,-16322,-16344,-16362
.dc.w -16374,-16382,-16384

147
ST 3D Graphics Abacus Software

.dc.w -16382,-16374,-16362,-16344, -16322,-16294,'-16262

. dc. w -16225,-16182

.dc.w -16135,-16083,-16026,-15964, -15897,-15826, -15749

.dc. w -15668

.dc. w -15582,-15491,-15396,-15296, -15191,-15082, -14962

• dc. w -14849

. dc. w -14726,-14598,-14466,-14330, -14189,-14044, -13894

.dc.w -13741

. dc. w -13583,-13421,-13255,-13085, -12911,-12733, -12551

.dc.w -12365

.dc.w -12176,-11982,-11786,-11585, -11381,-11174, -10963

.dc.w -10749

.dc.w -10531,-10311,-10087,-9860,- 9630,-9397,-9162,-8923

.dc.w -8682,-8438,-8192,-7943,-7692,-7438,-7182, -6924

.dc.w -6664,-6402,-6138,-5872,-5604,-5334,-5063, -4790

.dc.w -4516,-4240,-3964,-3686,-3406,-3126,-2845, -2563

.dc.w -2280,-1997,-1713,-1428,-1143,-857,-572,-286,0

.even

• bss

xO: • ds. w 1 * Position of the coordinate origin on


o

• ds. w 1 * the screen


>

zO: .ds. w 1

zl: .ds. w 1

linxy .ds. 1 1 * This is the address of the line array

nurrmark: . ds. w 1 * Number of points

numline: -ds. w 1 * Number of lines

pointx: . ds. 1 1 * Variables of point arrays for world,

pointy: .ds. 1 1 * view, and screen coordinates

pointz: • ds. 1 1

xplot .ds.l 1

yplot .ds. 1 1

datx: .ds.l 1

daty: .ds.l 1

datz: .ds.l 1

148
Abacus Software ST 3D Graphics

sinx: .ds.w 1 * Temporary storage for sine and

sinz: .ds.w 1 * cosine values

siny: .ds.w 1

cosx: .ds.w 1

cosz: .ds.w 1

cosy: .ds.w 1

varl: .ds.w 1 * general variables

var2: .ds.w 1

var3: .ds.w 1

xangle: .ds.w 1 * Variables for passing angles

yangle: . ds . w 1 * to the rotation subroutine

zangle: .ds.w 1

physbase: -ds. 1 1 * Address of first screen page

logbase: .ds. 1 1 * Address of second screen page

contrl: * Arrays for AES and VDI functions

opcode: .ds.w 1 * for passing parameters

sintin: .ds.w 1

sintout: .ds.w 1

saddrin: .ds.w 1

saddrout: .ds.w 1

.ds.w 6

global:

apversion: .ds.w 1

apcount: .ds.w 1

apid: .ds.w 1

apprivate: • ds. 1 1

apptree: .ds. 1 1

aplresv: .ds. 1 1

ap2resv: .ds. 1 1

ap3resv: . ds . 1 1

ap4resv: . ds. 1 1

intin: • ds.w 128

ptsin: .ds.w 256

intout: .ds.w 128

149
ST 3D Graphics Abacus Software

ptsout: .ds. w 128

addrin: .ds. w 128

addrout: . ds. w 128

grhandle: .ds. w 1

lineavar: .ds. 1 1 * Starting address of Line-A var

. data

vdipb: .dc. 1 contrl,intin,ptsin,intout,ptsout

aespb: . dc. 1 contrl,global,intin,intout,addrin,addrout

leftx: .dc. w 0

lefty: .dc.w 0

rightx: .dc. w 0

righty: . dc. w 0

plcode: .dc.w 0

p2code: .dc. w 0

codel: .dc.w 0

code2: .dc.w 0

mid_code: .dc.w 0

clipxule: .dc.w 0 * Clip window variables

clipyule: .dc.w 0

clipxlri: .dc.w 639

clipylri: .dc.w 399

dist: .dc.w 0

zobs: .dc.w 1500

rotxll: .dc.w 16384 * Space here for the result matri

rotxl2: .dc.w 0 * matrix multiplication

rotxl3: .dc.w 0

rotx21: .dc.w 0

rotx22: .dc.w 16384

rotx23: .dc.w 0

rotx31: .dc.w 0

rotx32: .dc.w 0

rotx33: .dc.w 16384

.bss

150
Abacus Software ST 3D Graphics

matrixll: * Space here for the general

matrixl2: * rotation matrix

matrixl3:

matrix21:

matrix22:

matrix23:

matrix31:

matrix32:

matrix33:

Desk File View Options


nmiffiiM 0 F:\3DUORK.DIRS
333956 bytes used In
253882 butes used a
1 1442236 butes used in 128 itens.
ASLINK1 BAT

I
K PRINTERS BASIC PRG 138944 li-20-Ul
X TUTORIAL BASIC RSC 4648 11-20 i] BASIC1 S
C FKY BASIC HRK 346 11-20- GRLIHK BAT
COM TTP BASIC1 BAR 14801 11-20 BRLINK1 0
KLIB PRG 6RLINK1 S
OUTPUT PRG OPEN APPLICATION 6R0UND1 S
SPLIT TTP HIDE1 PRG
STANDARD PRT Name: BATCH .TTP HIDE1 S
TEXTPRO PRG Parameters: . HOUSE1 PRG
TUTORIAL TXT aslink grllnkl housel|__- H0USE1 S
XTTUTORI TOC HAIN1 PRG
OK I I Cancel HAIN1 S
IkJUl HAIN1C0 PRG
IJI
1 MAIN1C0 S
ST 3D Graphics Abacus Software

4.1.1 Explanation of the subroutines used

grlinkl.s

The transfer of addresses of all data, coordinates, number of corners and


lines is not made directly, but through global variables. This increases
flexibility and makes it possible to use just one rotation routine. For
example, the perspective transformation routine (pers) transforms the data
whose beginning addresses are passed in the variables pointx,
pointy, pointz and the number of which is passed in the variable
nummark, in an array, whose starting address is also passed
(xplot, yplot). Because of this it does not matter where data is stored
in memory and the amount is irrelevant. For example, the transformation
can be carried out for all defined points or only for a few. The brief
overview which follows on the subroutines of the link file grlinkl. s
should be supplemented with the comments in the program.

s st art: Initialize the program.

aes: Call a function from the AES library.

vdi: Calls a function from the VDI library.

ap i n it: Announce an application.

openwork: Open a logical display.

gra f hand: Returns the number of this logical display.

mouse on: Enables the mouse and its controller through the
^ — operating system.

mouse_of f: Switches off mouse and controller.

sincos: Returns the sine (Dl) and cosine value (D2) of an


angle (-360,+360) passed in DO.

st art 1: Asks for the display address of the system and


recognizes what screen resolution is being used;
this serves to determine the two screen pages.

152
Abacus Software ST 3D Graphics

clwork: VDI-Function, clears the current logical display.

plotpt: Plots a point, X-coordinate in D2, Y-coordinate in


D3.

drawl: Draws a line from XI, Y1 to X2,Y2 taking the


Clip window specified by the variables clipule,
cliplre into account using the line-A routine.

rel_pos: Recognizes the area in which the point passed in


D6 (X-coord.) and D7 (Y-coord.) lies relative to
the clip window. The result is returned in D1 (4-bit
code).

end point: Finds, if present, an intersection point of the line


with the border of the clip window.

matinit: Initializes the main diagonal of the rotation matrix


(matrixll-matrix33) with 16384 which
corresponds to a sine value of one.

xrotate: Multiplies the rotation matrix by the matrix for


one rotation about the X-axis.

yrotate: Multiplies matrix with the matrix for rotation


about the Y-axis.

zrotate: Same for Z-axis.

Mtoate: This is the general rotation routine. Here every


point from the point array (passed in point x etc.)
is rotated around the angles xw, yw, zw, and then is
moved to point [xoffs, yoffs, zoffs] after a
preliminary displacement of the coordinate origin
to point [of fx, of fy, of f z],

pers: Calculates the perspective screen coordinates and


stores them at addresses passed in xplot,
yplot.

153
ST 3D Graphics Abacus Software

symbol: Connects the points in the screen coordinate array


with lines. The address of the line array is in
linxy, and the number of lines in numlin.

— pagedown: Turns on the logical screen page. After the call


drawing is done on the other page.

page up: Turns on the physical (higher) display page.


Subsequent drawing is done on the logical page
(toggle).

waitl: A timer loop which only counts the DO-register


down to -1.

wait: Waits for a key press and then returns.

inkey: Senses the keyboard without waiting. The ASCII


and key codes are returned in register D 0.

printf: Writes a string on the display which must be


terminated with a zero. The address is passed in
AO.

yrot: This routine, and the five following routines are


not used by the first main program. It rotates a
number of points around the Y-axis directly and
without use of matrix multiplication.
5/
filstyle: The VDI function sets the fill style which is passed
in DO (0=no fill, l=fill with color, 2=fill with dots,
\
3=shade, 4=user-defined fill pattern).

f i 1 index: Sets the various fill patterns according to style

f ilcolor: Determines the fill color (for monochrome display


only black or white, l=black).

f ilmode: Sets the write mode, 1 = replace.

filform: Subsequent filled surfaces will be surrounded with


a border after calling this routine.

154
Abacus Software ST 3D Graphics

**********************************************************************
* housel.s 14.1.1986 *

* Display a wire-model house Uwe Braun 1985 Version 1.1 *


* *

**********************************************************************

.globl main,xoffs,yoffs,zoffs,offx,offy,offz

.globl viewx,viewy,viewz

.globl wlinxy,setrotdp,inp_chan,pointrot

.text

main:

jsr apinit * Announce program

jsr grafhand * Get screen handler

jsr openwork ★ Announce screen

jsr mouse off * Turn off mouse

jsr getreso * which monitor is connected ?

jsr setcocli * Set clip window

jsr makewrld ★ Create the world system

jsr worldset * Pass the world parameters

jsr setrotdp ★ initialize obs. ref. point

jsr clwork ★ erase both screen pages

jsr ('•"pagedown^5 * Display logical screen page , j

jsr clwork .
jsr inp chan ★ Input and change parameters' ^ *

mainlopl:

]sr * rotate around obs. ref. point

jsr * perspective transformation

jsr * Draw lines in linxy array

jsr * Display physical screen page

jsr inp_chan * Input new parameters

jsr clwork * erase V^logical,; screen page

jsr pointrot * Rotate around Rot. ref. point

jsr pers * Transform, of new points

jsr drawnl_* draw in logical page, then _

jsr pagedowp!) * display this logical page

jsr inp chan- * Input and change parameters

155
ST 3D Graphics Abacus Software

jsr clwork * erase physical page

jmp mainlopl * to main loop

move. 1 physbase,logbase

jsr (pageup) * switch to normal display page

rts back to linkfile, and end

********************************************************************

* Remove all accumulated characters in the keyboard buffer "


********************************************************************

clearbuf: move.w #$b,-(a7) * Gemdos funct. Character in buffer?

trap #1

addq.1 #2,a7

tst. w dO * If yes, get character

beq clearend * If no, terminate

move.w #1, -(a7) * Gemdos funct.CONIN

trap #1 * repeat until all characters are

addq.1 #2,a7 * removed from the buffer

bra clearbuf

clearend: rts

**********************************************************************
* Change observation parameters with keyboard sensing *

* Angle increments, location of the projection plane, etc.


**********************************************************************

inp chan: jsr inkey * Read keyboard, code in DO

cmp .b #' D',dO * shift D = print

bne inpwait

jsr scrdmp * make hardcopy

inpwait: swap dO * test DO, if

cmp .b #$4d,dO * Cursor-right

bne inpl

addq.w #1,ywplus * if yes, add one to Y-angle

bra inpendl * increment and continue

inpl: cmp -b #$4b,dO * Cursor-left, if yes, then

bne inp2 * subtract one from Y-angle

156
Abacus Software ST 3D Graphics

subq.w #1, ywplus * increment

bra inpendl

inp2: cmp .b #$50,dO * Cursor-down, if yes

bne inp3

addq. w #1,xwplus * then add one to X-angle increment

bra inpendl

inp3: cmp. b #$48,dO * Cursor-up

bne inp3a

subq.w #1,xwplus * subtract one

bra inpendl

inp3a: cmp .b #$61,dO * Undo key

bne inp3b

subq.w #1,zwplus

bra inpendl

inp3b: cmp.b #$62,dO * Help key

bne inp4

addq.w #1,zwplus

bra inpendl

inp4: cmp.b #$4e,dO * plus key on numerical keypad

bne inp5 * if yes, subtract 25 from location

sub. w #25,dist * Projection plane (Z-coordinate)


\
bra inpendl

inp5: cmp.b #$4a,dO * minus key on the numerical keypad


*
bne inp6

add.w #25,dist * if yes, add 25

bra inpendl

inp6: cmp.b #$66,dO * astersisk key on numerical keypad

bne inp7 * if yes, subtract 15 from rotation

sub. w #15,rotdpz * point Z-coordinate

bra inpendl * Make changes

inp7: cmp.b #$65,dO * Division key on num.keypad

bne inplO

add. w #15,rotdpz * add 15

bra inpendl

157
ST 3D Graphics Abacus Software

inplO: cmp .b #$44,dO * F10 activated ?

bne inpendl

addq. 1 #4,a7 * if yes, jump to

bra mainend * program end

inpendl: move. w hyangle,dl * Rotation angle about Y-axis

add. w ywplus,dl * add increment

cmp. w #360,dl * if larger than 360, then

bge inpend2 * subtract 360

cmp. w #-360,dl * is smaller than 360, then

ble inpend3 * add 360

bra inpend4

inpend2: sub. w #360,dl

bra inpend4

inpend3: add.w #360,dl

inpend4: move.w dl,hyangle

move . w hxangle,dl * proceed in the same manner

add.w xwplus,dl * with the rotation angle about

cmp.w #360,dl * the X-axis

bge inpend5

cmp.w #-360,dl

ble inpend6

bra inpend7

inpend5: sub. w #360,dl

bra inpend7

inpend6: add.w #360,dl

inpend7: move.w dl,hxangle * store angle

move. w hzangle,dl

add.w zwplus,dl

cmp. w #360,dl

bge inpend8

cmp. w #-360,dl

ble inpend9

bra inpendlO

inpend8: sub. w #360,dl

bra inpendlO

inpend9: add. w #360,dl

158
Abacus Software ST 3D Graphics

inpendlO: move.w dl,hzangle

rts

* Initialize the rotation reference point to [0,0,0] *

move . w #0, dl * set the start-rotation-

move . w dl,rotdpx * datum-point

move. w dl,rotdpy

move.w dl,rotdpz

move . w #0,hyangle * Start-rotation angle

move. w #0,hzangle

move.w #0,hxangle

rts

* Rotation around one point, the rotation reference point *

pointrot: move.w hxangle,xangle * rotate the world around the angle

move.w hyangle,yangle * hxangle, hyangle, hzangle about the

move.w hzangle,zangle

move . w rotdpx,dO * rotation reference point

move.w rotdpy,dl

move.w rotdpz,d2

move. w dO,xoffs * add for back transformation.

move.w dl,yoffs

move.w d2,zoffs

neg. w dO

neg. w dl

neg. w d2

move.w dO,offx * subtract for transformation.

move.w dl, of fy

move.w d2,offz

jsr matinit * Matrix initialization

jsr zrotate * first rotate around Z-axis

jsr yrotate * rotate 'matrix' around Y-axis

jsr xrotate * then rotate around X-axis

jsr rotate * Multiply points with the matrix.

rts

159
ST 3D Graphics Abacus Software

* Creation of the world system from the object data *

makewrld: move.1 thousdatx, al * create the world system by

move.1 Ihousdaty,a2

move.1 #housdatz,a3

move.1 #worldx,a4

move.1 #worldy,a5

move.1 #worldz,a6

move.w hnummark,dO

ext .1 dO

subq.1 #1, dO

makewll: move.w (al) +, (a4) + * copying the house data into

move.w (a2)+, (a5) + * world data

move. w (a3)+, (a6) +

dbra dO,makewll

move . w hnumline,dO

ext. 1 dO

subq.1 U,dO

move. 1 thouslin,al

move . 1 dwlinxy,a2

makewl2: move. 1 (al) +, (a2) +

dbra d0,makewl2

rts

********************************** **********************************
* Pass the world parameters to the link file variables *
********************************** **********************************

worldset: move.1 #worldx,datx * Pass variables for

move.1 #worldy,daty * the rotation routine

move.1 #worldz,datz

move.1 #viewx,pointx

move.1 #viewy,pointy

move.1 #viewz,pointz

move.1 #wlinxy,linxy

move.w picturex,xO

move. w picturey,yO

move.w proz,zobs

move.w rlzl,dist

move.1 #screenx,xplot

160
Abacus Software ST 3D Graphics

move.1 Iscreeny,yplot

move.w hnumline,numline

move.w hnummark,nummark

rts

*********************************************************************
* sense current display resolution and set coordinate origin of the *

* screen system to the center of the screen *


*********************************************************************

getreso: move.w #4,-(a7)

trap #14

addq.1 #2,a7

cmp.w #2, dO

bne getrl

move. w #320,picturex * for monochrome monitor

move.w #200,picturey

bra getrend

getrl: cmp.w #1, dO

bne getr2

move. w #320,picturex * medium resolution (640*200

move.w #100,picturey

bra getrend

getr2: move.w #160,picturex * low resolultion (320*200)

move.w #100,picturey

getrend: rts

**********************************************************************
* Hardcopy of the display after activating Shift d on keyboard *
**********************************************************************

scrdmp: move.w #20,-(a7)

trap #14

addq.l #2,a7

jsr clearbuf * prevent another hardcopy

rts

161
ST 3D Graphics Abacus Software

*********************************************************************
* Sets the limit of the display window for the draw-line algorithm *

* built into the Cohen-Sutherland clip algorithm *

* The limits are freely selectable by the user, making the draw- 4

* line algorithm very flexible.


*********************************************************************

move.w #0, clipxule * Clip left X-Coord.

move.w #0,clipyule ★ ” Y-Coord

move. w picturex,dl

lsl. w #1, dl * times two

subq.w #1, dl * minus one equal

move.w dl,clipxlri * 639 for monochrom

move.w picturey,dl

lsl. w #1, dl * times two minus one equal

subq.w #1, dl * 399 for monochrom

move.w dl,clipylri ★ Clip right Y-Coord

rts

.even

* Here begins the variable area for the program module *


* *

* *

* Definition of the house *


* *

. data

housdatx: .dc.w -30,30,30,-30,30,-30,-30,30,0,0,-10,-10,10,10

,dc. w 30,30,30,30,30,30,30,30,30,30,30,30

housdaty: .dc.w 30,30,-30,-30,30,30,-30,-30,70,70,-30,0,0,-30

.dc.w 20,20,0,0,20,20,0,0
.dc.w -10,-10,-30,-30

162
Abacus Software ST 3D Graphics

housdatz: .dc.w 60,60,60,60,-60,-60,-60,-60,60,-60,60,60,60,60

.dc.w 40,10,10,40,-10,-40,-40,-10

.dc.w 0,-20,-20,0

houslin: .dc.w 1,2,2,3,3, 4,4,1,2,5,5,8,8,3,8,7,7,6,6,5,6,1,7,4

.dc.w 9, 10, 1, 9, 9, 2,5,10, 6,10, 11,12, 12, 13, 13,14

.dc.w 15,16,16,17,17,18,18,15,19,20,20,21,21,22,22,19

.dc.w 23,24,24,25,25,26,26,23

hnummark: .dc.w 26 * Number of corner points of the house

hnumline: .dc.w 32 * Number of lines of the house

hxangle: .dc.w 0 * Rotation angle of the house around X-axis

hyangle: .dc.w 0 * " " " Y-axis

hzangle: .dc.w 0 * " " " Z-axis

xwplus: .dc.w 0 * Angle increment around the X-axis

ywplus: .dc.w 0 * Angle increment around the Y-axis

zwplus: .dc.w 0 * Angle increment around the Z-axis

picturex: • dc.w 320 * Definition of zero point of display

picturey: .dc.w 200 * here it is in the display center

rotdpx: .dc.w 0 * Rotation datum point

rotdpy: .dc.w 0
rotdpz: .dc.w 0

rlzl: .dc.w 0
normz: . dc. w 1500

.bss

plusrot: . ds. 1 1
first: .ds. 1 1
second: . ds . w 1

deltal: .ds. w 1

.data

163
ST 3D Graphics Abacus Software

flag: . dc .b 1

. even

-bss

diffz: .ds.w 1

dx: .ds .w 1

dy: -ds.w 1

dz: .ds.w 1

worldx: .ds.w 1600 * World coordinate array


worldy: -ds. w 1600

worldz: .ds.w 1600

viewx: .ds.w 1600 * View coordinate array


viewy: .ds.w 1600

viewz: .ds.w 1600

screenx: .ds.w 1600 * Display coordinate array


screeny: .ds.w 1600

wlinxy: .ds. 1 3200 * Line array

.data

prox: .dc. w 0 * Coordinates of the Projection-


proy: .dc. w 0 * center, on the positive
proz: . dc. w 1500 * Z-axis

. data

of fx: .dc. w 0 * Transformation during Rotation


of fy: . dc. w 0 * to point [offx,offy,offz]
of fz: . dc. w 0
xoffs: .dc.w 0 * Back transformation to Point
yoffs: .dc. w 0 * [xof yof f s, zof fs]
zoffs: .dc.w 0 7
.bss

loopc: .ds. 1 1

• end

164
Abacus Software ST 3D Graphics

4.1.2 Description of the Subroutines of the first Main program:

main: This is the entry point to the program module. The


program announces itself and initializes the AES
and VDI functions and senses the current screen
resolution. The window size and the screen are
determined from the resolution. The program
section between the labels mainopl: and mainend:
is the main loop, which is repeated until the F10
key is pressed.

makewrld: Creates a world in the world coordinate system by


simple copying of the house data into the world
system. These are the coordinates of the house
(housdatx, housdaty, housdatz) in the
world coordinate system (wrldx, wrldy,
wrldz), the lines of the house in houslin in the
world line storage area (wlinxy), the number of
comer points the house (hnummark) in the total-
number variable of the world system (nummark)
and finally the number of house lines (hnumline)
in numline. This subroutine need only be called
once unless you want to add objects to the world
system which we will do in a later program.

wrldset: After creating the world system the array addresses


(wrldx etc.) must be passed to the global
variables of the rotation subroutine (datx etc.).
Furthermore the coordinate origin of the display is
determined in the Variables XO and YO, and the
presets for the perspective parameters
(zobs, dist).

setrotdp: Initializes the rotation reference point to [0,0,0]


and the rotation angles to 0 degrees.

165
ST 3D Graphics Abacus Software

pointrot: This subroutines provides the rotation routine with


the current data and then performs the rotation
around the point [rotdpx, rot dp, rotdpz] of
all three axes with a call to the proper routines of
the link file, in the sequence Z-axis, Y-axis, X-axis.
A change in the sequence also changes the results.

inp chan: Input and change the parameters, rotation angle,


rotation reference point and position of the projec¬
tion plane.

getreso: Checks the current display resolution and from this


determines the data for the screen center and the
clip window, which in this case is the whole visible
display.

scrdmp: Hardcopy routine, is called form inp_chan by


pressing shift ’D’ and replaces the key combination
Altemate/Help, which the operating system uses to
make a hardcopy of the screen. Since in this
program the displayed page is never the same as
the page in which the drawing occurs, a hardcopy
through Altemate/Help would not correspond to
the displayed picture but would print the picture
under construction or the just-erased display. The
trick is to call the scrdmp routine before the
displayed page is erased.

setcocli: Set the clip-window for the Cohen-Sutherland clip


algorithm on the whole display, 0,0 to 639,399 hi¬
res, 639,199 med-res, or 319,199 lo-res.

clearbuf: Remove characters that may be in the keyboard


buffer. Is used only by the hardcopy routine, since
several hardcopies could otherwise be made in
succession (Key repeat).

166
Abacus Software ST 3D Graphics

4.1.3 General comments on the program

The specific explanations of the variables can be found in the remarks in


the program listing. In each iteration of the main loop the program adds
an angle increment (xwplus, ywplus, zwplus) to the rotation angle
(hxangle, hyangle, hzangle) of the house. The input routine
changes the angle increments which causes the house to rotate faster on
the screen, though this is really an optical illusion. The end points of the
house have to travel a longer distance between each drawing operation,
which causes this effect. The cursor keys, the <Help> and <Undo> keys
control the rotation, the V and ’-’keys change the display size by
moving the projection plane, and the ’/’ and ’*’ keys move the rotation
reference point on the Z-axis. Pressing of the shift and ’D’ keys at the
same time produces hardcopy if a printer is attached.

The best thing to do is to try out the various changes possible, preferably
by changing the constants in the listing. You can, for example move the
rotation reference point on the X and Y axis, or the variable proz, which
changes the position of the projection center. The closer you move the
projection center in the direction of the house, the greater the perspective
distortion. You should also define an object yourself, and you should start
with a simple object, like a pyramid. You only have to enter the points of
the pyramid (in a pyramid with a quadratic base there are five) in place of
the house coordinates in the arrays (housdatx etc.). Furthermore, the
number of points (5) must be entered in houslin in hnummark, the
number of lines (8) in hnumline and then the information regarding
which points are connected by lines. You only have to change the storage
area and you can represent any defined object with the same program.

Here I want to provide some additional information about the storage


space required. The arrays (wrldx etc., viewx, screenx,
wlinxy) are already dimensioned quite generously for future expansion.
You can define objects with 1600 comers and connect these comers with
3200 lines. About 40 KByte of storage space is needed for this array
dimensioning. Even though 1600 comers appear to be sufficient at first
glance, we shall reach this number in the next chapter without too much
effort. But first of all stop for a while and play around with this program.
You can also add a window on the other side of the house by simply
entering the new coordinates.

167
LIS projection centers
Abacus Software ST 3D Graphics

4.2 Generation techniques for creating rotating objects c


\ 1'4,-fc, ?

If you have experimented with the construction of new objects, you


probably also noticed the considerable effort involved in construction,
especially for regularly-formed bodies with many comers. Imagine if you
had to input the end points of the ball approximated by polygons (See
figure 4.2.1).

Figure 4.2.1: Hardcopy of the rotation ball

The drudgery of input can be performed by the computer for all axis-
symmetrical objects. As an example, consider the ’’chess piece’’ from
Figure 4.2.2. This figure can be created by rotating a line (the definition
line) around any axis, in this case the Y-axis. The programmer must

169
ST 3D Graphics Abacus Software

define the one line and indicate how many times it should be rotated. You
can follow the construction of the figure easily on the following
hardcopies. The rotation number must be a division of 360 for
programming reasons or a portion of the figure will be missing. From two
to four to three hundred sixty rotations are available. More than 180 just
produces a heap of points on the display (the screen resolution is too
low). Now the space requirement will become obvious. If you rotate the
12 points 360 times it results in 4,332 points not to mention the 8,291
lines created by the rotation. The number of points is calculated as
follows: nummark: =numpt* (rotations + 1). The lines include the
connecting lines of the points in the rotating definition line as well as the
horizontal connecting lines of the points in the rotation line.

The routines for the creation of the rotation body are contained in the
listing of the file rotate 1. s. The rotation body is described by a line,
i.e. a number of points (rlnumpt), whose coordinates are in rlxdat,
rlydat, r 1 zdat and the number of rotations about the Y-axis which
this line should perform. The different bodies are created by varying the
number of rotations. The maximum number of rotations in our case is
120, which is predetermined by the dimensioning of the array to 1600 etc.
and of course could be changed. The number of points of the rotation
body is contained in the variable rlnumpt. The link file remains the
same as in the first program. You only have to assemble the first file and
link it to the link file: as link grlinkl rotatel.

Desk File View Options


A:\ D:\ K F:\3DU0RK.DIRS
253882 butes used i 1442236 butes used in 123 ltens. 333556 butes used tn
8 PRINTERS BASIC PR6 138944 11-28 H0USE1 PRB
8 TUTORIAL BfiSIC RSC 4648 11-28: NQUSE1 S
C FKY BASIC HRK 346 11-2 MAIN1 PRG
C0NV TIP BASIC1 BAK 14801 11-2 MAIN1 S
NL1B PRC MAIN1C0 PRC
OUTPUT PRC HAIN1C0 S
OPEN APPLICATION
SPLIT TTP HENU1 PRG
STftNDRRD PRT Nanei BATCH • TTP MENU1 S
TEXTPRO PRC Parameters: MULTIi PRG
TUTORIRL TXT aslink grlinkl rotatei[ MUL Til S
XTTUTORI TOC PAINT1 PRG
I OK I ^ | Cancel I
PAINT1 S
R0TATE1 PRG
R0TATE1 S

a*
WBSTSE23M
a*I'-H'Ji i"?u■a*iinn i:*tw
a?ttnm

170
Abacus Software ST 3D
ST 3D Graphics Abacus Software

**********************************************************************
* rotatel.s 16.1.1986 *
* Creation of rotation bodies Uwe Braun 1985 Version 2.0 *

.text

.globl main,xoffs,yoffs,zoffs,offx,offy, offz


-globl viewx,viewy,viewz
.globl wlinxy,mouse off,setrotdp,inp chan,pointrot

main:
jsr apinit * Announce program
jsr grafhand * Get screen handle
jsr openwork it
Display
jsr mouse_off * Turn off mouse
jsr getreso * Which monitor is connected ?
jsr setcocli * Set clip window

jsr makerotl

jsr makewrld * Create world system

jsr wrld2set * Pass world parameters

jsr setrotdp * initialize observation ref. point

jsr clwork
jsr pagedown * Display logical screen page

jsr clwork
jsr inp_chan * Input and change parameters

mainlopl:
jsr pointrot * rotate around observation ref. point

jsr pers * Perspective transformation


jsr drawnl
jsr pageup * Display physical page

jsr inp_chan * Input new parameters


jsr clwork * Erase logical page
jsr pointrot * Rotate around rotation ref. point
jsr pers * Transform, new points
jsr drawnl

jsr pagedown *
Display this logical page
jsr inp_chan *
Input and change

172
Abacus Software ST 3D Graphics

jsr clwork * clear physical page

jmp mainlopl * to main loop

mainend: move.1 physbase,logba:se

jsr pageup * switch to normal display page


* back to link file, and end
rts

*
****************
* remove all characters from the keyboard buffer

*
*******************************************************************

clearbuf: move.w #$b,-(a7) * Gemdos funct. char in buffer?

trap #1

addq.1 #2,a7

tst -w dO * if yes, get character

beq clearend * if no, terminate

move.w #1,- (a7) * Gemdos funct. CONIN

trap #1 * repeat until all characters

addq.1 #2,a7 * are removed from the buffer

bra clearbuf

clearend: rts

* Create the rotation body rl

makerotl: jsr rlset * Create the rotation body

jsr rotstart * first the coordinates,

jsr rotlin * then the lines

rt s

173
ST 3D Graphics Abacus Software

*******************it** + -k1t*-k-k**ic1t1r*1i1iii*1t1r1tl!*1cirit**ir1'1c1r1r***1i1t**i'*iiitit1i1r1rit

* Input and change observation parameters *

* the angles hxangle,hyangle,hzangle, are rotation angles of *


* world system *

inp chan: jsr inkey * Sense keyboard, code in


cmp.b #'D',dO
bne inpwait
jsr scrdmp *
make hardcopy

inpwait: swap dO * test DO if


cmp.b #$4d,dO * Cursor-right
bne inpl

addq. w #1,ywplus * if yes, add one to Y-angle increment


bra inpendl * and continue

inpl: cmp.b #$4b,dO * Cursor-left, if yes


bne inp2 * subtract one from Y-angle
subq.w #1,ywplus * increment
bra inpendl

inp2: cmp.b #$50,dO * Cursor-down, if yes


bne inp3

addq.w #1,xwplus * add one to X-angle increment


bra inpendl

inp3: cmp.b #$48,dO * Cursor-up


bne inp3a

subq. w #1,xwplus * subtract one


bra inpendl

inp3a: cmp.b #$61,dO ★ Undo-key


bne inp3b

subq.w #1,zwplus * lower Z-increment


bra inpendl

inp3b: cmp.b #$62,dO * Help-key


bne inp4

addq. w #1, zwplus *


add to Z-increment
bra inpendl

174
Abacus Software ST 3D Graphics

inp4: cmp .b #$4e,dO * plus key on keypad

bne inp5 * if yes, subtract 25 from

sub. w #25,dist * position of projection

bra inpendl * plane (Z-coordinate)

inp5: cmp.b #$4a,d0 * minus key on keypad

inp6 *
bne

add.w #25,dist * if yes, add 25

bra inpendl

inp6: cmp.b #$66,dO * times-key on the keypad

bne inp7 * if yes, then subtract 15

sub. w #15,rotdpz * from the rotation ref. point Z-coord.

bra inpendl * make changes

inp7: cmp.b #$65,dO * division-key on keypad

bne inplO

add.w #15, rotdpz * add 15

bra inpendl

inplO: cmp.b #$44,dO * F10 activated ?

bne inpendl

addq.1 #4,a? * if yes, jump to

bra mainend * Program end

move. w hyangle,dl * rotation angle, Y-axis


inpendl:
add.w ywplus,dl * add increment

cmp.w #360,dl * if larger than 360, then subtract 360

bge inpend2

cmp. w #-360,dl * if smaller than 360,

ble inpend3 * add 360

bra inpend4

inpend2: sub. w #360,dl

bra inpend4

inpend3: add. w #360,dl

inpend4: move. w dl,hyangle

move. w hxangle,dl * proceeed in the same

add. w xwplus,dl * manner with rotation

cmp.w #360,dl * angle, X-axis

bge inpend5

cmp. w #-360,dl

175
ST 3D Graphics Abacus Software

ble inpend6
bra inpend7
inpend5: sub. w #360,dl

bra inpend7
inpend6: add. w #360,dl
inpend7: move. w dl,hxangle
move. w hzangle,dl
add. w zwplus,dl
cmp. w #360,dl

bge inpend8

cmp. w #-360,dl

ble inpend9
bra inpendlO
inpend8: sub. w #360,dl

bra inpendlO
inpend9: add.w #360,dl

inpendlO: move.w dl,hzangle


rts

* Initialize the rotation reference point to [0,0,0] *

move.w #0, dl * set the start-rotation


move.w dl,rotdpx * reference point
move.w dl,rotdpy
move. w dl,rotdpz

move. w #0,hyangle * Start rotation angle


move.w #0,hzangle
move. w #0,hxangle

rts

******************************************************************
* Rotation of the total world system around the rotation *
* reference point *
******************************************************************

pointrot: move.w hxangle,xangle * rotate the world around


move.w hyangle,yangle

move.w hzangle,zangle

move.w rotdpx,d0 * the rotation reference point

176
Abacus Software ST 3D Graphics

move.w rotdpy,dl

move . w rotdpz,d2

move. w dO,xoffs * add for inverse transformation

move . w dl,yoffs

move.w d2,zof fs

neg. w dO

neg. w dl

neg. w d2

move. w dO,offx * subtract for transformation

move. w dl, offy

move. w d2,offz

jsr matinit * matrix initialization

jsr zrotate * rotate around Z-axis first

jsr yrotate * rotate 'matrix' around Y-axis

jsr xrotate * then rotate around X-axis

jsr rotate * multiply points with the

rts * matrix. The Z-axis is not taken into

★ * account

makewrld: move. 1 #rldatx,al * create the world system

move . 1 #rldaty,a2 * by copying data of rotation body

move.1 ffrldatz, a3 * into world system

move.1 #worldx,a4

move. 1 #worldy,a5

move. 1 #worldz,a6

move. w rlnummark,dO * number of corners repeated

ext .1 dO

subq. 1 #1, dO

makewll: move . w (al)+, (a4) + * Copy coordinates

move . w (a2)+, (a5) + * Y-coords.

move. w (a3)+, (a6) + * Z-coords.

dbra dO,makewll

move.w rlnumline,dO * Copy the line arrays

ext. 1 dO * of the rotation body

subq.1 #1, dO * into the world system

move.1 #rllin,al * Number of lines as counter

move.1 #wlinxy,a2

jnakewl2 : move.1 (al)+,(a2)+ * copy lines

dbra dO,makew!2

rt s

177
ST 3D Graphics Abacus Software

***********************************************************************

* Pass world parameters to variables of link files *


***********************************************************************

worldset: move.l #worldx,datx * Passing house variables


move.1 fworldy,daty * for the rotation routine
move.1 #worldz,datz * and the global subroutine
move.1 #viewx,pointx * of the link module
move.1 #viewy,pointy

move.1 #viewz,pointz

move.1 #wlinxy,linxy

move.w picturex,xO

move.w picturey,yO

move.w proz,temp * Projection center Z-coordinate


move.w rlzl,dist * Location of projection plane on
move.1 iscreenx,xplot * the Z-axis
move.1 iscreeny,yplot

move.w hnumline,numline * Number of house lines


move.w hnummark,nummark * Number of house corners
rts

***********************************************************************
* Creation of rotation body in the array, the address of which *

* is passed in the variables rotdatx, rotdaty, rotdatz *


***********************************************************************

rlset:

move. 1 #rlxdat,rotxdat ★ Transmit


move. 1 #rlydat,rotydat *
parameters of this
move.1 #rlzdat,rotzdat ★ rotation body to
move.1 irldatx,rotdatx ★
the routine for
move.1 ffrldaty, rotdaty *
creation of the
move.1 #rldatz,rotdatz *
rotation body
move.1 rotdatx,datx

move. 1 rotdaty,daty

move. 1 rotdatz,datz

move. w rlnumro,numro * Number of desired


move.w rlnumpt,numpt * rotations. Number
move.l #rllin,linxy * of points in def.line
rts ★
Address of line array

178
Abacus Software ST 3D Graphics

rotstart: move.w numpt,dO * Rotate def line

lsl. w #1, dO * numro+1 about Y-axis

ext. 1 dO

move.1 dO,plusrot

move.w numpt,nummark

move. 1 rotdatx,pointx * Pass data array

move.1 rotdaty,pointy * to subroutine yrot

move.1 rotdatz,pointz

move.w #0,yangle

move.w #360,dO * 360 / numro = angle increment

divs numro,dO * per rotation

move.w dO, plusagle

move. w numro,dO * numro +1 times

ext. 1 dO

rloopl: move.1 dO,loopc * as loop counter

move.1 rotxdat,datx * for passing to yrot

move.1 rotydat,daty

move.1 rotzdat,datz

jsr yrot * rotate

move. 1 pointx,dl * add offset to

add. 1 plusrot,dl * address

move. 1 dl,pointx

move. 1 pointy,dl

add. 1 plusrot,dl

move. 1 dl,pointy

move. 1 pointz,dl

add. 1 plusrot,dl

move. 1 dl,pointz

move.w yangle,d7 * Add angle increment

add. w plusagle,d7 * to rotation angle

move. w d7,yangle * and rotate line

move. 1 loopc,dO * again until all

dbra dO,rloopl * end points are generated.

move. w rlnumro,numro * store for following

move.w rlnumpt,numpt * routines for line generation

rts

179
ST 3D Graphics Abacus Software

rotlin: * Create the line array of the

move.w #1, d7 * rotation body

move.w numro,d4 * Number of rotations repeated

ext. 1 d4

subq. 1 #1, d4

move.w numpt,dl

subq.w *1, dl

lsl. w #2, dl

ext. 1 dl

move.1 dl,plusrot

rotlopl: move.w numpt,d5 * Number of points -

ext. 1 d5 * repeat once

subq.1 #2,d5

move.1 linxy,al * Lines created stored

move.w d7,d6 * here

rotlop2: move.w d6, (al) + * The first line goes from

addq.w #1, dG * point one to point two

move.w d6, (al) + * (1,2) then (2,3) etc.

dbra d5,rotlop2

move. 1 linxy,dl * generate cross connections

add. 1 plusrot,dl * of individual lines

move.1 dl,linxy

move.w numpt,dO

add.w d0,d7

dbra d4,rotlopl

move. w numpt,d7

move.w d7,deltal

lsl. w #2,d7

ext. 1 dl

move.1 d7,plusrot

move.w #l,d6

move. w numpt,dO

ext. 1 dO

subq.1 #1, dO

rotlop3 : move.w numro,dl

ext. 1 dl

subq.1 #1, dl

move.w d6,d5

180
Abacus Software ST 3D Graphics

move.w d5, (al) +

add. w deltal,d5

move.w d5,(al)+

dbra dl,rotlop4

add. w #l,d6

dbra dO,rotlop3

move. w numro,dl

add. w #1, dl

mul s nummark,dl

move.w dl,rlnummark * Store total number of

move.w numpt,dl * corners created

muls numro,dl

move . w numpt,d2

subq.w #1, d2

muls numro,d2

add. w dl, d2

move.w d2,rlnumline * Total of lines created

rts

* Pass parameters of the world system to variables *

* of the link file for the rotation body *

wrld2set: move.1 iworldx,datx * Pass parameter of

move.1 fworldy,daty * rotation body 'to the

move.1 #worldz,datz * subroutines in the link

move.1 #viewx,pointx * module

move.1 #viewy,pointy

move.1 #viewz,pointz

move.1 #wlinxy,1inxy

move.w picturex,xO

move.w picturey,yO

move. w proz,temp

move.w rlzl,dist

move.1 Iscreenx,xplot

move.1 #screeny,yplot

181
ST 3D Graphics Abacus Software

move.w rlnumline,numline * Number of lines

move.w rlnummark,nummark * Number of corners

rts

a**********************************************************************

* Sense current display resolution and set the coordinate *

* origin of the screen system to the screen center *


***********************************************************************

getreso: move. w #4,-(a7)

trap #14

addq. 1 #2,a7

cmp. w #2, dO

bne getrl

move.w #320,picturex * monochrome monitor

move. w #200,picturey

bra getrend

getrl: cmp. w #1, dO

bne getr2

move. w #320,picturex * medium resolution (640*200

move.w #100,picturey

bra getrend

getr2: move.w #160,picturex * low resolution (320*200)

move.w #100,picturey

getrend: rts

* Hardcopy after inp_chan call *

scrdmp: move. w #20,-(a7)

trap #14

addq. 1 #2, a7

jsr clearbuf

rts

182
Abacus Software ST 3D Graphics

*********************************************************************
* Set the limit of the window for the Cohen-Sutherland *

* clip algorithm built into the draw-line algorithm *

* The user can choose the limits freely, which makes the *

* draw-line algorithm very flexible. *


*********************************************************************

move.w #0,clipxule

move.w #0, clipyule

move.w picturex,dl

lsl .w #1, dl ★ times two

subq.w #1, dl A minus one equals

move.w dl,clipxlri * 639 for monochrom

move.w picturey,dl

lsl .w #1, dl ★ times two minus one

subq.w #1, dl ★ equals 399 for monochrom

move.w dl,clipylri

rts

. even

* Begin variable area for Program module *


* *

* Data area for the rotation body *

.bss * Space for the variables

numro: .ds.w 1

numpt: .ds.w 1

worldfla: .ds.l 1

rotxdat: .ds.l 1

rotydat: .ds.l 1

rotzdat: .ds.l 1
ST 3D Graphics Abacus Software

rotdatx: .ds. 1 1

rotdaty: .ds. 1 1

rotdatz: . ds. 1 1

rlnumline: .ds .w 1

rlnummark: .ds. w 1

rlnumfla: . ds. w 1

plusagle: . ds. w 1

rldatx: .ds. w 1540

rldaty: . ds. w 1540

rldatz: .ds. w 1540

rllin: .ds. 1 3200 * for every line 4-Bytes

.data

***********************************************************************
* These are the coordinates of the definition line which *

* generates the rotation body through rotation about *

* the Y-axis. By changing coordinates the body to be *

* created can be changed. Of course, the number of points in *

* rlnumpt must be adapted to the new situation. By changing *

* rlnumro the current body can be changed as well. *

* Storage reserved here is enough for a maximum 120 rotations *

* of 12 points. This means that for a user-defined *

* rotation line, the product of the number of points and *

* number of desired rotations plus one, cannot be greater *

* than 1500. *
***********************************************************************

rlxdat: .dc.w 0,40,50,50,20,30,20,30,70,80,80,0

rlydat: -dc.w 100,100,80,60,40,30,30,-70,-80,-90,-100,-100

rlzdat: .dc.w 0,0,0,0,0,0,0,0,0,0,0,0

rlnumpt: .dc.w 12
rlnumro: .dc.w 8 * Number of rotations for creation

184
Abacus Software ST 3D Graphics

* Definition of the house *


* *

.data

housdatx: .dc.w -30,30,30,-30,30,-30,-30,30,0,0,-10,-10,10,10

.dc.w 30,30,30,30,30,30,30,30,30,30,30,30

housdaty: .dc.w 30,30,-30,-30,30,30,-30,-30,70,70,-30,0,0,-30

.dc.w 20,20,0, 0,20,20,0,0


.dc.w -10,-10,-30,-30

housdatz: .dc.w 60,60,60,60,-60,-60,-60,-60,60,-60,60,60,60,60

.dc.w 40,10,10,40,-10,-40,-40,-10

.dc.w 0,-20,-20,0

houslin: .dc.w 1,2,2, 3,3, 4,4,1,2,5,5,8, 8,3, 8, 7,7,6, 6,5,6,1,7, 4

.dc.w 9,10,1,9,9,2,5,10,6,10,11,12,12,13,13,14

.dc.w 15,16,16,17,17,18,18,15,19,20,20,21,21,22,22,19

.dc.w 23,24,24,25,25,26,26,23

hnummark: .dc.w 26 * Number of corners in the house

hnumline: .dc.w 32 * Number of lines in the house

hxangle: .dc.w 0 * Rotation angle of house about X-axis

hyangle: .dc.w 0 * .in « Y-axis

hzangle: .dc.w 0 * " " " Z-axis

xwplus: .dc.w 0 * Angle increment around X-axis

ywplus: .dc.w 0 * Angle increment around Y-axis

zwplus: .dc.w 0 * Angle increment around Z-axis

picturex: .dc 0 * Definition of zero point of the screen

picturey: . dc 0 * provided with values from subroutine getreso

185
ST 3D Graphics Abacus Software

rotdpx: • dc.w 0

rotdpy: .dc.w 0

rotdpz: .dc.w 0

rlzl: .dc.w 0

normz: .dc.w 1500

.bss

plusrot: .ds. 1 1

first: . ds. w 1

second: .ds. w 1

deltal: .ds . w 1

.data

flag: .dc.b 1

.even

.bss

diffz: .ds. w 1

dx: .ds .w 1

dy: .ds. w 1

dz : .ds. w 1

worldx: . ds. w 1600 * World coordinate array

worldy: .ds. w 1600

worldz: . ds. w 1600

viewx: .ds. w 1600 * View coordinate array

viewy: .ds. w 1600

viewz: .ds. w 1600

screenx: ,ds. w 1600 * Screen coordinate array

screeny: . ds. w 1600

186
Abacus Software ST 3D Graphics

wlinxy: .ds. 1 3200 * Line array

• data

prox: . dc. w 0 * Coordinates for projection-

proy: . dc. w 0 * center here on the positive

proz: .dc. w 1500 * Z-axis

.data

of fx: .dc.w 0 ★ Transformation for rotation

of fy: . dc. w 0 * to point [offx,offy,offz]

of f z : .dc.w 0

xoffs: .dc.w 0 * Inverse transformation for point

yoffs: .dc.w 0 ★ [xoff,yoffs, zoffs]

zoffs: .dc.w 0

.bss

loopc: .ds. 1 1

.end

187
ST 3D Graphics Abacus Software

4.2.1 New subroutines in this program:

r 1 set: Supplies the rotation body creation routine with the


parameters of the specific rotation body, i.e. with
the address of its definition line, with the number
of the points forming this line and the desired
number of rotations.

makerotl: Creates the rotation body rotl in the array


rldatx, rldaty, rldatz, and the lines
(rllin) and passes the total number of points and
lines created.

rotstart: Creates the points of the rotation body and is called


by makerotl as is:

rot 1 in: Creates the lines of the rotation body.

wrld2set: Passes the parameters of the world system and the


rotation body to the link file variables. The
variables for storing of the rotation angle
hxangle remain the same, nothing in inp_chan
needs to be changed.

In contrast to the first program where the house was already explicitly
provided, the object to be represented must first be created. This is the
task of the subroutine makerotl, which generates the rotation body in
the array rldatx, rldaty, rldatz. This array corresponds to the
house array housdatx, housdaty, housdatz. The rotation body is
transferred to the world system and its position parameters in the main
loop are modified in a loop. You should experiment freely with this
program and change the definition line for the rotation body and the
number of rotations. The only limitation is in the maximum number of
points and lines where the total number of lines rlnumline is calculated
as follows:

rlnummark: Total number of comers in the rotation body

rlnumline: Total number of lines in the rotation body

r lnumpt: Number of points in the definition line

188
Abacus Software ST 3D Graphics

r lnumro: Number of desired rotations of the definition line

rlnumline:= ((rlnumpt - 1) * (rlnumro) +


(rlnumpt * rlnumro))

rlnummark:= (rlnumpt * (rlnumro + 1))

The number of points can not exceed 1600 and the number of lines
cannot be greater than 3200.

The expression (rlnumro+1) results from the programming trick, of


rotating the definition line one time more than necessary. The definition
line, which is the first line in the array, is created a second time at the end
of the array. This simplifies the construction of the line array. And now
you can try the various rotation lines such as the following:

Definition of a Ball:

* Definition line and parameter of the ball *


* from Fig. 4.2.1 ************

rlxdat: .dc.w 0,40,70,90,100,90,70,40,0


rlydat: .dc.w 100,90,70,40,0,-40,-70,-90,-100
rlzdat: , , , , , , ,
.dc.w 0 0 0 0 0 0 0 0,0
rlnumpt: .dc.w 9
rlnumro: .dc.w 60 * Number of rotations
for creation

You need only exchange the corresponding lines in the listing for these.

The operation parameters of the program are the same as in house 1:

cursor left and right:


Change the Y-rotation angle increment

cursor up and down:


Change the X-rotation angle increment

undo and help:


Change the Z-rotation angle increment

189
ST 3D Graphics Abacus Software

+ and - on the keypad:


Move the projection plane on the Z-axis (increase or decrease the size of
object).

* and / on the keypad:


Move the rotation reference point on the Z-axis

Shift 'D':
Hardcopy on the printer

190
Abacus Software ST 3D Graphics

4.3 Hidden line algorithm for convex bodies

If you are familiar with real time 3-D graphics on other computers, you
were probably surprised by the speed of the display of the wire frame
drawings on the Atari ST. On the other hand some game freaks may
remark that "I’ve seen the fastest 3-D games on my 8-bit C-64 and these
wire models just don’t compare.” For game programming, the main
emphasis is on the desired effect. Therefore the active figures for these 3-
D-Games are mostly space ships and landscapes which are pre-calculated
and their point coordinates are already stored in the computer. For the
display which follows on the screen, the object is simply drawn, which
naturally can be done quickly, even with 8-bit computers. A disadvantage
of this method is the enormous storage requirement, since every possible
position of the object must be available in memory, meaning that this
procedure cannot be used with complex bodies. In this case only the
rotation matrices for the rotation around three axes are calculated ahead
of time and stored in a table. Even with this method the limits of the
storage are reached quickly. An extreme example: If you want to
calculate the rotation matrices of all possible values for subsequent
rotation about three axes, with an angle increment of one degree
previously calculated, the result will be more than 46 million possibilities
(variations of three rotations around 360 possible angles). If this method
is used, the degree of freedom of the objects must be limited to one or
two possible axes, and/or the gradations of the angle values must be
raised so that the table is calculated, for example, only in ten degree
steps, or only rotations from zero to to ninety degrees are permitted.
Another common method consists of defining the objects as picture
shapes, quasi-sprites, in various positions and to switch back and forth
between the various shapes and to move the whole shape over the
display. Of course the last procedure is the fastest since nothing has to be
calculated and the only operation is moving data into the screen memory.

Now back to the Atari ST, which, because of its enormous computing
power, can not only calculate the wire frame drawing in real time, but as
you will see also offers the ability to display simple convex bodies in real
time without the hidden lines. The method used corresponds to the
surface method used in chapter 2.7. To use this method you must specify
every surface of the object precisely. For the example of our house, we
need two new variables. First the number of surfaces of the house
(hnumpla=13), and second the storage space for the description of
these surfaces (houspla). Every surface is described by the number of

191
ST 3D Graphics Abacus Software

lines pertaining to it, followed by the lines themselves. The description:


4,1,2,2,3,3,4,4,1 would mean:

Four lines belong to this surface and appear as follows:

Line #. connects Point # with Point #

1 1 2
2 2 3
3 3 4
4 4 1

To return to the example of our house, it will be necessary to describe all


of the surfaces of this house in the same manner. For this reason we draw
the various views of the house and number the surfaces in any desired
sequence as in Figures 4.3.1 to 4.3.6. In these illustrations the desired
result is already achieved, i.e. the hidden lines are already removed to
prevent confusion.

Figure 4.3.1 - 4.3.6: Hardcopy of House Views

192
Abacus Software ST 3D Graphics
ST 3D Graphics Abacus Software

point. To make it possible for the algorithm to recognize the hidden


surfaces, the sequence of the line points (the direction of the individual
lines) is not arbitrary but must be done in the clockwise direction. This is
the procedure:

1. Number the surfaces.

2. Create a surface array containing the number of lines


(counted clockwise) of each surface as well as the lines of
each surface, as viewed from the outside.

3. When all surfaces have been taken care of the number of


surfaces are stored in a variable (numpla).

Here is the surface list for the thirteen surfaces of the house from Figure
4.3.1. You can get the point indices from Figure 4.1.3.

Surface # Number Lines Lines from Point #


to Point #

1 4 1, 2 2, 3 3/ 4 4, 1
2 4 2, 5 5, 8 8, 3 3, 2
3 4 5, 6 6, 7 7, 8 8, 5
4 4 7, 6 6, 1 1/ 4 4, 7
5 4 4, 3 3, 8 8, 7 7, 4
6 4 2, 9 9, 10 10, 5 5, 2
7 4 10, 9 9, 1 1, 6 6,10
8 3 1/ 9 9, 2 2, 1
9 3 5, 10 10, 6 6, 5
10 4 11, 12 12,13 13, 14 14,11
11 4 15,16 16, 17 17,18 18,15
12 4 9,20 20,21 21,22 22,19
13 4 23,24 24,25 25,26 26,23

Number of surfaces: 13

With this method of surface definition you can describe up to 32,000


lines which can be the connecting lines for 16,000 different points,
though only if you have enough memory, of course. The actual main
program hidel. s corresponds to the first main program house 1. s.
Two subroutines have been added: hideit: and surfdraw: and two

196
Abacus Software ST 3D Graphics

other changes were made in the main loop. The subroutine hide it
determines which surfaces are visible from the projection center with the
help of the information in the surface arrary (wplane). The information
on the visible surfaces, which correspond to the normal surfaces in the
structure, first the number of lines followed by individual lines, is entered
into a second array (vplane) and the total number of visible surfaces is
stored in the surface counter surfcnt. All visible surfaces are
subsequently drawn on the display by the subroutine surf dr aw:
whereby many lines are drawn twice since the subroutine surf dr aw:
takes the lines to be drawn directly from the surface array (vplane).
Figure 4.3.1 and the connecting lines of points 2 and 3 show a concrete
illustration. This connecting line belongs to the visible surface 1 and the
visible surface 2. Naturally all the lines in the surface array (vplane)
could be sorted before drawing and double lines removed. My experience
shows that the time saved in drawing is lost in the additional sorting and
testing, at least for less complicated bodies. Furthermore, the surface
information is lost by the separation of the lines, which is needed in the
following program sections. Again to run this program you must first
compile and link it to grlinkl.s using the batch.ttp file and
entering: aslink grlinkl hidel

Desk
»' File
-0:\. 4I" " View Options
E: \ F:\3DW0RK.DIRS !
B butes used
BASIC1C0 S X ASStLPRO HIDEl S
S BATCH TTP X FILE-PRO H0USE1 PRG
X FORTOT H0USE1 S

BUS i iifl
X GEHDRAM

MAIH1
laTMt
PRG
?

bib OPEN APPLICATION rIKICO


PIN1C0
pro
S
Inlli COLOR
BAT
BAK
Kane: BATCH
Parameters: .
• TTP HU1
NU1
PRO
S
COLOR PRG aslink grlinkl hldelj- ILTI1 PRO
COLOR S
Cancel I
mil S
COLOR1 BAK IINT1 PRG
COLOR1 0 IINT1 S
COLOR1 S m R0TRTE1 PRG
w-1
r- im □ ROTATEl S
-is □

W
was Ol n
a*
-■! '."il'M
a" a*.
;irna nw rrtm flCCBESXMi

197
ST 3D Graphics Abacus Software

* hidel.s 19.1.86 Version 3.0 *

* House with hidden-line algorithm *


* *

.globl main,xoffs,yoffs,zoffs,offx,offy,offz

-globl viewx,viewy,viewz

.globl wlinxy,mouse off,setrotdp,inp chan,pointrot

.text

main:

jsr apinit * Announce program

jsr grafhand * Get screen handler

jsr openwork * Display

jsr mouse off * Turn off mouse

jsr getreso * what resolution ?

jsr setcocli * Prepare clip window

move.1 #houspla. worldpla * Address of surface array

jsr makewrld * Create world system

jsr wrldset * Pass world parameters

jsr setrotdp * initialize observer ref. point

jsr clwork

jsr pagedown * Display logical page

jsr clwork

jsr inp_chan * Input and change parameters

mainlopl:

jsr pointrot * rotate about observer ref. point

jsr pers * Perspective transformation

jsr hideit

jsr surfdraw

jsr pageup * Display physical page

jsr inp chan * Input new parameters

jsr clwork

jsr pointrot * Rotate around rotation ref. point

jsr pers * Transform new points

jsr hideit

jsr surfdraw

198
Abacus Software ST 3D Graphics

jsr pagedown * Display this logical page

jsr inp_chan * Input and change parameters

jsr clwork * erase physical page

jmp mainlopl * to main loop

mainend: move.1 physbase,logbase

jsr pageup * switch to normal display page

rt s * back to link file, and end

* Input and change parameters such as angle increments and

* Z-coordinate of the projection plane

jsr inkey * Sense keyboard, keyboard code in


inp_chan:

cmp.b #'D',dO

bne inpwait

jsr scrdmp * Make harcopy

inpwait: swap dO * Test DO for

cmp.b #$4d,dO * Cursor-right

bne inpl

addq. w #1,ywplus * if yes, then add one to

bra inpendl * Y-angle increment and continue

cmp.b #$4b,dO * Cursor-left, if yes


inpl:
bne inp2 * then subtract one from

subq.w #1,ywplus * Y-angle increment

bra inpendl

cmp.b #$50,dO * Cursor-down, if yes


inp2:
bne inp3

addq.w #1,xwplus * then add one to X-angle increment

bra inpendl

inp3: cmp.b #$48,dO * Cursor-up

bne inp3a

subq.w #1,xwplus * subtract one

bra inpendl

199
ST 3D Graphics Abacus Software

inp3a: cmp.b #$61,dO * Undo key

bne inp3b

subq * w #1,zwplus

bra inpendl

inp3b: cmp.b #$62,dO * Help key

bne inp4

addq.w #1,zwplus

bra inpendl

inp4: cmp.b #$4e,dO * + key on keypad

bne inp5 * if yes then subtract 25 from

sub. w #25,dist * location of projection plane

bra inpendl * (Z-coordinate)

inp5: cmp.b #$4a,dO * - key on keypad

bne inp6 *

add.w #25,dist * if yes then add 25

bra inpendl

inp6: cmp.b #$66,dO * * key on keypad

bne inp7 * if yes, subtract 15 from the

sub. w #15,rotdpz * rotation point Z-coordinate

bra inpendl * Make change

inp7 : cmp.b #$65,dO * / key of keypad

bne inplO

add.w #15,rotdpz * Add 15

bra inpendl

inplO: cmp.b #$44,dO * F10 pressed ?

bne inpendl

addq.1 #4,a7 * if yes, jump to

bra mainend * program end

inpendl: move.w hyangle,dl * Rotation angle about Y-axis

add. w ywplus,dl * add increment

cmp. w #360,dl * if larger than 360, subtract 360

bge inpend2

cmp. w #-360,dl * if smaller than 360

ble inpend3 * add 360

bra inpend4

200
Abacus Software ST 3D Graphics

inpend2: sub. w #360,dl

bra inpend4

inpend3: add.w #360,dl

inpend4: move.w dl,hyangle

move.w hxangle,dl * Treat

add.w xwplus,dl * rotation angle about X-axis

cmp. w #360,dl * in the same manner

bge inpend5

cmp. w #-360,dl

ble inpend6

bra inpend7

inpend5: sub. w #360,dl

bra inpend7

inpend6: add.w #360,dl

inpend?: move.w dl,hxangle

move.w hzangle,dl

add.w zwplus,dl

cmp. w #360,dl

bge inpend8

cmp. w #-360,dl

ble inpend9

bra inpendlO

inpend8: sub.w #360,dl

bra inpendlO

inpend9: add.w #360,dl

inpendlO : move.w dl,hzangle

rts

* Initialize the rotation reference point to [0,0,0] and the

* rotation angle also to 0,0,0


************************************************** ********************

setrotdp: move.w #0, dl * set the start rotation-

move. w dl,rotdpx * datum point

move.w dl,rotdpy

move.w dl,rotdpz

201
ST 3D Graphics Abacus Software

move.w #0,hyangle * Start rotation angle

move.w #0,hzangle

move.w #0,hxangle

rts

************************************************************************
* Rotate the total world system around one point, the rotation *
* reference point *
************************************************************************

pointrot: move.w hxangle,xangle * rotate the world around the


move.w hyangle,yangle

move.w hzangle,zangle
move.w rotdpx,dO * rotation reference point
move.w rotdpy,dl

move.w rotdpz,d2
move.w dO,xoffs * add for inverse transformation
move.w dl,yoffs

move. w d2,zoffs
neg. w dO

neg. w dl

neg. w d2

move.w dO,offx * subtract for transformation


move.w dl,offy

move.w d2,offz
jsr matinit * Matrix initialization
jsr zrotate * first rotate about Z-axis
jsr yrotate * rotate 'matrix' about Y-axis
jsr xrotate * then about X-axis
jsr rotate * Multiply points with matrix
rts

***********************************************************************
* Generate world system from object data. All points, lines, *

* and surfaces are transferred to the world system *


***********************************************************************

makewrld: move.1 #housdatx,al * Generate world system by


move. 1 ihousdaty,a2

move.1 #housdatz,a3

move.1 twrldx,&A

move.1 #wrldy,a5

202
Abacus Software ST 3D Graphics

move.l #wrldz,a6

move.w hnummark,dO

ext.l dO

subq.l #l,dO

makewll: move.w (al)+, (a4) + * Copying point coordinates

move.w (a2)+, (a5)+ * to world system

move.w {a3)+,(a6) +

dbra dO,makewll

move.w hnumline,dO * Number of house lines

ext.l dO

subq.1 #1/dO

move.l #houslin,al

move.l #wlinxy,a2

makewl2: move.l (al)+,(a2)+ * Copy all lines into

dbra d0,makewl2 * world system

move.l worldpla,aO

move.l #wplane,al

move.w hnumsurf,dO * Number of surfaces on house

ext.l dO

subq.l #l,dO

makewl3: move.w (aO)+,dl * Copy all surface

move.w dl,(al)+ * definitions into the

ext.l dl * world system

subq.l #l,dl

makewl4: move.l (a0)+, (al) + * Copy every line of this

dbra dl,makewl4 * surface into the world array

dbra d0,makewl3 * until all surfaces are processed

rts

* Passing the world parameters to the link file variables

wrldset: move.l #wrldx,datx * Pass variables for

move.l #wrldy,daty * the rotation routine

move.l #wrldz,datz

move.l ^viewx,pointx

203
ST 3D Graphics Abacus Software

move.1 #viewy,pointy

move.1 #viewz,pointz
move.1 #wlinxy,linxy

move.w picturex,xO

move.w picturey,yO

move.w proz,zobs

move.w rlz1,dist

move.1 tscreenx,xplot
move.1 tscreeny,yplot
move. w hnumline,numline
move.w hnummark,nummark

move.w hnumsurf,numsurf
rts

* remove all characters from the keyboard buffer *

clearbuf: move.w #$b,-(a7)

trap #1
addq.1 #2, a?

tst. w dO

beq clearnd
move. w #1,-(a7)

trap #1
addq.1 #2, a7

bra clearbuf

clearnd: rts

*********************************************************************
* Sense display resolution and set coordinate origin of screen *
* to screen center *
*********************************************************************

move.w #4,-<a7) * Sense screen resolution


trap #14

addq.1 #2,a7

cmp. w #2, dO

bne getrl

move.w #320,picturex * Monochrome monitor

204
Abacus Software ST 3D Graphics

move.w #200,picturey

bra getrend

getrl: cmp.w #l,d0

bne getr2
move.w #320,picturex * medium resolution (640*200)

move.w #100,picturey

bra getrend
getr2: move.w #160,picturex * low resolution (320*200)

move.w #100,picturey

getrend: rts

***********************************************************************

* Hardcopy routine, called by inp_chan


***********************************************************************

scrdmp: move.w #20,-(a7)

trap #14

addq.1 #2.a7

jsr clearbuf

rts

*********************************************************************
* Sets the limits of the display window for the Cohen-Sutherland

* clip algorithm built into the draw-line algorithm.


* The limits can be freely selected by the user, which makes the

* draw-line algorithm very flexible.


****************

move . w #0,clipxule

move.w #0,clipyule

move.w picturex,dl

#l,dl * times two


lsl. w
#l,dl * minus one equal
subq.w
dl,clipxlri * 639 for monochrome
move.w

move.w picturey,dl
* times two minus one equal
lsl. w #1, dl

subq.w #1, dl * 399 for monochrome

move.w dl,clipylri

rts

205
ST 3D Graphics Abacus Software

***********************************************************************
* Recognition of hidden surfaces and entry of these into the *

* vplane array, the surface information is in the surface array *

* wplane, as well as in view system, viewx, viewy, viewz, *

* also the total number of surfaces must be passed in numsurf *


***********************************************************************

hideit:

move.w numsurf,dO i' Number of surfaces as counter


ext .1 dO

subq.1 #1, dO

move.1 #viewx,al * Store point coordinates here


move.1 #viewy,a2

move.1 #viewz,a3

move.1 Iwplane,aO * Information for every surface

move.1 #vplane,a5 * here.

move.w #0, surfcount * counts the known visible surfaces.

visible: move.w (aO),dl * start with first surface, number

ext. 1 dl * of points of this surface in Dl.

move.w 2(aO),d2 * Offset of first point of this surf.

move.w 4(aO),d3 * Offset of second point


move.w 8(aO),d4 * Offset of third point

subq.w #1, d2 * for access to point arrays subtract


subq.w #1, d3 * one from current point offset

subq.w #1, d4 * multiply by two

lsl. w #l,d2

lsl. w #1, d3

lsl. w #l,d4 * and finally access current point


move.w (al,d3.w),d6 * coordinates
cmp. w (al, d4 . w) , d6 * comparison recognizes two points
bne doitl * with same coordinates which can
move.w (a2,d3.w),d6 * result during construction of
cmp. w (a2,d4.w),d6 * rotation bodies. During recognition
bne doitl * of two points in which all point
move.w <a3,d4.w),d6 * coordinates match (x,y,z) the
cmp.w (a3, d3 . w) , d6 * program selects a third point for
bne doitl * determination of the two vectors
move.w 12(aO),d4

subq.w #l,d4

lsl. w #1, d4

206
Abacus Software ST 3D Graphics

doit1:

move.w (al,d3.w),d5 * Here the two vectors, which lie

move.w d5, kx * in the surface plane, are

sub. w (al,d2.w),d5 * determined by subtracting the

move.w d5,px * coordinates of two points

move.w (a2,d3.w),d5 * from this surface.

move.w d5,ky * The direction coordinates of the

sub. w (a2,d2.w),d5 * vectors are stored in the

move.w d5,py * variables qx,qy,qz and px,py,pz

move.w (a3, d3 . w) , d5

move.w d5, kz

sub. w (a3,d2.w), d5

move.w d5, pz

move.w (al, d4.w),d5 * Calculate vector Q

sub. w (al, d2.w),d5

move.w (a2, d4.w),d6

sub. w (a2,d2.w) ,d6

move.w (a3,d4.w),d7

sub.w (a3,d2.w),d7

move.w d5,dl * qx

move. w d6,d2 * qy
move.w d7,d3 * qz

nulls py,d3 * Calculate the cross product

mu Is pz,d2 * of the vertical vector for the

sub.w d2,d3 * current surface.

move.w d3, rx

mu Is pz,dl

mu Is px,d7

sub. w d7,dl * The direction coordinates of the

move.w dl, ry * vertical vector are stored

mu Is px, d6 * zobsorarily in rx,ry,rz

mu Is py,d5

sub. w d5,d6

move.w d6, rz

move.w prox,dl * The projection center

sub.w kx,dl * is used as the comparison

move. w proy,d2 * point for the visibility

sub.w ky,d2 * of a surface.

move.w proz,d3 * One can also use the

207
ST 3D Graphics Abacus Software

sub. w kz, d3 * observation ref. point


mu Is rx, dl * as the comparison point. Now comes
mu Is ry, d2 * the comparison of vector R with
mu Is rz, d3 * the vector from a point on the
add. 1 dl, d2 * surface to the projection center
add. 1 d2,d3 * for creating the scalar product
bmi dosight * of the two vectors.

* If the scalar product is negative. the surface is visible

move.w (aO),dl * Number of lines of the surface


ext. 1 dl

lsl.l #2, dl * Number of lines times 4 = space for


addq.1 #2, dl *
lines plus 2 bytes for the number of

add. 1 dl, aO * lines added to surface array, for


sightl: dbra dO,visible * access to next surface. When all
bra hideend * surfaces completed then end.

dosight: move. w (aO),dl * Number of lines for this surface,


ext. 1 dl * gives the number of words to be
lsl.l #1, dl * transmitted when multiplied by 2.

sight3: move. w (aO)+,(a5)+ * pass the number of lines and the


dbra dl,sight3 * the individual lines

addq. w #1, surfcount * the number of surfaces plus one


bra sightl * and process the next

hideend: rts

* Draw visible surfaces passed in vplane *

surfdraw: * Draws a number of surfaces (passed


move.l xplot,a4 * in surfcount) whose description

move.l yplot,a5

move.1 #vplane,a6 * is in the array at address

move.w surfcount,dO * vplane, and was entered by routine

208
Abacus Software ST 3D Graphics

ext. 1 dO * hideit

subq.1 #1, dO * if no surface is entered in the

bmi surfend * array, then end.

surflopl: move. w (a6)+,dl * Number of lines in this surface as

ext. 1 dl * counter of lines to be drawn.

subq. 1 #1, dl

surflop2: move. 1 (a6) +,d5 * First line of this surface

subq.w #1, d5 * Access screen array which contains

lsl. w #1, d5 * screen coordinates of the points.

move . w 0(a4,d5.w),d2

move. w 0(a5,d5.w),d3 * extract points from routine and

swap d5 * pass.

subq.w #1, d5

lsl .w #1, d5

move.w 0 (a4,d5 .w) , a2 * second point of line

move.w 0(a5,d5.w),a3

jsr drawl * Draw line until all lines of this

dbra dl,surflop2 * surface have been drawn and repeat

dbra dO,surflopl * until all surfaces are drawn.

surfend: rts * Return.

* Here begins the variable area of the program module *


* *

* *

* Definition of the house *


* *

.data

housdatx: .dc.w -30,30,30,-30,30,-30,-30,30,0,0,-10,-10,10,10

• do. w 30,30,30,30,30,30,30,30,30,30,30,30

209
ST 3D Graphics Abacus Software

to
o
o

o
housdaty: .dc,, w 30, 30,-30,-30,30,30, -30, -30, 70, 70,- 30,

1
. dc,. w 20, 20, 0,0,20,20,0,0

. dc.. w -10 ,-10,-30,-30

housdatz: • dc., w 60, 60,60,60,-60,-60, -60, -60, 60, -60, 60, 60, 60, 60

.dc,. w 40, 10,10,40,-10,-40, -40, -10

. dc.. w o,- 20,-20,0

houslin: . dc.. w 1, 2 ,2,3,3,4,4,1,2,5, 5,8, 8,3, 8, 7 ,7, 6 , 6, 5, 6, 1,7,4

. dc., w 9, 10,1, 9, 9,2,5,10, 6, 10,11,12 , 12 ,13, 13, 14

. dc,. w 15, 16,16,17,17,18,18 ,15, 19,20,20,21 ,21 ,22,22,19

. dc.. w 23, 24,24,25,25,26,26 ,23

* here are the definitions of the surfaces belonging to the house *

houspla: .dc.w 4,1,2,2,3,3,4,4,1,4,2,5,5,8,8,3,3,2


.dc. w 4,5,6, 6, 7, 7,8, 8,5, 4,7,6, 6,1,1, 4,4,7
.dc.w 4,4,3,3,8, 8,7, 7,4,4,2,9, 9,10, 10,5, 5,2

.dc.w 4,10,9,9,1,1,6,6,10,3,1,9,9,2,2,1
.dc.w 3,5,10,10,6,6,5,4,11,12,12,13,13,14,14,11
.dc.w 4,15, 16, 16, 17, 17, 18, 18, 15,4, 19, 20,20, 21,21, 22, 22, 19
• dc.w 4,23,24,24,25,25/26,26,23

hnummark: .dc.w 26 * Number of corner points of the house

hnumline: .dc.w 32 * Number of lines of the house

hnumsurf: .dc.w 13 * Number of surfaces of the house

hxangle: .dc.w 0 * Rotation angle of house about X-axis


hyangle: .dc.w 0 * " " Y-axis
hzangle: .dc.w 0 * " " Z-axis

xwplus: .dc.w 0 * Angle increment about X-axis


ywplus: .dc.w 0 * Angle increment about Y-axis
zwplus: .dc.w 0 * Angle increment about Z-axis

picturex: .dc.w 0 * Definition of zero point of display

picturey: .dc.w 0 * entered by getreso

210
Abacus Software ST 3D Graphics

rotdpx: • dc. w 0

rotdpy: . dc. w 0

rotdpz: .dc. w 0

rlzl: . dc. w 0

normz: .dc. w 1500

.bss

plusrot: . ds. 1 1

first: .ds.w 1

second: .ds. w 1

deltal: .ds.w 1

worldpla: .ds. 1 1 * Address of surface array

.data

plag: .dc .b 1

.even

.bss

diffz: .ds.w 1

dx: .ds.w 1

dy: .ds.w 1

dz: .ds.w 1

wrldx: .ds.w 1600 * World coordinate array

wrldy: .ds.w 1600

wrldz : .ds.w 1600

viewx: .ds.w 1600 * View coordinate array

viewy: .ds.w 1600

viewz: .ds.w 1600

screenx: .ds.w 1600 * Display coordinate array

screeny: .ds.w 1600

211
ST 3D Graphics Abacus Software

wlinxy: • ds. 1 3200 * Line array

wplane: .ds. 1 6600 * Surface array

vplane: .ds. 1 6600 * Surface array of visible surfaces

surfcount: • ds. w 1

numsurf: • ds. w 1

zcount: . ds. 1 1 * Sum of all Z-coordinates


zpla: . ds. w 1 * Individual Z-coordinates of surface

sx: . ds. w 1
sy: .ds .w 1

sz: .ds .w 1

px: .ds. w 1

py: • ds. w 1

pz: • ds. w 1

rx: .ds. w 1

ry: .ds. w 1
rz: . ds. w 1

qx: .ds .w 1

qy: -ds. w 1
qz: .ds. w 1

kx: -ds. w 1
ky: .ds. w 1
kz: .ds. w 1

. data

prox: .dc.w 0 * Coordinates of the projection center

proy: . dc. w 0 * on the positive Z-axis

proz: .dc.w 1500

212
Abacus Software ST 3D Graphics

.data

of fx: . dc. w 0 * Transformation during rotation

.dc. w 0 * to point [offx,offy,offz]


of fy:

of f z: . dc. w 0

.dc. w 0 * Inverse transformation to point


xoffs:

. dc. w 0 ★ [xoff,yoffs, zoffs]


yoffs:

zoffs: .dc. w 0

.bss

loopc .ds. 1 1

. end

213
ST 3D Graphics Abacus Software

4.3.1 Explanation of the newly-added subroutines

hideit: In contrast to the explanation in the mathematical part, the


view system used by the program is a right system; this
saves the multiplication of the Z-values by -1. The
subroutine hideit forms two vectors within the surface
from the first three points of every surface. These are the
vectors from point one to point two as well as the vector
from point one to point three. These two vectors
correspond to the vectors P[px,py,pz] and
Q[qx,qy,qz] from chapter 2.7. Furthermore, a third
vector R[rx,ry,rz] is generated through the formation
of the cross product of the vectors P and Q. According to
the definition, the cross product is perpendicular to the
vectors P and Q and, in this sequence forms a right-hand
system with them [p, q, r]. Finally, a vector is created
from a point on the surface to the projection center
(S[sx,sy,sz]), and its direction is compared with the
direction of the vector R by creation of the scalar products
of the vectors S and R. All the surfaces which are in front
of the projection center are visible.

Scalar product= sx*rx+sy*ry+sz*rz =


!s|*|r|*cos(Alpha)

Alpha is the angle suspended between the vectors R and S.


If the result of the scalar product is negative, this means an
angle larger than 90 degrees and smaller than 270 degrees
between the two vectors, which point in different
directions (See also Figure 2.7.1), and so this surface is
visible, according to the surface definition (clockwise
direction) and right system used.

surf dr aw: Here the visible surfaces are displayed by drawing the
lines of the array vplane. The whole job was done
already by hideit.

The operation parameters of the program are the same as in house 1. s,


The rotation point on the Z-axis can be moved with the * and / keys on
the keypad, the projection plane can be moved with the - and + keys on
the keypad, and the angle increments of the rotation angle around the X

214
Abacus Software ST 3D Graphics

and Y-axis can be changed with the cursor keys and the Help and Undo
keys. Of course you can also change all the parameters within the
program (projection center, rotation reference point to X and Y-axis,
etc.).

4.3.1.1 Errors with non-con vex bodies

If the rotation creation routine is added to the main program and the chess
figure is created with hideit: and pladraw without hidden lines: you
can see the problem. With concave bodies such as this chess figure there
is the possibility that one of the surfaces recognized by the hideit:
algorithm as visible can be hidden by another visible surface during
viewing. In this case the hideit: algorithm fails and the problem must
be solved with another algorithm.

215
ST 3D Graphics Abacus Software

4.4 The painter algorithm

Recall the problem we‘re trying to solve: Surfaces which are seen from
an observation point have their surface normal vector pointed in another
direction from a vector from any point on the surface to the projection
center, are hidden by other surfaces which according to this criterium are
also visible. If you start from the observation point (projection center) on
the positive Z-axis, the middle Z-coordinate of a surface is a possible
description of it and its position in the world system. The middle Z-
coordinate is obtained by defining the arithmetic center of the comer
point coordinates belonging to the surface, i.e. summation of all surface
comer point Z-coordinates and division by the number of comer points
belonging to the surface. The relationship can be made clear with the
simple example with three different surfaces in Figure 4.4.1.

216
Abacus Software ST 3D Graphics

Viewing the defined world system from one point on the positive Z axis,
we can say: the surface with the largest middle Z-coordinate is visible in
its entire size and is not hidden by any other surface. Note that all
observed surfaces are on the negative Z-axis (-200 > -400). This
completely visible surface covers parts of surfaces with a smaller middle
Z coordinate. Surfaces 2 and 3 are covered by surface 1 and surface 3 is
again covered by surface 2. The surfaces are sorted by their Z-coordinates
and they are drawn starting with the smallest middle z-coordinate, surface
3, and then the surfaces with the larger Z coordinates, and we have found
a possible solution to the problem by covering hidden surfaces with other
surfaces. You must consider that it is not enough just to draw every
surface. The individual surfaces must be filled with "color" or a pattern so
that the surfaces drawn first are really covered. Figure 4.4.2 shows one
possible result

+Y

Figure 4.4.2

If we think about our rotation body from chapter 4.2, this means first of
all that when the rotation body is created its surfaces must also be

217
ST 3D Graphics Abacus Software

created, second a middle Z coordinate must be calculated and stored


some place for every surface. Another problem is sorting the surfaces. If
one wanted to sort every defined form with its lines, it would require an
enormous movement of data in storage. To avoid this, a new storage area
is created in which the Z-coordinates together with the beginning address
of the surface it pertains to are stored. The individual surfaces are stored
in a simple linear list. The beginning address of every surface is the
storage address at which the number of lines for this surface is stored.
Through storage of this address, it is possible to access every single
surface directly, which previously was not possible because of the
number of lines belonging to each surface.

To better handle the two pieces of information, (Z-coordinates of the


surface and address of the surface) we select a long word as data size for
both, i.e. in the newly constructed array (surfaddr) there are four
successive bytes for the Z-coordinate and four bytes for the address of the
surface. Each description of a surface "occupies" eight bytes of storage
space. This array contains the visible surfaces represented by their middle
Z-coordinates and their beginning addresses in the new addition to the
subroutine hide it: (sight 2). In this special case of the rotation body
whose surfaces all consist of four lines, the division by the line number
(4) for calculation of the middle Z-coordinate can be performed by
shifting right by two bit positions. If you want to include surfaces with
more or less than four lines in the paint routine, you must alter the
hide it-routine and divide by the number of surfaces. After the
adaptation of the subroutine hideit: all visible surfaces are in the two
arrays, in vplane: and in surfaddr:. The number of surfaces, like in
the first version of hideit:, goes in the variable placount:.
Fortunately, we do not have to write the shading function since the
operating system offers a function for filling display areas with a shading
pattern (Filled Area). This function fills a polygon whose points are
passed in the ptsin array, with one of a total of 36 different predefined,
and one user-defined shading pattern. Before calling this function with
the opcode 9, we set up the different shading parameters which is done
using the subroutines filmode, filform, filcolor, filstyle
and f ilindex which are contained in the link file (grlink 1).

The shading routine is called by the subroutine paint it, which first
sorts all surfaces contained in surfaddr: according to ascending Z-
coordinates. Next you must pass the individual surfaces, i.e. their end
point coordinates, to the function "Filled Area". This begins with the
surface which has the smallest middle Z-coordinate. The function "Filled

218
Abacus Software ST 3D Graphics

Area" can, in connection with the function "Set Clipping Rectangle",


Opcode 129, fill surfaces limited to a display window. It is necessary to
call the function "Set Clipping Rectangle" when the display window is
the total screen area, bordered by the coordinates 0,0 and 639,399 (for
BW monitors), if this is not done, "Filled Area" may draw parts of the
polygon sticking beyond the display frame on the neighboring display
page (wrapping). You could fill all surfaces with the same pattern, which
could also be white. You can assign a shading pattern for every surface
corresponding to its Z-coordinates. We will limit ourselves to only six of
the 36 possible fill patterns. This is done purely for optical reasons since
shaded surfaces, and even completely filled color surfaces, can have a
negative effect on the picture. You can influence this choice or omit it
entirely. Simply set the desired pattern on entry to the subroutine. With a
color monitor, a various fill colors can be used instead of a shading
pattern. The choice of colors is completely up to you. The visual effect of
these three-dimensional graphics can best be appreciated with a high-
resolution monitor. Doubling the resolution in both directions increases
the quality of the picture four times.

If you have a color monitor, you can choose between filling with color or
patterns. If you want to try filling with color you must call the function
filstyle with the value one in the DO register when entering the
paint it routine. The subroutine filcolor: makes it possible to use
different colors. Owners of monochrome monitors don’t have to change
anything in the program. To run this program call the batch file batch.ttp
then enter: as link grlinkl paintl

F:\3DM0RK.DIR\
253882 bytes used 1 1442236 bytes used in 125 itens 333956 butes used in
Si PRINTERS 38944 H0USE1 PRG
Si TUTORIAL H0USE1 S
C FRY HAIN1 PRG
COM TTP HRIN1 S
NLlfl PRG MAIK1C0 PR6
OUTPUT PRG OPEN APPLICATION MAIN1C0 S
SPLIT TTP HENU1 PRG
STRNDARD PRT Mane: BATCH .TTP MENU1 S
TEXTPRO PRG Paraneters: MULTI1 PRG
TUTORIAL TXT aslink grlinkl paintlj—_ MULTI1 S
XTTUTORI TOC PAINT1 PRG
PAINT1 S
R0TATE1 PRG

1 R0TATE1 S
ST 3D Graphics Abacus Software

Figure 4.4.3

Here is the listing of the fourth main program for the link file
grlinkl.s. It is called paintl.s. The operating parameters again
correspond to the previous program.

220
Abacus Software ST 3D Graphics

**********************************************************************
* paint 1.s 9.2.1986 *
* Display a shaded rotation body *
* *

★*********************************************************************
.text
.globl main,xoffs,yoffs, zoffs,offx,offy,offz
.globl viewx,viewy,viewz
.globl wlinxy,mouse_off,setrotdp,inp_chan,pointrot

main:
jsr apinit * Announce program

jsr grafhand * Get screen handler

jsr openwork * open workstation

jsr mouseoff * Turn off mouse

jsr getreso * Display resolution ?

jsr setcocli * Set clip window

jsr makerotl * Create rotation body

jsr makewrld * Create world system

jsr wrld2set ★ Pass world parameters

jsr setrotdp ★ initialize observation ref. point


jsr clwork
jsr pagedown * Display logical page

jsr clwork
jsr inp chan

mainlopl:
jsr pointrot * rotate around observ. ref. point
jsr pers * Perspective transformation

jsr hideit ★ hide hidden surfaces

jsr paintit ★ sort and shade

jsr pageup * Display physical page

jsr inp chan * Input new parameters


jsr clwork ★ clear screen page not displayed

jsr pointrot * Rotate around rot. ref. point

jsr pers ★ Transform new points

jsr hideit * hide

jsr paint it * sort and shade

221
ST 3D Graphics Abacus Software

jsr pagedown * Display this logical page

jsr inp chan * Input and change parameters

jsr clwork * erase physical page

jmp mainlopl * to main loop

move. 1 physbase,logbase

jsr pageup * Switch to normal screen page

rts ★ back to link file and end

A**********************************************************************
* Creation of rotation body by passing parameters *

* and calling rotation routine *


it**********************************************************************

makerotl: jsr rlset * Set parameters of rot. body

jsr rotstart * and create rot. body

rts

* Input and change parameters with the keyboard *

inp__chan: jsr inkey * Read keyboard, code in

cmp .b #'D',dO

bne inpwait

jsr scrdmp * Make hardcopy

inpwait: swap dO * Test DO for

cmp .b #$4d,dO * Cursor-right

bne inpl

addq. w #1,ywplus * if yes, add one to

bra inpendl * Y-angle increment and continue

inpl: cmp .b #$4b,dO * Cursor-left, if yes

bne inp2 * subtract one from

subq. w #1,ywplus * Y-angle increment

bra inpendl

222
Abacus Software ST 3D Graphics

inp2 : cmp .b #$50,dO * Cursor-down, if yes

bne inp3

addq. w #1, xwplus * add one to X-angle

bra inpendl * increment

inp3: cmp .b #$48,dO * Cursor-up

bne inp3a

subq.w #1,xwplus * subtract one


bra inpendl

inp3a: cmp .b #$61,dO * Undo key

bne inp3b

subq.w #1,zwplus * decrease Z-increment

bra inpendl

inp3b: cmp -b #$62,dO * Help key


bne inp4

addq.w #1,zwplus * increase Z-increment


bra inpendl

inp4 : cmp.b #$4e,dO * + key on keypad

bne inp5 * if yes, subtract 25 from

sub. w #25,dist * location of projection

bra inpendl * plane (Z-coordinate)


inp5: cmp.b #$4a,dO * minus key on keypad
*
bne inp6

add. w #25,dist * if yes, add 25

bra inpendl

inp6: cmp.b #$66,dO * * key on keypad

bne inp7 * if yes, subtract 15 from

sub. w #15,rotdpz * rotation point Z-coordinate


bra inpendl * Make change

inp7 : cmp.b #$65,dO * / key on keypad


bne inplO

add. w #15,rotdpz * add 15

bra inpendl

223
ST 3D Graphics Abacus Software

inplO: cmp .b #$44,dO * F10 pressed ?

bne inpendl

addq. 1 #4,a7 * if yes, then jump to

bra ma inend * program end

inpendl: move. w hyangle, dl * Rotat.angle about Y-axis

add. w ywplus,dl ★ add increment

cmp. w #360,dl * if larger than 360, then

bge inpend2 * subtract 360

cmp. w #-360,dl * if smaller than 360, then

ble inpend3 * add 360

bra inpend4

inpend2: sub. w #360,dl

bra inpend4

inpend3: add. w #360,dl

inpend4: move. w dl,hyangle

move. w hxangle,dl * do the same for

add. w xwplus,dl ★ the rotation angle

cmp. w #360,dl ★ about X-axis

bge inpend5

cmp. w #-360,dl

ble inpend6

bra inpend7

inpend5: sub. w #360,dl

bra inpend7

inpend6: add. w #360,dl

inpend7: move.w dl,hxangle *

move.w hzangle,dl

add. w zwplus,dl

cmp.w #360,dl

bge inpend8

cmp. w #-360,dl

ble inpend9

bra inpendlO

inpend8: sub. w #360,dl

bra inpendlO

inpend9: add. w #360,dl

224
Abacus Software ST 3D Graphics

inpendlO: move.w dl,hzangle

rts

* Initialize the rotation reference point to [0,0,0] *

move.w #0, dl * set the Initial rotation

move.w dl,rotdpx * ref. point

move. w dl,rotdpy

move.w dl,rotdpz

move. w #0,hyangle * initial rotation angle

move.w #0,hzangle

move.w #0,hxangle

rts

* Rotation around the rotation reference point about all *

* three axes *

pointrot: move.w hxangle,xan gle * rotate the world around

move.w hyangle,yangle

move.w hzangle,zangle

move.w rotdpx,dO * rotation reference point

move.w rotdpy,dl

move. w rotdpz,d2

move.w dO,xoffs * add for inverse transform

move.w dl,yoffs

move. w d2,zoffs

neg. w dO

neg. w dl

neg. w d2

move.w dO,of fx * subtract for transform

move.w dl,offy

move.w d2,offz

jsr matinit * initialize matrix


■k rotate first about Z-axis
jsr zrotate

yrotate * rotate 'matrix' about Y-axis


jsr

jsr xrotate * then rotate about X-axis

jsr rotate * Multiply points with matrix.

rts

225
ST 3D Graphics Abacus Software

***********************************************************************
* Create world system by copying the object data into world system *
***********************************************************************

makewrld: move. 1 #rldatx,al * Create world system by

move.1 #rldaty,a2

move.1 trldatz,a3

move.1 #wrldx,a4

move. 1 #wrldy,a5

move.1 #wrldz,a6

move.w rlnummark,dO

ext. 1 dO

subq.1 #l,dO

makewll: move.w (al)+, <a4)+ * copying point coordinates

move.w (a2)+, (a5) + * into the world system

move.w (a3)+, <a6)+

dbra dO,makewll

move.w rlnumline,dO

ext. 1 dO

subq.1 #lf dO

move.1 #rllin,al

move.1 #wlinxy,a2

makewl2: move.1 (al)+, (a2) + * Copy lines into world

dbra d0,makewl2 * system

move.1 worldpla,aO

move.1 #wplane,al

move.w rlnumsurf,dO

ext. 1 dO

subq.1 #1, dO

makew!3: move.w (aO)+,dl * Copy surfaces into

move.w dl, (al) + * world system

ext. 1 dl

subq.1 #1 / dl

makewl4: move.1 (aO)+,(al)+ * Copy every line of

dbra dl,makewl4 * this surface into

dbra dO,makewl3 * world array until all

rts * surfaces are completed

226
Abacus Software ST 3D Graphics

*********************************************************************
* Pass the world parameters to the variables in the *

* link files
*********************************************************************

wrldset: move.1 #wrldx,datx * Pass the variables

move. 1 #wrldy,daty * for the rotation

move.1 #wrldz,datz * routine

move.1 #viewx,pointx

move.1 #viewy,pointy

move.1 #viewz,pointz

move.1 #wlinxy,linxy

move. w picturex,xO

move.w picturey,yO

move.w proz,zobs

move.w rlzl,dist

move.1 Iscreenx,xplot

move.1 #screeny,yplot

move.w hnumline,numline

move.w hnummark,nummark

move.w hnumsurf,numsurf

rts

* Remove all characters from keyboard buffer

clearbuf: move. w #$b,-(a7)

trap #1

addq. 1 #2, a7

tst. w dO

beq clearnd

move.w #1,-{a7)

trap #1

addq.1 #2, a7

bra clearbuf

clearnd: rts

227
ST 3D Graphics Abacus Software

*********************************************************************
* Sense display resolution and set coordinate *

* origin to screen center *


*********************************************************************

getreso: move.w #4,-(a7) * Sense display resolution


trap #14

addq.1 #2,a7

cmp. w #2, dO

bne getrl

move.w #320,picturex ★ Monochrome monitor

move.w #200,picturey
bra getrend
getrl: cmp • w #1, dO

bne getr2
move.w #320,picturex * medium resolution (640*200
move.w #100,picturey
bra getrend
getr2: move.w #160,picturex * low resolution (320*200)
move.w #100,picturey
getrend: rts

* Hardcopy of screen, called by inp_chan *

* *

scrdmp: move. w #20,-(a7)

trap #14

addq. 1 #2,a7

jsr clearbuf

rts

228
Abacus Software ST 3D Graphics

*********************************************************************
* Sets the limits of the display window for the

* Cohen-Sutherland clipping algorithm built into the

* draw-line algorithm
* The limits can be freely selected by the user which makes ‘

* the draw-line algorithm very flexible. *


*********************************************************************
move.w #0, clipxule

move . w #0,clipyule

move.w picturex,dl
★ times two
lsl. w #l,dl

#l,dl ★ minus one equals


subq.w
dl,clipxlri * 639 for monochrom
move.w

move .w picturey,dl

#1, dl * times two minus one


lsl. w

#1, dl * equals 399 for monochrome


subq.w

move.w dl,clipylri

rts

* Pass visible surfaces into vplane array and

* into pladress array for subsequent sorting


*r
* of surfaces

hideit:
move. w numsurf, dO * Number of surfaces as

dO * counter
ext. 1

subq.1 #1, dO

move.1 #viewx,al * The point

move.1 #viewy,a2 * coordinates are stored here

move.1 #viewz,a3

#wplane,aO * Here is the information


move.1

move.1 fvplane,a5 * for every surface

move.w #0,surfcount * Counts the known visible surfaces.

tpladress,a6 * Address of surface storage


move.1

(aO) ,dl * Start with first surface


move.w

dl * Number of points on this surface in Dl


ext. 1

move. w 2 (aO),d2 * Offset of first point of this surface

229
ST 3D Graphics Abacus Software

move.w 4 <a0),d3 * Offset of second point


move.w 8 (aO),d4 * Offset of third point
subq.w #l,d2 * For access to point array
subq.w #1, d3 * subtract one from current
subq.w #1, d4 * point offset.
lsl. w #1/ d2 * Multiply by two
lsl. w #l,d3

lsl -w #1, d4 ★ and access current


move. w (al,d3.w),d6 * point coordinates
cmp. w (al,d4.w), d6 * Comparison recognizes two point
bne doit 1 ★ with the same coordinates
A ★ created through

move.w (a2,d3.w) ,d6 * construction of


cmp. w (a2,d4.w) ,d6 ★ rotation bodies. When
bne doitl ★ two points are found
move.w (a3,d4.w),d6 ★ where all point coordinates (x.
cmp. w (a3,d3.w),d6 ★ match, the program selects the
bne doitl * third point to find
move. w 12(aO),d4 * both vectors
subq.w #l,d4

lsl .w #l,d4

doit 1:

move.w (al,d3.w),d5 * the two vectors which


move.w d5, kx * lie in the surface plane
sub.w (al,d2.w),d5 * are found by subtracting the
move.w d5,px * coordinates of two points
move.w <a2,d3.w),d5 * in this surface
move.w d5, ky * the direction coord, of the
sub. w <a2,d2.w),d5 * vectors is stored in
move. w d5,py * variables qx,qy,qz and

move. w (a3,d3.w),d5 * px,py,pz

move. w d5, kz

sub. w (a3,d2.w),d5
move.w d5, pz

move.w (al,d4.w),d5 * Calculate vector Q

sub. w (al,d2.w),d5

move.w (a2,d4.w),d6

sub. w (a2,d2.w),d6

move.w (a3,d4.w),d7

sub. w (a3,d2.w),d7

230
Abacus Software ST 3D Graphics

move. w d5,dl * qx

move.w d6,d2 * qy

move.w d7,d3 * qz

mu Is py,d3 * Compute cross product

mu Is pz, d2 * of the vector perpendicular

sub.w d2,d3 * to the current surface

move. w d3, rx

mu Is pz, dl

mu Is px,d7

sub.w d7, dl * The direction coordinates of

move.w dl, ry * the vector perpendicular to

mu Is px, d6 * the surface are stored

mu Is py,d5 * in rx,ry,rz

sub - w d5,d6

move. w d6, rz

move. w prox,dl * The projection center serves as

sub.w kx, dl * comparison point for the visibility

move .w proy,d2 * of a surface which seems

sub.w ky,d2 * adquate for the viewing

move. w proz,d3 * situation. The observation

sub.w kz, d3 * ref. point can also

mu Is rx, dl * be used as the comparison point.

muls ry,d2 * Compare vector R and

mu Is rz, d3 * the vector from one

add. 1 dl,d2 * point of the surface to

add. 1 d2,d3 * the projection center by forming

bmi dosight * the scalar product of the two vector:

scalar product is negative, surface is visible

(aO),dl * Number of lines in surface


move.w

ext. 1 dl

lsl.l #2, dl ★ Number of lines times 4 = space for line

#2, dl * plus 2 bytes for number of lines


addq.1

add. 1 dl, aO ★ add to surface array for

dbra dO,visible * access to next surface

bra bideend * All surfaces processed ? End

231
ST 3D Graphics Abacus Software

dosight: move.w (aO),dl * Number of lines for this surface

ext.1 dl * multiplied by two results in

*************
** Changes from the program rotl.s * it

** **

**********************************

move.1 dl,d2

lsl.l #l,dl * Number of words to be passed


move.l a0,a4

addq.l #2,a4 * Access to first line of the surface


move.w #0,zsurf * Clear addition storage

sight2: move.l (a4)+,d6 * first line of surface


swap d6 * first point in lower half of DO
subq.w #l,d6 * fit index

lsl.w #1,d6 * fit operand size (2-Byte)

move.w (a3,d6.w),d6 * Z-coordinate of this point


add.w d6,zsurf * add all Z-coordinates
dbra d2,sight2 * until all lines are computed

move.w zsurf,d6 * Divide sum of all Z-coordinates



* for this
ext.l d6 * surface by the number of lines
lsr.l #2,d6 * Surfaces created by rotation
ext.l d6 * always have four lines.
move.1 d6, (a6) + * Store middle Z-Coordinate
move.1 aO, (a6) + * followed by address of surface

sight3: move.w (a0)+, (a5) + * pass number of lines

dbra dl,sight3 * and individual lines


addq.w #1,surfcount * increase number of surfaces by one
bra sight 1 * and work on next surface
hideend: rts

232
Abacus Software ST 3D Graphics

***********************************************************************
* Create rotation body by passing parameters, *

* rotating the definition line, and creating the line and

* surface arrays *
***********************************************************************

rlset:
* Pass the
move.1 #rlxdat,rotxdat
* parameters for
move. 1 #rlydat,rotydat

#rlzdat,rotzdat * rotation body to


move.1
*
move.1 #rldatx,rotdatx routine for
* creating the
move.1 #rldaty,rotdaty
★ rotation body
move.1 #rldatz,rotdatz

rotdatx,datx * array addresses of


move.1

rotdaty,daty ★ the points


move.1

move. 1 rotdatz,datz
★ Number of desired rotations
move.w rlnumro,numro

rlnumpt,numpt ★ Number of points to be rotated


move. w
★ Address of line array
move.1 #rllin,linxy
#rlplane,worldpla * Address of surface array
move.1

rts

rotstart: move.w numpt,dO * Rotation of def line

lsl.w #l,dO * numro+1 times about Y-axis

ext.1 dO

move.1 dO,plusrot * Storage for one line

move.w numpt,nummark * Number of points

move.1 rotdatx,pointx * rotated

move.1 rotdaty,pointy

move.1 rotdatz,pointz

move.w #0,yangle

move.w #360,dO * 360 / numro = angle increment

divs numro,d0 * per rotation

move.w dO,plusagle * store

move.w numro,d0 * numro +1 times

ext.l dO

rloopl: move.1 d0,loopc * as loop counter

move.l rotxdat,datx

move.1 rotydat,daty

move.l rotzdat,datz

jsr yrot * rotate

233
ST 3D Graphics Abacus Software

move.1 pointx,dl * add offset

add. 1 plusrot,dl

move.1 dl,pointx

move.1 pointy, dl

add. 1 plusrot,dl

move. 1 dl,pointy

move. 1 pointz,dl

add. 1 plusrot,dl

move.1 dl,pointz

move.w yangle,d7

add.w plusagle,d7

move. w d7,yangle

move.1 loopc,dO

dbra dO,rloopl

move.w rlnumro,numro

move.w rlnumpt,numpt

jsr rotlin * Create line array

jsr rotsurf * Create surface array

rts

rotlin:

move.w #1, d7

move. w numro,d4 * * Number of rotations

ext .1 d4

subq.1 #1, d4

move. w numpt,dl ★ Number of points in def. lin

subq.w #1, dl * both as counters

lsl. w #2,dl ★ times two

ext. 1 dl

move.1 dl,plusrot

rotlopl: move.w numpt,d5 ★ Number of points minus once

ext. 1 d5 * repeat, last line

subq.1 #2,d5 ★ connect points (n-l,n)

move.1 linxy,al

move.w d7,d6

rotlop2: move.w d6, (al) + ★ first line connects

addq.w #l,d6 ★ points (1,2) then (2,3) etc.

move.w d6, (al) +

dbra d5,rotlop2

234
Abacus Software ST 3D Graphics

move.1 linxy,dl

add. 1 plusrot,dl

move . 1 dl,linxy

move .w numpt,dO

add. w d0,d7

dbra d4,rotlopl

move . w numpt,d7

move.w d7,deltal

lsl. w #2,d7

ext. 1 d7

move . 1 d7,plusrot

move. w #1, d6

move.w numpt,dO

ext. 1 dO

subq.1 #l,dO

rotlop3: move.w numro,dl

ext. 1 dl

subq.1 #1, dl

move. w d6, d5

d5,(al)+ ★ generate cross


rotlop4 : move . w
deltal,d5 * connection lines which
add. w

move. w d5, (al) + * connect lines created

dl,rotlop4 ★ by rotation
dbra

add.w #1, d6

dbra dO,rotlop3

move.w numro,dl

add. w #1, dl

mu Is nummark, dl

move. w dl,rlnummark

move.w numpt,dl

mu Is numro,dl

move. w numpt,d2

subq.w #1, d2

mu Is numro,d2

add.w dl, d2

235
ST 3D Graphics Abacus Software

move.w d2,rlnumline * store number of lines


rt s

rotsurf: move.w numro,dO * Create surfaces of


ext. 1 dO * rotation body
subq.1 #1, dO

move.w numpt,d7 * Number of points minus one


ext. 1 d7 * repeat
subq.1 #2,d7

move.1 d7,plusrot

move.1 worldpla,aO * Address of surface array


move.w #1/ dl

move. w numpt,d2 * Number of points


addq.w #l,d2

rotf11: move.1 plusrot,d7 * Offset


rotf12: move.w dl, d4

move.w d2,d5

addq.w #l,d4

addq.w #1, d5

move.w #4, (aO) + * Number of lines/surfaces

move.w dl, (aO) + * first surface created here


move.w d4, (aO) +

move.w d4, (aO) +

move.w d5,(aO)+

move.w d5,(aO)+

move.w d2,(aO)+

move.w d2, (aO) +

move.w dl, (aO) +

addq.w #1, dl

addq.w #1, d2

dbra d7,rot f12

addq.w #1, dl
addq.w #1, d2

dbra dO,rotf11

move.w numpt, dl

subq.w #1, dl

muls numro,dl

236
Abacus Software ST 3D Graphics

move.w dl,rlnumsurf

rts

* Pass data and parameters to the link file routines

wrld2set: move.1 #wrldx,datx

move -1 #wrldy,daty

move.1 #wrldz,datz

move.1 #viewx,pointx

move.1 #viewy,pointy

move . 1 #viewz,pointz

move.1 #wlinxy,linxy

move. w picturex,xO

move. w picturey, yO

move.w proz,zobs

move.w rlzl,dist

move. 1 #screenx,xplot

move. 1 #screeny,yplot

move.w rlnumline,numline

move.w rlnummark,nummark

move.w rlnumsurf,numsurf

rts

***********************************

* Sort surfaces stored in pladress
***********************************

sort it: move. 1 #pladress,aO

move. w surfcount,d7

ext. 1 d7 * for i = 2 to n corresponds to

subq. 1 #2, d7

bmi serror * for i = 1 to n-1 because of

move. 1 #l,dl * different array structure

sortmain: move . 1 dl, d2

subq.1 #l,d2 * j = i -1

move.1 dl, d3 * i

lsl.1 #3, d3

237
ST 3D Graphics Abacus Software

move.1 (a0,d3.1) ,d5 * Comparison value x = a[i]

move.1 4(aO,d3.1),d6 h
Address of surface
move. 1 d5,platz * a [0] = x = a [-1] in
move.1 d6,platz+4 * this array
sortlopl: move.1 d2, d4 *
j
lsl.l #3, d4 *
j times 8 for access to array
cmp. 1 (aO,d4.1),d5 * Z-coordinate of surface
bge sortwl * while x < a[j] do

move. 1 (aO, d4.1), 8(aO,d41.1) * a [ j + 1] = a [ j]


move.1 4(aO,d4.1),12(aO, d4.1) * Address of surface ar:
subq.1 #1, d2 * j = 3-1
bra sortlopl

sortwl: move. 1 d5, 8 (aO, d4.1) *


a Cj+1] = x
move. 1 d6, 12(aO,d4.1) * pass address also
addq. 1 #1, dl * i = i + 1
dbra d7, sortmain * until all surfaces are sorted
sortend: rts

serror: rts * On error simply return

* Fill surfaces stored in pladress *

paintit: jsr setclip *


GEM clipping routine for Filled Area
jsr sortit Hr
Sort surfaces according to Z-coords.
move.w #1, dO * Write mode to replace
jsr filmode
jsr filform * border filled surfaces
jsr filcolor * Fill color is one
move.w #2,dO ★ Fill style
jsr filstyle
move.1 xplot,al * Address of screen coordinates
move.1 yplot,a2

move.w surfcount,d7 * Number of surfaces to be filled


ext. 1 d7 Hr
as counter
subq.1 #1/ d7 * access to last surface in the array
move.1 d7, dO * multiply by eight
lsl.l #3, dO

238
Abacus Software ST 3D Graphics

move.1 #pladress,aO * here are the surfaces


move.1 (a0,d0.1),d5 * largest Z-coordinate

move. 1 #0, dl
move. 1 (aO,dl.1), d6 * first surface in array

neg. 1 d6 * smallest Z-coordinate

add. 1 d6,d5 * subtract from each other

pa inti: move. 1 d5,d0


move.1 (a0,dl.l),d2 * first surface in array

add. 1 d6, d2 * plus smallest Z-coordinate


lsl.l #3,d2 * times eight, eight different

divs d0,d2 * fill patterns, divide by difference

neg.w d2 * leave out last pattern

add. w #6,d2
bpl paint2
move.w #1, d2

paint2: move•w d2,d0 * Set fill index

jsr filindex
move. 1 #ptsin,a3 * Enter points here

move. 1 4(a0,dl.l),a6 * Address of surface

move. w (a6)+,d4 * Number of lines

addq. w #1, d4 * first point counts double

move. w d4,contrl+2
move. 1 (a6)+,d3 * first line of surface

swap d3
subq.w #1, d3
lsl. w #1, d3
move.w (al,d3.w),(a3)+ * transfer to ptsin array

move.w (a2, d3 . w) , (a3) + * transmit Y-coordinate

swap d3
sub. w #l,d3
lsl. w #1, d3
move.w (al, d3 . w) , (a3) + * transmit next point

move.w (a2,d3.w),(a3)+ * transmit Y-coordinate

subq.w #3,d4 * two points already transmitted

ext. 1 d4 * one because of dbra

paint3: move.1 (a6)+,d3 * next line

subq.w #1, d3
lsl -W #1, d3
move.w (al,d3.w),(a3)+ * X-coordinate

move.w (a2,d3.w), (a3) + * Y-coordinate

dbra d4,paint3 * until all points in ptsin arra;

239
ST 3D Graphics Abacus Software

move.w #9,contrl * then call the


move.w #0,contrl+6 * function Filled
move. w grhandle,contrl+12 * Area

movem.1 d0-d2/a0-a2,- (a7)

jsr vdi

movem.1 U7)+,d0-d2/a0-a2

add. 1 #8, dl * work on next


dbra d7,paintl * surface in plad.
rts

* VDI clipping, used only with VDI functions, also for *

* filling surfaces. *

setclip: move.w #129,contrl

move. w #2,contrl+2

move.w #1,contrl+6

move.w grhandle,contrl+12

move.w #1,intin

move. w clipxule,ptsin

move . w clipyule,ptsin+2

move. w clipxlri,ptsin+4

move.w clipylri,ptsin+6

jsr vdi

rts

. even

* Start of variable area *


* *

* Data area for rotation body *

.bss

240
Abacus Software ST 3D Graphics

numro: .ds.w 1

numpt: .ds.w 1

rotxdat: . ds . 1 1

rotydat: .ds.l 1

rotzdat: .ds.l 1

rotdatx: .ds.l 1

rotdaty: .ds.1 1
rotdatz: .ds.l 1

rlnumline: .ds.w 1

rlnummark: .ds.w 1

rlnumsurf: .ds.w 1

plusagle: .ds.w 1

rldatx: .ds.w 1540

rldaty: .ds.w 1540

rldatz: .ds.w 1540

rllin: •ds.l 3200 * 4-Bytes for every line

rlplane: .ds.l 6600

.data

rlxdat: .dc.w 0,40,50,50,20,30,20,30,70,80,80,0

rlydat: .dc.w 100,100,80,60,40,30,30,-70,-80,-90,-100,-100

rlzdat: .dc.w 0,0, 0,0, 0, 0,0,0,0, 0,0, 0

rlnumpt: .dc.w 12

rlnumro: .dc.w 8 * Number of rotations for creation

241
ST 3D Graphics Abacus Software

* *

* *

* Definition of the house *


* *

• data

housdatx: .dc.w -30,30,30,-30,30,-30,-30,30,0,0,-10,-10,10,10


. dc. w 30,30,30,30,30,30,30,30,30,30,30,30

housdaty: . dc. w 30,30,-30,-30,30,30,-30,-30,70,70,-30,0,0,-30

. dc. w 20,20,0,0,20,20,0,0

.dc. w -10,-10,-30,-30

housdatz: .dc .w 60,60, 60, 60,-60,-60,-60,-60, 60,-60, 60,60, 60, 60

.dc.w 40,10,10,40,-10,-40,-40,-10

.dc. w 0,-20,-20,0

houslin: .dc.w 1,2,2,3,3, 4,4,1,2,5, 5, 8,8, 3, 8,7, 7, 6, 6, 5, 6,1,7, 4

.dc.w 9,10,1,9,9,2,5,10,6,10,11,12,12,13,13,14
.dc.w 15,16, 16, 17,17,18; 18, 15,19,20,20,21,21,22,22, 19
.dc.w 23,24,24,25,25,26,26,23

* here are the definitions of the surfaces for the House *


* *

houspla: .dc. w 4, 1,2,2,3,3,4,4, 1,4,2,5,5,8, 8,3, 3,2


.dc. w 4, 5,6, 6,7,7,8,8, 5, 4,7, 6, 6,1, 1*4, 4,7

.dc. w 4, 4,3,3,8,8,7,7, 4,4,2, 9, 9,10 , 10, 5,5, 2


.dc. w 4, 10,9,9,1,1,6,6 ,10,3,1, 9, 9, 2,2, 1
.dc. w 3, 5,10,10,6,6,5, 4,11,12,12,13,13 *14, 14,11
.dc. w 4, 15,16,16,17,17 ,18,18,15,4, 19,2 0,20 ,21,21,22,22,19
-dc. w 4, 23,24,24,25,25 ,26,26,23

hnuinmark: .dc. w 26 * Number of corner points in the house


hnumline: . dc. w 32 * Number of lines in the house

hnumsurf: .dc:. w 13 * Number of surfaces in the house

242
Abacus Software ST 3D Graphics

hxangle: .dc.w 0 ★ Rotation angle of house about X-axis

hyangle: . dc. w 0 ★ " ” " Y-axis


hzangle: .dc.w 0 * ■' " " Z-axis

xwplus: .dc.w 0 ★ Angle increment about X-axis

ywplus: • dc.w 0 ★ Angle increment about Y-axis

zwplus: • dc.w 0 ★ Angle increment about Z-axis

picturex: .dc.w 0 * Definition of zero point of display

picturey: .dc.w 0 * entered by getreso

rotdpx: .dc.w 0
rotdpy: .dc.w 0
rotdpz: .dc.w 0

rlzl: .dc.w 0
normz: .dc.w 1

.bss

plusrot: .ds.l 1
first: • ds .w 1
second: .ds. w 1
deltal: .ds. w 1

worldpla: .ds.l 1

.data

plag: .dc .b 1
. even

.bss

diffz: .ds. w 1

243
ST 3D Graphics Abacus Software

dx: . ds. w 1
dy: . ds. w 1
dz: . ds. w 1

wrldx: .ds .w 1600 * World coordinate array


wrldy: .ds. w 1600
wrldz: • ds. w 1600

viewx: .ds. w 1600 * View coordinate array


viewy: • ds. w 1600
viewz: . ds. w 1600

screenx: .ds .w 1600 * Screen coordinate array


screeny: .ds. w 1600

wlinxy: -ds. 1 3200 * Line array

wplane: • ds. 1 6600 * Surface array

vplane: -ds. 1 6600 * Surface array of visible surfaces

platz: .ds. 1 2
pladress: • ds.l 3000 * Surface array

surfcount: .ds .w 1

numsurf: . ds. w 1

zcount: .ds.l 1 * Sum of all Z-coord.


zsurf: • ds. w 1 * Individual Z-coord.of surface

sx: -ds. w 1
sy: .ds. w 1
sz : .ds .w 1

px: -ds. w 1
py: • ds. w 1
pz: .ds. w 1

244
Abacus Software ST 3D Graphics

rx: .ds. w 1
ry: . ds. w 1
rz: . ds. w 1

qx: .ds. w 1
qy: .ds. w 1
qz: .ds.w 1

kx: . ds. w 1
ky: • ds.w 1
kz: • ds.w 1

. data

prox: .dc.w 0 * Coordinates of projection


proy: .dc. w 0 * center, on the positive

proz: • dc.w 1500 * Z-axis

.data

of f x: .dc.w 0 * Transformation through rotation

of fy: • dc.w 0 * to point [offx,offy,offz]


of fz: .dc.w 0

xoffs: .dc.w 0 * Inverse transformation to point

yoffs: .dc.w 0 * [xoff,yoffs,zoffs]


zoffs: .dc.w 0

.bss

loopc: .ds. 1 1
. end

245
ST 3D Graphics Abacus Software

4.4.1 New things in the main program rotatel.s:

The creation of a surface array during construction of the rotation body is


accomplished through the subroutine rotsurf:. The array (rlplane)
is of course passed from the subroutine makewrld: into the world
system (wplane). Furthermore, the subroutines hideit:, setclip:
and paintit: as well as the sort routine sort it: are new and have
already been explained. This sort routine sorts the array surfaddr,
which contains the Z-coordinates of the visible surfaces as well as the
addresses of the visible surfaces, according to increasing Z-coordinates.
The subroutine sort it: uses the old trick, an additional array index at
the beginning of the array. You can recognize this by the variable
space: in the variable part of the program. The variable space:
reserves additional space for a data record in the surfaddr-arrays. The
additional space is used as a marker during sorting. The actual sort
algorithm is nothing but a simple insert sort. For better understanding,
here is a structogram of the sort algorithm:

Figure 4.4.4: Structogram of the sort algorithm

246
Abacus Software ST 3D Graphics

4.4.2 Sort algorithm:

In this program too, you should change various parameters to see what
they do. Up to now you had to change all the parameters in the program
text. This meant that you had to do a lot of assembling and linking just to
change a few parameters. The sort algorithm will allow you to change
parameters while the program is running. One method to change these
parameters is through a menu. See the diagram below. More about this in
the next section.
ST 3D Graphics Abacus Software

4.5 Entering rotation lines with the mouse

We are now ready to combine the subroutines which we have so far used
separately and to construct a little program for creating rotation bodies,
including the removal of hidden lines and shading surfaces. Furthermore,
we also want to be able to enter the creation lines for the rotation body
with the mouse so that we don’t have to reassemble the program when we
want to use a new definition line. Owners of 520ST’s may find
themselves running short of memory. The available storage space permits
the input of 25 points for a definition line of the rotation body which can
then be rotated 60 times about the Y-axis. Thus a maximum of
25*61 = 1525 points and about 3000 lines and almost 1500 surfaces will
be created. To store this many parameters as well as the program we need
about 190Kbytes of memory, about a third of which is wasted because the
object is defined twice (datx, daty, datz, wrldx, wrldy,
wrldz). This is done to make things easier, but also in consideration of
the next main program which displays several objects at the same time.
We also have to keep in mind the memory require by the two screen
pages-about 64K

The amount of memory reserved in this program is intended for use on


the "smaller" model. Owners of 1 mega byte computers can display larger
objects if they want by reserving more space for the individual arrays.
The following relationships as are used to calculate the memory
requirements:

Number of points:= rlnumpt * (rlnumo+1)

Number of lines:= ( (rlnumpt-1) *rlnumro) +


(r1numpt * r1nuraro)

Number of surfaces:= (rlnumpt-1) * rlnumro

The number of lines can be estimated by multiplying the number of


points by two. Each point naturally requires two bytes of storage space.
You must also remember that every surface of the rotation body, requires
18 bytes of storage space since it is always constructed of four lines. In
the surfaddr array every surface requires 8 bytes of additional storage
space. With this information you can expand the programs yourself if you
have a 1040ST. The introduction of the operating system in ROM will
ease the lack of storage space. About 200K of RAM will be released by

248
Abacus Software ST 3D Graphics

using the ROM. If you want to generate rotation bodies with more points
without RAM enhancement, whether through ROMs or RAM chips, you
can change the program so that the rotation body is not duplicated in the
arrays rldatx, rldaty, rldatz, but generated only in the world
system wrldx, wrldy, wrldz and the definition of rldatx,
rldaty, rldatz is completely omitted. This will free about 50
Kbytes of storage which includes the savings from the line array
(rllin) and surface array (rlplane). This space can be distributed
over the world array and thus used to generate larger bodies. The product
of the number of points and the number of rotations plus one is limited.
You can for example, rotate 16 points 90 times, or 40 points 30 times, etc.
The only limits placed are those of your imagination. The number of
rotation points to be entered is determined by the variable maxpoint and
can be changed there.

The use of this program differs in a few points from the programs
presented thus far. After the program start, a menu appears where you can
determine the desired number of rotations of a rotation line already
defined in the program. After you press one of the function keys F2 to F8,
the familiar chess figure appears in the ’’wire model mode" with the
desired number of rotations. The actual rotation parameters such as
position of the rotation point and rotation angle increments can be
changed with the cursor-keys. To remove hidden lines in this rotation
body press the H key on the keyboard (H for Hide). After the visible
surfaces have been drawn, you can fill them with a pattern by pressing the
P key (P for Paint). In both cases you can obtain a hardcopy by pressing
the <Altemate> and <Help> keys at the same time since the surfaces are
drawn and in the visible screen page (physical display ). The picture
drawn on the display remains until the <Retum> key is pressed and
cannot be changed. As a further option you can fill all the surfaces in the
"wire model mode" (P key), not only the visible ones. For hardcopy of a
wire model, press Shift D. By pressing the F10 key you return to the main
menu and you can enter a new rotation line with FI and the help of the
mouse.

After pressing FI a small crosshair and a cartesian coordinate system


whose origin is the. middle of the screen appear. By clicking the left
mouse button you can enter up to 25 points for a definition line. The right
mouse button ends the definition after which you must press a key to
return to the menu. You can set the number of rotations with the function
keys. We almost forgot to mention the significance of the F9 function key
which displays a mouse pointer when pressed in the wire model mode

249
ST 3D Graphics Abacus Software

and allows you to set a new coordinate origin on the screen (left mouse
button). Here are some examples of definition lines and the rotation
bodies which result.
Abacus Software ST 3D Graphics
ST 3D Graphics Abacus Software

252
Abacus Software ST 3D Graphics
ST 3D Graphics Abacus Software
Abacus Software ST 3D Graphics
ST 3D Graphics Abacus Software

Figure 4.5.13

Don’t let the program listing frighten you. First of all, if you have entered
the previous programs, all you have to do is enter the new subroutines
and change the main loop a bit. Second, you can get a disk containing all
of the programs in the book from Abacus Software or your dealer.

253882 bytes used 1 1442236 bytes used in 123 itens. 333356 butes used in
138344 11-2 □ H0USE1 PRG

l
K PRINTERS BASIC PRG
N TUTORIAL BASIC RSC 4648 11-2 H0USE1 S
C FRY BASIC URK 346 11-2 MAINi PR6
CONV TTP BASIC1 BAR 14801 11-2 HAIN1 S
HL10 PRO HAIN1C0 PRG
OUTPUT PRG MAIN1CD S
OPEN APPLICATION
SPLIT TTP MENU1 PRG
STANDARD PRT Nane: BATCH .TTP MENU1 S
TEXTPRO PRG Parameters: . HULTI1 PRG
TUTORIAL TXT aslink grlinkl nenul|_ _ HULTI1 S
XTTUTORI TOC PAINT1 PRG


PAIKT1 S
R0TATE1 PRG

1 1

R0TATE1 S
Abacus Software ST 3D Graphics

***********************************************************************
* menul.s 2/18/1986 *
* Creation of rotation bodies Uwe Braun 1985 Version 2.2 *
* with hidden line algorithm and painting *
* *

.globl main,xoffs,yoffs,zoffs,offx,offy,offz
•globl viewx,viewy,viewz
.globl wlinxy,mouse_off,setrotdp,inp_chan,pointrot
. text

main:
jsr apinit * Announce programm
jsr grafhand * Get screen handler
jsr openwork * Display
jsr mouse off * Turn off mouse
jsr getreso * Display resolution
jsr setcocli * set Cohen Sutherland clip.

mainl: jsr clearbuf


jsr menu

jsr makerotl * create rotation body

jsr makewrld * create world system


jsr wrld2set * pass world parameters
jsr pageup
jsr clwork
jsr setrotdp * initialize observer ref. point
jsr pagedown * Display logical screen page
jsr clwork
jsr inp_chan

mainlopl:
jsr pointrot * rotate around observ. ref. point
jsr pers * Perspective transformation
jsr drawnl

jsr pageup * Display physical screen page


jsr testhide

257
ST 3D Graphics Abacus Software

jsr inp chan * Input new parameters

jsr clwork * clear page not displayed

jsr pointrot * Rotate around rot ref . point

jsr pers * Transform new points

jsr drawnl

jsr pagedown * Display this logical page

jsr inp chan * Input and change parameters

jsr clwork * erase physical page

jmp mainlopl * to main loop

move.1 physbase,logbase

jsr pageup * switch to normal screen page

rts * back to link file and end

* Display menu and selection of menu points

menu: jsr switch * Display and draw the same

move.1 #text2,a0 * screen page

jsr printf * Display menu list

move.1 #text3,aO

jsr printf

menuO: jsr inkey * Read keyboard

swap dO

cmp.b #$3b,dO * Fl key pressed ?

bne menul

jsr inpmous * if yes, enter a line

bra menu

menul: cmp .b #$3c,d0 * F2 key pressed ?

bne menu 2

move.w #4, rlnumro * if yes, then initial number of

bra menend * rotations to four

258
Abacus Software ST 3D Graphics

menu2: cmp.b #$3d,d0 * F3 key

bne menu 3

move.w #8,rlnumro

bra menend

menu3: cmp.b #$3e,d0 * F4 key

bne menu 4

move.w #12,rlnumro

bra menend

menu*?: cmp.b #$3f,dO * F5 key

bne menu 5

move.w #18, rlnumro

bra menend

menu5: cmp.b #$40,dO * F6 key

bne menu6

move. w #24,rlnumro

bra menend

menu6: cmp.b #$41,dO * F7 key

bne menu7

move. w #45,rlnumro

bra menend

menu7: cmp.b #$42,dO * F8 key

bne menu 8

move.w #60,rlnumro

bra menend

* Room for additional keyboard commands


menu8 :

menu9: cmp. b #$44,dO * F10 key

bne menuO

addq.1 #4,a7

bra ma inend

menend: rts

259
ST 3D Graphics Abacus Software

*****************************************************************
* Test if removal of hidden surface and shading of surfaces *

* is desired *
*****************************************************************

jsr inkey * Read keyboard

swap dO

cmp.b #$23,dO ★ h key pressed ?

beq dohide * if yes, call hideit

cmp .b #$19,dO * p key pressed ?

beq dopaint * is yes, shade

rts ★ if not, return

★A*****************************************************************
* Call hideit routine to remove hidden Surfaces *
*******************************************************************

dohide: jsr switch * or you won't see anything

jsr clwork * erase display

jsr hideit * remove

jsr surfdraw * and draw

dohidel: jsr inkey * shade too ?

swap dO

cmp.b #$19,dO * if yes, call fill routine

beq dopain2

cmp.b #$lc,d0 * if not, wait for activation of

bne dohidel * Return key on main keyboard

jsr pageup

rts * and back

dopain2: jsr paintit * Shade surfaces

dopain3: jsr inkey

swap dO

cmp.b #$lc,dO * wait for return key

bne dopain3

jsr pageup

rts

260
Abacus Software ST 3D Graphics

***********************************************************************

* Shade all surfaces defined in the world system


**************,********************************************************

dopaint: jsr switch

jsr clwork

jsr paintall * shade all

dopaintl: jsr inkey

swap dO

cmp. b #$lc,dO * and wait for Return key on the

bne dopaintl * main keyboard

jsr pageup

rts

* Create the rotation body

makerot1: jsr rlset * Set parameters of this rot. body

jsr rotstart * Create rot. body

rts

* Input and change parameters

inp chan: jsr inkey * Read keyboard, key code in

cmp.b #' D',dO

bne inpwait

jsr scrdmp * Make hardcopy

inpwait: swap dO * Test DO for

cmp.b #$4d,d0 * Cursor-right

bne inpl

addq.w #1,ywplus * if yes, add one to Y-angle

bra inpendl * and continue

261
ST 3D Graphics Abacus Software

inpl: cmp .b #$4b,dO * Cursor-left, if yes, subtract

bne inp2 * one from Y-angle increment

subq.w #1,ywplus

bra inpendl

inp2: cmp .b #$50,dO * Cursor-down, if yes

bne inp3

addq.w #1, xwplus * add one to X-angle increment

bra inpendl

inp3: cmp .b #$48,dO * Cursor-up

bne inp3a

subq.w #1,xwplus * subtract one

bra inpendl

inp3a: cmp.b #$61,dO * Undo key

bne inp3b

subq.w #1,zwplus

bra inpendl

inp3b: cmp.b #$62,dO * Help key

bne inp4

addq.w #1,zwplus

bra inpendl

inp4: cmp.b #$4e,dO * plus key on the keypad

bne inp5 * if yes, subtract 25 from base of

sub. w #25,dist * projection plane (Z-coordinate)

bra inpendl

inp5: cmp.b #$4a,d0 * minus key on the keypad

inp6 *
bne

add. w #25,dist * if yes, add 25

bra inpendl

inp6: cmp.b #$66,dO * * key on keypad

bne inp7 * if yes, subtract 15 from rotation

sub.w #15,rotdpz * point Z-coordinate

bra inpendl * make changes

262
Abacus Software ST 3D Graphics

inp7 : cmp -b #$65,dO * Division key on keypad

bne inp8

add. w #15,rotdpz * add 15

bra inpendl

inp8: cmp. b #$43,dO * F9 pressed ?, if yes,

bne inplO

jsr newmidd * display new screen center

bra inpendl

inplO: cmp -b #$44,dO * F10 pressed ?

bne inpendl

addq.1 #4,a7 * if yes, jump to new input

bra mainl

inpendl: move.w hyangle,dl * Rotation angle about the Y-axis

add.w ywplus,dl * add increment

cmp. w #360,dl * if larger than 360, subtract 360

bge inpend2

cmp. w #-360,dl * if smaller than 360,

ble inpend3 * add 360

bra inpend4

inpend2: sub. w #360,dl

bra inpend4

inpend3: add. w #360,dl

inpendl: move.w dl,hyangle

move.w hxangle,dl * proceed in the same manner with

add. w xwplus,dl * rotation angle about the X-axis

cmp. w #360,dl

bge inpend5

cmp. w #-360,dl

ble inpend6

bra inpend7

inpend5: sub. w #360,dl

bra inpend7

inpend6: add.w #360,dl

inpend7: move . w dl,hxangle ★

263
ST 3D Graphics Abacus Software

move.w hzangle,dl

add. w zwplus,dl

cmp. w #360,dl

bge inpend8

cmp. w #-360,dl

ble inpend9

bra inpendlO

inpend8: sub. w #360,dl

bra inpendlO

inpend9: add. w #360,dl

inpendlO: move.w dl,hzangle

rt s

fr*****************************************************************
* Set the location of the coordinate origin of the screen *

* system with the mouse *


******************************************************************

newmidd: jsr switch

jsr mousform ★ change mouse form

newmiddl: move. w x0,d2

move. w y0,d3

jsr mouspos * wait for mouse input

move. w x0,d2 ★ must be called for unknown reasons

move. w y0,d3 ★ twice for one input of the

jsr mouspos * Position

cmp.b #$20,dl * left button ? if not, then

bne newmiddl ★ once more from the beginning


*
rv>

o
a

move. w store new coordinates


X
*•

move.w d3,y0

rts

********************************************************************
* Determine the current screen resolution *
********■*********★**********★**************"****'****•***'*■*******■****

getreso: move.w #4,-(a7)

trap #14

addq.l #2,a7

264
Abacus Software ST 3D Graphics

cmp.w #2, dO

bne getrl

move.w #320, picturex * Monochrome monitor

move. w #200,picturey

bra getrend

getrl: cmp. w #1, d0

bne getr2

move.w #320,picturex * medium resolution (640*200

move. w #100,picturey

bra getrend

move. w #160,picturex * low resolution (320*200)


getr2:
move. w #100,picturey

getrend: rts

* Hardcopy of screen, called by inp_chan

scrdmp: move.w #20,-(al)

trap #14

addq.l #2,a7

jsr clearbuf

rts

* Initialize the rotation reference point to [0,0,0] *

move . w #0,dl * set the initial rotation

move.w dl, rotdpx * ref. point

move.w dl, rotdpy

move. w dl,rotdpz

move.w #0,hyangle * initial rotation angle

move.w #0,hzangle

move.w #0,hxangle

move. w #0,ywplus

move. w #0,xwplus

move -w #0,zwplus

rts

265
ST 3D Graphics Abacus Software

* Rotation around the rot. ref. point about all three axes *

move.w hxangle,xangle ★ rotate the world around the


move. w hyangle,yangle

move. w hzangle,zangle

move.w rotdpx,dO ★ rotation ref. point


move.w rotdpy,dl
move.w rotdpz,d2

move. w dO,xoffs * add for inverse transformation


move.w dl,yoffs

move.w d2,zoffs

neg. w dO

neg. w dl

neg.w d2

move.w dO,offx * subtract for tranformation


move.w dl,offy

move. w d2,offz

jsr matinit ★ initialize matrix


jsr zrotate ★ rotate 'matrix' about Z-axis
jsr yrotate * rotate 'matrix' about Y-axis
jsr xrotate * then rotate about X-axis
jsr rotate A multiply point with matrix
rts

*********************************************************************
* Set the limit of display window for the Cohen-Sutherland clip *

* algorithm built into the draw-line algorithm *

* The limits are freely selectable by the user which makes the *

* draw-line algorithm very flexible. *


*********************************************************************

move. w #0,clipxule

move.w #0,clipyule

move. w picturex,dl
Isl. w #1, dl * times two
subq.w #1, dl ■k
minus one equals
move. w dl,clipxlri * 639 for monochrome

move.w picturey,dl

lsl. w #l,dl ★ times two minus one equals


subq.w #1, dl * 399 for monochrome

266
Abacus Software ST 3D Graphics

move.w dl,clipylri

rt s

* Transfer object data into the world system .

makewrld: move.1 trldatx,al * create the world system through

move. 1 trldaty,a2

move.1 #rldatz,a3

move. 1 #wrldx,a4 * copying the point coordinates

move. 1 #wrldy,a5 * into the world system

move . 1 #wrldz,a6

move. w rlnummark,dO

ext. 1 dO

subq. 1 #l,dO

makewll: move.w (al) +, <a4) +

move.w (a2) +, (a5) +

move.w (a3)+,(a6)+

dbra dO,makewll

move.w rlnumline,dO * Number of lines

ext. 1 dO

subq.1 #1, dO

move.1 #rllin,al

move. 1 twlinxy,a2

move. 1 (al) +, (a2) + * Copy lines into world Line


makewl2:

dbra d0,makewl2 * array

move.1 worldpla,aO * Adress of surface definition

move.1 #wplane,al * of the body,

move.w rlnumsurf,dO * Number of surfaces on the body

ext. 1 dO * as counter

subq.1 #1, dO

move. w (aO) +,dl * All lines in this surface,


makewld:

move. w dl, (al) + * and of course the number of

ext. 1 dl * surfaces copied to world surface

subq. 1 #1, dl * array

move . 1 (aO)+,(al)+ * copy every line of this surface


makewl4:
dbra dl, makew!4 * to the world array

267
ST 3D Graphics Abacus Software

dbra d0,makewl3 * until all surfaces are completed


rts

wrldset: move. 1 #wrldx,datx * Pass variables for


move.1 #wrldy,daty * the rotation routine
move.1 #wrldz,datz
move.1 #viewx, pointx
move.1 #viewy,pointy
move -1 fviewz,pointz
move.1 Iwlinxy,linxy
move.w picturex,xO * Coordinate source for the
move.w picturey,yO * screen system
move.w proz,zobs * projection center
move.w rlzl,dist * position of projection plane
move.1 tscreenx,xplot
move.1 iscreeny,yplot
move, w hnumline,numline
move, w hnummark,nummark
move.w hnumsurf,numsurf
rts

268
Abacus Software ST 3D Graphics

*********************************************************************

* Enter visible surface into the vplane array


*********************************************************************

hideit:
numsurf, dO * Number of surfaces as counter
move . w
ext. 1 dO
subq.1 #1* dO
#viewx,al * point coordinates stored here
move.1
move . 1 #viewy,a2
move.1 ffviewz, a3
move.1 #wplane,aO ★ here is information for every

move. 1 #vplane,a5 ★ surface

move.w #0,surfcount * counts the known visible surfaces

move.1 #pladress,a6 * Address of the surface storage

visible: move.w (aO),dl ★ start with first surface, number

ext. 1 dl * of points in this surface in Dl

move.w 2(aO),d2 * Offset of first point of this surface

4(aO),.d3 * Offset of second point


move.w
move. w 8(aO),d4 ★ Offset of third point

subq.w #1, d2 * Subtract one from current point offset

#1, d3 ★ for access to point srray


subq. w
subq.w #l,d4
lsl. w #lf d2 ★ then multiply by two

lsl. w #l,d3
lsl. w #l,d4 * and finally access the current

move. w (al,d3.w),d6 * point coordinates


cmp. w (al,d4.w),d6 ★ comparison recognizes two points
bne doitl ★ with some coordinates which can occur

move. w (a2,d3.w) ,d6 ★ during construction of rotation


cmp. w (a2,d4.w),d6 * bodies. If two
bne doitl ★ points where all point coordinates

move.w (a3,d4.w),d6 ★ (x,y,z) match, the program selects

cmp.w <a3,d3.w),d6 * a third point to determine the two


bne doitl ★ vectors

move . w 12 (aO) , d4
subq.w #l,d4
lsl. w #1, d4

269
ST 3D Graphics Abacus Software

doit1:

move.w (al,d3.w) ,d5 * here the two vectors which lie in


move.w d5, kx * surface plane are detemined by
*
* subtraction
sub. w (al, d2.w),d5 * of coordinates from two points of
move.w d5, px * points in this surface
move.w (a2,d3.w),d5

move.w d5, ky * the direction coordinates of the


sub. w (a2,d2.w),d5 * vector are stored in the variable;
move.w d5,py * qx,qy,qz and px,py,pz

move. w (a3, d3 . w) , d5

move.w d5, kz

sub. w (a3,d2.w),d5

move.w d5,pz

move.w (al,d4.w),d5 * calculation of vector Q


sub. w <al,d2.w),d5
move.w (a2,d4.w),d6

sub. w (a2,d2.w),d6

move. w {a3, d4.w),d7

sub. w (a3, d2.w),d7

move. w d5, dl * qx
move.w d6,d2 * qy
move. w d7,d3 * qz

mu Is py,d3 * calculation of the cross product

mu Is pZ/ d2 * of the vector perpendicular to



* the surface

sub.w d2,d3

move.w d3, rx

muls pz, dl

mu Is px, d7

sub. w d7, dl * the direction coordinates of

* the vector
move.w dl, ry * which is perpendicular to the
muls px, d6 * surface area stored temporarily
muls py,d5 * rx,ry,rz
sub. w d5,d6

move.w d6, rz

move.w prox,dl * The projection center is used as

sub.w kx, dl * the comparison point for the

270
Abacus Software ST 3D Graphics

move.w proy,d2 ★ visibility of a surface, which is

Jcy,d2 ★ adequate for this viewing


sub. w

proz,d3 * situation. One can also use


move. w

sub.w kz, d3 * the observation ref. point

rx, dl * as the comparison point.


mu Is

ry,d2 * Now follows the comparison of the


mu Is

rz, d3 * vector R and the vector from


muls

dl, d2 * one point on the surface to the


add. 1

add. 1 d2,d3 * projection center by creating the

bmi dosight ★ scalar product of the two vectors

* the surface is visible, otherwise continue with next surface.

move.w (aO),dl * Number of lines in surface

ext.1 dl
lsl.l #2,dl * Number of lines times 4 = space for lines

addq.l #2,dl * plus 2 bytes for the number of lines

add.1 dl,aO * add to surface array, for access to

sight1: dbra dO,visible * next surface. If all surfaces

bra hideend * completed, go to end.

dosight: move.w <aO),dl * Number of lines in this surface

ext.l dl * multiplied by two gives result of

move.l dl,d2
lsl.l #l,dl * number of words to be transmitted

move.l a0,a4
addq.l #2,a4 * Access to first line of surface

move.w #0,zsurf * Erase addition storage

sight2: move.l (a4)+,d6 * first line of surface

swap d6 * first point in lower half of DO

subq.w #l,d6 * adapt Index

lsl. w #1,d6 * adapt Operand size (2-byte)

move.w (a3,d6.w),d6 * Z-coordinate of this point

add.w d6,zsurf * add all Z-Coordinates

dbra d2,sight2 * until all lines have been processed

271
ST 3D Graphics Abacus Software

move. w zsurf,d6 ★ Divide sum of all Z-coordinates of

ext. 1 d6 * this surface by the number of lines in

lsr. 1 #2,d6 * the surface. Surfaces created by

ext .1 d6 * rotation always have four lines

move. 1 d6, (a6) + * store middle Z-coordinates

move.1 aO, <a6)+ * followed by address of surface

sight3: move.w (aO)+, (a5) + * transmit the number of lines

dbra dl,sight3 * and the individual lines

addq. w #1,surfcount * add one to the number of surfaces

bra sight1 * and work on next one

hideend: rts

* Draw all surfaces contained in vplane *

surfdraw: * Draws the number of surfaces passed

move.1 xplot,a4 ★ in surfcount whose descriptions

move.1 yplot,a5

move.1 #vplane,a 6 ★ were entered by hideit in the array

move.w surfcount,dO * at address vplane

ext. 1 dO

subq.1 #l,dO ★ if there are no surfaces in the arr.

bmi surfend * then end.

surflopl: move.w (a6) +,dl * Number of lines in this surface

ext. 1 dl * as counter of lines to be drawn.

subq.1 #1, dl

surflop2: move.1 (a6)+,d5 ★ first line of this surface

subq.w #1, d5 ★ Access to screen array where

lsl. w #1, d5 ★ screen coordinates of points are.

move. w 0 (a4,d5.w),d2

move . w 0 (a5,d5.w),d3 ★ extract points

swap d5 * pass routine.

272
Abacus Software ST 3D Graphics

subq. w #1, d5

lsl. w #1, d5

move.w 0 <a4, d5 . w) , a2 * second point belonging to

move.w 0(a5,d5.w),a3 * line


jsr drawl * draw line, until all lines in this

dbra dl,surflop2 * surface are drawn and repeat

dbra dO,surflopl * until all surfaces are drawn.

surfend: rts * finally return.

* Set parameters of this rotation body *

rlset:

move. 1 #rlxdat,rotxdat * Pass parameters of this

move. 1 #rlydat,rotydat * rotation body to routine

move. 1 frlzdat,rotzdat * for generating the

move.1 #rldatx,rotdatx
move.1 #rldaty,rotdaty * rotation body

move.1 #rldatz,rotdatz
move . 1 rotdatx,datx * Array addresses of points

move.1 rotdaty,daty
move. 1 rotdatz,datz
move.w rlnumro,numro * Number of desired rotatations.

move.w rlnumpt,numpt * Number of points to be rotated

move.1 #rllin,linxy * Address of line array

move.1 #rlplane,worldpla * Address of surface array

rts

************** •k


* and create rotation body
*

rotstart: move.w numpt,dO * Rotate the def line

lsl. w #1, dO * numro+1 times about the Y-axis

ext .1 dO

move. 1 dO,plusrot * Storage space for one line

273
ST 3D Graphics Abacus Software

move.w numpt,nummark * Number of points

move.1 rotdatx,pointx * rotate to here

move. 1 rotdaty,pointy

move. 1 rotdatz,pointz

move. w #0, yangle

move.w #360,dO * 360 / numro = angle increment

divs numro,dO * per rotation

move.w dO, plusagle * store

move. w numro,dO * numro +1 times

ext. 1 dO

rloopl: move.1 dO, loopc * as loop counter

move.1 rotxdat,datx

move.1 rotydat,daty

move.1 rotzdat,datz

jsr yrot * rotate

move.1 pointx,dl * add offset

add. 1 plusrot,dl

move.1 dl,pointx

move.1 pointy,dl

add. 1 plusrot,dl

move.1 dl,pointy

move. 1 pointz,dl

add. 1 plusrot,dl

move.1 dl,pointz

move. w yangle,d7

add.w plusagle,d7

move.w d7,yangle

move.1 loopc,dO

dbra dO,rloopl

move.w rlnumro,numro

move.w r1numpt,numpt

jsr rotlin * Create line array

jsr rotsurf * Create surface array

rts

rotlin:

move. w #l,d7

move.w numro,d4 * Number of rotations

ext. 1 d4

subq.1 #l,d4

274
Abacus Software ST 3D Graphics

move. w numpt,dl * Number of points in the def. line

subq.w #1, dl * both as counter

lsl. w #2, dl * times two

ext. 1 dl

move. 1 dl,plusrot

rotlopl: move. w numpt,d5 * Number of points minus one

ext .1 d5 * repeat, last line

subq.1 #2, d5 * connects the points (n-l,n)

move.1 linxy,al

move. w d7,d6

rotlop2: move.w d6, <al) + * the first line connects the

addq.w #l,d6 * points (1,2) then (2,3) etc.

move.w d6,(al)+

dbra d5,rotlop2

move.1 linxy,dl

add. 1 plusrot,dl

move.1 dl,linxy

move.w numpt,dO

add. w d0,d7

dbra d4,rotlopl

move. w numpt,d7

move. w d7,deltal

lsl. w #2, d7

ext. 1 d7

move. 1 d7,plusrot

move. w #1, d6

move . w numpt,dO

ext. 1 dO

subq.1 #l,dO

rotlop3: move. w numro,dl

ext. 1 dl

subq.1 #1, dl

move.w d6,d5

rotlop4 : move.w d5, (al) + * now generate the cross connections

add. w deltal,d5 * which connect the individual lines

move.w d5, (al) + * created by rotation

dbra dl,rotlop4

275
ST 3D Graphics Abacus Software

add. w #l,d6

dbra dO,rotlop3

move.w numro,dl

add. w #l,dl

mu Is nummark,dl

move.w dl,rlnummark

move.w numpt,dl

mu Is numro,dl

move.w numpt,d2

subq.w #1, d2

mu Is numro,d2

add. w dl, d2

move.w d2,rlnumline * Number of lines stored

rts

rotsurf: move. w numro,dO * create surfaces of the

ext .1 dO * rotation body

subq.1 #l,dO

move. w numpt,d7 * Number of points minus one

ext .1 d7 * repeat

subq.1 #2,d7

move.1 d7,plusrot

move.1 worldpla,aO * Address of surface array

move.w #1, dl

move.w numpt,d2 * Number of points

addq.w #1, d2

276
Abacus Software ST 3D Graphics

rotf11: move.l plusrot,d7 * Offset

rotf12 : move.w dl,d4

move.w d2,d5

addq.w #l,d4

addq.w #l,d5
move.w #4, (aO> + * Number of lines / surfaces

move.w dl,(aO)+ * the first surface is

move.w d4,(a0) + * created here

move. w d4, (aO) +

move.w d5, (aO) +

move . w d5, (aO) +

move . w d2, (aO) +

move . w d2, (aO) +

move . w dl, (aO) +

addq.w #l,dl

addq.w #l,d2

dbra d7,rotfl2

addq.w #l,dl

addq.w #l,d2

dbra dO,rotfll

move.w numpt,dl

subq.w #1, dl

muls numro,dl

move.w dl,rlnumsurf

rts

277
ST 3D Graphics Abacus Software

************************************************************************

* Transfer the world parameters and the variables to the link file *
************************************************************************

wrld2set: move.1 #wrldx,datx * transfer the world parameters

move. 1 #wrldy,daty * and the variables to the

move.1 #wrldz,datz * routines in the link file

move.1 #viewx,pointx

move.1 #viewy,pointy

move.1 Iviewz,pointz

move.1 #wlinxy,linxy

move.w picturex,xO

move.w picturey,yO

move.w proz,zobs

move.w rlzl,dist

move.1 #screenx, xplot

move.1 #screeny,yplot

move.w rlnumline,numline

move.w rlnummark,nummark

move -w rlnumsurf,numsurf

rts

**********************************************************************

* Sort all surfaces entered in pladress *

sortit: move.1 #pladress,aO

move.w surfcount,d7

ext. 1 d7 •* for i = 2 to n corresponds to

subq.1 #2, d7 * number of runs

bmi serror ★ for i = 1 to n-1 because of

move.1 #1, dl * different array structure

sortmain: move.1 dl, d2

subq.1 #l,d2 ★ j = i -1

move.1 dl, d3 * i

lsl.l #3,d3

(aO, d3.1),d5 * Comparison value x = a[i]


move.1

move.1 4(a0,d3.1),d6 * address of the surface

move.1 d5,space * a [0] = x = a[-l] in this

move.1 d6,space+4 * array

278
Abacus Software ST 3D Graphics

sortlopl: move.1 d2,d4 * j


lsl.l #3, d4 * j times 8 for access to array

cmp. 1 (a0,d4.1) ,d5 * Z-coordinate of surface

bge sortwl * while x < a[j] do

move.1 (a0,d4.1),8(a0,d4 .1) * a [ j + 1] = a [ j]

move.1 4(a0,d4.1),12(aO, d4.1) * Address of surface array

subq.1 #1, d2 * j = j-1

bra sortlopl

sortwl: move. 1 d5,8(aO,d4.1) * a[j+1] = x

move. 1 d6,12(a0,d4.1) * Pass address also

addq. 1 #1/ dl * i - i + 1

dbra d7,sortmain * Until all surfaces have been sorted

sortend: rts

serror: rts * On error simply return

********************************************************************
* paintall draws all surfaces in world array wplane independent of *

* their visibility; all surface addresses and middle Z-coordinates *

* are entered into the pladress array. *


********«**************+********************************************

paintall:

move.w numsurf,dO * Number of surfaces

ext.l dO

subq.l #l,dO * if no surface present

bmi pquit * then terminate

move.1 #viewz,a3

move.l #wplane,aO

move.w #0,surfcount * Surface counter for surfdraw

move.l #pladress,a6 * surfaces are entered here

svisible:

move.w (aO),dl * all surfaces are visible

ext.1 dl

subq.l #l,dl

move.w #0,zsurf * middle Z-coordinate

move.l a0,a4

addq.l #2,a4

279
ST 3D Graphics Abacus Software

ssightbl: move.1 (a4) +,d2 * first line of surface

swap d2

subq.w #1» d2

lsl. w #1/ d2

ddoit1: move.w (a3,d2.w),d6 * add all Z-coordinates of this

add. w d6,zsurf * surface

dbra dl,ssightbl

move.w zsurf,d6

ext. 1 d6 * then divide by four, shifting

lsr. 1 #2,d6 * is possible only with rotation

ext. 1 d6 * bodies since each surface has

move.1 d6, <a6)+ * exactly four lines otherwise divide

move.1 aO, (a6) + * by number of lines

addq.w #1,surfcount * increment surface counter for surfdraw

move.w (aO),dl * AO still points to number of lines

ext. 1 dl * in this surface

lsl.l #2, dl * Number of lines times four (1 long)

addq.1 #2, dl * 2 bytes for the number of lines

add. 1 dl, aO * AO points to next surface

dbra dO, svisible

move.w numsurf,surfcount

jsr paintit * Fill surfaces in pladress

pquit: rt s

paintit: jsr setclip * GEM clipping routine for filled area

jsr sortit * Sort surfaces according to Z-coordinates

move.w #1, dO * Write mode to replace

jsr filmode

jsr filform * frame filled surface

jsr filcolor * Shading color is one

move. w #2, dO * Fill style

jsr filstyle

move. 1 xplot,al * Address of screen coordinates

move. 1 yplot,a2

move. w surfcount,dl * Number of surface to be filled

ext .1 d7 * as counter

subq.1 #1, d? * access last surface in array

move.1 dl, dO * multiply by eight

280
Abacus Software ST 3D Graphics

lsl.l #3, dO

move. 1 tpladress,aO * here are largest Z-coordinate

move. 1 (aO, dO . 1) , d5 * surfaces

move.1 #0, dl

move.1 (aO,dl.1), d6 * first surface in array

neg. 1 d6 * smallest Z-coordinate

add. 1 d6,d5 * subtract from one another

paintl: move.1 d5,d0

move.1 (aO,dl.l),d2 * first surface in array

add. 1 d6,d2 * plus smallest Z-coordinate

lsl.l #3,d2 * times eight, eight different

divs dO, d2 * shading patterns, divide by

neg.w d2 * difference leave out last

add. w #6,d2 * pattern.

bpl paint2

move.w #1, d2

paint2: move.w d2, dO * set fill index

jsr filindex

move.1 #ptsin,a3 * enter points here

move.1 4 (aO, dl. 1) ,a6 * Address of surface

move.w (a6)+,d4 * Number of lines

addq.w #1»<34 * first point counted twice

move.w d4,contrl+2

move.1 (a6) + ,d3 * first line of surface

swap d3

subq.w #l,d3
lsl. w #l,d3

move.w <al,d3.w>,(a3)+ * transfer to ptsin array

move.w (a2,d3.w),(a3) + * pass Y-coordinate

swap d3

sub. w #1» d3

lsl. w #l»d3
move.w (al,d3.w),(a3)+ * transmit next point

move.w (a2, d3 . w) , (a3) + * transmit Y-coordinate

subq.w #3, d4 * already two points transmitted

ext .1 d4 * and one because of dbra

paint3: move.1 (a6)+,d3 * next line

subq.w #1< d3

lsl. w #l,d3

move.w (al, d3.w), (a3) + * X-coordinate

move.w (a2, d3 . w), (a3) + * Y-coordinate

281
ST 3D Graphics Abacus Software

dbra d4,paint3 * until all points in Ptsin-Array

move.w #9,contrl * then call the fill area function

move. w #0,contrl+6

move.w grhandle,contrl+12

movem.1 dO-d2/aO-a2,-(a7)

jsr vdi

movem.1 (a7)+,d0-d2/a0-a2

#8, dl * work on next surface in pladress


add. 1

dbra d7,paintl

rts

* VDI clipping, only needed when VDI functions are used, *

* for surface filling.

setclip: move.w #129,contrl

move.w #2,contrl+2

move.w #1,contrl+6

move.w grhandle,contrl+12

move.w #1,intin

move.w clipxule,ptsin

move.w clipyule,ptsin+2

move.w clipxlri,ptsin+4

move. w clipylri,ptsin+6

jsr vdi

rts

*******************************************************************
* this subroutine allows coordinates to entered with the Mouse

* The maximum number of points is in the variable maxpoint, and

* is limited only by storage space


*******************************************************************

inpmous:

jsr switch

move. w #5, dO

jsr setform

move.w #1, dO * set input mode to mouse-reguest

move.w #1, dl * wait for mouse input which is

jsr setmode * terminated by key activation and

282
Abacus Software ST 3D Graphics

jsr coord * mouse clicking

move. 1 #0,adressx

move.w #5,d0 * set polymarker to diagonal cross

jsr marktype

mouslopl: jsr mouspos * For unknown reasons function must

move. w picturex,d2 * be called twice to work once.

add. w #15,d2

move.w picturey,d3

sub.w #40,d3

jsr mouspos

cmp .b #$20,dl * wait until the left mouse button is

bne mouslopl * pressed

move.1 #rlxdat,a4 * arrays in which input

move.1 #rlydat,a5 * coordinates are entered; enough

move.1 #rlzdat,a6 * storage must have been reserved

move.w d2,newx * store mouse X and Y positions

move.w d3,newy

jsr saveit * and pass line array

move.w newx,d2

move.w newy,d3

jsr markit * set a polymarker

add. 1 #1,adressx * increment counter

mousl: nop

move.w newx,altx

move.w newy,alty

mouslop2: move.w altx,d2 * pass old position of the mouse

move. w alty,d3

jsr mouspos * and call again

jsr mouspos

cmp .b #$21,dl * if right mouse button, then

beq mousend * end of mouse input

cmp.b #$20,dl

bne mouslop2

move.w d2,newx * store mouse coordinates

move.w d3,newy

jsr saveit * store in array

283
ST 3D Graphics Abacus Software

move.w newx,d2 * draw line from (n-1) n'th point

move.w newy,d3

move.w altx,a2

move. w alty,a3

jsr drawl

move.w newx,d2

move.w newy,d3

jsr markit * and mark point with marker

add. 1 #1,adressx * increment counter

move.1 adressx,d7

cmp. 1 maxpoint,d7 * and compare with maximum point count

bne mousl * if not equal, continue

move.1 adressx,dO

move.w dO,rlnumpt * Number of points input

rts

mousend: move.w d2,newx

move.w d3,newy

move.w altx,a2

move.w alty,a3

jsr markit

jsr drawl * draw last line

jsr wait * and wait for keypress

jsr saveit

add. 1 #1,adressx * also add last point

move. 1 adressx, dO

move.w dO,rlnumpt * now store total number of points

rts * finally back to caller

***********************************************************************

* Wait for mouse input, returns also on keyboard input *


***********************************************************************

mouspos: move.w #28,contrl * Mouse input, the desired coordinates

move.w #1,contrl+2 * where the mouse should appear,

move.w #0,contrl+6 * are passed in

284
Abacus Software ST 3D Graphics

move. w grhandle,contrl+12

move . w d2,ptsin * D2 ,and D3

move. w d3,ptsin+2

jsr vdi

move.w intout,dl * the result - coordinates

move. w ptsout,d2 * are also returned in D2 and

move . w ptsout+2,d3 * D3

rts

* Set the polymarker type *

marktype: move.w #18,contrl * determines the appearance of

move. w #0,contrl+2 * the polymarker, desired

move. w #1,contrl+6 * type is passed in DO

move.w grhandle,contrl+12

move.w dO,intin

jsr vdi

rts

* Set a polymarker, number in contrl+2 *

markit: move. w #7,contrl

move. w #1,contrl+2 * Number of points.

move.w #0,contrl+6 * case only one

move.w grhandle,contrl+12

move. w d2,ptsin

move.w d3,ptsin+2

movem.1 d0-d2/a0-a2,-(a7)

jsr vdi * draw marker

movem.1 (a7)+,dO-d2/aO-a2

rts

285
ST 3D Graphics Abacus Software

A**********************************************************************
* Set input mode
***********************************************************************

setmode: move. w #33,contrl * Set input mode

move.w #0,contrl+2

move.w #2,contrl+6

move.w grhandle,contrl+12

move.w dO,intin

move.w dl,intin+2 * Parameters in DO

jsr vdi

rts

* Store coordinates entered in point array *

sub.w picturex,d2 * Pass mouse coordinates to

move. w d2,(a4)+ * rotation line array, with

sub.w picturey,d3 * adaptation to coordinate system

neg.w d3

move.w d3, (a5) +

move.w #0, (a6) +

rts
Abacus Software ST 3D Graphics

**********************************************************************
* Display and describe the same screen page *
**********************************************************************

move.w #-l,-(a7) * Display of Display Page,

move.1 physbase. “ (a7) * where drawing is made

move.1 physbase. - (a7)

move.w #5,-(a7)

trap #14

add. 1 #12,a7

rts

* Change the mouse form *

setform: move. w #78, contrl * Set mouse form desired shape

move.w #1, contrl+2

move.w #1,contrl+4 * passed in DO

move.w #1,contrl+6

move.w #0,contrl+8

move.w dO,intin

jsr aes

rts

***********************************************************************
* Drawing a coordinate system for mouse input *
***********************************************************************

jsr clwork. * draw coordinate

move.w #0, d2 * for mouse input

move.w picturey,d3

move.w picturex,d5

lsl. w #1, d5

move.w d5,a2

287
ST 3D Graphics Abacus Software

move.w d3,a3

jsr drawl

move.w picturex,d2

move.w #0, d3

move. w d2,a2

move.w picturey,d5

lsl. w #1, d5

move.w d5,a3

jsr drawl

rts

******************************************************************
* remove all characters present in the keyboard buffer *
******************************************************************

clearbuf: move - w #$b,-(a7) * Gemdos fnct. character in Buffer ?

trap #1

addq.1 #2,a7

tst. w dO * if yes, get character

beq clearnd * if no, terminate

move.w #1,-U7> * Gemdos fnct. CONIN

trap #1 * repeat, until all characters

addq.1 #2,a7 * are removed from the buffer

bra clearbuf

clearnd: rts

* Definition of a custom mouse form - Data in mousforl *

mousform: move.1 #15,dO * permits the definition of

move.1 Imousforl,al * new mouse form, data is

move.w #111,contrl * in mousforl

move.w #0,contrl+2

move.w #37,contrl+6

move.w grhandle,contrl+12

move.w #8,intin

move.w #8,intin+2

288
Abacus Software ST 3D Graphics

move.w #1,intin+4

move.w #0,intin+6

move.w #1,intin+8

move.1 tintin+10,a5

move.1 (al)+,(a5)+

dbra dO,forlop

jsr vdi

rts

.even

* Beginning of the Variable area *


* *

* Data area for the rotation body *

.bss

numro: .ds. w 1

numpt: .ds. w 1

rotxdat: .ds.l 1

rotydat: .ds.l 1

rotzdat: .ds.l 1

rotdatx: .ds.l 1

rotdaty: .ds.l 1

rotdatz: .ds.l 1

rlnumline .ds. w 1

rlnummark .ds. w 1

rlnumsurf . ds. w 1

plusagle: .ds. w 1

rldatx: . ds. w 1600

rldaty: • ds. w 1600

rldatz: . ds . w 1600

289
ST 3D Graphics Abacus Software

rllin: .ds.l 3200 * 4-Bytes for every line e


rlplane: .ds.l 6600

• data

rlxdat: .dc.w 0,40,50,50,20,30,20,30,70,80,80,0


.dc.w 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

rlydat: .dc.w 100,100,80,60,40,30,30,-70,-80,-90,-100,-100


.dc.w 0,0,0,0,0,0, 0,0, 0,0, 0,0,0,0, 0,0, 0,0, 0,0

rlzdat: .dc.w 0,0,0,0,0,0,0,0,0,0,0,0


.dc.w 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

rlnumpt: .dc.w 12
rlnumro: .dc.w 8

* *

* *

* Definition of the house *


* *

************************************************************************

.data

housdatx: .dc.w -30,30,30,-30,30,-30,-30,30,0,0,-10,-10,10,10


.dc.w 30,30,30,30,30,30,30,30,30,30,30,30

housdaty: .dc.w 30,30,-30,-30,30,30,-30,-30,70,70,-30,0,0,-30


.dc.w 20,20,0,0,20,20,0,0
.dc.w -10,-10,-30,-30

housdatz: .dc.w 60,60,60,60,-60,-60,-60,-60,60,-60,60,60,60,60

.dc.w 40,10,10,40,-10,-40,-40,-10
.dc.w 0,-20,-20,0

290
Abacus Software ST 3D Graphics

houslin: .dc.w 1,2,2,3,3,4,4,1,2,5,5,8,8,3,8,7,7,6,6,5,6,1,7,4


.dc.w 9,10,1,9,9,2,5,10,6,10,11,12,12,13,13,14
.dc.w 15,16,16,17,17,18,18,15,19,20,20,21,21,22,22,19
.dc.w 23,24,24,25,25,26,26,23

***********************************************************************
* Here is the definition of the surfaces belonging to the house *
***********************************************************************

houspla: .dc.w 4,1,2,2,3,3,4,4,1,4,2,5,5,8,8,3,3,2

.dc.w 4,5,6,6,7,7,8,8,5,4,7,6,6,1,1,4,4,7
.dc.w 4,4,3,3,8,8,7,7,4,4,2,9,9,10,10,5,5,2
.dc.w 4,10,9, 9,1,1, 6,6,10,3,1, 9, 9, 2,2,1
.dc.w 3,5,10,10,6,6,5,4,11,12,12,13,13,14,14,11
.dc.w 4,15,16,16,17,17,18,18,15,4,19,20,20,21,21,22,22,19

.dc.w 4,23,24,24,25,25,26,26,23

hnummark.: .dc.w 26 * Number of corner points in the house


hnumline: .dc.w 32 * Number of Lines in the House
hnumsurf: .dc.w 13 * Number of Surfaces in the House

hxangle: .dc.w 0 * Rotation angle of House about the X-axis


hyangle: .dc.w 0 * " " " Y-axis
hzangle: .dc.w 0 * ” " " Z-Axis

xwplus: .dc.w 0 * Angle increment about the X-axis

ywplus: .dc.w 0 ★ Angle increment about the Y-axis

zwplus: .dc.w 0 ★ Angle increment about the Z-axis

picturex: .dc.w 0 ★ Definition of zero point of screen

picturey: .dc.w 0 ★ entered by getreso

rotdpx: .dc.w 0
rotdpy: .dc.w 0
rotdpz: .dc.w 0

291
ST 3D Graphics Abacus Software

rlzl: . dc. w 0
normz: . dc. w 1500

.bss

plusrot: . ds. 1 1
first: . ds. w 1
second: • ds. w 1
deltal: .ds. w 1
T3

•—i

worldpla: 1
w

. data

plag: • dc.b 1
.even

.bss

diffz: • ds .w 1

dx: .ds. w 1
dy: .ds. w 1
dz: • ds. w 1

wrldx: .ds. w 1600 * World coordinate array


wrldy: . ds. w 1600
wrldz: . ds. w 1600

viewx: . ds. w 1600 * View coordinate array


viewy: . ds. w 1600
viewz: . ds. w 1600

screenx: . ds. W 1600 * Screen coordinate array


screeny: .ds .w 1600

292
Abacus Software ST 3D Graphics

wlinxy: .ds. 1 3200 * Line array

wplane: .ds. 1 6600 * Surface array

vplane: .ds. 1 6600 * Surface array of visible surface

space: .ds. 1 2
pladress: .ds. 1 3000 * Surface array

surfcount : .ds.w 1

numsurf: .ds .w 1

zcount: .ds. 1 1 * Sum of all Z-coord.

zsurf: .ds.w 1 * Individual Z-coord. of surface

sx: .ds.w 1
sy: .ds.w 1
sz: .ds.w 1

px: .ds.w 1
py: .ds.w 1
pz: .ds.w 1

rx: .ds.w 1
ry: • ds.w 1
rz: • ds.w 1

qx: .ds.w 1
qy: .ds. w 1
qz: .ds.w 1

kx: .ds.w 1
ky: .ds.w 1
kz : .ds.w 1

293
ST 3D Graphics Abacus Software

Hr ************** + ********************★***
.data
• even

maxpoint: .dc. 1 25
mousx: .dc.w 0
mousy: • dc.w 0
mousbut: .dc.w 0
kybdstat: .dc.w 0

altx: .dc.w 0
alty: .dc.w 0
newx: .dc.w 0
newy: .dc.w 0

adressx: .dc. 1 1
.data

prox: .dc.w 0 * Coordinates of the projections


proy: .dc.w 0 * center on the positive
proz: .dc.w 1500 * Z-axis

.data

of fx: .dc.w 0 * Transformation during rotation


of fy: .dc.w 0 * to point (offx,offy,offz]
of f z: • dc.w 0

xoffs: • dc.w 0 * Inverse transformation to point


yoffs: • dc.w 0 * [xoff,yoffs,zoffs]
zoffs: .dc.w 0

textl: .dc.b 27,'Y',56,61,' <c) Uwe Braun 1985 ',0


text2: .dc .b 27,'E',27,'p',13,' Input ',' 4-Pts ' , ' -Pts
.dc.b ' 12-Pts '
.dc.b ' 18-Pts 24-Pts 45-Pts 60-Pts
.dc.b ' POS ',' Quit' ,27,'q',0
text3: .dc.b 13, 10,' F-l F-2 F-3 F-4
.dc.b ' F-5 F-6 F-7 F-8
.dc.b ' F-9 F-10 't13,0

294
Abacus Software ST 3D Graphics

mousforl: .dc.w

.dc.w %11111111111111U

.dc.w

• dc.w

.dc.w

.dc.w

.dc.w

.dc.w

.dc.w

.dc.w

.dc.w

.dc.w %iiiiiiiimniii
.dc.w

.dc.w

.dc.w

.dc.w

mousdatl: .dc.w %ooooooinuooooo


.dc.w %0000110000010000
.dc.w %0001001111001000
• dc.w %0010010000100100
.dc.w %0100100000010010
.dc.w %1001000000010100
.dc.w %1001000000010100
.dc.w %1000100000100101
.dc.w %0100011111001001
.dc.w %0010000000010010
.dc.w %0001111111100101
.dc.w %0011111111111001

.dc.w

.dc.w %0111111111111111

• dc.w %1111111111111110

.dc.w %0000000000000000

.bss

loopc: .ds.l 1

. end

295
ST 3D Graphics Abacus Software

4.5.1 Description of the new subroutines:

menu: Display a small menu and wait for a function key


to be pressed. (F10 returns to Desktop
immediately)

testhide: Test if H or P key pressed, branch accordingly to


dohide or dopaint.

dohide: Calculate visible surfaces and draw. Then check if


filling is required, if not, wait for <Retum>.

dopaint: Fill all surfaces of rotation body and wait for


<Retum>.

paint a 11: Enter all surfaces of rotation body into surf addr
array, sort and fill.

inpmous: Enter up to 25 points (maxpoint) with the left


mouse button. These points are entered through
save it into the point array of the rotation body.
Enough space must be reserved in the point array
by entering zeros here. For entering fewer than
maxpoint points end input with the right mouse
button.

mouspos: Wait for mouse input, also returns after keypress.


Therefore it checks to see which event occured.
This GEM function must be called twice for
unknown reasons in order to wait once for an input.

marktype: Determines the appearance of the marker set by


function polymarker.

markit: Call the function polymarker to set a marker.

setmode: Set input mode.

save it: Stores the coordinates entered with the mouse in


the point array of the definition line for the rotation
body.

296
Abacus Software ST 3D Graphics

saveit: Stores the coordinates entered with the mouse in


the point array of the definition line for the rotation
body.

switch: Switches the logical page to the displayed page so


that the page being drawn is the page being
displayed. Otherwise the filling will not be seen
and the hardcopy with <Altemate> and <Help>
will not function either.

setf orm: Change mouse form.

coord: Draw a coordinate system.

mousform: Permits the definition of a user-defined mouse


form whose data follows after mousforl. This
new mouse form appears after F9 is pressed and
looks like a snail. You can change the data in the
program according to your own taste.

297
ST 3D Graphics Abacus Software

4.6 Handling several objects

All subroutines discussed up to now really allow the simultaneous display


of several objects. The only changes required are limited to the
construction of an object definition block for each object, as well as an
exchange of the makewrld routine. Let us consider the concrete
example of the house from hidel. s and the changes that would be
required, to construct a world system with two houses using the existing
definition.

The most promising approach appears to be to copy all of the house


definitions (housdatx, houslin, houspla, etc.) into the
corresponding arrays of the world system several times. The point
coordinate arrays housdatx etc. do not present problems. They can be
simply appended to the world system. A world system containing two
houses would contain 52 points. More difficult is the creation of the
world line array since the line definition of the individual objects, here
the two houses, always starts at point offset one; the first line of every
object starts at point 1 and runs to point 2 for the houses. If the world
point array is extended by another house, it becomes apparent that the
first line of the second house starts at point 27 of the world point array
and runs to point 28, since the first 26 points belong to the first object
The necessary procedure is simple: when constructing the line array from
the individual object line arrays, add the total number of points in the first
object to each line definition of the second object. Analagously, with
three objects the sum of the points of the first two objects is added to the
line definitions of the third object during construction of the world line
array.

The principle of the construction of the world line array is also used
during construction of the world surface array, for example the first
surface definition of the second house within the world surface array:

4,27,28,28,29,29,30,30,27

Furthermore, the total number of all points, lines and surfaces must be
calculated and recorded.

If we start with a realistic world description, the positions of the objects


in this world system can change continuously-recall the airplane and the
tanker truck from Section 4.1. As a consequence of this, it is necessary to

298
Abacus Software ST 3D Graphics

objects belonging to it. The recreation is limited to the coordinate arrays


however, since only they change. The line and surface arrays are not
affected by the position change. The line and surface world arrays are
created only once at the beginning of the program. The coordinate array
is created twice in every main loop pass.

Now to the object definition block, which contains all the information
describing the individual object. The idea was to extend the available
world system by one object through addition of the definition block to the
existing blocks and incrementing the "object counter." Here for
clarification is an object definition block in which N is replaced with the
index of the current object:

objectN:
objNxda: .dc.l Address of the X-coordinate
array of the obj.
objNyda: .dc.l Address of the Y-coordinate
array of the obj.
objNzda: .dc.l Address of the Z-coordinate
array of the obj.
objNlin: .dc.l Address of the object line
array
objNpla: .dc.l Address of the object surface
array
objmrk: .dc.w Number of points in this
object
objNali: .dc.w Number of lines in this
object
objpln: .dc.w Number of surfaces on this
object
objNxO: .dc.w X-position of object in world
system
objNyO: .dc.w Y-position of object in world
system
objNzO: .dc.w Z-position of object in world
system
objNxw: .dc.w Rotation angle of obj. about
X-axis
objNyw: .dc.w Rotation angle about Y-axis
objNzw: .dc.w Rotation angle aboutZ-axis

299
ST 3D Graphics Abacus Software

The angles and also the position in the world system relate to the
"rotationally neutral” point of the current object, the origin of the object
definition coordinate system. As a whole, the block consists of 38 bytes,
but can easily be extended with additional information, such as scale
factors, etc. If two identical objects are to be created, you write two
object definition blocks this is important since the creation routine finds
the next block using the distance of 38 bytes between two blocks. Since
two identical objects are to be created, the addresses for the two blocks
are the same and only the position of the objects and perhaps the rotation
angles differ. After the definition has been completed, the total number of
objects, in this case two, is placed in the variable numob j: and now the
total world system can be generated with a single subroutine call.

Examine the definition blocks in the following listing of multil. s, in


which four identical objects are already created through concatenation of
four object definition blocks. Naturally, you are not limited to the
creation of identical objects. You can define a new object, such as a
church, and enter its definition array address and desired position into an
object block. Three houses and your church will be displayed.

Description of the new subroutines in multil. s:

The main loop is easily changed. Here the total number of the desired
objects, four, is passed and the new subroutines new_wrld and
new_mark are called.

new_wrld: The one-time call to the subroutine first creates the


entire world system consisting of coordinate, line
and surface arrays with corresponding parameter
passing of the lines created, etc. Furthermore, the
world parameters are passed to the variables of the
link file. This assignment was previously
performed by subroutine wrldset.

new_mark: Change the position of an object in the world


system this subroutine recreates the total
coordinate system with the aid of the modified
parameters and at the same time passes the world
parameters to the variables of the link file.

300
Abacus Software ST 3D Graphics

new_it:, surf_lin:, surf_arr:


These three subroutines are called by new_wrld
and new , mark and handle the actual creation of
the world system from the individual object
definitions.

change: Change the object parameters of the individual


objects. For simplification, modification is passed
to all four objects.

General comments on the program:

Beside being able to display multiple objects, this program offers another
novelty: two successive transformations of the same object. First, the four
objects are "set" into the world system with new_mark: after they have
first been rotated about three axes. After all objects have been ‘’rotated" in
the world system you can, through control with the keyboard, rotate the
entire system consisting of the four houses around a point in the world
system, or move the projection plane similar to previous programs. The
four houses of the system rotate around different axes of their
"rotationally neutral” points at various places in the world system. The
display on the screen occurs after the removal of the hidden lines with the
familiar subroutine hide it:, which is used on the complete world array
so that the four houses are not created through mirroring or something
similar, but the hidden surfaces of all four objects are calculated in real¬
time. The hide it algorithm of this program does not recognize
covering by other visible surfaces so that a house covered by other houses
will be drawn.

Control keys are again the cursor, help and undo keys, as well as the / * -
+ keys on the keypad.

The speed is quite impressive. One enhancement, besides the addition of


user-defined objects, is the ability to change an object’s parameters in the
subroutine change: by keyboard input, for example, and to change the
position of single objects in the system.

301
ST 3D Graphics Abacus Software

* multil.s 22.2.1986 *

* Multiple objects, four houses *

* with hidden line algorithm *


* *

. globl main,xoffs,yoffs,zoffs,offx,offy,offz

.globl viewx,viewy,viewz

.globl wlinxy,mouse off,setrotdp,inp_chan,pointrot

.globl wrldx,wrldy,wrldz,gnummark,gnumline,gnumpla

.globl viewx,viewy,viewz,wplane

.globl new it,new_wrld,obj2mrk, obj2pln

.text

* The program starts here—called by link-file *

main:

jsr apinit * Announce program

jsr grafhand * Get screen handle

jsr openwork * Announce screen

jsr mouse off * Switch off mouse

jsr getreso * Screen resolution

jsr setcocli * set Cohen-Sutherland clip.

mainl: jsr clearbuf

move.w #4,gnumobj * announce four objects

jsr pageup

jsr clwork * Screen resolution

jsr setrotdp * initialize obs. ref. point.

jsr pagedown * Display logical screen page

jsr clwork

jsr inp_chan * Input and change world parameters

jsr change * Change object parameters

jsr new_wrld ★ create lines and surfaces

302
Abacus Software ST 3D Graphics

mainlopl:

jsr pointrot * rotate around observ. ref. point

jsr pers * Perspective transformation

jsr hideit * calculate hidden surface

jsr surfdraw ★ and draw

jsr pageup ★ Display physical screenpage

jsr change * change object parameters and

jsr new mark ★ calculate new coordinates

jsr inp chan * Input new parameters

jsr clwork * erase page not displayed

jsr pointrot ★ Rotate around rot. ref. point

jsr pers ★ Transform new points

jsr hideit * Calculate hidden surfaces

jsr surfdraw * and draw them

jsr pagedown * Display this logical page

jsr change * Change object parameters

jsr new mark ★ Calculate new point coordinates

jsr inp chan * Input and change parameters

jsr clwork * erase physical page

jmp mainlopl ★ to main loop

move.1 physbase. logbase

jsr pageup * switch to normal display page

rts * back to link file, and end

A**********************************************************************

* Create the point coordinates of the world array with the *

* information from the object parameter block (objectl) *


***********************************************************************

new mark: move. w #0,offx


o

4-1
4-1

move.w
0

>■

move. w #0,offz

jsr new it

move.1 #viewx,pointx

move.1 #viewy,pointy

move.1 #viewz,pointz

move . 1 #wrldx,datx

move. 1 #wrldy,daty

mo ve. 1 #wrldz,datz

move.1 #wlinxy,linxy

303
ST 3D Graphics Abacus Software

move.w gnummark, nummark

move.w gnumline,numline

move.w gnumpla,numsurf

rts

* Change the object parameter, in this case the rotation angle

* in the object parameter block, which is then taken into account *

* when calculating point coordinates with rnew_mark


**********************************************************************

change : move. w objlyw,dO

add. w #4, dO

cmp.w #360,dO

bit changwl

sub.w #360,dO

changwl:

move.w dO,objlyw

move.w dO,obj2xw

move.w dO,obj3zw

move.w dO,obj4xw

move.w dO,obj4yw

move.w dO,obj4zw

rts

***********************************************************************
* Set all world parameters for the link file variables and

* create the point, line, and surface arrays of the world system

***********************************************************************

new wrld: move. w #0, dO

move.w dO,offx

move.w dO,offy

move.w dO,offz

move.w proz,zobs

move.w #0,dist * Location of projection plane

move.1 iscreenx,xplot * Address of screen array

move.1 #screeny,yplot

move.w picturex,xO * Screen center

move.w picturey,yO
Abacus Software ST 3D Graphics

jsr new it * Pass coordinates

jsr surf lin * Pass lines

jsr surf arr * Pass surfaces of

move. w gnummark,nummark * all objects to world system

move.w gnumline,numline * Total number of corners, lines

move.w gnumpla,numsurf ★ and surfaces of world system

move.1 #wrldx,datx * Pass parameters of world system to

move.1 #wrldy,daty * link file variables

move.1 #wrldz,datz

move. 1 #viewx,pointx

move.1 #viewy,pointy

move.1 #viewz,pointz

move.1 #wlinxy,linxy

rts

* Subroutine for creating the world system coordinate array *

•ft**********************************************************************

new it: move.1 #0,mark_it * Pointer in wrldx,wrldy,wrldz

move.w gnumobj,dO * Total number of objects

ext .1 dO * as counter

subq.1 #1, dO * Address of first object parameter

move.1 #objectl, aO * block after AO.

new_lopl: move.1 (aO),datx * Objectldatx, daty,datz, pass

move.1 4(aO),daty * addresses of point array of

move.1 8(aO),datz * first object.

move.1 mark_it,d7 * Offset in point array


* times two bytes per entry
lsl.l #1, d7

move.1 d7,d6

add. 1 #wrldx,d7 * equals offset in world system array

move.1 d7,pointx * Target of transmission

move.1 d6,d7

add. 1 #wrldy,d7

move.1 d7,pointy

add. 1 twrldz,d6

move.1 d6,pointz * Array of world coordinates

move.w 20(aO),nummark * Number of corners in the object

move.w 2 6{aO),xoffs * X-offset

move.w 28(aO),yoffs * Y-offset in the world system

move.w 30(aO),zoffs * Z-offset

305
ST 3D Graphics Abacus Software

move.w 32(aO),xangle * Rotation angle of object around

move.w 34(aO),yangle * the three coordinate axes

move.w 36 (aO),zangle

movem.1 d0-d7/a0-a6, -(a?) * Save registers

jsr matinit * Initialize rotation matrix


* rotate first about the Z-axis, then
jsr zrotate

jsr yrotate * around Y-axis, and finally

jsr xrotate * around the X-axis (matrix).

jsr rotate * rotate in world coordinate system

movem.1 <a7)+,d0-d7/a0-a6

move.w 20 (aO),d7 ★ Number of corners in the object

ext. 1 d7

d7,mark_it ★ as offset in point array for


add. 1

add. 1 #38,aO * the next object

dbra dO, new_lopl * repeat, until all objects

move.1 mark_it,d7 ★ have been pased. After end in

move.w d7,gnummark * mark it the total number of


it points in the world system
rts

***********************************************************************
* Pass all lines to world system, one-time call at *

* program start since nothing changes in the lines


***********************************************************************

surf lin: move.w gnumobj,dO * Total of all objects

ext. 1 d°

subq.1 #1, dO * as counter

move.1 #objectl,aO * Address of first Object par. blk.

move. 1 #0,linpntr * Pointer to line array

move. w #0,mark it * Pointer to point array

sflnlopl: move. 1 linpntr,d7 * Line pointer times four,

lsl.l #2,d7 * one lines requires four

move. 1 d7,d6 * bytes.

add. 1 twlinxy,d7 * Start address of line array, add

move.1 d7, a2 * to line pointer

move. 1 12(aO),al * Address of line array of object

move.w 22(aO),dl * Number of lines in this object

ext. 1 dl

lsl.l #1, dl * Number of lines times two equals

subq.1 #1, dl * Loop counter for word transmission

306
Abacus Software ST 3D Graphics

sflnlop2: move.w (al)+ ,d7 * first point of first line

add. w mark it,d7 * add the offsets of current


move . w d7,(a2)+ * objects, and store in world line;
dbra dl,sflnlop2 * array, until all lines of this
* object

move.w 20(aO),d7 * Number of corners of last object

add. w d7,mark it * add to corner pointer

move. w 22 (aO) ,d7 ★ Number of lines

ext. 1 d7

add. 1 d7,linpntr * Total number of lines

add. 1 #38,aO * Object offset, distance to next

dbra dO,sflnlopl ★ object. When all objects are


* completed

move. 1 linpntr,d7 ★ then store total number of lines

move.w d7,gnumline * in the world system and

rts * back

s surface array of the


r*********************'

move.w #0,mark_it * Create the array of surfaces

move.1 #0,plapntr

move.w #0,gnumpla * Counter of surfaces

move.w gnumobj,dO * Number of objects

ext. 1 dO * as loop counter

subq.1 #l,d0

move.1 #objectl,aO * Address of first object param. blk

move.1 plapntr,d7 * Pointer to surface array

add. 1 #wplane,d7 * World surface array

move.1 d7,a2

move. w 24 (aO),dl * Number of surfaces on this object

ext .1 dl * as loop counter

subq.1 #1, dl

move.1 16 (aO),al * Address of surface array of the object

move.w (al),d2 * Number of lines of this surface

ext. 1 d2

lsl.l #1, d2 * times four (one line = four bytes)

307
ST 3D Graphics Abacus Software

move.1 d2,d6

lsl.l #l,d6 * complete the mult. by 4

addq.1 #2,d6 * plus 2 bytes for number of lines

subq.1 #l,d2 * counter

add. 1 d6,plapntr

move. w (al)+, (a2) + * Number of lines in this surface

sfarlop3: move.w <al)+ , d7 * From the object surface array

add. w mark_it,d7 * Add point offset of the object

move.w d7, <a2) + * to world surface array

dbra d2,sfarlop3 * until all lines of this surface

dbra dl,sfarlop2 * until all surfaces on this object

move.w 20 (aO),d7 * Number of corners

add.w d7,mark it * add to point offset

move.w 24 (aO),d7

add.w d7,gnumpla * add to total number

add. 1 #38,aO * Object offset to next object

dbra dO, sfarlopl * until all objects of the world

rts * and return

* Input and change parameters


********************************************************************

inp_chan: jsr inkey * Read keyboard, key code in

cmp.b #'D',dO

bne inpwait

jsr scrdmp * make hardcopy

inpwait: swap dO * DO , test if

cmp.b #$4d,d0 * Cursor-right

bne inpl

addq.w #1,ywplus * if yes, add one to Y-angle

bra inpendl * increment and continue

inpl: cmp.b #$4b,dO * Cursor-left, if yes then

bne inp2 * subtract one from Y-angle

subq.w #1,ywplus * increment

bra inpendl

308
Abacus Software ST 3D Graphics

inp2: cmp .b #$50,dO * Cursor-down, if yes then


bne inp3

addq. w #1,xwplus * add one to X-angle increment


bra inpendl

inp3 : cmp .b #$48,dO * Cursor-up

bne inp3a

subq.w #1,xwplus * subtract one

bra inpendl

inp3a: cmp .b #$61,d0 * Undo key

bne inp3b

subq.w #1,zwplus

bra inpendl

inp3b: cmp.b #$62,dO * Help key

bne inp4

addq ..w #1,zwplus

bra inpendl

inp4 : cmp.b #$4e,dO * plus key on the keypad

bne inp5 * if yes, subtract 25 from position

sub. w #25,dist * projection plane (Z-coordinate)

bra inpendl

inp5: cmp.b #$4a,d0 * minus key on the keypad

bne inp6 *

add. w #25,dist * if yes, add 25

bra inpendl

inp6: cmp.b #$66,dO * times key on keypad

bne inp7 * if yes, then subtract 15 from the


★ rotation

sub.w #15,rotdpz * point Z-coordinate

bra inpendl * Make change

inp7: cmp.b #$65,dO * Division key on keypad

bne inp8

add. w #15,rotdpz * add 15

bra inpendl

inp8:

309
ST 3D Graphics Abacus Software

inplO: crop -b #$44,dO * F10 pressed ?

bne inpendl

addq.1 #4, a 7 * if yes, jump to

bra mainend * new input

inpendl: move. w hyangle,dl * Rotation angle about Y-axis

add. w ywplus,dl * add increment

cmp. w #360,dl * when larger than 360, then subtract

bge inpend2

cmp. w #-360,dl * if smaller then 360, then

ble inpend3 * add 360

bra inpend4

inpend2: sub. w #360,dl

bra inpend4

inpend3: add.w #360,dl

inpend4: move.w dl,hyangle

move.w hxangle,dl * proceed in the same manner with

add.w xwplus,dl * Rotation angle about the X-axis

cmp. w #360,dl

bge inpend5

cmp.w #-360,dl

ble inpend6

bra inpendl

inpend5: sub. w #360,dl

bra inpend7

inpend6: add.w #360,dl

inpend7 : move.w dl,hxangle

move.w hzangle,dl

add.w zwplus, dl

cmp.w #360,dl

bge inpend8

cmp.w #-360,dl

ble inpend9

bra inpendlO

inpend8 : sub. w #360,dl

bra inpendlO

inpend9: add.w #360,dl

310
Abacus Software ST 3D Graphics

inpendlO: move.w dl,hzangle

rts

******************************************************************
* Determine the current screen resolution *
******************************************************************

getreso: move. w #4,-(a7)

. trap #14

addq.1 #2,a7

cmp. w #2, dO

bne getrl

move.w #320, picturex * Monochrome monitor

move.w #200,picturey

bra getrend

getrl: cmp. w #l,d0

bne getr2

move.w #320,picturex * medium resolution (640*200)

move.w #100,picturey

bra getrend

getr2: move.w #160,picturex * low resolution (320*200)

move.w #100,picturey

getrend: rts

* Hardcopy of screen, called by inp_chan *

scrdmp: move.w #20,- (a7)

trap #14

addq.1 #2,a7

jsr clearbuf

rts

311
ST 3D Graphics Abacus Software

A**********************************************************************

* Initialize the rotation reference point to [0,0,0] *


***********************************************************************

setrotdp: move.w #0, dl * set the initial rotation

move.w dl,rotdpx * reference point

move.w dl,rotdpy

move. w dl,rotdpz

move.w #Q,hyangle * initial rotation angle

move. w #0,hzangle

move.w #0,hxangle

move.w #0,ywplus

move. w #0,xwplus

move.w #0,zwplus

rts

*******************************************************************,

* Rotation around the rot. ref. point around all three axes
*******************************************************************>

pointrot: move.w hxangle,xangle * rotate the world around

move.w hyangle,yangle

move.w hzangle,zangle

move.w rotdpx,dO * rotation ref. point

move.w rotdpy,dl

move.w rotdpz,d2

move.w dO,xoffs * add for inverse transformation

move.w dl,yoffs

move.w d2,zoffs

neg .w dO

neg. w dl

neg. w d2

move.w dO,offx * subtract for transformation

move.w dl,offy

move.w d2,offz

jsr matinit * matrix initialization

jsr zrotate * rotate 'matrix' aboutZ-axis

jsr yrotate * rotate 'matrix' about Y-axis

jsr xrotate * then rotate around X-axis

jsr rotate * Multiply points with the matrix

312
Abacus Software ST 3D Graphics

*********************************************************************
* Set the limits of screen window for the Cohen-Sutherland *

* clip algorithm built into the draw-line algorithm *

* The limits can be freely selected by the user, which makes the *

* draw-line algorithm very flexible. *


*********************************************************************

setcocli: move.w #0,clipxule

move.w # 0,c1ipyule

move.w picturex,dl

lsl.w #1,dl

subq.w #l,dl

move.w dl,clipxlri

move.w picturey,dl

lsl.w #1,dl

subq.w #l,dl

move.w dl,clipylri

rts

* Entry of visible Surfaces into the vplane array *


*********************************************************************

hideit:
move.w numsurf,dO * Number of surfaces as counter

ext.1 dO

subq.l #l,dO
move.1 #viewx,al * The point coordinates are stored

move.l #viewy,a2 * here

move.l #viewz,a3

move.l #wplane,aC * here is the information for

move.l #vplane,a5 * every surface

move.w #0,surfcount * counts the known visible surfaces.

visible: move.w (aO),dl * start with first surface. Number of

ext.l dl * points on this surface in Dl.

move.w 2(a0),d2 * Offset of first point on this surface

move.w 4(a0),d3 * Offset of second point

move.w 8(a0),d4 * Offset of third point

subq.w #l,d2 * subtract one for access to point array

subq.w #1,d3 * from current point offset.

313
ST 3D Graphics Abacus Software

subq.w #l,d4

lsl. w #l,d2 ★ continue to multiply with two

lsl. w #l,d3

lsl. w #1, d4 * and then access current

move.w (al,d3.w),dS ★ point coordinates

cmp.w <al,d4.w),d6 * Comparison recognizes two points

bne doitl * with matching coordinates, which can

move.w (a2,d3.w),d6 * occur during construction of rotation

cmp. w (a2,d4.w),d6 * bodies. When two identical points

bne doitl * are found, the program

move.w (a3,d4.w),d6 * selects a third point for determination

cmp. w (a3,d3.w),d6 * of the two vectors.

bne doitl

move.w 12(aO),d4

subq.w #l,d4

lsl. w #1, d4

doitl:

move.w (al,d3.w),d5 * here the two vectors which lie in the

move.w d5, kx * surface plane are determined through

sub. w (al,d2.w),d5 * subtraction of the coordinates from

move.w d5,px * two points of the surface

move.w (a2,d3.w),d5

move.w d5,ky * The direction coordinates of the

sub.w U2,d2.w) ,d5 * vectors are stored in the variables

move.w d5,py * qx,qy,qz and px,py,pz.

move.w (a3,d3.w),d5

move.w d5, kz

sub. w (a3,d2.w),d5

move.w d5, pz

move.w (al,d4.w),d5 * Calculate vector Q

sub. w (al,d2.w),d5

move.w (a2,d4.w) ,d6

sub. w (a2,d2.w),d6

move.w (a3,d4.w),d7

sub. w (a3,d2.w),d7

move. w d5,dl * qx

move . w d6,d2 * qy
move. w d7,d3 * qz

314
Abacus Software ST 3D Graphics

mu Is py,d3 * Calculate of the cross product

mu Is pz, d2 * of the vector perpendicular

sub. w d2,d3 * to the current surface

move.w d3, rx

mu Is pz, dl

mu Is px,d7

sub. w d7, dl * the direction coordinates of the

vector

move.w dl, ry * standing vertically to the surface

mu Is px, d6 * are temporarily stored in rx,ry,rz

mu Is py,d5

sub. w d5,d6

move.w d6, rz

move.w prox,dl * The projection center serves as

the comparison

sub. w kx,dl * point for the visibility of a surface.

move.w proy,d2 * which is acceptable for the viewing

sub. w ky,d2 * situation chosen here. One can also

move.w proz,d3 * use the observation ref. point as

sub. w kz, d3 * comparison point.

mu Is rx, dl * Now follows the comparison of vector

muls ry, d2 * R and the vector from one point of the

mu Is rz,d3 * surface to the projection center

add.l dl, d2 * by creation of the scalar product


* of the two vectors.
add.l d2,d3

bmi dosight

* the surface is visible, otherwise continue with next surface.

move.w (aO),dl * Number of lines of the surface

ext.l dl
lsl.l #2,dl * Number of lines times 4 = space for Lines

addq.l #2,dl * plus 2 bytes for the number of lines.

add.l dl,aO * add to surface array for access

sightl : dbra dO,visible * to next surface. If all surfaces

bra hideend * are completed, then end.


ST 3D Graphics Abacus Software

dosight: move. w (aO),dl * Number of lines in this surface,

ext .1 dl * multiplied by two equals the

move.1 dl, d2

lsl.l #1,dl * number of words to be passed

move. 1 aO, a4

addq.1 #2,a4 * Access to first line of the Surface

sight3: move.w (aO)+,(a5)+ * Pass the number of the lines

dbra dl,sight3 * and the individual lines

addq. w #1,surfcount * the number of surfaces plus

bra sight1 * one, and work on next one

hideend: rts

*********************************************************************
* Draw surfaces entered in vplane *
**********************************************************************

surfdraw: * draw surfaces with the count

move.1 xplot,a4 * of surfaces passed in surfcount

move.1 yplot,a5

move. 1 #vplane,a6 * Description in array at address

move.w surfcount,dO * vplane, was entered by routine hideit

ext .1 dO

subq.1 #1, dO * if no surface was entered in array,

bmi surfend * then end.

surflopl: move.w (a6)+,dl * Number of lines on this surface

ext. 1 dl * as counter of lines to be drawn.

subq.1 #l,dl

surflop2: move.1 (a6)+,d5 * first line of this surface

subq.w #l,d5 * Access to screen array, which contains

lsl.w #1,d5 * display coordinates of the

move.w 0 (a4,d5.w),d2 * points.

move.w 0 (a5,d5.w),d3 * extract points, pass from

swap d5 * the routine.

316
Abacus Software ST 3D Graphics

subq. w #l,d5

lsl. w #l,d5

move. w 0 (a4, d5 . w) , a2 * second point belonging to the the line

move. w 0 (a5, d5 . w) , a3

jsr drawl * draw line, until all lines of this

dbra dl,surflop2 * surface have been drawn and repeat

dbra dO,surflopl * until all surface have been drawn.

rt s * finally return.

* Display and description of the same screen page *

switch: move.w #-l,-<a7) * show display page in which

move. 1 physbase,- (a7) * drawing is being made

move.1 physbase,- (a7)

move. w #5,-<a7)

trap #14

add. 1 #12,a7

rts

******************************************************************

* remove all characters present in the keyboard buffer *

clearbuf: move.w #$b,-(a7) ★ Gemdos function, character in buffer

trap #1

addq.1 #2,a7

tst. w dO * if yes, get character

beq clearnd ★ if no, terminate

move. w #1,-(a7) ★ Gemdos function CONIN

trap #1 ★ repeat until all characters have

addq.1 #2,a7 ★ been removed from the buffer

bra clearbuf

clearnd: rts

. even

317
ST 3D Graphics Abacus Software

• data

housdatx: .dc. w -30,30,30,-30,30,-30,-30,30,0,0,-10,-10,10,10


.dc.w 30,30,30,30,30,30,30,30,30,30,30,30

housdaty: .dc. w 30,30,-30,-30,30,30,-30,-30,70,70,-30,0,0,-30


.dc.w 20,20,0,0,20,20,0,0
.dc.w -10,-10,-30,-30

housdatz: .dc.w 60, 60, 60, 60,-60,-60,-60,-60, 60,-60,60,60, 60, 60


• dc.w 40,10,10,40,-10,-40,-40,-10
.dc.w 0,-20,-20,0

houslin: .dc.w 1,2,2,3, 3, 4,4,1,2,5,5, 8, 8,3, 8, 7,7,6, 6, 5, 6,1,7,4


.dc.w 9,10,1,9,9,2,5,10,6,10,11,12,12,13,13,14
.dc.w 15, 16, 16, 17, 17,18,18, 15, 19,20,20,21,21,22,22,19
.dc.w 23,24,24,25,25,26,26,23

318
Abacus Software ST 3D Graphics

* here is the definition of the surfaces belonging to the house *

houspla: .dc.w 4,1, 2,2,3,3, 4,4,1, 4,2,5, 5,8, 8,3, 3,2

.dc.w 4,5,6,6,7,7,8,8,5,4,7,6,6,1,1,4,4,7

.dc.w 4,4,3,3,8,8,7,7,4,4,2,9,9,10,10,5,5,2

.dc.w 4,10,9,9,1,1,6,6,10,3,1,9,9,2,2,1

.dc.w 3,5,10,10,6,6,5,4,11,12,12,13,13,14,14,11

.dc.w 4,15,16,16,17,17,18,18,15,4,19,20,20,21,21,22,22,19

.dc.w 4,23,24,24,25,25,26,26,23

hnummark: .dc.w 26 * Number of corner■ points of the house

hnumline: .dc.w 32 ■* Number of lines of the house

hnumpla: .dc.w 13 ★ Number of surfaces of the house

hxangle: .dc.w 0 * Rotation angle of house about X-axis

hyangle: .dc.w 0 * H II II Y-axis

hzangle: .dc.w 0 * 11 II M Z-axis

xwplus: .dc.w 0 * Angle increment about X-axis

ywplus: • dc.w 0 * Angle increment about Y-axis

zwplus: .dc.w 0 * Angle increment about Z-axis

picturex: .dc.w 0 * Definition of zero point on the screen

picturey: .dc.w 0 ★ entered by getreso

rotdpx: .dc.w 0

rotdpy: .dc.w 0

rotdpz: . dc. w 0

rlzl: .dc.w 0

normz: .dc.w 1500

.bss

319
ST 3D Graphics Abacus Software

plusrot: .ds. 1 1

first: . ds. w 1

second: .ds. w 1

deltal: .ds. w 1

worldpla: .ds. 1 1

.data

plag: .dc .b 1

. even

.bss

diffz: • ds .w 1

dx: .ds .w 1

dy: .ds .w 1

dz: .ds. w 1

wrldx: .ds. w 1600 * world coordinate array

wrldy: • ds .w 1600

wrldz: .ds .w 1600

viewx: • ds. w 1600 * view coordinate array

viewy: .ds .w 1600

viewz: • ds. w 1600

screenx: .ds. w 1600 * screen soordinate array

screeny: .ds. w 1600

wlinxy: .ds. 1 3200 * line array

wplane: .ds. 1 6600 * surface array

vplane: .ds. 1 6600 * surface array

320
Abacus Software ST 3D Graphics

space: . ds. 1 2

pladress: .ds. 1 3000 ** surface array

surfcount: . ds. w 1

numsurf: . ds. w 1

zcount: . ds. 1 1 *

zsurf: . ds . w 1 *

*****************************

.data

gnumobj: .dc.w 2

gnummark: . dc. w 0

gnumline: .dc.w 0

gnumpla: .dc.w 0

mark_it: .dc. 1 0

linpntr: .dc. 1 0

plapntr: .dc. 1 0

objectl:

objlxda: .dc. 1 housdatx

objlyda: .dc. 1 housdaty

objlzda: .dc. 1 housdatz

objllin: .dc. 1 houslin

objlpla: .dc. 1 houspla

objlmrk: .dc.w 26

objlali: .dc.w 32

objlpln: .dc.w 13

objlxO: .dc.w 150

objlyO: .dc.w 100

objIzO: .dc.w 0

objlxw: .dc.w 20

objlyw: .dc.w 0

objlzw: .dc.w 0

object2:

obj2xda: • dc. 1 housdatx

obj2yda: .dc. 1 housdaty

obj2zda: .dc. 1 housdatz

321
ST 3D Graphics Abacus Software

obj21in: .dc.1 houslin

obj2pla: .dc.l houspla

obj2mrk: .dc.w 26

obj2ali: .dc.w 32

obj2pln: .dc.w 13

obj2x0: .dc.w -150

obj2y0: .dc.w 100

obj2z0: .dc.w 0

obj2xw: .dc.w 0

obj2yw: .dc.w 20

obj2zw: .dc.w 0

object3:

obj3xda: .dc.l housdatx

obj3yda: .dc.l housdaty

obj3zda: .dc.l housdatz

obj31in: .dc.l houslin

obj3pla: .dc.l houspla

obj3mrk: .dc.w 26

obj3ali: .dc.w 32

obj3pln: .dc.w 13

obj3x0: .dc.w -150

obj3y0: .dc.w -100

obj 3z0: • dc.w 0

obj3xw: .dc.w 0

obj3yw: .dc.w 20

obj3zw: .dc.w 0

object4 :

obj4xda: .dc.l housdatx

obj4yda: .dc.l housdaty

obj4zda: .dc.l housdatz

obj41in: .dc.l houslin

obj4pla: .dc.l houspla

obj4mrk: .dc.w 26

obj4ali: .dc.w 32

obj4pln: .dc.w 13

obj 4x0: .dc.w 150

obj4y0: .dc.w -100

obj4z0: .dc.w 0

322
Abacus Software ST 3D Graphics

obj 4xw: .dc. w 0

obj 4yw: .dc.w 0

obj4zw: .dc. w 0

.bss

sx: .ds.w 1

sy: .ds.w 1

sz: .ds.w 1

px: .ds.w 1

py: .ds.w 1

pz: .ds.w 1

rx: .ds.w 1
ry: .ds.w 1
rz: -ds.w 1

qx: .ds.w 1

qy: .ds.w 1

qz: .ds.w 1

kx: .ds.w 1
ky: .ds.w 1

kz: • ds.w 1

**********************

.data

• even

maxpoint: .dc. 1 25

mousx: .dc.w 0

mousy: .dc. w 0

mousbut: .dc. w 0

kybdstat: .dc.w 0

altx: .dc.w 0

alty: .dc.w 0

newx: .dc.w 0

newy: .dc.w 0

323
ST 3D Graphics Abacus Software

addrssx: .dc. 1 1
.data

prox: .dc.w 0 * Coordinates of Projection

proy: . dc. w 0 * Center on the positive

proz: • dc.w 1500 * Z-axis

• data

of fx: .dc. w 0 * transformation during Rotation

of fy: . dc. w 0 * to Point [offx,offy,offz]

of f z: .dc.w 0

xoffs: .dc. w 0 * Inverse transformation to point

yoffs: .dc.w 0 * [xoff,yoffs,zoffs]

zoffs: .dc.w 0

.bss

loopc: .ds. 1 1

.end

Desk File View Options


A:\ D:\ F:\3DU0RK.DIRS
253882 butes used 1 1442236 butes used In 129 itens, ■ 333956 bytes used In
X PRINTERS BASIC PRG 138344 11-21 HOUSE1 PRG
8 TUTORIAL BASIC RSC 4648 11-21 HOUSE1 S
C FRY BASIC MRK 346 11-26 MAIK1 PRG
COMV TTP BASIC1 BAR 14801 11-26 KAIN1 S
NLlfl PRO HAIN1C0 PRG
OUTPUT PRG MAIN1C0 S
OPEN APPLICATION
SPLIT TTP MEKU1 PRG
STANDARD PRT Nane: BATCH .TTP HENU1 S
TEXTPRO PRG Paraneters; . MULTI1 PRG
TUTORIAL TXT asllnk grllnkl miltll)- MULTI1 S
XTTUTORI TOC PAINT1 PRG
I OK 1 I Cancel PRINT1 S
ROTATE1 PRG
R0TATE1 S

10 a?
min \trrm ■n'v.-r rrrwt j-amattfil jS33HEflB33i
a? ■BSffiSMI

324
Abacus Software ST 3D Graphics

5. Suggestions for additional development

One application of this program module for manipulating three-


dimensional objects that will occur to almost everyone is a flight
simulator. The last program can in fact be used as a basis for a flight
simulator. We are missing the description of the position of the airplane
in the world system as well as a modified point rot routine. The
modified point rot routine, after rotation around the reference point,
should not transform all of the world coordinates back to the old
coordinate origin, which occurred in the old point rot routine by
adding the reference point coordinates after the rotation. Furthermore,
houses do not change position in the world system of a flight simulator
and for an airport other structures must be developed (hangar, tower). In
addition, fields, forests, and landing strips can be simulated with simple
rectangular surfaces.

The position of the airplane, or to be exact, the center of its cockpit


windshield, in the the reference point in the world system for all
transformations to follow, especially that of the creation of the view
system. For simulation of airplane movement, the reference point must be
manipulated with keyboard input. This input must affect both the point
coordinates as well as the orientation of the plane in space. The
orientation of the airplane in space- is described with the three angles
(hxangle,hyangle,hzangle) so that even adventurous flight
situations (spins) can be simulated. For adjustment of the world system to
the airplane system the following operations are required:

1. Move the coordinate origin of the world system to the


cockpit center by subtracting the cockpit windshield
center-coordinates from all point coordinates.

2. Rotate of the displaced world system about the three


rotation angles which describe the position of the airplane
in relation to its three axes.

327
ST 3D Graphics Abacus Software

3. Remove hidden surfaces with hideit, noting that the


reference point for the calculation of the vector S through
point [0,0,0], the cockpit center-point (coordinate origin of
the view system) is chosen and not the projection center,
which of course can also be freely selected in this
observation model. From the endpoint of vector S the
direct result: all objects outside the cockpit window are, if
they satisfy the criteria for visibility, visible.

4. Projection on the screen through the perspective transform


routine.

After the observed world is displayed, the parameters such as the position
of the airplane in the world system or the position of other objects in the
world system, such as a second airplane, can be changed. Now the
procedure described above is called again and this cycle repeated
continually.

328
Abacus Software ST 3D Graphics

5.1 Light and Shadow

To enhance the program module to correctly define a light source, as in


section 2.8, it is necessary to have the vectors L, i.e. the vector, which
points from each surface to the observation reference point as well as the
vector N, which points from the light source to the current surface, as unit
vectors of length one. One should divide the vector coordinates (x, y, z)
by the root of the sum of its squares V(x2+y2 + z2). Furthermore, the
data structure of the objects must be changed since you want to shade the
surfaces according to the light intensity and not according to their Z-
coordinates. It is possible to enter the intensity of every surface in the
extended surfaddr array and give each surface an individual shading
pattern, either through comparison of the light source vectors or
completely at random.

329
ST 3D Graphics Abacus Software

5.2 Animated Cartoons

In principle even this application has already been realized in program


multi. s. You simply create more objects in a world system and then
changes their position and place in the system continuously. The world
line and surface arrays, as we have seen, need be created only once while
the coordinate array is created with every pass through the main loop.
After the line surface array has been constructed, you have free choice in
the number of displayed objects, i.e. you can define, for example, ten
objects through object definition blocks but at the creation of the comers
you could only actually create and display. One possible application is
moving text where the letters are three dimensional objects. You could
have several letters "fly" together from various directions and assemble
them on the screen into a word. The complete word could then be rotated
around some point. The individual letters could even be constructed with
the mouse.

330
Abacus Software ST 3D Graphics

Appendix A: Number systems

Every number, in any number system, is represented by a sequence of


digits. This sequence may be interrupted by a decimal point. We can
write the following for the digit sequence:

(...a4a3a2aiao.a-ia-2a-3a-4.")b =
+ a4*b4 + a3*b3 + a2*b2 + ai*bl + ao*bO + a-i*b_l + ...

Here the coefficients a-4 to a4 represent the individual digits of the


number and b is the number base. Here is an example of the most
commonly used number system, the decimal system:

(3423.87)10 =
3*103 + 4*102 + 2*101 3*100 + 8*10-1 + 7*10-2 =
3000 + 400 + 20 + 3 + 0.8 + 0.07 = 3423.87

Two number systems often encountered, in computer programming, are


the binary (base 2) and the hexadecimal systems (base 16). Binary uses
only the two numbers 0 and 1 as digits. An example:

1110010010010= 1*212+1*211+1*210+1*27+1*24+1*21=
4096 + 2048 + 1024 + 128 + 16 + 2 = 7314

Numbers in the hexadecimal system with base 16 are generally indicated


by a leading dollar sign ($). For representation of numbers in this format,
the standard ten digits from 0 to 9 are not enough. For this reason the first
six characters of the alphabet are added (A through F). A has the value of
10, and F means 15. It is especially easy to convert between binary and
hexadecimal. Four binary digits (4 bits) are grouped together, starting
from the decimal point, to form one hexadecimal digit.

333
ST 3D Graphics Abacus Software

The unwieldy binary number 1110010010010 becomes the hexadecimal


number $1C92. The conversion into the Decimal system is done in the
same manner as for the binary system. $1C92 means therefore:

1*163 + 12*162 + 9*161 + 2*160 =


1*4096 + 12*256 + 9*16 + 2*1 = 7314

334
Abacus Software ST 3D Graphics

Appendix B: Analytical geometry of planes and space

The cartesian coordinate system is defined as a system of perpendicular


lines in which the horizontal line is designated as the X-axis (abscissa)
and the line perpendicular to it is called the Y-axis (ordinate). The
intersection of the two lines is the origin of the system. Now all points
within the system can be defined unambiguously by specifying their
coordinate values (x,y).

A line in such a system is defined by two points which belong on the line.
All points on the line can be ascertained with the following equation.

y-yi y2:yl
for (x2-xl) <> 0
x-xl x2-xl

In this two point format, the expression (y2-yl)/(x2-xl) gives the slope m
of the straight line, which simultaneously represents the tangent of the
angle between the line and the X-axis (phi).

335
ST 3D Graphics Abacus Software

y2-yi
x2-xt

Figure B.l: Line in the plane

With the definition of the slope m as well as the axis intersection a, the
intersection of the line with the Y-axis, we get what is called the normal
form of the straight line equation.
y = m*x + a

With this equation you can calculate all points on the line by introducing
various X values into the above equation, knowing the slope m and axis
intersection a.

For the middle-point of a straight line which connects two points (PI,
P2), we can easily calculate the coordinates of this segment:

xl+x2 yl+y2
Xm = Ym =
"T* 2

336
Abacus Software ST 3D Graphics

The two equations above are used in the Cohen-Sutherland clipping


algorithm.

The geometry of a plane is just a special case of the geometry of space


and therefore the same laws apply to a straight line in space as to a
straight line in a plane, i.e. two points are also sufficient to define a point
in space. One difference from the plane is the Z-axis which, if one leaves
the X and Y axis unchanged, can point in different directions. Depending
on the direction used, this system is called a right-hand or left-hand
system. They differ therefore only in the orientation of the Z-axis.

Figure B.2

An easy way to distinguish between the right- and left-hand systems as


well as all operations within the system is possible with the aid of a screw
(imagine simply a normal screw inside the system). The screw transfers a
rotating motion into a movement along the rotation axis and there are
basically two types of screws: those with left-handed threads and those
with right-handed threads. For a complete system description, we still
need to know how positive angles are measured and for equalization of
both coordinate systems the following definition is agreed upon:

337
ST 3D Graphics Abacus Software

Rotation about the: positive angle is measured:

Z-axis from +X to +Y axis


Y-axis from +Z to +X axis
X-axis from +Y to +Z axis

With the aid of this definition we can say for the system and the screw: If
a screw is placed in such a system (in the direction of a coordinate axis)
and the screw is turned about a positive angle (see above definition), then
the screw moves in the direction of a positive coordinate axis. You can
determine the position axis of a coordinate system through the definition
of the positive angle as well as the selection of the screw, or you can
recognize the type of an existing coordinate system. As an explanation, in
a right-hand system the right-handed screw moves in the direction of a
positive coordinate axis when rotated about a positive angle. On the other
hand, a left handed screw in a left-hand system rotated about a positive
angle will also move in the direction of positive coordinate axis. Since in
our country, screws with right-handed threads are most common, we shall
follow the positive rotations of a right-handed screw in a right-hand
system.

338
Abacus Software ST 3D Graphics

Figure B.3: Screws in a right-hand system

339
ST 3D Graphics Abacus Software

Two points in space or in a plane are sufficient to describe a line. Under


consideration of Z-coordinates the following relationships hold:
y-yl = y2-y 1 z-zl _ z2-zl
x-xl x2-xl x-xl x2-xl

Using a parameter u, which can assume real values between -infinity and
+infinity, all points on a line running through points Pl[xl,yl,zl]
and P2 [x2, y2, z2 ] can be determined. For individual coordinates the
values are:

x = (x2-xl) * u + xl
y = (y2-yi) * u + yl
z = (z2-zl) * u + zl
If we use only u real numbers between 0 and 1, all points on the line
between PI and P2 can be calculated. The line would not run beyond PI
and P2, but would be cut off at the two points. From the lines we get a
vector, which has a definite direction in space. In our example it points
from PI to P2.

A vector is a directional line, the connecting line between two points in a


coordinate system. The coordinates of the vector are calculated by
subtracting the point coordinates. The vector is therefore indicated by the
vector coordinates and its direction. The direction is shown in the
illustration by an arrow. A vector can be moved along its axis without
consequences for the total system, since only the length and direction are
of significance.

340
Abacus Software ST 3D Graphics

The vector S in Figure 6.3.4 is given by its vector coordinates


S [sx, sy, sz] = [x2-xl,y2-yl,z2-zl] and its value, the length
of the distance S, can be determined as follows:
Value S = |S| = V (sx2 + sy2 + sz2)

A unit vector is a vector whose value is one. If you want to generate a


unit vector to a given vector S, a vector which points in the same
direction as S but has a value of one, the vector coordinates of the unit
vector are I [ix,iy,iz]:
sx sy . sz
ix =—— iv — iz =rr:—

Dividing the individual vector coordinates of vector S [sx,sy,sz]by


the length of vector S results in the vector coordinates of the unit vector.

341
ST 3D Graphics Abacus Software

Various operations can be performed on the vectors and those important


for our purposes are:

1. The scalar product (A-B)

2. The cross product (AXB)

B.l Scalar Product

The scalar product is the sum of the products of the individual vector
coordinates and is important to determine angles (phi) between two
vectors (A,B).
A-B = ax*bx+ay*by+az*bz = | A| *|B|*cos(phi)
A-B =V ( (ax2+ay2+az2) * (bx2+by2+bz2) ) *cos (phi)

See also Figure 2.7.5.

B.2 Cross Product

The cross product (AxB), in contrast to the scalar product, is not a real
number but another vector (C). The resultant vector stands perpendicular
to the plane between the vectors A and B and together with them forms a
new coordinate system. The rule of the screw helps us again in the
determining the direction of the resulting vector:

In a right-hand system the result vector (C = [cx,cy,cz]) of the


cross product points in the same direction in which a screw with right-
handed threads would move from A to B when turned. The vectors A, B,
and C form a right-hand system. Similarly for a left-hand system: if one
turns a left-threaded screw from A to B, then C points in the direction in
which^tfie~stxew would move. This connection can be seen easily in
Figure 6.3.5 afad in our program is responsible for the recognition of
visible, surfaces)
Abacus Software ST 3D Graphics

Figure B.5

To determine the result vector C [cx,cy,cz] one proceeds as follows:


A*B = [ ax*bz-az*by, az*bx-ax*bz, ax*by-ay*bx]

343
ST 3D Graphics Abacus Software

Appendix C: Matrix calculations

A matrix (m,n) is a square number system consisting of m by n numbers.

an an an ... am
a2i a22 a23 ... a2n
a3i a32 a33 ... 33n

ami 4m2 Sm3 ... amn

aik where i = 1,2.. .m and k =


---- -HIV A ulllVj Uliu U1V VIVUlUiUJ

aik,a2ic,.-amk form the kth column of the matrix. If the number of columns
is equal to the number of rows (m=n), A is called a square matrix. A few
rules can be stated for matrix calculation.

1. Matrices are designated with uppercase letters (A-Z). The


individual elements of a matrix carry the corresponding
lower case letter (a-z).

2. The element aik is located in the ith row, kth column of


matrix A. i is the row index and k is the column index.

3. The matrix A(m,n) is of the type (m,n) and is defined as a


two-dimensional matrix with m rows and n columns.

4. Matrices with one row and any number of columns, of the


type (l,n), are called row vectors and those of type (n,l)
are called column vectors.

344
Abacus Software ST 3D Graphics

C.l Adding matrices

The addition of matrices is defined only for matrices of the same


dimensions. Here is an example with two (3,3) matrices, A with the
elements aik and the matrix B with the elements bik- During addition, the
sum matrix S is created with elements sik- S=A+B.

1 2 3 1 2 3
A= 4 5 6 B= 4 5 6
7 8 9 7 8 9

2 4 9
C = A+B = 8 10 12
14 16 18

The elements of the sum matrix result from: sik = aik + bik for i,k from 1
to 3. The limits of the variables i and k are written in mathematical form:
i,k = 1(1)3. The value in front of the parentheses is the start value, the
value in the parenthesis is the increment and the last number designates
the final value of the variables. In this example, i and k take values of one
through three with an increment of one. These are the numbers 1,2,3.
During matrix addition, one adds the elements which are in the same
place in each matrix, to obtain the elements of the sum matrix S. One
proceeds in the same manner when multiplying of matrix A with a
constant factor f ac. The elements of the product matrix P are calculated
by multiplying each element in A by the factor.
pik = fac * aik i,k= 1(1)3

345
ST 3D Graphics Abacus Software

C.2 Multiplying Matrices

The multiplication of two matrices A and B is somewhat more complex


than addition and has some limitations. The product of two matrices is
only defined when the number of columns of A matches the number of
rows in B. For two square matrices with i=k=constant, the multiplication
is always defined. The product of two matrices A (ajj) and B (bjk) is
defined as follows: A is a matrix of type (m,l) and B is of type (l,n), then
the product of the matrices A and B is A*B, the result matrix P is (pik),
whose elements are calculated in the following manner:

pik = sum of j=l to 1 over aij*bjk


with i - l(l)m and j = l(l)n.

This connection can be recognized in the following example.

A= 1 2 B= 5 6
3 4 7 8

C = A*B = 1*5 + 2*7 1*6+ 2*8


3*5 + 4*7 3*6 + 4*8

19 22
43 50

The result matrix P therefore contains the same number of lines as the
multiplicand A and the same number of rows as the multiplier B. In
regard to matrix multiplication there is a neutral element, i.e. for every
matrix A there is a matrix N with which A can be multiplied without
changing the original matrix. A*N=A. N is called the unit matrix and the
elements of the diagonal are one. All others have the value zero.
Moreover, the associative and the distributive law are valid during
multiplication.

A*(B*C) = (A*B)*C Associative Law


A*(B+C) = (A*B)+(A*C) Distributive Law

346
Abacus Software ST 3D Graphics

The commutative law does not hold for matrix multiplication. This means
A*B is not necessarily equal to B*A. The order of the multiplication is
not arbitrary, as you see, and must be observed.

347
ST 3D Graphics Abacus Software

Appendix D: Bibliography

[1] Foley James D., van Dam A., Fundamentals of Interactive


Computer graphics, Addison Wesley Publishing Company
(1984)

[2] Harrington Steven, Computer Graphics, McGraw-Hill


(1983)

[3] Newman William M., Principles of Interactive Computer


Graphics, McGraw-Hill (1984)

[4] Knuth Donald E., The Art of Computer Programming


Volume 2 Seminumerical Algorithms, Addison-Wesley
Publishing Company (1981)

[5] Kane Gerry, Hawkins Doug, Leventhal Lance, 68000


Assembly Language Programming, McGraw-Hill (1981)

[6] Bruckmann, Englisch, Gerrits, Atari ST Internals, Abacus


Software (1986)

[7] Szczepanowski, Gunther, Atari ST GEM Programmer’s


Reference, Abacus Software (1986)

348
Abacus Software ST 3D Graphics

INDEX

3-D wire models 4 Computer animation 3, 56


3-D wire models 56 Computer science 122
68000 computer 78 Convex polygons 57
Coordinate arrays 299
Coordinate origin 11
A Cray II 3,4,56
Cross product 214, 342

Abscissa 335
Apple Macintosh 4 D
Assembler 88, 89
Associative 346
Axis-symmetrical objects 169 Data system 117
Decimal system 333, 334
Definition block 298-300
B Definition line 169, 189
Desktop 296
Digital Research 88
BASIC1.S 92 Direction Binary number 334
Batch file 90 Display coordinate system 7
Binary system. Display of reveral objects 298
BIOS 88 Distributive law 346
Draw-line-algorithm 107

C
E

C programming language 4
CAD systems 3 Extended coordinate system 31
CAD-CAM 3 Extended-BIOS 88
Cartesian coordinate system 7, 108,
249, 335
CAT scans 3 G
Clip algorithm 122
Cohen-Sutherland clipping algorithm
122, 337 GEM functions 88
Color monitor 219 GEM-DOS 88
Column vectors 344 Global variables 152
Commodore Amiga 4 GRLINK1.S 123

349
ST 3D Graphics Abacus Software

H O

Harddisk 4 Object definition block 299-300


Hexadecimal number 334 Object definition coordinate system
Hexadecimal system 333 300
Hidden lines 4, 248, 249, 301 Observation coordinate system 44
Hidden surfaces 217, 301 Observation direction point (ODP) 43,
HIDE1.S 198 44,116,216
HOUSE1.S 155 Observation reference point (ORP) 43,
44,46
Observation window 43
I Observer system 118
Operating system 30, 34,107,218,
248
Indirect illumination 69 Ordinate 335
Inverse transformation matrices 41

P
L

PAINT1.S 221
Left coordinate system 30, 34,47,48, Pascal 4
59, 64, 337, 338, 342 Perspective transformation 31,119,
Line array 298 122
Link file 170 Picture coordinate system 8-9
Linking programs 89 Plot-point routine 80
Point coordinate arrays 298
Point light sources 70
M-N Polygon 11
Polymarker 296
Projection 38,51
Machine language 4, 89 Projection center 50,116,167, 214
Matrices 344 Projection plane 50, 51, 56
Matrix addition 345
Matrix multiplication 119,346
MENUl.S 257 R
Metacombco Editor 107
Monochrome monitors 219
Motorola MC68000 3-4 Real time 3-D graphics 191
MULTII.S 302 Reflection coefficient 69,71
Normal vector 116 RELMOD 88

350
Abacus Software ST 3D Graphics

Relocator 89 Unit vector 341


Right coordinate systems 64,122,214, USA Today 3
337, 338, 342 User-defined objects 301
ROTATEl.S 172 vectors 38, 340
Rotating definition line 170 View coordinate system 8,11,15,43,
Rotation 16, 32, 35, 37,41 116,118,119, 122,214
Rotation body 170,188,296 Visible surfaces 296
Rotation line 170,249
Rotation matrices 42,191
Rotation reference point 167 W
Row vectors 344

Window size 165


S Wire model mode 249
World array 298, 301
World coordinate system 8, 11,15,44,
Scalar product 214, 342 109,116,117,119, 120,121,122,165,
Scaling 10, 12 188,217,246,298, 299, 300, 301
Scaling matrix 32 World parameters 300
Scan line algorithm 56 World surface array 298
Screen memory 79
Screen pages 87
Screen switch routine 122
Screen system 118
Shading routine 218,248
Shading surfaces 248
Sine table 78
Size manipulation 120
Slope 81,335,336
Square matrix 344
Straight line equation. 336
Structogram 246
Surface normal vector 216
Surface world arrays 299

Taylor series 76
Transformations 31,121
Unit matrix 346

351
Optional Diskette

For your convenience, the program listings contained in this book are
available on an SF354 formatted floppy disk. You should order the diskette
if you want to use the programs, but don’t want to type them in from the
listings in the book.

All programs on the diskette have been fully tested. You can change the
programs for your particular needs. The diskette is available for $14.95 plus
$2.00 ($5.00 foreign) for postage and handling.

When ordering, please give your name and shipping address. Enclose a
check, money order or credit card information. Mail your order to:

Abacus Software
P.O. Box 7219
Grand Rapids, MI 49510

Or for fast service, call 1- 616 / 241-5510.


AA Rated Software
Atari and Abacus PaintPro
^ /*V ^
C. Create double¬
sized pictures
Fohrq ^-

PaintPr >
Multiple J
windows

ST TextPro
For creative illustrations on the ST

ST PaintPro
r
A simple-to-use and versatile database Wordprocessor with professional A GEM™ among ST drawing programs.
manager. Features help screens; features and easy-to-use! Full-screen Very friendly, but very powerful design
lightning-fast operation; tailorable editing with mouse or keyboard and painting program, A must for
display using multiple fonts; shortcuts. High speed input, scrolling everyone's artistic or graphics needs.
user-definable edit masks; capacity up and editing; sideways printing; Use up to three windows. You can
to 64,000 records. Supports multiple multi-column output; flexible printer even cut S paste between windows.
files. RAM-disk support for 1040ST. installation; automatic index and table Free-form sketching; lines, circles,
Complete search, sort and file of contents; up to 180 chars/line; 30 ellipses, boxes, text, fill, copy, move,
subsetting. Interfaces to TextPro. Easy definable function keys; metafile zoom, spray, paint, erase, undo, help.
printer control. $49.95 output; much more. $49.95 Double-sized picture format. $49.95

AssemPro PCBoard
The complete 68000
assembler development
Designer
Create printed circuit board layouts
package for the ST

^
wuMmfimmWiiWilsmm Auto routing, component list, pinout list, net list
ST Forth/MT ST AssemPro PCBoard Designer
Powerful, multi-tasking Forth for the ST. Professional developer's package Interactive, computer aided design
A complete, 32-bit implementation includes editor, two-pass interactive package that automates layout of printed
based on Forth-83 standard. Develop¬ assembler wrth error locator, online help circuit boards. Auto-routing, 45° or
ment aids: full screen editor, monitor, including instruction address mode and 90° traces; two-sided boards; pin-to-pin,
macro assembler. 1500+ word library. GEM parameter information, pin-to-BUS or BUS-to-BUS. Rubber-
TOS/LINEA commands. Floating point monitor-debugger, disassembler and banding of components during place-
and complex arithmetic. $49.95 68020 simulator, more. $59.95 ment. Outputs pinout, component and
net list. $395.00
ST and 1040ST are trademarks ot Atari Corp.
Call now for the name of the dealer nearest you. GEM is a trademark at Digital Research Inc.

Or order directly using your MC, Visa or Amex


card. Add $4.00 per order for shipping. Foreign
orders add $10.00 per item. Call (616) 241-5510
or write for your free catalog. 30-day money
back software guarantee. Dealers inquires
Abacus mi
P.0. Box 7219 Dept.NB Grand Rapids, Ml 49510
welcome-over 1400 dealers nationwide. Phone 616/241 -5510 • Telex 709-101 • Fax 616/241-5021
PRESENTING THE ST ST Beginner's Oulda ST INTERNALS OEM ProgrammeCe Rat. MACHINE LANGUAGE
Gives you an indnpth look at Writtan tor tha Srtthand ST Eaaantlal guida to tha Insida For aarioua programmara Program In tha lastaat lang¬ Fantaatlc collection ol pro-
thla • •nietlonal n.w user Gat a baalc undaratand- Inlormation ol tha ST naadlng detailed nlctmation uaga lor your ATARI ST grama and Into lor tha ST
computsr. Discuss as tha ing ol your ST Explora Datailad daacrlptlona ol on GEM Praaantad in an Laarn 68000 Meernbly lang. Complata programa Induda.
aichltactura ot tha ST. work¬ LOGO and BASIC from tha ■ound and graphica chips. oony-to-undoratand format. uaga. ita numbanng ayatam, euperlast RAM dtak; lima-
ing with GEM. tha mouaa. ground up Simpla explan Intarnal hardware, I/O porta, All aiamplaa ara In C and uaa cd raglatara. atructura S aadng prlntar apoolar, color
operating system, all tha atlona o' tha hardware and ualng GEM Commented aaaamBty language Covara Important datarla of Inatruc-
varioua intarfaoaa, tha 68000 print hardcopy plotter output
Intarnal workings o' tha ST BIOS Hating. An Indiapan VOI and AES function# No tion aat, and uaa ol intarnal
chip and Ita inetructlons. hardcopy, craating access
llluatratona. diagrams Gloaa- aibla ralaranea lor your ST aarioua programar ahould ba ayatam routines Gaarad lot
LOGO 290 pp *16 86 orlaa Monay aavlng tricks
ary indax. 200pp $i« 95 library «0pp »'«» without dIOpp SI 9 99 tha ST 280pp *19 95 andtipa 2S0cc *19 99

ST GRAPHICS 4SOUNO ST LOGO OUIOE ST PEEKS * POKES BASIC Training Guide BASIC to C 30 GRAPHICS
Datailad guida to graphica Taka contd of your ST by Enhance your programa with Thorough guida lor Isarning Move up from BASIC to C. If FANTASTICI Rotate, zoom,
and aound on tha ST. 20 6 learning ST LOGO—tha aaay tha aiamplaa found within ST BASIC programming. you're elreedy a BASIC and shade 3D objects. All
3D kjnctlon ptottara. Molr* to uaa. powerful languaga thla booh Explorea ualng Detaled programming funda¬ programmer, you can laarn C programa written In machine
patlarna, graphic memory Topic* Induda 61a handling, different languages BASIC. mentals, commands descilp- all that much Merer Parallel language (or high speed.
and varioua reaolutiona. recurslon-HHbert A Sierpinskl C. LOGO and machine tiona. ST graphics 8 aound. examples demoslrate tha Learn the mathematics
(ractala, recursion waveform curvaa. 2D and 30 (unction languaga. using various using GEM in BASIC, lila programing techniques and behind 3D graphica. Hidden
generation Eianpiet written plots, data atructura. error intarlacaa. memory uaaga, management, disk operation. constucts In both languages. line removal, shading With
In C, LOGO. BASIC and handling Helpful guide lor reading and saving horn and Tutorial problems give hand* Variables pointers, arrays, 3D pattern maker and
ModMe2 250pp 519 99 ST LOGO users. *19.99 todlak, more 280pp $16 95 on experience. 300pp $16 95 daU atructura 290pp*19 99 animator. $2* 96
The ATARI logo and ATARI ST era Swdamerkaof Alan Carp.

Abacusiuiiiiiiil Software
P.0. Box 7219 Dept. A9 Grand Rapids, Ml 49510 • Telex 709-101 ■ Phone (616) 241-5510
Optional diskettes are available tor all book titles at $14.95
Call now for the name of your nearest dealer. Or order directly from ABACUS with your MasterCard, VISA, or Amex card. Add
$4.00 per order for postage and handling. Foreign add $10.00 per book. Other software and books coming soon. Call or
write for your free catalog. Dealer inquiries welcome-over 1400 dealers nationwide.
M
Q> $&></■>
Vn
\ O ° <
1
D o / O

3 L> ^ 1 1

T C> / 150

j 0 / D )

l 0 1 1 0

1 o n\
j C?D O
$
I OO)
°1
\ o / -o
tp h
l J II
( 1 c
& ! ! ?£>
'X
IX 1 / of
X3

iV £ j l t V

/f f in/
FANTASTIC! Learn how to create impressive and fascinating 3D
graphics on the Atari ST. Covers introductory concepts and
background materials, graphic animation, using the assembler
and much more. Learn real-time animation with dozens of
graphic routines. 3D Graphics is an amazing book for all
programmers interested in advanced level graphics. Some of
the areas covered include:

• 2D & 3D Transformations • Spatial projection


• Hidden lines & surfaces • Screen manipulation
• Data structure for 3D objects • Rotation of objects
• Object animation • Light and shadows

About the author:


Uwe Braun is a member of the Data Becker Product
Development team, based in Duesseldorf, W. Germany. He is a
knowledgeable machine language programmer with extensive
graphics programming experience.

isbn o-nibMai-bn-D

The ATARI logo and ATARI ST are trademarks of Atari Corp.

A Data Becker book published by


aI You Can 0n ImiHHiixil e+
Abacus \mm Software
P.0. Box 7219 Grand Rapids. Ml 49510 - Telex 709-101 - Phone 616/241-5510

You might also like