home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / graphic / dwb / ray.h < prev    next >
C/C++ Source or Header  |  1990-05-15  |  18KB  |  658 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1987, David B. Wecker                 *
  4.  *                          All Rights Reserved                         *
  5.  *                                                                      *
  6.  * This file is part of DBW_Render                                      *
  7.  *                                                                      *
  8.  * DBW_Render is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts               *
  10.  * responsibility to anyone for the consequences of using it or for     *
  11.  * whether it serves any particular purpose or works at all, unless     *
  12.  * he says so in writing. Refer to the DBW_Render General Public        *
  13.  * License for full details.                                            *
  14.  *                                                                      *
  15.  * Everyone is granted permission to copy, modify and redistribute      *
  16.  * DBW_Render, but only under the conditions described in the           *
  17.  * DBW_Render General Public License. A copy of this license is         *
  18.  * supposed to have been given to you along with DBW_Render so you      *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this     *
  21.  * notice must be preserved on all copies.                              *
  22.  ************************************************************************
  23.  *                                                                      *
  24.  * Authors:                                                             *
  25.  *      DBW - David B. Wecker                                           *
  26.  *      jhl - John H. Lowery (IBM conversion)                           *
  27.  *                                                                      *
  28.  * Versions:                                                            *
  29.  *      V1.0  870125 DBW - First released version                       *
  30.  *      V1.01 880918 jhl - ported to IBM PC (MCGA/VGA display hardware) *
  31.  *            890121 jhl - MAXROW & MAXCOL become variables, add        *
  32.  *                         'D' command for Display max <hor> <vert>     *
  33.  *                                                                      *
  34.  ************************************************************************/
  35.  
  36. #define IBM_PC 1      /* V1.01       */
  37. #define OS2    1      /* Only changes version to V1.02-OS/2 Family */
  38. #include <math.h>
  39. #include <stdio.h>
  40. #include <setjmp.h>
  41.  
  42. #ifdef IBM_PC
  43. #include <malloc.h>
  44. #endif
  45.  
  46. #ifdef OS2
  47. #define VERSION "RAY v1.02 900515 (OS/2 Bound)\n Copyright (C) 1989 J. Lowery and D. Wecker - all rights reserved\n          OS/2 Conversion by Kevin Lowey\n\n"
  48. #else
  49. #define VERSION "RAY v1.02 891031 (IBM PC/XT/AT)\n Copyright (C) 1989 J. Lowery and D. Wecker - all rights reserved\n"
  50. #endif
  51.  
  52. #define pi 3.141592654
  53.  
  54. #define X 0
  55. #define Y 1
  56. #define Z 2
  57. #define W 3
  58. #define B 0
  59. #define G 1
  60. #define R 2
  61.  
  62. #define minimum( a, b ) ( ((a) < (b)) ? (a) : (b) )
  63. #define maximum( a, b ) ( ((a) > (b)) ? (a) : (b) )
  64.  
  65. typedef float vector[3];
  66. typedef float matrix[4][4];
  67.  
  68. typedef struct 
  69. {
  70.      float xmin, xmax;
  71.      float ymin, ymax;
  72. }
  73. window;
  74.  
  75. #define setwindow( xmin, ymin, xmax, ymax, w ) {\
  76.     w.xmin = xmin;\
  77.     w.ymin = ymin;\
  78.     w.xmax = xmax;\
  79.     w.ymax = ymax; }
  80.  
  81. #define copywindow( f, w ) {\
  82.     w.xmin = f.xmin;\
  83.     w.ymin = f.ymin;\
  84.     w.xmax = f.xmax;\
  85.     w.ymax = f.ymax; }
  86.  
  87. typedef struct 
  88. {
  89.      float xmin, xmax;
  90.      float ymin, ymax;
  91.      float zmin, zmax;
  92. }
  93. volume;
  94.  
  95. #define setvolume( xmin, ymin, zmin, xmax, ymax, zmax, v ) {\
  96.     v.xmin = xmin;\
  97.     v.ymin = ymin;\
  98.     v.zmin = zmin;\
  99.     v.xmax = xmax;\
  100.     v.ymax = ymax;\
  101.     v.zmax = zmax; }
  102.  
  103. #define copyvolume( f, v ) {\
  104.     v.xmin = f.xmin;\
  105.     v.ymin = f.ymin;\
  106.     v.zmin = f.zmin;\
  107.     v.xmax = f.xmax;\
  108.     v.ymax = f.ymax;\
  109.     v.zmax = f.zmax; }
  110.  
  111. typedef struct 
  112. {
  113.      vector vrp, vpn, vup, cop;
  114.      volume vol;
  115. }
  116. perspective_projection;
  117.  
  118. typedef struct 
  119. {
  120.      int tex;
  121.      float fuz, ref, idx;
  122.      vector tra, amb, dif;
  123. }
  124. attributes;
  125.  
  126. typedef struct 
  127. {
  128.      vector p;
  129.      float g;
  130.      int s;
  131. }
  132. fracvert;
  133.  
  134. #define CV(x,y,z,v)     v[0]=(x); v[1]=(y); v[2]=(z)
  135. #define VECZERO(v)      v[0] = 0.0; v[1] = 0.0; v[2] = 0.0
  136. #define VECCOPY(frm,to) to[0] = frm[0]; to[1] = frm[1]; to[2] = frm[2]
  137. #define VECDUMP(v,str)  printf("%s\t%5.3f %5.3f %5.3f\n",str,v[0],v[1],v[2])
  138. #define VECSUB(v1,v2,r) r[0] = v1[0] - v2[0]; r[1] = v1[1] - v2[1];\
  139.                         r[2] = v1[2] - v2[2]
  140. #define VECSUM(v1,v2,r) r[0] = v1[0] + v2[0]; r[1] = v1[1] + v2[1];\
  141.                         r[2] = v1[2] + v2[2]
  142. #define VECMUL(v1,v2,r) r[0] = v1[0] * v2[0]; r[1] = v1[1] * v2[1];\
  143.                         r[2] = v1[2] * v2[2]
  144. #define VECDIV(v1,v2,r) r[0] = v1[0] / v2[0]; r[1] = v1[1] / v2[1];\
  145.                         r[2] = v1[2] / v2[2]
  146. #define VECSCALE(s,v,r) r[0] = (s)*v[0]; r[1] = (s)*v[1]; r[2] = (s)*v[2]
  147. #define NORM(v)         ((float)sqrt((v[0]*v[0])+(v[1]*v[1])+(v[2]*v[2])))
  148. #define DOT(v1,v2)      ((v1[0]*v2[0])+(v1[1]*v2[1])+(v1[2]*v2[2]))
  149. #define CROSS(v1,v2,r)  r[0] = (v1[1] * v2[2]) - (v2[1] * v1[2]);\
  150.                         r[1] = (v1[2] * v2[0]) - (v2[2] * v1[0]);\
  151.                         r[2] = (v1[0] * v2[1]) - (v2[0] * v1[1])
  152. #define DIRECTION(f,t,d)        VECSUB(t,f,d); normalize(d)
  153. #define SPHERENORMAL(cent,p,n)  DIRECTION(cent,p,n);
  154. #define PLANENORMAL(ve,vp,n)    CROSS(vp,ve,n); normalize(n);
  155.  
  156. #define ERROR( str ) { fprintf( stderr, "%s\n", str ); exit(0); }
  157.  
  158. #ifndef IBM_PC
  159. extern char *malloc();
  160. #endif
  161.  
  162. extern long time();
  163.  
  164. #define CHECK_ALLOC( ptr, typ ) {\
  165.     if (!(ptr = (typ *) malloc( sizeof( typ )))) {\
  166.         ERROR( "MALLOC - out of memory" ); \
  167.         exit( 0 ); \
  168.         } \
  169.     }
  170.  
  171. #define CHECK_ALLOCA( ptr, typ, num ) {\
  172.     if (!(ptr = (typ *) malloc( sizeof( typ ) * num ))) {\
  173.         ERROR( "MALLOC - out of memory" ); \
  174.         exit( 0 ); \
  175.         } \
  176.     }
  177.  
  178. #define sptr( p ) ((sphere *) p)
  179. #define tptr( p ) ((triangle *) p)
  180. #define qptr( p ) ((quad *) p)
  181. #define rptr( p ) ((ring *) p)
  182. #define eptr( p ) ((extent *) p)
  183. #define cptr( p ) ((cylinder *) p)
  184.  
  185. #define EXTENT 0
  186. #define SPHERE 1
  187. #define TRIANGLE 2
  188. #define QUAD 3
  189. #define RING 4
  190. #define CYLINDER 5
  191.  
  192. typedef float hvector[4];
  193.  
  194. typedef struct NODE 
  195. {
  196.      struct NODE *next;
  197.      int kind;
  198.      attributes attr;
  199. }
  200. node;
  201.  
  202. typedef struct 
  203. {
  204.      node *next;
  205.      int kind;
  206.      attributes attr;
  207.      vector position, ve, vp;
  208. }
  209. quad;
  210.  
  211. typedef struct 
  212. {
  213.      node *next;
  214.      int kind;
  215.      attributes attr;
  216.      vector position, ve, vp;
  217. }
  218. triangle;
  219.  
  220. typedef struct 
  221. {
  222.      node *next;
  223.      int kind;
  224.      attributes attr;
  225.      vector position, ve, vp;
  226.      float minrad, maxrad;
  227. }
  228. ring;
  229.  
  230. typedef struct 
  231. {
  232.      node *next;
  233.      int kind;
  234.      attributes attr;
  235.      vector center;
  236.      float radius;
  237. }
  238. sphere;
  239.  
  240. typedef struct 
  241. {
  242.      node *next;
  243.      int kind;
  244.      attributes attr;
  245.      vector bottom, top;
  246.      float a, b, c;
  247. }
  248. cylinder;
  249.  
  250. typedef struct 
  251. {
  252.      node *next;
  253.      int kind;
  254.      attributes attr;
  255.      vector base, apex;
  256.      float baserad;
  257. }
  258. cone;
  259.  
  260. typedef struct 
  261. {
  262.      node *next;
  263.      int kind;
  264.      attributes attr;
  265.      vector center;
  266.      float radius;
  267.      node *sub;
  268. }
  269. extent;
  270.  
  271. typedef struct 
  272. {
  273.      vector intensity;
  274.      vector direction;
  275.      float distscale, radius;
  276.      int kind;
  277. }
  278. lighttype;
  279.  
  280. typedef struct 
  281. {
  282.      vector center;
  283.      float wavelength, amplitude, drag, propagate;
  284. }
  285. wavetype;
  286.  
  287. typedef struct 
  288. {
  289.      vector color;
  290.      float distscale;
  291. }
  292. hazetype;
  293.  
  294. typedef struct 
  295. {
  296.      vector color;
  297.      float start, scale;
  298. }
  299. blendtype;
  300.  
  301. typedef struct 
  302. {
  303.      float start, altscale, altfactor, threshhold;
  304. }
  305. snowtype;
  306.  
  307. typedef struct 
  308. {
  309.      float scale, zoom;
  310. }
  311. pebbletype;
  312.  
  313. typedef struct 
  314. {
  315.      int level;
  316.      float xscale, yscale, zscale;
  317.      int texture;
  318. }
  319. fractaltype;
  320.  
  321. typedef struct 
  322. {
  323.      vector color;
  324.      float x, y, z, bevel, angle;
  325.      int beveltype;
  326. }
  327. checkertype;
  328.  
  329. typedef struct 
  330. {
  331.      vector othercolor;
  332.      float thickscale, ringspacing, turbscale;
  333.      int squeeze;
  334. }
  335. woodtype;
  336.  
  337. typedef struct 
  338. {
  339.      vector veincolor;
  340.      float xscale, turbscale;
  341.      int squeeze;
  342. }
  343. marbletype;
  344.  
  345. typedef struct 
  346. {
  347.      vector min, max;
  348. }
  349. rextent;
  350.  
  351. #define unionvector( re, v, nre ) {\
  352.     int i; \
  353.     for (i = 0; i < 3; i++) {\
  354.         (nre)->min[i] = minimum( (re)->min[i], (v)[i] ); \
  355.         (nre)->max[i] = maximum( (re)->max[i], (v)[i] ); \
  356.         } \
  357.     }
  358.  
  359. #define unionrextent( re1, re2, nre ) {\
  360.     int i; \
  361.     for (i = 0; i < 3; i++) {\
  362.         (nre)->min[i] = minimum( (re1)->min[i], (re2)->min[i] ); \
  363.         (nre)->max[i] = maximum( (re1)->max[i], (re2)->max[i] ); \
  364.         } \
  365.     }
  366.  
  367. #define MAXX      640           /* absolute maximum pixel columns    */
  368. #define MAXY      480           /* absolute maximum pixel rows       */
  369.  
  370. #define BPP         4           /* Bits resolution per primary color */
  371.  
  372. #define DISPWID     11          /* Width of display surface          */
  373. #define DISPHI      8           /* Height of display surface         */
  374.  
  375. #define MAXGRAY     (1 << BPP)
  376. #define PPW         (sizeof( int ) * 8 / BPP)
  377. #define WPSL        (MAXX / PPW)
  378. #define MAXLIT      5
  379. #define MAXPEBBLE   2
  380. #define MAXSNOW     2
  381. #define MAXBLEND    2
  382. #define MAXWAVE     10
  383. #define MAXHAZE     2
  384. #define MAXCHECKER  2
  385. #define MAXFRACTAL  2
  386. #define MAXWOOD     2
  387. #define MAXMARBLE   2
  388. #define SMALL       0.001
  389. #define MAXOBJ      50  /* maximum object intersections for one ray */
  390.  
  391. #ifndef FALSE
  392. #define FALSE       0
  393. #endif
  394.  
  395. #ifndef TRUE
  396. #define TRUE        1
  397. #endif
  398.  
  399. typedef int         scanlinetype[ WPSL ];
  400. typedef float       matrix3[3][3];
  401.  
  402. #ifdef MODULE_RAY
  403. node                *root,
  404. *g_objpairs[ MAXOBJ ];
  405. FILE                *df,*fp,*fpout;
  406. char                fname[256], outname[256], str[256];
  407. scanlinetype        blueline,
  408. greenline,
  409. redline;
  410. lighttype           light[ MAXLIT ];
  411. marbletype          marble[ MAXMARBLE ];
  412. woodtype            wood[ MAXWOOD ];
  413. fractaltype         fractal[ MAXFRACTAL ];
  414. checkertype         checker[ MAXCHECKER ];
  415. pebbletype          pebble[ MAXPEBBLE ];
  416. snowtype            snow[ MAXSNOW ];
  417. blendtype           blend[ MAXBLEND ];
  418. hazetype            haze[ MAXHAZE ];
  419. wavetype            wave[ MAXWAVE ];
  420. int                 sline,
  421.   curr_runs,
  422.   max_runs,
  423.   allopaque = 1,
  424.   numlits = 0,
  425.   numwaves = 0,
  426.   numcheckers = 0,
  427.   numfractals = 0,
  428.   numwoods = 0,
  429.   nummarble = 0,
  430.   numblends = 0,
  431.   numsnows = 0,
  432.   numhazes = 0,
  433.   numpebbles = 0,
  434.   ambientlight = 0,
  435.   amblitnum = 1,
  436.   amblitdenom = 2,
  437.   antialias = 1,
  438.   dopseudo = 0,
  439.   histogram = 0,
  440.   startrow = 0,
  441.   endrow = MAXY,
  442.   g_objcounter;
  443. long                sort_size = 0L,
  444.   sorts = 0L,
  445.   total_runs = 0L,
  446.   max_intersects = 0L,
  447.   stacktop,
  448.   stackbot;
  449. float               maxhours = 12.0,
  450.   brickheight = 3.0,
  451.   brickwidth = 8.0,
  452.   brickdepth = 4.0, 
  453.   brickmortar = 0.5,
  454.   idxref = 1.0,
  455.   aperture = 0.0,
  456.   focus = 100,
  457.   ambscale = -1.0,
  458.   variance = 0.00097,
  459.   brickmortarwidth,
  460.   brickmortarheight,
  461.   brickmortardepth,
  462.   sqrt3,
  463.   sqrt3times2,
  464.   d_maxgray,
  465.   g_distances[ MAXOBJ ];
  466. vector              mortardiffuse = 
  467. {
  468.      0.8, 0.85, 0.99 
  469. }
  470. ,
  471. eye,
  472. vrp,
  473. vu,
  474. vr,
  475. backgroundval,
  476. g_points[ MAXOBJ ],
  477. xaxis = 
  478. {
  479.      1.0, 0.0, 0.0 
  480. }
  481. ,
  482. yaxis = 
  483. {
  484.      0.0, 1.0, 0.0 
  485. }
  486. ,
  487. zaxis = 
  488. {
  489.      0.0, 0.0, 1.0 
  490. }
  491. ;
  492. matrix              woodorient;
  493. jmp_buf             env;
  494. int                 MAXROW = 200;  /* default: VGA rows run 0-199    */
  495. int                 MAXCOL = 320;  /* default:     Cols run 0-319    */
  496.  
  497. #else
  498.  
  499. extern node         *root,
  500.   *g_objpairs[];
  501. extern FILE         *df,*fp,*fpout;
  502. extern char         fname[],  outname[], str[];
  503. extern scanlinetype blueline, greenline, redline;
  504. extern lighttype    light[];
  505. extern marbletype   marble[];
  506. extern woodtype     wood[];
  507. extern fractaltype  fractal[];
  508. extern checkertype  checker[];
  509. extern pebbletype   pebble[];
  510. extern snowtype     snow[];
  511. extern blendtype    blend[];
  512. extern hazetype     haze[];
  513. extern wavetype     wave[];
  514. extern int          sline,
  515.   curr_runs,
  516.   max_runs,
  517.   allopaque,
  518.   numlits,
  519.   numwaves,
  520.   numcheckers,
  521.   numfractals,
  522.   numwoods,
  523.   nummarble,
  524.   numblends,
  525.   numsnows,
  526.   numhazes,
  527.   numpebbles,
  528.   ambientlight,
  529.   amblitnum,
  530.   amblitdenom,
  531.   antialias,
  532.   dopseudo,
  533.   histogram,
  534.   startrow,
  535.   endrow,
  536.   g_objcounter;
  537. extern long         sort_size,
  538.   sorts,
  539.   total_runs,
  540.   max_intersects,
  541.   stacktop,
  542.   stackbot;
  543. extern float        maxhours,
  544.   brickheight,
  545.   brickwidth,
  546.   brickdepth,
  547.   brickmortar,
  548.   idxref,
  549.   aperture,
  550.   focus,
  551.   ambscale,
  552.   variance,
  553.   brickmortarwidth,
  554.   brickmortarheight,
  555.   brickmortardepth,
  556.   sqrt3,
  557.   sqrt3times2,
  558.   d_maxgray,
  559.   g_distances[];
  560. extern vector       mortardiffuse,
  561.   eye,
  562.   vrp,
  563.   vu,
  564.   vr,
  565.   backgroundval,
  566.   g_points[],
  567.   xaxis,
  568.   yaxis,
  569.   zaxis;
  570. extern matrix       woodorient;
  571. extern jmp_buf      env;
  572.  
  573. extern int          MAXROW;
  574. extern int          MAXCOL;
  575.  
  576. #endif
  577.  
  578. #ifndef MODULE_CALC
  579. extern void         spherenormal();     /* center to dir                */
  580. extern void         planenormal();      /* ve vp n                      */
  581. extern void         calcripple();       /* point w ripple               */
  582. extern void         noise3();           /* point total                  */
  583. extern float        noise();            /* point                        */
  584. extern float        turbulence();       /* point                        */
  585. extern void         dodirection();      /* val eye2 d2 atten amblits    */
  586. #endif
  587.  
  588. #ifndef MODULE_EXTENT
  589. extern void         setextent();        /* ep re                        */
  590. extern void         getextent();        /* np re                        */
  591. #endif
  592.  
  593. #ifndef MODULE_HIT
  594. extern void         findnormal();       /* np p n                       */
  595. extern int          hitcylinder();      /* top bottom a b c eye d p t   */
  596. extern int          hitsphere();        /* center radius eye d p t      */
  597. extern int          hitplane();         /* p ve vp eye d sfs inter      */
  598. extern int          hittriangle();      /* tp eye d p t                 */
  599. extern int          hitquad();          /* qp eye d p t                 */
  600. extern int          hitring();          /* rp eye d p t                 */
  601. extern void         shell();            /* v v1 v2 n                    */
  602. #endif
  603.  
  604. #ifndef MODULE_FILEIO
  605. extern void         dumpnode();         /* n                            */
  606. extern void         copyattr();         /* oa na                        */
  607. extern void         read_vec();         /* v                            */
  608. extern void         read_attr();        /* attr                         */
  609. extern void         dofractal();        /* level a b c attr             */
  610. extern void         readimagefile();    /* opp                          */
  611. extern void         getinput();         /* argc argv                    */
  612. extern void         write_scanline();   /*                              */
  613. extern void         getoutput();        /* argc argv                    */
  614. #endif
  615.  
  616. #ifndef MODULE_INTER
  617. extern node *get_next_intersection();   /* which best_p best_t          */
  618. extern void         add_intersection(); /* np p t attenuating           */
  619. extern void       calc_intersections(); /* np eye d attenuating         */
  620. extern void         all_intersects();   /* eye d attenuating            */
  621. extern int          bounce_lighting();  /* pintens plitdir bouncep lit  */
  622. extern void         blendcolor();       /* orig target scale result     */
  623. extern void         getatten();         /* atten p d lit pointdist      */
  624. #endif
  625.  
  626. #ifndef MODULE_MATH
  627. extern void         veczero();          /* v                            */
  628. extern void         veccopy();          /* from to                      */
  629. extern void         vecdump();          /* v str                        */
  630. extern float        hlsvalue();         /* n1 n2 hue                    */
  631. extern void         cv();               /* x y z v                      */
  632. extern void         hls();              /* h l s v                      */
  633. extern void         vecsub();           /* v1 v2 r                      */
  634. extern void         vecsum();           /* v1 v2 r                      */
  635. extern void         vecmul();           /* v1 v2 r                      */
  636. extern void         vecdiv();           /* v1 v2 r                      */
  637. extern void         vecscale();         /* s v r                        */
  638. extern float        norm();             /* v                            */
  639. extern void         normalize();        /* v                            */
  640. extern float        dot();              /* v1 v2                        */
  641. extern void         cross();            /* v1 v2 r                      */
  642. extern void         direction();        /* f t d                        */
  643. extern long         curstack();         /*                              */
  644. #endif
  645.  
  646. #ifndef MODULE_RND
  647. extern float    rnd();
  648. #endif
  649.  
  650. #ifndef MODULE_TEXTURE
  651. extern void         gettex();           /* diffuse np p n               */
  652. #endif
  653.  
  654. #ifndef MODULE_VAL
  655. extern void         getval();           /* val np p d atten ambientlit  */
  656. #endif
  657.  
  658.