home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / software / testi / corsoasm / sorgenti_3d / voxel.txt < prev   
Text File  |  1998-02-18  |  8KB  |  181 lines

  1.  
  2.  
  3.  
  4.                      Voxel Landscapes and How I Did It
  5.                                By Tim Clarke
  6.                       Email: tjc1005@hermes.cam.ac.uk
  7.  
  8.  This document describes the method I used in my demo of a Martian terrain,
  9. which can be found at garbo.uwasa.fi:/pc/demo/mars10.zip.
  10.  It's similar to a floating horizon hidden line removal algorithm, so you'll
  11. find discussion of the salient points in many computer graphics books. The
  12. difference is the vertical line interpolation.
  13.  
  14.  
  15. First, some general points
  16. --------------------------
  17.  
  18.  The map is a 256x256 grid of points, each having an 8-bit integer height
  19. and a colour. The map wraps round such that, calling w(u,v) the height at
  20. (u,v), then w(0,0)=w(256,0)=w(0,256)=w(256,256). w(1,1)=w(257,257), etc.
  21.  
  22.  Map co-ords: (u,v) co-ordinates that describe a position on the map. The
  23. map can be thought of as a height function h=w(u,v) sampled discretely.
  24.  
  25.  Screen co-ords: (x,y) co-ordinates for a pixel on the screen.
  26.  
  27.  
  28. To generate the map
  29. -------------------
  30.  
  31.  This is a recursive subdivision, or plasma, fractal. You start of with
  32. a random height at (0,0) and therefore also at (256,0), (0,256), (256,256).
  33. Call a routine that takes as input the size and position of a square, in the
  34. first case the entire map.
  35.  This routine get the heights from the corners of the square it gets given.
  36. Across each edge (if the map has not been written to at the point halfway
  37. along that edge), it takes the average of the heights of the 2 corners on that
  38. edge, applies some noise proportional to the length of the edge, and writes
  39. the result into the map at a position halfway along the edge. The centre of
  40. the square is the average of the four corners+noise.
  41.  The routine then calls itself recursively, splitting each square into four
  42. quadrants, calling itself for each quadrant until the length of the side is
  43. 2 pixels.
  44.  This is probably old-hat to many people, but the map is made more realistic
  45. by blurring:
  46.  
  47.      w(u,v)=k1*w(u,v)+k2*w(u+3,v-2)+k3*w(u-2,v+4) or something.
  48.  
  49.  Choose k1,k2,k3 such that k1+k2+k3=1. The points at which the map is sampled
  50. for the blurring filter do not really matter - they give different effects,
  51. and you don't need any theoretical reason to choose one lot as long as it
  52. looks good. Of course do everything in fixed point integer arithmetic.
  53.  The colours are done so that the sun is on the horizon to the East:
  54.  
  55.      Colour=A*[ w(u+1,v)-w(u,v) ]+B
  56.  
  57. with A and B chosen so that the full range of the palette is used.
  58.  The sky is a similar fractal but without the colour transformation.
  59.  
  60.  
  61. How to draw each frame
  62. ----------------------
  63.  
  64.  First, draw the sky, and blank off about 50 or so scan lines below the
  65. horizon since the routine may not write to all of them (eg. if you are on top
  66. of a high mountain looking onto a flat plane, the plane will not go to the
  67. horizon).
  68.  Now, down to business. The screen is as follows:
  69.  
  70.      ---------------------------
  71.      |                         |
  72.      |                         |
  73.      |           Sky           |
  74.      |                         |
  75.      |                         |
  76.      |a------------------------| Horizon
  77.      |                         |
  78.      |                         |    Point (a)=screen co-ords (0,0)
  79.      |          Ground         |     x increases horizontally
  80.      |                         |     y increases downwards
  81.      |                         |
  82.      ---------------------------
  83.  
  84.  Imagine the viewpoint is at a position (p,q,r) where (p,q) are the (u,v)
  85. map co-ordinates and r is the altitude. Now, for each horizontal (constant v)
  86. line of map from v=q+100 (say) down to v=q, do this:
  87.  
  88.   1. Calculate the y co-ordinate of map co-ord (p,v,0) (perspective transform)
  89.  
  90.  
  91.     you:->------------------------ Horizontal view
  92.          :
  93.       r  :
  94.          :
  95.          :
  96.      -----------------------------P Ground
  97.          ......................... (q-v)
  98.          q                       v
  99.  
  100.  You have to find where the line between P and you intersects with the 
  101. screen (vertical, just in front of 'you'). This is the perspective transform:
  102.    y=r/(q-v).
  103.  
  104.   2. Calculate scale factor f which is how many screen pixels high a mountain
  105. of constant height would be if at distance v from q. Therefore, f is small
  106. for map co-ords far away (v>>q) and gets bigger as v comes down towards q.
  107.  
  108.   So, f is a number such that if you multiply a height from the map by f, you 
  109. get the number of pixels on the screen high that height would be. For 
  110. example, take a spot height of 250 on the map. If this was very close, it 
  111. could occupy 500 pixels on the screen (before clipping)->f=2.
  112.  
  113.   3. Work out the map u co-ord corresponding to (0,y). v is constant along
  114. each line.
  115.  
  116.   4. Starting at the calculated (u,v), traverse the screen, incrementing the
  117. x co-ordinate and adding on a constant, c, to u such that (u+c,v) are the map
  118. co-ords corresponding to the screen co-ords (1,y). You then have 256 map
  119. co-ords along a line of constant v. Get the height, w, at each map co-ord and
  120. draw a spot at (x,y-w*f) for all x.
  121.  
  122.  I.e. the further away the scan line is, the more to the "left" u will start,
  123. and the larger c will be (possibly skipping some u columns if c > 1); the
  124. closer the scan line, the lesser u will start on the "left", and c will be
  125. smaller.
  126.  
  127.  
  128.  Sorry, but that probably doesn't make much sense. Here's an example:
  129. Imagine sometime in the middle of drawing the frame, everything behind a
  130. point (say v=q+50) will have been drawn:
  131.  
  132.      ---------------------------
  133.      |                         |
  134.      |                         |
  135.      |                         |
  136.      |           ****          |
  137.      |        *********        | <- A mountain half-drawn.
  138.      |-----**************------|
  139.      |*************************|
  140.      |*********       *********|
  141.      |******             ******|
  142.      |.........................| <- The row of dots is at screen co-ord y
  143.      |                         |   corresponding to an altitude of 0 for that
  144.      ---------------------------   particular distance v.
  145.  
  146.  Now the screen-scanning routine will get called for v=q+50. It draws in a
  147. point for every x corresponding to heights at map positions (u,v) where u
  148. goes from p-something to p+something, v constant. The routine would put points
  149. at these positions: (ignoring what was there before)
  150.  
  151.      ---------------------------
  152.      |                         |
  153.      |                         |
  154.      |                         |
  155.      |                         |
  156.      |                         |
  157.      |-------------------------|
  158.      |          *****          |
  159.      |       ***     ***       |
  160.      |*******           *******|
  161.      |.........................|
  162.      |                         |
  163.      ---------------------------
  164.  
  165.  So, you can see that the screen gets drawn from the back, one vertical
  166. section after another. In fact, there's more to it than drawing one pixel
  167. at every x during the scan - you need to draw a vertical line between
  168. (x,y old) to (x,y new), so you have to have a buffer containing the y values
  169. for every x that were calculated in the previous pass. You interpolate
  170. along this line (Gouraud style) from the old colour to the new colour also,
  171. so you have to keep a buffer of the colours done in the last pass.
  172.  Only draw the vertical lines if they are visible (ie. going down,
  173. y new>y old). The screen is drawn from the back so that objects can be drawn
  174. inbetween drawing each vertical section at the appropriate time.
  175.  
  176.  If you need further information or details, mail me or post here... Posting
  177. will allow others to benefit from your points and my replies, though.
  178.  
  179.  Thank you for the response I have received since uploading this program.
  180.  
  181.