home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / OTFACE.ZIP / FACESORT.DOC next >
Text File  |  1996-08-29  |  8KB  |  145 lines

  1.  
  2.                      - THE OUTLAW TRIAD DEMO-SERIES -
  3.  
  4. ───────────────────────────────■ PART X ■─────────────────────────────────────
  5.  
  6.                          Written by : Vulture/OT
  7.                          Code in    : Pascal
  8.                          Topic      : Face sorting
  9.  
  10. ──────────────────────────────■ Introduction ■────────────────────────────────
  11.  
  12.  Welcome to the Outlaw Triad demo-series! In these series we will be talking
  13.  about programming demo-effects in either pascal or assembler. Theory behind
  14.  the effects shall be discussed while a full sourcecode is also provided.
  15.  The tenth release in the Outlaw Triad Demo Series! We will be talking about
  16.  face sorting in 3d objects. Face sorting will enable you to determine in
  17.  which order polygons in 3d objects should be drawn on the screen. It's an
  18.  essential part of coding 3d graphics. Enjoy!
  19.  
  20. ─────────────────────────────────■ Theory ■───────────────────────────────────
  21.  
  22.  We will be building a basic 3d engine this time using polygons to create
  23.  closed 3d objects. I will asume you know the basics of 3d graphics coding,
  24.  that is, how to rotate 3d points and how to calculate 2d screen coÖrdinates.
  25.  
  26.  The most important thing in 3d engines at this point is how to draw the 3d
  27.  object on the screen. Rotating the 3d object and calculating 2d coÖrdinates
  28.  shouldn't be real problems. However, drawing the object correctly on the
  29.  screen might cause significant troubles. Polygons in 3d objects are to be
  30.  drawn in a specific order, that is, those polygons which are way deep in
  31.  space have to be drawn first. The polygons which are closer to the origin
  32.  (closer to the eye) have to be drawn last. Like this:
  33.  
  34.        ------------------ Poly 1           (small z)
  35.        |                |
  36.        |                |
  37.    -------------------------- Poly 2       (large z)
  38.    |                        |
  39.    |                        |
  40.    |                        |
  41.    |                        |
  42.    |                        |
  43.    |                        |
  44.    --------------------------
  45.  
  46.  Hmm, it ain't very much 3d but with a little imagination you can see that
  47.  poly 1 has to be drawn first since it's drawn partially behind poly 2. It's
  48.  located deeper in space than the second poly. If you draw poly 2 first and
  49.  then poly 1, poly 2 will be partially overwritten by poly 1 and that's not
  50.  what we want. To be short, you have to draw your polys from back to front,
  51.  so that the nearest polys (the ones close to the eye) overwrite the futher
  52.  polys. This is also known as "the painter's algorithm".
  53.  
  54.  This is where face sorting comes in! Face sorting means finding out in
  55.  which order the polygons must be drawn on the screen. In our case, we'll
  56.  use the most simple method around and that's sorting the polygons on their
  57.  average z value! Let's take a look at this in detail...
  58.  
  59.  A polygon is made up by a number of 3d-points. And each 3d-point consists
  60.  of x,y,z values. If we add the z values from all 3d-points of a polygon and
  61.  divide the resulting value by the number of 3d-points, we've calculated the
  62.  average z value of that polygon, that is, how deep the polygon is in space.
  63.  Remember to do this AFTER you have rotated the 3d-point! (note: in the code,
  64.  I did not divide the resulting value as it is not really necessary)
  65.  We calculate the average z value for all the polygons in the 3d object and
  66.  store those values in a seperate list. Now we keep two lists. One for all
  67.  the average z values and one for the exact drawing order of the polygons.
  68.  At start of each frame, the orderlist looks like:
  69.  
  70.      1 2 3 4 5 6 7 8 9 etc etc...
  71.  
  72.  So, each polygon got his own number. Ok, now we sort the polygons on their
  73.  z values, that is, we sort the average z values list. For example: compare
  74.  the first two values. If the first value (average z of poly 1) is greater
  75.  than the second value, swap them. Repeat this until you've got an average z
  76.  list in which the values go from small to large. In the sourcecode, I used
  77.  a bubblesort routine to do this. These bubblesort routines work most of the
  78.  time but there are better algorithmes to sort values in a list. I mean, try
  79.  to use the provided bubblesort routine to sort, like, 400 values each frame.
  80.  Bad idea... Just so you know... :-) (hint: use a quicksort routine instead)
  81.  
  82.  Anyway, when you swap values in the average z value list, you must also swap
  83.  the corresponding values in the order list! So, if you swap the first two
  84.  values in the z value list, you must also swap the first two values in the
  85.  polygon orderlist. Ok, after you sorted the average z value list, the final
  86.  polygon orderlist could then look something like:
  87.  
  88.      3 5 6 4 2 8 1 9 7 etc etc...
  89.  
  90.  At this time you've created a list in which the exact drawing order of the
  91.  polygons is saved! In our case we have to draw polygon 3 first, then 5, then
  92.  6, then 4 etc. Polygon 3 has the smallest average z which means it lies far
  93.  deep in space so it has to be drawn first. There! Quite simple, eh? :-)
  94.  Of course, as said before, you will have to reset the orderlist at start of
  95.  each frame to its original state (1,2,3,4 etc).
  96.  
  97.  Ok, that's practically all there is to it. Simply sort all polygons on their
  98.  average z value and draw them from back to front. You can also implement
  99.  code to determine the face-normal. With this, we can entirely skip drawing
  100.  polygons which would not be visible on the screen when the entire object
  101.  has been drawn. This makes the engine a lot faster. Also, you can go and
  102.  do lightshading using face-normals. Hmm... something for a future trainer.
  103.  
  104.  The example source is a basic 3d system. It was the first 3d engine I coded
  105.  using this method so it's not that hot and needs a lot of improvements. It
  106.  should be enough to get started, though, as it shows how a basic 3d engine
  107.  works. We will be working on this 3d system in future trainers. What about
  108.  implementing glenzing and various kinds of shading. Watch out for more docs
  109.  from OT. Anyway, hope this helped you a bit futher in 3d graphics coding.
  110.  If it has, greet Outlaw Triad in your productions. We like that... :-)
  111.  
  112.  This is all for now. Happy coding!
  113.  
  114.      -Vulture/Outlaw Triad-
  115.  
  116. ───────────────────────────────■ Distro Sites ■───────────────────────────────
  117.  
  118.  Call our distribution sites! All our releases are available at:
  119.  
  120.   BlueNose      World HQ          +31 (0)345-619401
  121.   The Force     Distrosite        +31 (0)36-5346967
  122.   Bugs'R'Us     Distrosite        +31 (0)252-686092    More distros wanted!
  123.   The 7 Angels  Distrosite        +31 (0)715-148377    (preferably outside
  124.   ShockWave     South African HQ  +27 (011)888-6345     of the Netherlands)
  125.   Society HQ    United States HQ  +1  (518)465-6721
  126.   ACe World     Brazilian HQ      +55 (21)-259-8847
  127.  
  128.  Also check the major FTP/WWW sites for Outlaw Triad productions.
  129.  
  130. ──────────────────────────────────■ Contact ■─────────────────────────────────
  131.  
  132.  Want to contact Outlaw Triad for some reason? You can reach us at our
  133.  distrosites in Holland. Or if you have e-mail access, mail us:
  134.  
  135.    Vulture   (coder/pr)   comma400@tem.nhl.nl
  136.  
  137.  Our internet homepage:
  138.  
  139.    http://www.tem.nhl.nl/~comma400/vulture.html
  140.  
  141.  These internet adresses should be valid at least till june 1997.
  142.  
  143. ──────────────────────────────────────────────────────────────────────────────
  144.  
  145.       Quote: To do nothing is exhausting because you can't take breaks.