home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / 3dstruct.h next >
C/C++ Source or Header  |  1996-03-19  |  10KB  |  302 lines

  1. /* Data structures for 3D modelling */
  2.  
  3. /* Written by Bernie Roehl and Dave Stampe, December 1991 */
  4. /* updated 10/1/91 for renderer clip (first stage */
  5. /* Integerizaation finished 19/1/91 and comments added */
  6.  
  7. // Massive upgrades since-- who's counting?
  8.  
  9. /*
  10.  This code is part of the VR-386 project, created by Dave Stampe.
  11.  VR-386 is a desendent of REND386, created by Dave Stampe and
  12.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  13.  Stampre for VR-386.
  14.  
  15.  Copyright (c) 1994 by Dave Stampe:
  16.  May be freely used to write software for release into the public domain
  17.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  18.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  19.  this software or source code into their products!  Usually there is no
  20.  charge for under 50-100 items for low-cost or shareware products, and terms
  21.  are reasonable.  Any royalties are used for development, so equipment is
  22.  often acceptable payment.
  23.  
  24.  ATTRIBUTION:  If you use any part of this source code or the libraries
  25.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  26.  and any other authors in your documentation, source code, and at startup
  27.  of your program.  Let's keep the freeware ball rolling!
  28.  
  29.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  30.  REND386, improving programmer access by rewriting the code and supplying
  31.  a standard API.  If you write improvements, add new functions rather
  32.  than rewriting current functions.  This will make it possible to
  33.  include you improved code in the next API release.  YOU can help advance
  34.  VR-386.  Comments on the API are welcome.
  35.  
  36.  CONTACT: dstampe@psych.toronto.edu
  37. */
  38.  
  39.  
  40.  
  41. /* NOTE ON FORMATS:
  42.  <nn.ff> means that nn bits are available as the integer part, and
  43.  ff bits are used as a fractional part.  Multiply a float by 2^ff
  44.  to get a long value to feed in.
  45.  
  46.  World coordinates should be kept below signed 24 bits (16.7M) to
  47.  assure no errors.  This allows a range of 1mm->16.7 km, which
  48.  should be plenty!
  49.  
  50.  Angles are in <16.16> format, where the integer part is in degrees.
  51. */
  52.  
  53.  
  54. #ifndef MATRIXDEF
  55. typedef long MATRIX[4][3];        /* 3x3 rotate plus 3 translate para's */
  56.             /* rotate matrix is <3.29>, translate is <32.0> */
  57. #define MATRIXDEF 1
  58. #endif
  59.  
  60. /* new vertex copies, internal to renderer */
  61.  
  62. typedef struct nV NVERTEX;
  63. struct nV{
  64.         long  x, y, z;             /* viewport coordinates (used for saving time) */
  65.       long  xs, ys ;          /* screen coordinates */
  66.         unsigned char outcode;  /* XY clip outcodes */
  67.         unsigned char perspect; /* flags perspective done */
  68.         };
  69.  
  70. #define LEFT   1    /* XY outcode bits */
  71. #define RIGHT  2
  72. #define TOP    4
  73. #define BOTTOM 8
  74.  
  75.  
  76.  
  77. /* world database vertices */
  78. /* object coords are referenced to object */
  79. /* world coords are updated when moving or rotating object */
  80. /* all others are renderer workspace */
  81.  
  82. typedef struct {
  83.     long ox, oy, oz;   /* object coordinates */
  84.     long x, y, z;      /* world coordinates  */
  85.     long cz;       /* converted Z coord  */
  86.     NVERTEX *new_copy;  /* non-zero if x and y transformed */
  87.     unsigned char z_transformed;   /* non-zero if z has been transformed */
  88.     unsigned char z_outcode;       /* 1 set if hither, 2 set if yon */
  89.     } VERTEX;
  90.  
  91. #define HITHER 16    /* Z outcode bits */
  92. #define YON    32
  93.  
  94.  
  95. /* world database polys */
  96. /* object-based normal must be rotated to world copy with object */
  97. /* color will have 8-bit color, 8-bit reflectance field */
  98.  
  99. #ifndef POLYDEF
  100. #define POLYDEF 1
  101.  
  102. typedef struct {
  103.     unsigned color;   /* color (not used yet-- will set chroma, reflectance */
  104.     VERTEX **points;  /* array of pointers to the vertices of this polygon */
  105.     int npoints;      /* number of entries in points[] */
  106.     long onormalx,
  107.          onormaly,
  108.          onormalz;    /* unit length surface normal (VISOBJ) */
  109.     long normalx,
  110.          normaly,
  111.          normalz;     /* unit length surface normal (WORLD)*/
  112.     struct _object *object;
  113.     } POLY;
  114.  
  115. #endif
  116.  
  117.  
  118. /* renderer poly copy */
  119. /* paernt points back to original poly for lighting */
  120. /* color computed by cosine lighting (currently 0-15) */
  121. /* maxz is deepest poly point for sorting */
  122.  
  123. typedef struct {
  124.     int npoints;       /* number of entries in points[] MUST BE FIRST */
  125.     POLY *parent;
  126.     unsigned color;       /* color after illumination */
  127.     long maxz;        /* maximum Z value (for sorting) */
  128.     } NPOLY;
  129.  
  130.  
  131. typedef struct { NPOLY *ptr; long depth; } DSORT; /* used for depth sorting */
  132.  
  133. #ifndef REPDEF
  134. #define REPDEF 1
  135.  
  136. typedef struct _rep {     /* a representation for an object */
  137.     int size;            /* if the object is bigger than this, use this rep */
  138.     int nverts, npolys;   /* number of vertices, number of polys */
  139.     VERTEX *verts;        /* array of vertices */
  140.     POLY *polys;          /* array of polygons */
  141.     struct _rep *next;    /* pointer to next rep in list */
  142.     long update_count;    /* inc. every time rep moves */
  143.     unsigned flags;
  144.     } REP;
  145.  
  146. #endif
  147.  
  148.  
  149. /* world database object */
  150. /* sphx, sphy, sphz, sphr used for sphere object clipping */
  151.  
  152. #define OBJ_FLAG_MASK  0x7C00
  153. #define OBJ_DEPTH_MASK 0x010F
  154.  
  155. #ifndef OBJDEF
  156. #define OBJDEF 1
  157.  
  158. typedef struct _object OBJECT;    // for our purposes...
  159.  
  160. typedef struct _object VISOBJ;  // this is a special case of OBJECT now
  161.  
  162. struct _object
  163.  {
  164.     unsigned int oflags;
  165. #define OBJ_REPLOCK      0x0400
  166. #define OBJ_NONSEL       0x0800 /* can't be selected (i.e. pointer) */
  167. #define OBJ_INVIS        0x1000
  168. #define OBJ_HIGHLIGHTED  0x2000
  169. #define OBJLIST_HEADER   0x4000
  170. #define IS_VISOBJ        0x8000    /* required by renderer: it will set */
  171.  
  172. //other uses of flags: for segments:
  173. #define IS_SEGMENT    0x0080
  174. #define SYSTEM_OWNED  0x0040
  175. #define IS_MOVEABLE   0x0010    // segment or attached object
  176.  
  177.  
  178. #define DEEPEST 0x0000        /* sort polys by deepest point */
  179. #define ATBACK  0x0001        /* push this object's poly's waaaay back */
  180. #define AVERAGE 0x0002        /* sort polys by average depth */
  181.  
  182. #define BYOBJECT   0x0100    /* sort by object */
  183. #define BYPOLY     0x0000    /* put polys in world before sort */
  184.  
  185.     struct _object *prev;
  186.     struct _object *nnext;
  187.  
  188.     void *owner;       /* for example, a body segment description struct */
  189.     REP *replist;              /* pointer to list of representations */
  190.     REP *current_rep;          /* the currently-active rep */
  191. //    int (*coll_eval)(VISOBJ *obj, long x, long y, long z);    /* evaluate collision */
  192.  
  193.     long osphx, osphy, osphz;     /* object-coord sphere center */
  194.     long sphx, sphy, sphz, sphr;  /* bounding sphere center and radius */
  195.     long update_count;         /* inc. every time object moved */
  196.  };
  197.  
  198. #endif
  199.  
  200.  
  201. /* world database object list head */
  202. /* dual linked for fast remove/insert needed for splits */
  203.  
  204. #ifndef OBJLDEF
  205. #define OBJLDEF 1
  206.  
  207. typedef struct _objlist
  208.  {
  209.     unsigned int oflags;    /* so it LOOKS like part of OBJ */
  210. #define OBJ_INVIS        0x1000
  211. #define OBJ_HIGHLIGHTED  0x2000
  212. #define OBJLIST_HEADER   0x4000
  213. #define IS_VISOBJ        0x8000    /* required by renderer: it will set */
  214.  
  215.     struct _object *prev;
  216.     struct _object *nnext;
  217.  } OBJLIST;
  218.  
  219. #endif
  220.  
  221.  
  222. #ifndef VIEWDEF
  223. /* renderer viewpoint/screen control structure */
  224. /* viewoint in X, Y, Z coords */
  225. /* pan, tilt, roll in (float*65536) formats */
  226. /* zoom is equiv. to magnification from 90 deg. FOV (also float*65536) */
  227. /* aspect sets how much to magnify Y more than X to fix up displays */
  228. /* light source point in world coordinates */
  229. /* left, right, top, bottom set edges of screen */
  230. /* hither sets closest point: keep >16 for best range of world coords */
  231. /* yon sets max. distance: keep it 1<<26 if not used */
  232. /* all others are renderer workspace */
  233.  
  234. #define NOFLIP 0      /* for orientation flags */
  235. #define XFLIP  1
  236. #define YFLIP  2
  237.  
  238. typedef struct {
  239.     long zoom;              /* <16.16> 1/tan(H FOV/2) 0.5 to 16     */
  240.  
  241.                  /* SCREEN DATA */
  242.     long left,right;        /* <25.0> clipping planes */
  243.     long top, bottom;
  244.     long hither, yon;   /* <25.0> near and far clipping planes   */
  245.     long aspect;        /* <16.16> x:y fixup factor (magnify Y by..*/
  246.  
  247.     /* RENDERING CONTROL */
  248.     unsigned flags;     /* 16 bits of flags */
  249.  
  250.                  /* ADVANCED SCREEN DATA */
  251.     long x_offset, y_offset; /* amount to move screen center in pixels */
  252.     unsigned orientation;    /* used to mirror screen image */
  253.     MATRIX eye_xform;
  254.  
  255.              /* INTERNAL RECORDS */
  256.     long hsw, hsh;        /* half screen width, height */
  257.     long hsc, vsc;        /* screen center (with offset) */
  258.     long scx, scy;        /* full-resolution scaling for horizon */
  259.     long sx,sy;        /* mantissa of screen scaling  */
  260.     int  xshift, yshift;    /* scaling bit shifts */
  261.  
  262.     long left_C, left_M;    /* spherical clip coefficients */
  263.     long right_C, right_M;
  264.     long top_C, top_M;
  265.     long bot_C, bot_M;
  266.  
  267.            } VIEW;
  268.  
  269. /* View flags: */
  270.  
  271. #define WIREFRAME           0x0001
  272. #define VIEWDEF 1
  273. #endif
  274.  
  275. /* Prescaling (used in hrendo5.c and 3dsupp.c (in where_screen_pt()) */
  276.  
  277. #define PRESCALE  2    /* frac bits in XY coords */
  278. #define PRESCALEZ 2
  279.  
  280. #ifndef SCRIDEF
  281. #define SCRIDEF 1
  282. struct Screeninfo {
  283.     int xmin, ymin, xmax, ymax, xcent, ycent, colors, pages, bw;
  284.     long aspect;
  285.     char id[80];
  286.     };
  287. #endif
  288.  
  289. extern struct Screeninfo *screeninfo;
  290.  
  291.                     /* create rotation/translation */
  292.                     /* "matrix" from angle data    */
  293.                     /* CALL setup_render FIRST!    */
  294.  
  295. /* End of structs */
  296.  
  297.  
  298. #define OUT_OF_VIEW 0x80000000L // returned by clip_by_volume() if out
  299.  
  300. /* End of 3dstruct.h */
  301.  
  302.