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