home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #2 / Amiga Plus CD - 1995 - No. 2.iso / internet / faq / englisch / 3d-informationforprogrammer next >
Encoding:
Text File  |  1995-04-11  |  34.8 KB  |  848 lines

  1. Archive-name: 3d-programmer-info
  2. Version: $Id: .header,v 1.8 1994/05/23 15:55:59 pat Exp pat $
  3.  
  4.                       O                                O
  5.  
  6.  ---+  +---- F A Q ------------------+  +------- F A Q ---------------+  +---
  7.  %[%|  |X$HOOR$H\[[8@DoooDDDDD8@@[%[%|  |X$HOOR$H\[[8@DoooDDDDD8@@[%[%|  |X$H
  8.  [[%|  |$C$OOR$H\%[8DooooDDDD8D@@[[[%|  |$C$OOR$H\%[8DooooDDDD8D@@[[[%|  |$C$
  9.  [[%|  |X$HHRR$H@[            88@@[[%|  |X$HHRR            DDDD88@@[[%|  |X$H
  10.  [[[|  |$$$HRR$H@%[@8ooooD   888@[[[[|  |$$$HRR$H@%[@8o   DDDD888@[[[[|  |$$$
  11.  [[[|  |X$$H            /   /  88[[[[|  |X$          /   /    D888[[[[|  |X$$
  12.  [%[|  |XC$H    \%[@8DDD   DD   @[[%[|  |XC    $H\%[@   DDDD   88@[[%[|  |XC$
  13.  [[%|  |XC$H    \%%@8DD   DDDD   @[[%|  |XC    $H\%%   DDDDDD   8@@[[%|  |XC$
  14.  [%[|  |X$$H    \%%@88DDD   D8   @[%[|  |X$    $H\%%@8   DDD8   8@@[%[|  |X$$
  15.  [%[|  |CX$H    \%%[88DDD88  D   @[%[|  |CX    $H\%%[88D  88D   8@@[%[|  |CX$
  16.  [%[|  |X$CH    \%[[@888D8D8  |  @[%[|  |X$   _$H\%[[@888  88   88@[%[|  |X$C
  17.  [%[|  |XXCH    |  [@888D888  | @@[%[|  |XX  |  H\@[[@888  8   88@@[%[|  |XXC
  18.  [%[|  |XC$$    \  \       |  |88@[%[|  |XC  \  \       |  |  8888@[%[|  |XC$
  19.  [%[|  |XX$HOR$HH@  [@88888  8D8@@[%[|  |XX$HOR  H@[%[@8  @8888D8@@[%[|  |XX$
  20.  [%[|  |X$CHOR$H\@%  @@@@Y  8888@@[%[|  |X$CHOR$  @%%[Y  @8888888@@[%[|  |X$C
  21.  [%[|  |CX$$OR$HH@%%      .d8D88@@[%[|  |CX$$OR$H      .d@@888D88@@[%[|  |CX$
  22.  [[[|  |X$$HOR$HH@%%[[@@@@88D8D88@[[[|  |X$$HOR$HH@%%[[@@@@88D8D88@[[[|  |X$$
  23.  [%[|  |CX$HOR$HH\%[%[@@@@888D88@@[%[|  |CX$HOR$HH\%[%[@@@@888D88@@[%[|  |CX$
  24.  ---+  +--------------- F A Q -------+  +------------------ F A Q ----+  +---
  25.  
  26. Contents:
  27. 1) General references for 3-d graphics questions.
  28. 2) How do I define an object?
  29. 3) How do I define space?
  30. 4) How do I define position?
  31. 5) How do I define orientation?
  32. 6) How do I define a velocity?
  33. 7) Drawing three-dimensional objects on a two-dimensional screen.
  34. 8) Vector Math - Dot Product and Cross-Product.
  35. 9) Matrix Math
  36. 10) Collisions.
  37. 11) Perspective.
  38. 12) Z-Buffering & the Painters Algorithm & BSP-Trees.
  39. 13) Shading.
  40. 14) 3-space clipping.
  41. 15) 3-d scanning.
  42. 16) Publically available source-code.
  43. 17) Books on the topics.
  44. 18) Other forums.
  45. 19) Current Contents of Archive ftp.csh.rit.edu::/pub/3dfaq
  46.  
  47. Last update:
  48.      03Jan95
  49.  
  50. What's new?
  51.  
  52.      Added a bunch of new books without net-reviews and a few more http
  53.      sites.
  54.  
  55.      Added some information on velocity calculations.
  56.  
  57. 1) General references for 3-d graphics questions.
  58.  
  59.      Well, this FAQ is just getting off the ground.  Hopefully it will
  60. touch on most of the bases you need to get started for now, and
  61. hopefully it will expand at least as fast as you need it too.  But...
  62. regardless, things you'll want to locate for more help are Matrix
  63. Algebra books, Physics books talking about Eulerian motion, and some
  64. books on the Graphics Hardware you want to program for.  The code
  65. examples included in this FAQ will most likely be in C with pseudo-code
  66. in comments.
  67.  
  68.      One of the most popular references, (and one of my favorites), is:
  69.           Computer Graphics: Principles and Practice
  70.           ------------------------------------------
  71.           Foley, van Dam, Feiner, and Hughes
  72.           Addison Wesley -- Reading, Massachusetts
  73.           (c) 1990.  ISBN 0-201-12110-7
  74.  
  75.      But, you'll also want to definitely check out the FAQ for
  76. comp.graphics.  That FAQ touches mainly on 2-D needs, but some 3-D
  77. aspects are reviewed there, too.
  78.  
  79. 2) How do I define an object?
  80.  
  81.      There are lots of ways to define objects.  One of the most commonly
  82. used is the OFF (Object File Format).  The OFF toolkit and a library of
  83. objects are available via anonymous ftp from gatekeeper.dec.com -- XXX
  84. ??? (I can't find it anymore.  I found it here once about 2 years ago,
  85. but I haven't found it since).  The format provides easy methods for
  86. extensions and a base set of things you can expect for each object.  The
  87. toolkit is a bit bulky, but the file format (in ascii) is easy enough to
  88. parse by hand.
  89.  
  90.      The OFF.aoff file contains information about the object.  The most
  91. important one there is the location of the surface specification file
  92. (usually object name.geom).  This file also contains other attributes
  93.                -
  94. and file names relevant to this object.
  95.  
  96.      The OFF surface specification begins with the number of points, the
  97. number of polygons and the number of segments.
  98.  
  99.         npts nplys nsegs
  100.  
  101. This line is followed by the floating point coordinates for the points
  102. that make up the object.
  103.  
  104.         x1 y1 z1
  105.         x2 y2 z2
  106.         x3 y3 z3
  107.            .
  108.  
  109.            .
  110.  
  111.            .
  112.         x(npts) y(npts) z(npts)
  113.  
  114. Then, it gets a bit more complicated.  The following lines begin with a
  115. number to indicate the number of vertices in this polygon.  That number
  116. is followed by that many numbers, one for each vertex.  These are given
  117. in an order specified in the .aoff (usually conter-clockwise).  So, for
  118. example, a triangle and a pentagon which share a side are shown below.
  119.  
  120.         3       1 3 4
  121.         5       2 4 3 6 7
  122.  
  123. Here is some quick and dirty sample code to read in the .geom file:
  124.  
  125. struct polygon {
  126.     int nvert;          /* Number of vertices in this polygon */
  127.     int *verts;         /* Vertices in this polygon */
  128. };
  129.  
  130. struct object {
  131.     int npts;           /* The number of points */
  132.     int npolys;         /* The number of polygons */
  133.     int nsegs;          /* The number of segments */
  134.     double *point x,*point y,*point z;
  135.                  -        -        -
  136.     struct polygon *polys;
  137. };
  138.  
  139. int
  140. read geom file( char *geom file, struct object *obj )
  141.     -    -                -
  142. {
  143.     FILE *fp;
  144.     int i,j;
  145.  
  146.     if (!(fp = fopen(geom file,"r")))           /* Open the .geom file */
  147.                          -
  148.         return -1;
  149.  
  150.                                                 /* Get header information */
  151.     fscanf(fp,"%d %d %d",&obj.npts,&obj.npolys,&obj.nsegs);
  152.  
  153.         /*
  154.         ** Allocate room for the points.
  155.         */
  156.     obj.point x = (double *)malloc(obj.npts*sizeof(double));
  157.              -
  158.     obj.point y = (double *)malloc(obj.npts*sizeof(double));
  159.              -
  160.     obj.point z = (double *)malloc(obj.npts*sizeof(double));
  161.              -
  162.  
  163.     for (i=0;i<obj.npts;++i)
  164.         fscanf(fp,"%lf %lf %lf",&obj.point x[i],
  165.                                           -
  166.                                 &obj.point y[i],
  167.                                           -
  168.                                 &obj.point z[i]);
  169.                                           -
  170.  
  171.         /* Allocate room for the polygons.  */
  172.     obj.polys = (struct polygon *)malloc(obj.npolys*sizeof(struct polygon));
  173.  
  174.     for (i=0;i<obj.npts;++i) {
  175.             /* See how many vertices this has */
  176.         fscanf(fp,"%d",&obj.polys[i].nvert);
  177.  
  178.             /* Allocate room for vertices */
  179.         obj.polys[i].verts = (int *)malloc(obj.npolys*sizeof(int));
  180.  
  181.             /* Get each vertex */
  182.  
  183.         for (j=0;j<obj.polys[i].nvert;++j)
  184.  
  185.             fscanf(fp,"%d",&obj.polys[i].verts[j]);
  186.     }
  187. }
  188.  
  189. 3) How do I define space?
  190.  
  191.      There are several things to consider when picking a coordinate
  192. system.  Most important of these is how you intend to handle objects.
  193. If your objects are defined in terms of <x,y,z> triplets, it will
  194. require a fair bit of work on reading them in to turn them into
  195. spherical coordinates.  If you're looking to this FAQ for information on
  196. how to define the space your objects will be in, I'd strongly suggest
  197. using rectangular coordinates and some derivative of the OFF-format.
  198.  
  199.      For starters, let me just throw in that while our universe may be
  200. infinite in all directions, that doesn't make for good programming.  We
  201. have to limit ourselves to small enough numbers that we can multiply
  202. them together without overflowing them, we can divide them without
  203. crashing our systems, and we can add them without accidentally flipping
  204. a sign bit.
  205.  
  206.      Now, the fun begins.  The simplest form of defining the Universe is
  207. to flat out say that the Universe stretches over these coordinates, say
  208. in the bounding box of <-65536, -65536, -65536> to <65536, 65536,
  209. 65536>.  This is often referred to as a Universal Coordinate system or
  210. an Absolute Coordinate system.  Then, each object in the Universe will
  211. be centered about some coordinate in that range.  This includes your
  212. viewpoint.  Several strategies are available for dealing with the edge
  213. of the Universe.  One can make the Universe wrap around so that an
  214. object leaving the cube at < X, Y, 65536> will re-appear in the Universe
  215. at < X, Y, -65536>.  Or, one can make objects bounce or stop at the edge
  216. of the Universe.  And, given any approach, one can have the edge of the
  217. Universe be transparent or opaque.
  218.  
  219.      In an Absolute Coordinate system, all objects must be shown from
  220. the position of your viewpoint.  This involves lots of interesting math
  221. that we'll get into later.  But, in general, an objects position with
  222. respect to you is it's absolute position - your absolute position (with
  223. all kinds of hell breaking loose if you can see past the edge of the
  224. Universe).  Then, after this position is calculated, it must be rotated
  225. based on your orientation in the Universe.
  226.  
  227.      Another possibility for defining space is a Relative Coordinate
  228. system or a View-Centered Coordinate system.  In this sort of system,
  229. the Viewpoint is always at coordinates <0,0,0> and everything else in
  230. the Universe is based relatively to this home position.  This causes
  231. funky math to come into play when dealing with velocities of objects,
  232. but... it does wonders for not having to deal with the 'edge of the
  233. Universe'.  This is the Schroedinger's cat method of the 'edge of the
  234. Universe'.... in the truest sense of out of sight is out of mind.  Small
  235. provisions have to be made if objects aren't to wrap around.  But... a
  236. Relative Coordinate system can be used to give the illusion of infinite
  237. space on a finite machine.  (Yes, even your 486/66DX is finite).
  238.  
  239.      I'll leave spherical coordinates to a later version if people think
  240. they'll be of use...
  241.  
  242. 4) How do I define position?
  243.  
  244.      Position in an Absolute Coordinate system is easy.  Each object has
  245.  
  246. three coordinates.  These are often stored in a data-type called a
  247. vector to abstract further the notion that these numbers belong
  248. together.
  249.  
  250.         typedef struct {
  251.                 long x;
  252.                 long y;
  253.                 long z;
  254.         } VECT;
  255.  
  256. Usually, each object in the Universe is defined about its center with
  257. each coordinate on its surface being centered at its own <0,0,0>.  This
  258. helps tremendously in rotating the object, and I would highly recommend
  259. this.  Then, the object as a whole is given a position in space.  When
  260. it comes time to draw this object, its points' coordinates get added on
  261. to its position.
  262.  
  263.      In a Relative Coordinate system, position is also fairly straight
  264. forward.  The view-point always has position VECT={ 0, 0, 0 };.  Other
  265. objects follow the same sort of system that they would in Absolute
  266. Coordinate systems.
  267.  
  268. 5) How do I define orientation?
  269.  
  270.      Orientation can be quite tricky.  I interchange some of the terms
  271. here quite often.  In 3-space, orientation must be defined be two-and-
  272. a-half angles.  "Two and a half?" you say.  Well, almost everyone uses
  273. three because two just isn't enough, but if you want to be technical,
  274. one of those angles only has to range from 0 - 180 degrees (0 - PI/2
  275. radians).
  276.  
  277.      But, taking that for granted now.... you have to pick an
  278. orientation for your view.  I personally prefer to have the X-axis run
  279. from left to right across the center of my screen.  I also like to have
  280. the Y-axis run from the bottom of my screen; and I also like to have the
  281. Z-axis running from me straight into my screen.  With some tweaking of
  282. plus and minus signs and a bit of re-ordering, all of the math here-in
  283. can be modified to reflect any orientation of the coordinate system.
  284. Some people prefer to have the Y-axis heading into the screen with the
  285. Z-axis going vertically.  It's all a matter of how you want to define
  286. stuff.
  287.  
  288.      Given that you've agreed with me that Z can go into the screen,
  289. what 3-angles do you need?  (Here's where I stand the biggest chance of
  290. mucking up the terms.)  You need roll, pitch, and yaw.  (I often mix up
  291. roll and yaw and such... so if you can follow along without getting
  292. locked into my terminology, future FAQ's will correct it.)
  293.  
  294.      Look at your monitor as you're reading this.  Now tilt your head so
  295. that your right ear is on your right shoulder.  This change in
  296. orientation is roll (or yaw... but I call it roll).
  297.  
  298.      Ok, now sit up straight again. Now bring your chin down to meet
  299. your chest.  (Hmmm... LOOK BACK NOW!!!, whew... glad you heard me.)
  300. That motion was pitch.
  301.  
  302.      Ok, now look over your right shoulder keeping your head vertical to
  303. see who's behind you.  (LOOK BACK AGAIN!!.)  Ok... that was yaw (or
  304. roll, but I call it yaw).
  305.  
  306.      That's the basics.  Now, what do I do with them?  Well, here's
  307.  
  308. where a nice book on Matrix Arithmetic will help you out.  You have to
  309.  
  310. use these three angles to make a Transformation matrix.  [See the
  311. section on Matrix Math].  Here is a typical method of doing these
  312. transformations: [Note, if you don't have Z going into your screen
  313. you'll have to munge these considerably].
  314.  
  315.         typedef double matrix[4][4];
  316.         double sr,sp,sy,cr,cp,cy;
  317.         matrix mr, mp, my;      /* individual transformations */
  318.         matrix s;               /* final matrix */
  319.  
  320.         sr = sin( roll );     cr = cos( roll );
  321.         sp = sin( pitch );    cp = cos( pitch );
  322.         sy = sin( yaw );      cy = cos( yaw );
  323.  
  324.                                 /* clear all matrixes
  325.                                 ** [See the section on Matrix Math]
  326.                                 */
  327.         identity( &mr ); identity( &mp ); identity( &my );
  328.                                 /* prepare roll matrix */
  329.         mr[0][0] = mr[1][1] = cr;
  330.         mr[1][0] = - (mr[0][1] = sr);
  331.  
  332.                                 /* prepare pitch matrix */
  333.         mp[1][1] = mp[2][2] = cp;
  334.         mp[1][2] = - (mp[2][1] = sp);
  335.  
  336.                                 /* prepare yaw matrix */
  337.         my[0][0] = my[2][2] = cy;
  338.         my[0][2] = - (my[2][0] = sy);
  339.  
  340.         multiply( &mr, &my, &s );
  341.         multiply( &s, &mp, &s );
  342.  
  343. 6) How do I define a velocity?
  344.  
  345. I've always been annoyed by programs that make it advantageous to run on
  346. a slower machine.  A naive view of velocity as n-units per time-
  347. through-this-loop means that things move faster if you get through the
  348. loop faster.
  349.  
  350.      In order to by-pass this problem, you have to keep track of time in
  351. some non-hardware-dependent way.  Then, you can define your velocity in
  352. n-units per known-time-unit.  For ease of mental calculations, I always
  353. define my velocities in units per second.  (In actuality, in programming
  354. in DOS, I define my velocity in n-units per 160/182ths of a second
  355. because I'd rather divide by 16 than 18.2).
  356.  
  357.      Here is some pseudo-code for an object moving along a line....
  358.  
  359.     int x = 0;                  /* position */
  360.     int vx = 10;                /* velocity */
  361.     int dx;
  362.     int err fact = 0;
  363.            -
  364.     int old time = gettime(); /* in milliseconds for instance */
  365.            -
  366.     int new time;
  367.            -
  368.     int elapsed;
  369.  
  370.     for (;;) {
  371.         new time = gettime();
  372.            -
  373.         elapsed = new time - old time;
  374.                      -          -
  375.         dx = vx * elapsed + err fact;
  376.                                -
  377.  
  378.         x += dx/1000;
  379.  
  380.         err fact = dx - (dx/1000)*1000;
  381.            -
  382.         old time = new time;
  383.            -          -
  384.     }
  385.  
  386.      You can ignore all of the err fact stuff if you like, but then
  387.                                   -
  388. velocities that are much lower than your time frequency could end up
  389. excessively slow or fast.
  390.  
  391.      This approach can be used for each linear velocity (x, y, and z).
  392. But, this approach isn't nearly so good for rotational velocities.  I'm
  393. still stewing on the best way to handle them.
  394.  
  395. 7) Drawing three-dimensional objects on a two-dimensional screen.
  396.  
  397.      Modified from comp.graphics FAQ:
  398.  
  399.      "There are many ways to do this.  Some approaches map the
  400.      viewing rectangle onto the scene, by shooting rays through
  401.      each pixel center and assigning color according to the object
  402.      hit by the ray.  Other approaches map the scene onto the
  403.      viewing rectangle, by drawing each object into the region,
  404.      keeping track of which object is in front of which.
  405.  
  406.      The mapping mentioned above is also referred to as a
  407.      'projection', and the two most popular projections are
  408.      perspective projection and parallel projection.  For example,
  409.      to do a parallel projection of a scene onto a viewing
  410.      rectangle, you can just discard the Z coordinate, and 'clip'
  411.      the objects to the viewing rectangle (discard portions that
  412.      lie outside the region).  To do a perspective projection,
  413.      dividing each the x and the y by some multiple or the Z-depth
  414.      is the usual approach.
  415.  
  416.      For details on 3D rendering, the Foley, van Dam, Feiner and
  417.      Hughes book, reading.  Chapter 6 is 'Viewing in 3D', and
  418.      chapter 15 is 'Visible-Surface Determination'.  For more
  419.      information go to chapter 16 for shading, chapter 19 for
  420.      clipping, and branch out from there."
  421.  
  422. 8) Vector Math - Dot Product and Cross-Product.
  423.  
  424.      Adding and subtracting vectors is as easy as subtracting their
  425. respective parts:
  426.  
  427.         <A,B,C> + <D,E,F> = <A+D, B+E, C+F>
  428.         <A,B,C> - <D,E,F> = <A-D, B-E, C-F>
  429.  
  430.      Scaling vectors is as simple as multiplying each part by a
  431. constant:
  432.  
  433.         S * <A,B,C> = <S*A, S*B, S*C>
  434.  
  435.      The Dot-Product of two vectors is simply the sum of the products of
  436. their respective parts:
  437.  
  438.         <A,B,C> . <D,E,F> = A*D + B*E + C*F
  439.  
  440. Note that this value is not a vector.
  441.  
  442.      The Cross-Product of two vectors is a bit more complex (it is the
  443. determinant of the matrix with the direction vector as the first row,
  444. the first vector as the second row, and the second vector as the third
  445. row):
  446.  
  447.         <A,B,C> X <D,E,F> = <B*F - C*E, C*D - A*F, A*E - B*D>
  448.  
  449. Note that:
  450.  
  451. <A,B,C> X <D,E,F> = -1 * ( <D,E,F> X <A,B,C> )
  452.         -and-
  453. (<A,B,C> X <D,E,F>) . <A,B,C> = (<A,B,C> X <D,E,F>) . <D,E,F> = 0
  454.  
  455.      More later.
  456.  
  457. 9) Matrix Math
  458.  
  459.      The identity matrix is a square matrix (same number of rows as
  460. columns) with all elements {i,j} given by:
  461.  
  462.                   { 1.0   if i == j
  463.         m[i][j] = {
  464.                   { 0.0   otherwise
  465.  
  466.      Multiplication of matrices:
  467.  
  468.     if X is a matrix that is m rows and n columns (an m-by-n (or mxn) matrix)
  469.     and Y is a matrix that is n rows and r columns (nxr), then the product
  470.     X * Y ==> m[i][j] = sum{ a=0, a<n, X[i][a] * Y[a][j] };
  471.  
  472. As you can see in this example, the result is a matrix with m rows and r
  473. columns (an mxr) matrix.  The most usual case in basic 3-d graphics is
  474. multiplication of 3x3 or 4x4 matrices to each other.
  475.  
  476.      Some important things to remember about matrix multiplication: (The
  477. following assume X and Y and Z are matrices and I is an identity matrix)
  478.  
  479.     X * Y rarely equals Y * X.  but,
  480.     X * ( Y * Z )  equals ( X * Y ) * Z
  481.     X * I = I * X = X
  482.     if (X * Y = I) then (Y * X = I)
  483.  
  484.      Multiplication of a Matrix and a Vector is simply a special case of
  485. multiplying two matrixes.  It just so happens that a vector is a matrix
  486. with only one column.  So, in order to multiply an mxn matrix by a
  487. vector, you have to have an nx1 matrix ("an n-vector" or "a vector in
  488. n").  The result is a vector in m.  As a quick example:
  489.  
  490.     [ a b ]   [ g ]   [ a*g + b*h ]
  491.     [ c d ] x [ h ] = [ c*g + d*h ]
  492.     [ e f ]           [ e*g + f*h ]
  493.  
  494. 10) Collisions.
  495.  
  496.      Sorry... This is a FAQ in-progress.  Will do more later.  Please
  497. feel free to send me a paragraph for this section.
  498.  
  499. 11) Perspective.
  500.  
  501. (Message nova:2746)
  502. From: Raul Deluth Miller <rockwell@nova.umd.edu>
  503.  
  504.      Perspective is easy: realize that the viewpoint isn't on the
  505. surface of the screen but is some distance back.  Then,
  506.  
  507.      [a] convert to a relative coordinate system centered around the
  508. viewpoint, oriented so that x and y are like this:
  509.                  y
  510.                  |
  511.  
  512.              ----+----x
  513.                  |
  514.  
  515. and z is the distance into the screen.  Now, divide your coordinates by
  516. z.
  517.  
  518.      That's all.
  519.  
  520.      Ok, sure, you don't really have to call your directions x y and z,
  521. but that's just changing the description which fits the math.  And,
  522. sure, you have to decide what to do about things that are "the other
  523. direction from the screen" -- but that's really something different.
  524.  
  525.      And, ok, you have to decide how many game units the viewpoint is,
  526. behind the screen [this is something you add to the z coordinate before
  527. dividing].  And, ok, maybe you want to scale things a bit to make it
  528. look nicer.  But none of that is really all that hard to deal with.
  529.  
  530.      What's annoying is that you don't want to do division for every
  531. pixel that you draw on the screen.  That means, for instance, that you
  532. might have a line where both ends are off the screen but part of the
  533. line [in the middle] are visible.  However, that's related to the
  534. problem of detecting collisions.  Or, better yet, clipping.  Yeah,
  535. that's it take a look at the section on clipping...
  536. Raul D. Miller             n =: p*q               NB. prime p, q, e
  537. <rockwell@nova.umd.edu>                           NB. public e, n, y
  538.                            y =: n&|&(*&x)^:e 1
  539.                            x -: n&|&(*&y)^:d 1    NB. 1 < (d*e) +.&<: (p,q)
  540.  
  541. 12) Z-Buffering & the Painters Algorithm & BSP-Trees.
  542.  
  543. There are several methods available for displaying polygons in a 3-d
  544. object so as to assure that what's behind is behind and what's in front
  545. is in front.  Each, however, has its pros and cons.  With each of these
  546. methods, it is often useful to implement backface culling.  Backface
  547. culling is simply not rendering surfaces which aren't facing you.
  548.  
  549.      The Painter's Algorithm is probably the most intuitive for the
  550. beginner, one of the easiest to implement, and the hardest to get right.
  551. The Painter's Algorithm, quite simply, is Paint the from back to front.
  552. Once it has been determined which polygons will be drawn, it is
  553. necessary to sort them from furthest away to closest.  Then, starting
  554. with the furthest polygon and moving to the closest, draw them.
  555.  
  556.      The tricky part of the Painter's Algorithm is determining when one
  557. polygon is closer or farther than another.  The closest to satisfactory
  558. method I've found uses this criteria -- Surface A is further away then
  559. surface B if:
  560.         1) The furthest point of A is further away than the furthest
  561.             point of B.
  562.  
  563.         2) B faces you more directly than A.
  564.  
  565.         3) The closest point of A is further than the closest point of B.
  566.         4) Surface B is more "important" than surface A.
  567. These are to be taken in order until a decision is reached.  But, I've
  568. yet to find good enough criteria to work well on most objects that
  569. people design.  If you keep these criteria in mind when making the
  570. objects, it can help.  But, if you're using 3rd party objects, I'd
  571. suggest a different method.
  572.  
  573.      Z-Buffering is a buzzword that's finally come down to reality.
  574. It's been replaced by 'texture-mapping' and 'bump-mapping' and 'realtime
  575. raytracing' as the 'what's fancy' buzzword.  Z-buffering is costly in
  576. terms of memory and processing time, but gives beautiful results.  If
  577. you've got some freedom with your memory usage and a bit of processor
  578. time to spare, and you're ok with filling your own polygons, then Z-
  579. buffering may be for you.
  580.  
  581.      To Z-buffer, one keeps a 2d-array of numbers (shorts or ints
  582. usually) the same dimensions as the viewport.  As each <X,Y,Z> point is
  583. prepared for display, the final value is compared to the Z value already
  584. in the <X,Y> position in the Z-buffer array.  If the new Z is less than
  585. the one already in the buffer, the pixel-color is placed in the Screen-
  586. Buffer at position <X,Y> and the new Z is copied into position <X,Y> of
  587. the Z-Buffer.
  588.  
  589.      The expense of Z-Buffering is having to set each element to the
  590. MAX Z VALUE after each frame and keeping track of the Z-value for every
  591.    - -
  592. pixel in the Screen Buffer and every pixel in the polygon you're
  593. drawing.  In most applications, it is unnecessary to keep track of the
  594. Z-values of any more than the vertices of a polygon.
  595.  
  596.      And, my favorite of the bunch (but the nastiest to store in an
  597. object file, is BSP-trees.  BSP-tree stands for Binary Space Partition
  598. tree.  They are based the idea that surfaces of most objects don't
  599. change positions relative to each other.  Objects whose surfaces change
  600. relative positions cannot be (to my knowledge) easily used with BSP-
  601. trees.
  602.  
  603.      The first step in using BSP-trees is to break up your object into a
  604. 'good' object.  But, I can't think of a way to describe a 'good' object
  605. without just telling you how to make one.  So, here's a general
  606. algorithm for making a BSP-tree object from ye average object:
  607.     1) Pick a surface to call the root node.
  608.     2) Compare every other surface in the set of surfaces to the
  609.         root node.
  610.     3) If a surface is In-Front-Of the root node, then put it in
  611.         the 'Front Heap'.
  612.        If a surface is Behind the root node, then put it in the
  613.         'Back Heap'.
  614.        If neither of the above is true, chop up the surface into
  615.         smaller surfaces that are all either In-Front-Of or Behind
  616.         the root node.  (This is the tricky bit).
  617.     4) Repeat these steps with the 'Front Heap' and the 'Back Heap'.
  618. As an interesting note, I can see no advantage at all to trying to keep
  619. a relatively balanced tree.
  620.  
  621.      Now comes the interesting bit.  Now that the object is a 'good'
  622. BSP-tree object, it is time for the drawing algorithm.  This is fairly
  623. straight- forward.
  624.     1) Compare the root node with you.
  625.     2) If you are In-Front-Of the root node,
  626.         draw 'Root->Back', then the Root surface, then 'Root->Front'.
  627.        If you are Behind the root node,
  628.  
  629.         draw 'Root->Front', then the Root surface (unless you're
  630.  
  631.         backface culling), then 'Root->Back'.
  632.  
  633.      I fully admit that I didn't believe BSP-trees would work when I
  634. first read about them in Foley-van Dam, nor did I believe they would
  635. work after seeing them work for me.  Now, three months later, I fully
  636. believe they work, and I can even say I understand why.  I may soon
  637. attempt an explanation here, but suffice it for now to say, 'they can be
  638. grokked'.
  639.  
  640. 13) Shading.
  641.  
  642.      Sorry... This is a FAQ in-progress.  Will do more later.  Please
  643. feel free to send me a paragraph for this section.
  644.  
  645. 14) 3-space clipping.
  646.  
  647.      Sorry... This is a FAQ in-progress.  Will do more later.  Please
  648. feel free to send me a paragraph for this section.
  649.  
  650. 15) 3-d scanning.
  651.  
  652. Three dimensional scanning is a verifiable pain.  I've only really heard
  653. of three basic techniques.  Two of them require the scanned item to be
  654. entirely still, and one of them even destroys the object being scanned.
  655.  
  656.      The first technique I ever saw was in 'The Making of T-2'.  An
  657. actor was dressed in tight, dark clothing and a mesh of masking tape was
  658. placed over his body.  This mesh was filmed simultaneously from several
  659. different camera positions as the actor walked.  The films were then
  660. digitally processed to provide a close model of a person walking.  The
  661. amount of work and expense involved in creating the images, registration
  662. of the multiple images, and conversion into 3-space data makes this a
  663. big time investment.
  664.  
  665.      The second technique I saw set the item to be scanned on a
  666. turntable.  A laser sensing system or simple armature-stylus were used
  667. to obtain a bump-map along a vertical slice of the item.  Then, the item
  668. was rotated 5 degrees and the process repeated.  The gives a very
  669. detailed cylindrical bump-map of an item.
  670.  
  671.      The third technique is by far the most interesting I've seen.  But,
  672. it is useful mostly only for height-field generation.  This method
  673. employs a CAT-scan like approach.  First, the object is painted solid
  674. black (or white).  Then, the item is dipped in milk (or chocolate syrup)
  675. to a certain depth.  A still photo is taken.  Then, the item is dipped
  676. further into the liquid.  Another still is taken.  This is done to
  677. obtain slice view of the item.  The photos are then digitally scanned to
  678. locate the boundaries of the items.
  679.  
  680. 16) Publically available source-code.
  681.  
  682.      Well, I've started a collection at ftp.csh.rit.edu::/pub/3dfaq/src.
  683. Feel free to upload relevant stuff (that I can't find on archie in under
  684. twenty minutes).  Some other sites of interest:
  685.     3d images;
  686.         ftp://wuarchive.wustl.edu/graphics/ray
  687.         ftp://princeton.edu/pub/Graphics
  688.  
  689.     3d Information:
  690.  
  691.         http://www.mindspring.com/~cwatkins/algor.html
  692.  
  693.         http://archpropplan.auckland.ac.nz/People/Paul/Paul.html
  694.         ftp://x2ftp.oulu.fi/pub/msdos/programming/docs/zed3d023.zip
  695.         http://www.csh.rit.edu/~pat/misc/3dFaq.html
  696.  
  697.     3d geometry files:
  698.         http://archpropplan.auckland.ac.nz/People/Paul/Paul.html
  699.         ftp://ftp.kgc.com/pub/mirror/avalon
  700.  
  701.     ray tracers:
  702.         ftp://alfred.ccs.carleton.ca/pub/pov-ray
  703.         http://www.mindspring.com/~cwatkins/algor.html
  704.  
  705.     stereograms:
  706.         ftp://ftp.comlab.ox.ac.uk/pub/Documents/3d
  707.         http://enigma.phys.utk.edu/stereo/index.html
  708.         ftp://sugrfx.acs.syr.edu/3d/stereograms
  709.  
  710.      Sorry... This is a FAQ in-progress.  Will do more later.  Please
  711. feel free to send me a paragraph for this section.
  712.  
  713. 17) Books on the topics.
  714.  
  715. Computer Graphics: Principles and Practice Foley, van Dam, Feiner, and
  716.      Hughes; Addison Wesley -- Reading, Massachusetts; (c) 1990.  ISBN
  717.      0-201-12110-7.
  718.  
  719.      I highly reccomend this book for the person seriously interested in
  720.      understanding Computer Graphics concepts for 2-D image-generation
  721.      and 3-D representation.  As a warning though, if you're struggling
  722.      to follow vector math and such, you might not spend the $60-$80
  723.      bucks on this one yet.
  724.  
  725. Computer Graphics Handbook: Geometry and Mathematics, Michael E.
  726.      Mortenson.  ISBN 0-8311-1002-3
  727.  
  728.      I've never seen this one, but it comes net-recommended.  Anyone
  729.      care to make a more complete statement?
  730.  
  731. Programming in 3 Dimensions Christopher D. Watkins and Larry Sharp;
  732.      Barnes & Noble.  ISBN: 1-55851-220-9 $39.95
  733.  
  734.      I've never seen this book.  I've got an ad for it in front of me.
  735.      I would guess it's a very low-density version of some of the 3-D
  736.      things from Foley.  The book boasts sample source code on MS/PC-DOS
  737.      floppy included.  "This one is for all graphics enthusiasts who
  738.      want a detailed look at 3-D graphics and modeling.  Also features
  739.      discussions of popular ray tracinge methods and computer
  740.      animation." [sic]
  741.  
  742.      From: MarsSaxMan@aol.com
  743.  
  744.      You mentioned Watkins & Sharp's book "Programming in 3
  745.      Dimensions" in your FAQ... I have this book, so I thought I'd
  746.      give you a capsule review.
  747.  
  748.      I like the book overall. It is cleanly written & they give a
  749.      pretty good explanation of the mathematics (of course, having
  750.      spent eons in advanced math has probably bent my perspective
  751.      on what's clear in mathematics). However it doesn't get too
  752.      bogged down in theory, which is nice. There are also a bunch
  753.      of interesting side issues that get covered, such as fractals,
  754.  
  755.      color cycling, color tables, etc. Complete source code to a
  756.  
  757.      ray tracer, an animation interface to the ray tracer, and some
  758.      interesting display hacks are included.
  759.       Overall a very practical way to get into 3-D graphics; it's
  760.      pretty easy to see how the code could be modified for this or
  761.      that purpose. It'll take some work to turn its raytracer into
  762.      a Doom engine or whatever but it could be done & would still
  763.      be worth it.
  764.  
  765.      Only thing I didn't really like (though this is probably a
  766.      natural consequence of the down-to-earth real-world nature of
  767.      the book) is that it is a bit too DOS PC specific. I hack
  768.      Macintosh so a lot of the graphics code is unusable and some
  769.      of the memory things have to be changed; one chapter on VGA
  770.      graphics is wasted on me. But overall I think the book is a
  771.      good buy. Also it's $39.95, so it's not a bad buy compared to
  772.      some other 3-D books I've seen (gacckkk!)
  773.  
  774.      Just thought you might like to know, and maybe have a bit more
  775.      filler to stash in the FAQ...
  776.  
  777.      -MarsSaxMan Red Planet Software
  778.  
  779. And, if you liked that... Christopher Watkins sent me a more complete
  780.      book list (most of which follows).  Feel free to send me any
  781.      personal reviews of any of these books.  Like or dislike.
  782.  
  783. Stereogram Programming Techniques.  Christopher D. Watkins and Vincent
  784.      P. Mallette (Charles River Media, Inc.), 1995 ISBN: 1-886801-00-2
  785.      $34.95 Learning Windows(tm) Programming Using Virtual Reality.
  786.      Christopher D. Watkins and Russ Berube (Academic Press
  787.      Professional, Inc.), 1994 ISBN: 0-12-737842-1
  788.  
  789.      learning Microsoft Windows(tm) programming by developing a
  790.      texturing 3-D game engine and application similar to Wolfenstein
  791.      3-D(tm) and the popular game DOOM(tm)
  792.  
  793. Virtual Reality ExCursions with Programs in C.  Christopher D. Watkins
  794.      and Stephen R. Marenka (Academic Press Professional, Inc.), 1994
  795.      ISBN: 0-12-737865-0 $39.95
  796.  
  797.      produce polygonal 3-D virtual worlds on your PC, understand human
  798.      perception issues, near complete list of VR information and
  799.      technology sources, includes all source code, includes anaglyph 3-D
  800.      glasses
  801.  
  802. Photorealism and Ray Tracing in C Christopher D. Watkins and Stephen B.
  803.      Coy (M&T Books, Inc.), 1992 ISBN: 1-55851-247-0 $44.95
  804.  
  805.      generate photorealistic ray-traced computer graphics, includes all
  806.      source code for a photorealistic renderer and assorted fractal (and
  807.      other) database generators, includes many 3-D models
  808.  
  809. Exploring Photorealism and Ray Tracing Christopher D. Watkins, Vincent
  810.      Mallette, Stephen R. Marenka and Robert Johnson (M&T Books, Inc.),
  811.      1995 ISBN: 1-55851-383-3 $29.95
  812.  
  813.      source code for a beginner's ray tracer, the definitive beginner's
  814.      book on ray tracing, faster BOB/VIVID executable than in the
  815.      original, Photorealism and Ray Tracing in C book with even more
  816.      texture mapping and texturing capabilities, a CD ROMs worth of 3-D
  817.      models, worlds and tools, various 3-D modeling tools (blobs,
  818.      polygonal, etc.), learn how to raytrace the beautiful quaternion
  819.  
  820.      Julia set fractals (Quaternion -> 4-dimensional object)
  821.  
  822. Advanced Graphics Programming in C and C++ Roger Stevens and Christopher
  823.      D. Watkins (M&T Books, Inc.), 1991 ISBN: 1-55851-171-7 $39.95
  824.  
  825.      beginners book to computer graphics, includes a simple ray tracer,
  826.      polygon renderer, and height-field renderer, and fractals, includes
  827.      all source code, PASCAL version of this book also available, 1990
  828.  
  829. 18) Other forums.
  830.  
  831.      Sorry... This is a FAQ in-progress.  Will do more later.  Please
  832. feel free to send me a paragraph for this section.
  833.  
  834. 19) Current Contents of Archive ftp.csh.rit.edu::/pub/3dfaq
  835.  
  836.    2 -rw-r--r--    1 pat      member    219 Apr  2 20:40 README
  837.   16 -rw-r--r--    1 pat      member   7787 Mar 28 19:11 DoomTechniques.gz
  838.   10 -rw-r--r--    1 pat      member   4243 May 23 12:34 bsp.gz
  839.   14 -rw-r--r--    1 pat      vr       6266 Apr  2 20:38 imath.gz
  840.  
  841.   22 -rw-r--r--    1 pat      vr      11262 Apr  2 20:43 imath.tar.gz
  842.  
  843. -- 
  844.     "li ci cu ba na kancu .i ba kancu fi li ri .gi ko ba na kancu
  845.      fi li vo gi'enai gi ko ba kancu fi li re gi'o ko ba ba kancu
  846.      fi ci .i ju'onai zo mu drani"
  847.  
  848.