home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / FAQSYS18.ZIP / FAQS.DAT / 3DFAQ.TXT < prev    next >
Internet Message Format  |  1996-01-04  |  17KB

  1. From pat@nick.csh.rit.edu (Pat Fleckenstein (HoseNode))
  2. Newsgroups: rec.games.programmer
  3. Subject: FAQ: 3-D Information for the Programmer
  4. Date: Thu, 3 Mar 1994 18:26:18 GMT
  5.  
  6.  
  7.           ______________________________________
  8.           |                                    |\
  9.           |  @@@@@    @@@@    @@@@  @@   @@@   |*|
  10.           |     @     @@  @   @@   @@ @ @@  @  |*|
  11.           |    @@  @@ @@  @   @@@@ @@@@ @@  @  |*|
  12.           |  @@  @    @@  @   @@   @@ @ @@ @@  |*|
  13.           |   @@@     @@@@    @@   @@ @  @@@@  |*|
  14.           |____________________________________|*|
  15.            \***********************************\*|
  16.             \*better*graphics*later*I*promise***\|
  17.              ------------------------------------
  18.  
  19.  
  20. Contents:
  21.  
  22. 1) General references for 3-d graphics questions.
  23.  
  24. 2) How do I define an object?
  25.  
  26. 3) How do I define space?
  27.  
  28. 4) How do I define position?
  29.  
  30. 5) How do I define orientation?
  31.  
  32. 6) How do I define a velocity?
  33.  
  34. 7) Drawing three-dimensional objects on a two-dimensional screen.
  35.  
  36. 8) Vector Math - Dot Product and Cross-Product.
  37.  
  38. 9) Matrix Math
  39.  
  40. 10) Collisions.
  41.  
  42. 11) Perspective.
  43.  
  44. 12) Z-Buffering & the Painters Algorithm.
  45.  
  46. 13) Shading.
  47.  
  48. 14) 3-space clipping.
  49.  
  50. 15) 3-d scanning.
  51.  
  52. 16) Publically available source-code.
  53.  
  54. 17) Books on the topics.
  55.  
  56. 18) Other forums.
  57.  
  58.  
  59. Last update:
  60.      21Dec93
  61.  
  62. What's new?
  63.  
  64.      This whole ball of wax.
  65.  
  66. 1) General references for 3-d graphics questions.
  67.  
  68.      Well, this FAQ is just getting off the ground.  Hopefully it will
  69. touch on most of the bases you need to get started for now, and hope-
  70. fully it will expand at least as fast as you need it too.  But...
  71. regardless, things you'll want to locate for more help are Matrix Alge-
  72. bra books, Physics books talking about Eulerian motion, and some books
  73. on the Graphics Hardware you want to program for.  The code examples
  74. included in this FAQ will most likely be in C with pseudo-code in com-
  75. ments.
  76.  
  77.      But, you'll also want to defintely check out the FAQ for
  78. comp.graphics.  That FAQ touches mainly on 2-D needs, but some 3-D
  79. aspects are reviewed there, too.
  80.  
  81. 2) How do I define an object?
  82.  
  83.      There are lots of ways to define objects.  One of the most commonly
  84. used is the OFF (Object File Format).  The OFF toolkit and a library of
  85. objects are available via anonymous ftp from gatekeeper.dec.com -- XXX
  86. ???.  The format provides easy methods for extensions and a base set of
  87. things you can expect for each object.  The toolkit is a bit bulky, but
  88. the file format (in ascii) is easy enough to 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.         x(npts) y(npts) z(npts)
  109.  
  110. Then, it gets a bit more complicated.  The following lines begin with a
  111. number to indicate the number of vertices in this polygon.  That number
  112. is followed by that many numbers, one for each vertice.  These are given
  113. in an order specified in the .aoff (usually conter-clockwise).  So, for
  114. example, a triangle and a pentagon which share a side are shown below.
  115.  
  116.         3       1 3 4
  117.         5       2 4 3 6 7
  118.  
  119. Here is some quick and dirty sample code to read in the .geom file:
  120.  
  121. struct polygon {
  122.     int nvert;          /* Number of vertices in this polygon */
  123.     int *verts;         /* Vertices in this polygon */
  124. };
  125.  
  126. struct object {
  127.     int npts;           /* The number of points */
  128.     int npolys;         /* The number of polygons */
  129.     int nsegs;          /* The number of segments */
  130.     double *point x,*point y,*point z;
  131.                  -        -        -
  132.     struct polygon *polys;
  133. };
  134.  
  135. int
  136. read geom file( char *geom file, struct object *obj )
  137.     -    -                -
  138. {
  139.     FILE *fp;
  140.     int i,j;
  141.  
  142.     if (!(fp = fopen(geom file,"r")))           /* Open the .geom file */
  143.                          -
  144.         return -1;
  145.  
  146.                                                 /* Get header information */
  147.     fscanf(fp,"%d %d %d",&obj.npts,&obj.npolys,&obj.nsegs);
  148.  
  149.         /*
  150.         ** Allocate room for the points.
  151.         */
  152.     obj.point x = (double *)malloc(obj.npts*sizeof(double));
  153.              -
  154.     obj.point y = (double *)malloc(obj.npts*sizeof(double));
  155.              -
  156.     obj.point z = (double *)malloc(obj.npts*sizeof(double));
  157.              -
  158.  
  159.     for (i=0;i<obj.npts;++i)
  160.         fscanf(fp,"%lf %lf %lf",&obj.point x[i],
  161.                                           -
  162.                                 &obj.point y[i],
  163.                                           -
  164.                                 &obj.point z[i]);
  165.                                           -
  166.  
  167.         /* Allocate room for the polygons.  */
  168.     obj.polys = (struct polygon *)malloc(obj.npolys*sizeof(struct polygon));
  169.  
  170.     for (i=0;i<obj.npts;++i) {
  171.             /* See how many vertices this has */
  172.         fscanf(fp,"%d",&obj.polys[i].nvert);
  173.  
  174.             /* Allocate room for vertices */
  175.         obj.polys[i].verts = (int *)malloc(obj.npolys*sizeof(int));
  176.  
  177.             /* Get each vertex */
  178.         for (j=0;j<obj.polys[i].nvert;++j)
  179.             fscanf(fp,"%d",&obj.polys[i].verts[j]);
  180.     }
  181. }
  182.  
  183. 3) How do I define space?
  184.  
  185.      There are several things to consider when picking a coordinate sys-
  186. tem.  Most important of these is how you intend to handle objects.  If
  187. your objects are defined in terms of <x,y,z> triplets, it will require a
  188.  
  189. fair bit of work on reading them in to turn them into spherical
  190.  
  191. coordinates.  If you're looking to this FAQ for information on how to
  192. define the space your objects will be in, I'd strongly suggest using
  193. rectangular coordinates and some derivative of the OFF-format.
  194.  
  195.      For starters, let me just throw in that while our universe may be
  196. infinite in all directions, that doesn't make for good programming.  We
  197. have to limit ourselves to small enough numbers that we can multiply
  198. them together without overflowing them, we can divide them without
  199. crashing our systems, and we can add them without accidentally flipping
  200. a sign bit.
  201.  
  202.      Now, the fun begins.  The simplest form of defining the Universe is
  203. to flat out say that the Universe stretches over these coordinates, say
  204. in the bounding box of <-65536, -65536, -65536> to <65536, 65536,
  205. 65536>.  This is often referred to as a Universal Coordinate system or
  206. an Absolute Coordinate system.  Then, each object in the Universe will
  207. be centered about some coordinate in that range.  This includes your
  208. viewpoint.  Several strategies are available for dealing with the edge
  209. of the Universe.  One can make the Universe wrap around so that an
  210. object leaving the cube at < X, Y, 65536> will re-appear in the Universe
  211. at < X, Y, -65536>.  Or, one can make objects bounce or stop at the edge
  212. of the Universe.  And, given any approach, one can have the edge of the
  213. Universe be transparent or opaque.
  214.  
  215.      In an Absolute Coordinate system, all objects must be shown from
  216. the position of your viewpoint.  This involves lots of interesting math
  217. that we'll get into later.  But, in general, an objects position with
  218. respect to you is it's absolute position - your absolute position (with
  219. all kinds of hell breaking loose if you can see past the edge of the
  220. Universe).  Then, after this position is calculated, it must be rotated
  221. based on your orientation in the Universe.
  222.  
  223.      Another possibility for defining space is a Relative Coordinate
  224. system or a View-Centered Coordinate system.  In this sort of system,
  225. the Viewpoint is always at coordinates <0,0,0> and everything else in
  226. the Universe is based relatively to this home position.  This causes
  227. funky math to come into play when dealing with velocities of objects,
  228. but... it does wonders for not having to deal with the 'edge of the
  229. Universe'.  This is the Schroedinger's cat method of the 'edge of the
  230. Universe'.... in the truest sense of out of sight is out of mind.  Small
  231. provisions have to be made if objects aren't to wrap around.  But... a
  232. Relative Coordinate system can be used to give the illusion of infinite
  233. space on a finite machine.  (Yes, even your 486/66DX is finite).
  234.  
  235.      I'll leave spherical coordinates to a later version if people think
  236. they'll be of use...
  237.  
  238. 4) How do I define position?
  239.  
  240.      Position in an Absolute Coordinate system is easy.  Each object has
  241. three coordinates.  These are often stored in a data-type called a vec-
  242. tor to abstract further the notion that these numbers belong together.
  243.  
  244.         typedef struct {
  245.                 long x;
  246.                 long y;
  247.                 long z;
  248.         } VECT;
  249.  
  250. Usually, each object in the Universe is defined about its center with
  251. each coordinate on its surface being centered at its own <0,0,0>.  This
  252.  
  253. helps tremendously in rotating the object, and I would highly recommend
  254.  
  255. this.  Then, the object as a whole is given a position in space.  When
  256. it comes time to draw this object, its points' coordinates get added on
  257. to its position.
  258.  
  259.      In a Relative Coordinate system, position is also fairly straight
  260. forward.  The view-point always has position VECT={ 0, 0, 0 };.  Other
  261. objects follow the same sort of system that they would in Absolute Coor-
  262. dinate systems.
  263.  
  264. 5) How do I define orientation?
  265.  
  266.      Orientation can be quite tricky.  I interchange some of the terms
  267. here quite often.  In 3-space, orientation must be defined be two-and-
  268. a-half angles.  "Two and a half?" you say.  Well, almost everyone uses
  269. three because two just isn't enough, but if you want to be technical,
  270. one of those angles only has to range from 0 - 180 degrees (0 - PI/2
  271. radians).
  272.  
  273.      But, taking that for granted now.... you have to pick an orienta-
  274. tion for your view.  I personally prefer to have the X-axis run from
  275. left to right across the center of my screen.  I also like to have the
  276. Y-axis run from the bottom of my screen; and I also like to have the Z-
  277. axis running from me straight into my screen.  With some tweaking of
  278. plus and minus signs and a bit of re-ordering, all of the math here-in
  279. can be modified to reflect any orientation of the coordinate system.
  280. Some people prefer to have the Y-axis heading into the screen with the
  281. Z-axis going vertically.  It's all a matter of how you want to define
  282. stuff.
  283.  
  284.      Given that you've agreed with me that Z can go into the screen,
  285. what 3-angles do you need?  (Here's where I stand the biggest chance of
  286. mucking up the terms.)  You need roll, pitch, and yaw.  (I often mix up
  287. roll and yaw and such... so if you can follow along without getting
  288. locked into my terminology, future FAQ's will correct it.)
  289.  
  290.      Look at your monitor as you're reading this.  Now tilt your head so
  291. that your right ear is on your right shoulder.  This change in orienta-
  292. tion is roll (or yaw... but I call it roll).
  293.  
  294.      Ok, now sit up straight again. Now bring your chin down to meet
  295. your chest.  (Hmmm... LOOK BACK NOW!!!, whew... glad you heard me.)
  296. That motion was pitch.
  297.  
  298.      Ok, now look over your right shoulder keeping your head vertical to
  299. see who's behind you.  (LOOK BACK AGAIN!!.)  Ok... that was yaw (or
  300. roll, but I call it yaw).
  301.  
  302.      That's the basics.  Now, what do I do with them?  Well, here's
  303. where a nice book on Matrix Arithmetic will help you out.  You have to
  304. use these three angles to make a Transformation matrix.  [See the sec-
  305. tion on Matrix Math].  Here is a typical method of doing these transfor-
  306. mations: [Note, if you don't have Z going into your screen you'll have
  307. to munge these considerably].
  308.  
  309.         typedef double matrix[4][4];
  310.         double sr,sp,sy,cr,cp,cy;
  311.         matrix mr, mp, my;      /* individual transformations */
  312.         matrix s;               /* final matrix */
  313.  
  314.         sr = sin( roll );     cr = cos( roll );
  315.         sp = sin( pitch );    cp = cos( pitch );
  316.  
  317.         sy = sin( yaw );      cy = cos( yaw );
  318.  
  319.                                 /* clear all matrixes
  320.                                 ** [See the section on Matrix Math]
  321.                                 */
  322.         identity( &mr ); identity( &mp ); identity( &my );
  323.                                 /* prepare roll matrix */
  324.         mr[0][0] = mr[1][1] = cr;
  325.         mr[1][0] = - (mr[0][1] = sr);
  326.  
  327.                                 /* prepare pitch matrix */
  328.         mp[1][1] = mp[2][2] = cp;
  329.         mp[1][2] = - (mp[2][1] = sp);
  330.  
  331.                                 /* prepare yaw matrix */
  332.         my[0][0] = my[2][2] = cy;
  333.         my[0][2] = - (my[2][0] = sy);
  334.  
  335.         multiply( &mr, &my, &s );
  336.         multiply( &s, &mp, &s );
  337.  
  338. 6) How do I define a velocity?
  339.  
  340. Sticky question.  I'll get to it in the next rev.
  341.  
  342. 7) Drawing three-dimensional objects on a two-dimensional screen.
  343.  
  344.      From comp.graphics FAQ:
  345.  
  346.      "There are many ways to do this.  Some approaches map the
  347.      viewing rectangle onto the scene, by shooting rays through
  348.      each pixel center and assigning color according to the object
  349.      hit by the ray.  Other approaches map the scene onto the view-
  350.      ing rectangle, by drawing each object into the region, keeping
  351.      track of which object is in front of which.
  352.  
  353.      The mapping mentioned above is also referred to as a 'projec-
  354.      tion', and the two most popular projections are perspective
  355.      projection and parallel projection.  For example, to do a
  356.      parallel projection of a scene onto a viewing rectangle, you
  357.      can just discard the Z coordinate (divide by depth), and
  358.      'clip' the objects to the viewing rectangle (discard portions
  359.      that lie outside the region).
  360.  
  361.      For details on 3D rendering, the Foley, van Dam, Feiner and
  362.      Hughes book, reading.  Chapter 6 is 'Viewing in 3D', and
  363.      chapter 15 is 'Visible-Surface Determination'.  For more
  364.      information go to chapter 16 for shading, chapter 19 for clip-
  365.      ping, and branch out from there."
  366.  
  367. 8) Vector Math - Dot Product and Cross-Product.
  368.  
  369.      Adding and subtracting vectors is as easy as subtracting their
  370. respective parts:
  371.  
  372.         <A,B,C> + <D,E,F> = <A+D, B+E, C+F>
  373.         <A,B,C> - <D,E,F> = <A-D, B-E, C-F>
  374.  
  375.      Scaling vectors is as simple as multiplying each part by a con-
  376. stant:
  377.  
  378.         S * <A,B,C> = <S*A, S*B, S*C>
  379.  
  380.      The Dot-Product of two vectors is simply the sum of the products of
  381. their respective parts:
  382.  
  383.         <A,B,C> . <D,E,F> = A*D + B*E + C*F
  384.  
  385. Note that this value is not a vector.
  386.  
  387.      The Cross-Product of two vectors is a bit more complex (it is the
  388. determinant of the matrix with the direction vector as the first row,
  389. the first vector as the second row, and the second vector as the third
  390. row):
  391.  
  392.         <A,B,C> X <D,E,F> = <B*F - C*E, C*D - A*F, A*E - B*D>
  393.  
  394. Note that:
  395.  
  396. <A,B,C> X <D,E,F> = -1 * ( <D,E,F> X <A,B,C> )
  397.         -and-
  398. (<A,B,C> X <D,E,F>) . <A,B,C> = (<A,B,C> X <D,E,F>) . <D,E,F> = 0
  399.  
  400.      More later.
  401.  
  402. 9) Matrix Math
  403.  
  404.      The identity matrix is the one with all elements {i,j} given by:
  405.  
  406.                   { 1.0   if i == j
  407.         m[i][j] = {
  408.                   { 0.0   otherwise
  409.  
  410.      Sorry... wanted to get at least a partial FAQ out soon, so this
  411. section is mostly blank for now.  Will do more later.
  412.  
  413. 10) Collisions.
  414.  
  415.      Sorry... wanted to get at least a partial FAQ out soon, so this
  416. section is mostly blank for now.  Will do more later.
  417.  
  418. 11) Perspective.
  419.  
  420.      Sorry... wanted to get at least a partial FAQ out soon, so this
  421. section is mostly blank for now.  Will do more later.
  422.  
  423. 12) Z-Buffering & the Painters Algorithm.
  424.  
  425.      Sorry... wanted to get at least a partial FAQ out soon, so this
  426. section is mostly blank for now.  Will do more later.
  427.  
  428. 13) Shading.
  429.  
  430.      Sorry... wanted to get at least a partial FAQ out soon, so this
  431. section is mostly blank for now.  Will do more later.
  432.  
  433. 14) 3-space clipping.
  434.  
  435.      Sorry... wanted to get at least a partial FAQ out soon, so this
  436. section is mostly blank for now.  Will do more later.
  437.  
  438. 15) 3-d scanning.
  439.  
  440.      Sorry... wanted to get at least a partial FAQ out soon, so this
  441. section is mostly blank for now.  Will do more later.
  442.  
  443. 16) Publically available source-code.
  444.  
  445.      Sorry... wanted to get at least a partial FAQ out soon, so this
  446. section is mostly blank for now.  Will do more later.
  447.  
  448. 17) Books on the topics.
  449.  
  450.      Sorry... wanted to get at least a partial FAQ out soon, so this
  451. section is mostly blank for now.  Will do more later.
  452.  
  453. 18) Other forums.
  454.  
  455.      Sorry... wanted to get at least a partial FAQ out soon, so this
  456. section is mostly blank for now.  Will do more later.
  457.  
  458. -- 
  459.     mu
  460.  
  461.