home *** CD-ROM | disk | FTP | other *** search
- ---------------- More advanced coding techniques ----------------
- ----------- By Kreator of ANARCHY UK ----------
- ---- 3 D routines ----
-
- I am going to approach this topic
- from a mathematical point of view, the
- mathematics invloved are quite simple
- but if you are not particularly adept
- in this area dont worry it isn't
- essential to understand the underlying
- theory.
- Now suppose we are given an object
- to transfer into a wireframe
- representation on screen. We must
- construct a list of coordinates which
- specify the vertices of the object and
- also a connection list which tells the
- computer how to connect these points
- together.
- eg. A cube has 8 vertices, and
- 12 connecting sides, the vertices are
- as follows
- (50,50,50) (-50,50,50) (-50,-50,50)
- (50,-50,50) (50,50,-50) (50,-50,-50)
- (-50,-50,-50) (-50,50,-50)
-
- and if these are then labelled 1-8 we
- have the connections as follows
- 1-2 2-3 3-4 4-1 5-6 6-7 7-8 8-5
- 1-5 2-6 3-7 4-8
-
-
-
-
- If you don't believe the example try
- to draw the cube yourself and
- visualise the coordinates. Notice that
- the point (0,0,0) is at the centre of
- the cube, this is important because in
- our rotations this will be the only
- point which remains stationary.
-
- When rotating the object what we are
- in fact doing is rotating the vertices
- about the fixed ORIGIN (0,0,0). There
- is a mathematical theorem which states
- that any 3 dimensional rotation can be
- split into 3 individual rotations in
- only 2 dimensions, which is a much
- simpler thing to calculate. Now in
- general it is quite difficult to
- calculate these rotations from an
- arbitrary 3D rotation, but happily
- enough this doesn't matter when
- writing demos because by simply
- performing 2D rotations and varying
- the 3 Angles of rotation we achieve
- an interesting effect.
-
- The formula for 2D rotation is given
- as follows,
-
- x = X cos(s) - Y sin(s)
- y = Y cos(s) + X sin(s)
-
- This can easily be shown with simple
- trigonometry.
-
- These formulae enable us to rotate a
- point in just two dimensions, but all
- we now do is to rotate the point three
- times in different planes.
- In other words if we are given a
- general point (x,y,z) and a,b,c are
- the three angles of rotation then to
- calculate the rotated point follow the
- procedure below (just for interest the
- angles a,b,c are called the Euler
- angles)
-
- x1 = x cos(a) - y sin(a)
- y1 = y cos(a) + x sin(a)
-
- y2 = y1 cos(b) - z sin(b)
- z1 = z cos(b) + y1 sin(b)
-
- z2 = z1 cos(c) - x1 sin(c)
- x2 = x1 cos(c) + z1 sin(c)
-
- Then (x2,y2,z2) holds the rotated
- coordinate. To implement this on the
- Amiga use a Sintable which has values
- from -32768 to 32767, this can be
- reused for the cosine calculations
- as cos (a) = sin (a+90 degrees). You
- could code the routine something like
-
- Move x,d3
- Move y,d4
- Move z,d5
- Lea Sin,a0
- Lea Cos,a1
- Move d3,d6
- Move d4,d7
- Move a,d0 ;a holds 2x the angle
- Move (a0,d0),d1
- Move (a1,d0),d2
-
- Muls d2,d6
- Muls d1,d7
- Sub d7,d6
- Add.l d6,d6
- Swap d6 ;Calculation of x1
-
- Muls d2,d4
- Muls d1,d3
- Add d3,d4
- Add.l d4,d4
- Swap d4 ;Calculation of y1
- etc......
-
- Now up until now we haven't considered
- how the lines will be drawn to the
- screen, I shall assume you have access
- to a blitter line draw routine, if not
- there is one included on the disk,
- which is from the System Programmers
- Guide. There are two options open now
- we can leave the coordinates as they
- are and simply add a displacement to
- them before plotting the lines, or go
- for the more realsitic technique of
- perspective. This invloves scaling the
- x,y coords. according to how far into
- the screen we are. A reasonable way of
- doing that is as follows
-
- x,y,z in d3,d4,d5
-
- Add #Depth+Scale,d5
- Move.l #Scale*65536,d6
- Divu d6,d5
-
- Muls d5,d3 ;Scale the X coord
- Add.l d3,d3
- Swap d3
-
- Muls d5,d4 ;Scale the Y coord
- Add.l d3,d4
- Swap d4
-
- Alternatively you can use a table of
- scaling values.
- Now all that remains is to plot the
- lines. Dont forget up until now all
- vertices have been calculated with the
- origin at (0,0) but now we must move
- the origin to the centre of the screen
- or where ever else you want it. This
- means adding a displacement to each
- pair of coordinates.
- To see how these techniques are
- implemented I've included some source
- for you to examine.
- These routines can easily be adapted
- to other purposes, eg.to create vector
- bobs, use a single point for each bob
- and before plotting sort the z
- coordinates and plot the bobs in
- reverse order, also a simple form of
- hidden line removal can be implemented
- by creating a list of surfaces,
- calculating the normals to these
- surfaces and if the normals point away
- from you dont plot any lines in the
- surface. For an example of this see my
- Magnetic Fields Party Demo.
- Next month I will write about sine
- scrollers, Kreator .......
-
-