home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / net / raytrace < prev    next >
Internet Message Format  |  1986-08-29  |  17KB

  1. From fritzz@net1.UCSD.EDU (john) Wed Aug 27 16:21:25 1986
  2. Path: beno!seismo!columbia!caip!clyde!cbatt!cbosgd!ucbvax!sdcsvax!net1!fritzz
  3. From: fritzz@net1.UCSD.EDU (john)
  4. Newsgroups: net.sources
  5. Subject: one more time... ray tracing
  6. Message-ID: <247@net1.UCSD.EDU>
  7. Date: 27 Aug 86 20:21:25 GMT
  8. Reply-To: fritzz@net1.UUCP (john)
  9. Organization: abercrombie bmf graphics
  10. Lines: 725
  11.  
  12.  
  13. well, it seems that between a few typo's in my old source, news
  14. truncating the articles, and our news acting strange, it is belatedly
  15. time for son of son of tracer. I keep on promising that I won't post
  16. these anymore, but demand stays demanding, so here it is:
  17. tracer 2.1
  18. it is in three shars, with this the first one holding all code and
  19. header files, and the other two holding support documents and data files
  20. including the much demanded susie (you guys are all sick, it's only
  21. black and white =)
  22. so, without further ado:
  23. ----------------------------------------------
  24.  
  25.  
  26. # This is a shell archive.  Remove anything before this line,
  27. # then unpack it by saving it in a file and typing "sh file".
  28. #
  29. # Wrapped by wizard!fritzz on Wed Aug 27 13:03:37 PDT 1986
  30. # Contents:  find.c g_bal.c g_bod.c refract.c shade.c support.c tracer.c
  31. #    extern.h macros.h rtd.h
  32.  
  33. echo x - find.c
  34. sed 's/^@//' > "find.c" <<'@//E*O*F find.c//'
  35. #include <math.h>
  36. #include "rtd.h"
  37. #include "extern.h"
  38. #include "macros.h"
  39.  
  40.  
  41. double  findo (m, s) /* finds where a ray inside the ball exits. */
  42. struct mat *m;
  43. struct sphere  *s;
  44. {
  45. /* foops id the rotated position vector. */
  46.     struct vector   foops;
  47.     double  t;
  48.     MTV (foops, (*m), s -> cent);
  49. /* see if it hits the ball (it better)*/
  50.     t = s -> rad * s -> rad - foops.y * foops.y - foops.z * foops.z;
  51.     if (t > 0)
  52.     t = foops.x + sqrt (t);
  53.     else
  54.     t = 0;
  55. /* return how far along the ray you were when you hit */
  56.     return (t);
  57. }
  58.  
  59. double  find (m, s)/* finds whether a ray hits a ball*/
  60. struct mat *m;
  61. struct sphere  *s;
  62. {
  63.     struct vector   foops;
  64.     double  t;
  65.     MTV (foops, (*m), s -> cent);
  66.     t = s -> rad * s -> rad - foops.y * foops.y - foops.z * foops.z;
  67.     if (t > 0)
  68.     t = foops.x - sqrt (t);
  69.     else
  70.     t = 0;
  71.     return (t);
  72. }
  73.  
  74. double  finds (m, s)/* finds if a ball is between a point and a 
  75.             lightsource. Returns how obscuring the ball is */
  76. struct mat *m;
  77. struct sphere  *s;
  78. {
  79.     struct vector   foops;
  80.     double  t;
  81.     MTV (foops, (*m), s -> cent);
  82.     t = s -> rad - sqrt (foops.y * foops.y + foops.z * foops.z);
  83.     if (t > 0)
  84.     t = t / foops.x;
  85.     else
  86.     t = 0;
  87.     return (t);
  88. }
  89.  
  90.  
  91.  
  92.  
  93. double  shadow (p)/* finds if a point is in a shadow, or if it is on edge */
  94. struct vector  *p;
  95. {
  96.     struct mat  trans;
  97.     struct sphere   ss;
  98.     struct vector   d;
  99.     int     c,
  100.             i;
  101.     double  l,
  102.             k,
  103.             x,
  104.             y,
  105.             z,
  106.             finds ();
  107.     l = 0.0;
  108.     c = -1;
  109.     SV (d, ls.cent, (*p));
  110.     d.l = LEN (d);
  111.     d.xzl = XZL (d);
  112.     mt (&(d), &trans);
  113.  
  114.     for (i = 0; i < nob; i++) {
  115.     ss.rad = bl[i] -> s.rad;
  116.     SV (ss.cent, bl[i] -> s.cent, (*p));
  117.     if ((k = finds (&trans, &ss)) > l) {
  118.         c = i;
  119.         l = k;
  120.     }
  121.     }
  122.     if (c == -1)
  123.     k = 200.0;
  124.     else {
  125.     k = 1.0 - l / ((ls.rad) / (d.l));
  126.     if (k < 0.0)
  127.         k = 0.0;
  128.     k *= 200.0;
  129.     }
  130.     return (k);
  131. }
  132. @//E*O*F find.c//
  133. chmod u=rw,g=r,o=r find.c
  134.  
  135. echo x - g_bal.c
  136. sed 's/^@//' > "g_bal.c" <<'@//E*O*F g_bal.c//'
  137. #include <stdio.h>
  138. #include "rtd.h"
  139. #include "extern.h"
  140. g_bal (df)
  141. FILE * df;
  142. {
  143.     int     i;
  144.     double  x,
  145.             y,
  146.             z,
  147.             r,
  148.             ior,
  149.             rfr,
  150.             rfl,
  151.             dif,
  152.             amb;
  153.     for (i = 0;
  154.         fscanf (df, "%F %F %F %F %F %F %F %F %F",
  155.         &x, &y, &z, &r, &ior, &rfr, &rfl, &dif, &amb) != EOF;
  156.         i++) {
  157.     bl[i] = (struct ball   *) malloc (sizeof (struct ball));
  158.     bl[i] -> s.cent.x = x;
  159.     bl[i] -> s.cent.y = y;
  160.     bl[i] -> s.cent.z = z;
  161.     bl[i] -> s.rad = r;
  162.     bl[i] -> ior = ior;
  163.     bl[i] -> rfr = rfr;
  164.     bl[i] -> rfl = rfl;
  165.     bl[i] -> dif = dif;
  166.     bl[i] -> amb = amb;
  167.     }
  168.     return (i);
  169. }
  170.  
  171. @//E*O*F g_bal.c//
  172. chmod u=rw,g=r,o=r g_bal.c
  173.  
  174. echo x - g_bod.c
  175. sed 's/^@//' > "g_bod.c" <<'@//E*O*F g_bod.c//'
  176. #include <stdio.h>
  177. #include <math.h>
  178. #include "extern.h"
  179. #include "macros.h"
  180.  
  181.  
  182. g_bod (f)
  183. FILE * f;
  184. {
  185.     int     k,
  186.             x;
  187.     double  big = 0.0,
  188.             little = HUGE;
  189.     char    buf[512];
  190.  
  191.  
  192.     for (ysue = 0;; ysue++) {
  193.     if (fgets (buf, 512, f) == NULL)
  194.         break;
  195.     xsue = strlen (buf) - 1;
  196.     for (x = 0; x < xsue; x++) {
  197.         k = buf[x];
  198.         suzie[x][ysue] = (double) k;
  199.         if (big < k)
  200.         big = k;
  201.         if (little > k)
  202.         little = k;
  203.     }
  204.     }
  205.     big = big - little;
  206.     for (k = 0; k < ysue; k++)
  207.     for (x = 0; x < xsue; x++)
  208.         suzie[x][k] = (suzie[x][k] - little) / big;
  209. }
  210. @//E*O*F g_bod.c//
  211. chmod u=rw,g=r,o=r g_bod.c
  212.  
  213. echo x - refract.c
  214. sed 's/^@//' > "refract.c" <<'@//E*O*F refract.c//'
  215. #include <math.h>
  216. #include "rtd.h"
  217. #include "macros.h"
  218. #include "extern.h"
  219.  
  220. int    rlev;
  221. int     refract (r, bll)
  222. struct ray *r;
  223. struct ball *bll;
  224. {
  225.     struct vector   new,
  226.                     norm;
  227.     struct mat  trans;
  228.     struct ray  ir;
  229.     double  l,
  230.             refk (), getcapt (), capt, inside ();
  231.     double  stupid;
  232.     struct sphere   ss;
  233.  
  234.     SV (norm, r -> org, bll -> s.cent);
  235.     norm.l = bll-> s.rad;
  236.  
  237.     capt = getcapt (&norm, &(r -> dir), bll -> ior);
  238.  
  239.  
  240. /* get the addition factor for the normal for refraction*/
  241.     stupid = refk (&(norm), &(r -> dir), bll -> ior);
  242.     SCMLT (stupid, norm);
  243.  
  244.     AV (ir.dir, r -> dir, norm);
  245.     MV (r -> org.x, r -> org.y, r -> org.z, ir.org);
  246.  
  247. /* now get it for reflection */
  248.     SV (norm, r -> org, bll -> s.cent);
  249.     norm.l = bll -> s.rad;
  250.     SCMLT (1.0 / norm.l, norm);
  251.     stupid = 2.0 * DOT (norm, r -> dir);
  252.     SCMLT (stupid, norm);
  253.     SV (r -> dir, r -> dir, norm);
  254.  
  255.     return ((int) ((1.0 - capt) * (double) shade (r) + ((capt) * inside (&ir, bll))));
  256. }
  257.  
  258. double  inside (r, bll)
  259. struct ray *r;
  260. struct ball *bll;
  261. {
  262.     struct vector   new,
  263.                     norm;
  264.     struct mat  trans;
  265.     struct ray  er;
  266.     double  findo (), lght, l, refk (), getcapt (), capt;
  267.     double  stupid;
  268.     struct sphere   ss;
  269.  
  270.  
  271.     if (++rlev < RLEV) {
  272.     r -> dir.l = LEN (r -> dir);
  273.     r -> dir.xzl = XZL (r -> dir);
  274.     mt (&(r -> dir), &trans);
  275.     ss.rad = bll -> s.rad;
  276.     SV (ss.cent, bll -> s.cent, r -> org);
  277.  
  278.     l = findo (&trans, &ss);
  279.     MV (l * trans.x.x, l * trans.x.y, l * trans.x.z, new);
  280.     AV (er.org, r -> org, new);
  281.     AV (r -> org, r -> org, new);
  282.     SV (norm, er.org, bll -> s.cent);
  283.  
  284.     norm.l = bll -> s.rad;
  285.     capt = getcapt (&norm, &(r -> dir), 1.0 / bll -> ior);
  286.  
  287.     stupid = refk (&norm, &(r -> dir), 1.0 / bll -> ior);
  288.     SCMLT (stupid, norm);
  289.     AV (er.dir, norm, r -> dir);
  290.  
  291.     SCMLT (1.0 / norm.l, norm);
  292.     stupid = 2.0 * DOT (norm, r -> dir);
  293.     SCMLT (stupid, norm);
  294.     SV (r -> dir, r -> dir, norm);
  295.     lght = (1.0 - capt) * inside (r, bll) + (capt * (double) shade (&er));
  296.     }
  297.     else
  298.     lght = 0.0;
  299.     rlev--;
  300.      if (lght<0.0) lght=0.0;
  301.      if (lght>255.0) lght=255.0;
  302.     return (lght);
  303. }
  304.  
  305.  
  306.  
  307. double  refk (nrm, in, ior)
  308. struct vector  *nrm,
  309.                *in;
  310. double  ior;
  311. {
  312.     double  dt,
  313.             ln,
  314.             li,
  315.             ret;
  316.  
  317.     ior = ior * ior;
  318.     dt = DOT ((*nrm), (*in));
  319.     ln = LN2 ((*nrm));
  320.     li = LN2 ((*in));
  321.     if (dt < 0)
  322.     ret = (-dt - sqrt (dt * dt - ln * li * (1 - ior))) / ln;
  323.     else
  324.     ret = (-dt + sqrt (dt * dt - ln * li * (1 - ior))) / ln;
  325.     return (ret);
  326. }
  327.  
  328. double  getcapt (nrm, dr, ior)
  329. struct vector  *nrm,
  330.                *dr;
  331. double  ior;
  332. {
  333.     double  dt,
  334.             cs1,
  335.             cs2,
  336.             p,
  337.             s;
  338.     dt = DOT ((*nrm), (*dr));
  339.     dt = dt * dt / LN2 ((*nrm)) / LN2 ((*dr));
  340.     cs1 = sqrt (dt);
  341.     cs2 = sqrt (1.0 - (1.0 - dt) / ior);
  342.     p = cs1 / (cs1 + ior * cs2);
  343.     s = cs1 / (ior * cs1 + cs2);
  344.     return (2.0 * (p * p + s * s));
  345. }
  346. @//E*O*F refract.c//
  347. chmod u=rw,g=r,o=r refract.c
  348.  
  349. echo x - shade.c
  350. sed 's/^@//' > "shade.c" <<'@//E*O*F shade.c//'
  351. /*
  352.  * this subroutine does all the gritty work- it calculates 
  353.  * what shade each pixel should be. I like recursion.
  354.  */
  355. #include <math.h>
  356. #include "rtd.h"
  357. #include "macros.h"
  358. #include "extern.h"
  359.  
  360. int     shade (r)
  361. struct ray *r;
  362. {
  363.     int     i,
  364.             c,
  365.             refract ();
  366.     struct ray  refr;
  367.     double  lght,
  368.             x,
  369.             y,
  370.             z,
  371.             l,
  372.             k,
  373.             dot (), find (), shadow ();
  374.     int     sx,
  375.             sy;
  376.     double  stupid;
  377.     struct vector   new,
  378.                     norm;
  379.     struct mat  trans;
  380.     struct sphere   ss;
  381.     if (++level <= LEVEL) {
  382.     c = -1;
  383.     l = HUGE;
  384. /* get vector length and xz component for mt() */
  385.     r -> dir.l = LEN (r -> dir);
  386.     r -> dir.xzl = XZL (r -> dir);
  387. /* make a transform matrix that rotates something in space so
  388.    that the ray will be aligned with the x axis */
  389.     mt (&(r -> dir), &trans);
  390.  
  391. /* for starters we find out whether we hit anything. */
  392.     for (i = 0; i < nob; i++) {
  393.         ss.rad = bl[i] -> s.rad;
  394.         SV (ss.cent, bl[i] -> s.cent, r -> org);
  395.         if ((k = find (&trans, &ss)) > 0.0 && k < l) {
  396.         c = i;
  397.         l = k;
  398.         }
  399.     }
  400.     if (c >= 0 && (l * trans.x.y + r -> org.y) > 0.0) {
  401.                 /* WE HIT SOMETHING */
  402.         MV (l * trans.x.x, l * trans.x.y, l * trans.x.z, new);
  403.         new.l=l;
  404. /* move the new orgin of the ray to the intersection */
  405.         AV (refr.org, new, r -> org);
  406.         AV (r -> org, new, r -> org);
  407.         MV (r -> dir.x, r -> dir.y, r -> dir.z, refr.dir);
  408. /* get a normal vector for the intersection point */
  409.         SV (norm, r -> org, bl[c] -> s.cent);
  410.         norm.l=bl[c] ->s.rad;
  411.  
  412. /* ambient lighting */
  413.         lght = 200.0 * bl[c] -> amb;
  414.  
  415. /* shaded lighting (diffuse). subroutine shadow is in find.c */
  416.         if (bl[c] -> dif != 0.0) {
  417.         SV (new, ls.cent, r -> org);
  418.         new.l = LEN(new);
  419.         if ((k = DOT (new, norm)) > 0.0)
  420.             lght += bl[c] -> dif * shadow (&(r -> org)) * k / (new.l) / (norm.l);
  421.         }
  422.  
  423. /*reflection... easy */
  424.         if (bl[c] -> rfl != 0.0) {
  425. /* make the normal unit length */
  426.         SCMLT ((1.0 / norm.l), norm);
  427. /* get the length of the ray's component in the normal direction */
  428.         stupid = 2.0 * DOT (norm, r -> dir);
  429.         SCMLT (stupid, norm);
  430. /* subtract double the normal component- !reflection! */
  431.         SV (r -> dir, r -> dir, norm);
  432.         lght += bl[c] -> rfl * (double) shade (r);
  433.         }
  434.  
  435. /* refraction. this is ugly, which is why I choose to deal with
  436.    it in it's own subroutine which comes after this one */
  437.         if (bl[c] -> rfr != 0.0) {
  438.         lght += bl[c] -> rfr * (double) refract (&refr, bl[c]);
  439.         }
  440.  
  441.  
  442.  
  443.     }
  444.     else {            /* hit no objects... */
  445.         if ((r -> dir.y) < 0.0) {/* crosses floor */
  446.         z = -(r -> org.y) / (r -> dir.y);
  447.         (r -> org.x) += z * (r -> dir.x);
  448.         (r -> org.z) += z * (r -> dir.z);
  449.         (r -> org.y) = 0.0;
  450.  
  451.         SV (new, ls.cent, r -> org);
  452.         new.l = LEN(new);
  453.         sx = (int) (r -> org.x / 1.5) % xsue;
  454.         if (sx < 0)
  455.             sx += xsue;
  456.         sy = -(int) (r -> org.z / 1.5) % ysue;
  457.         if (sy < 0)
  458.             sy += ysue;
  459.         lght = (sam * suzie[sx][sy] + 1.0 - sam) * (0.8 *
  460.             shadow (&(r -> org)) * (new.y) / (new.l) + 40.0);
  461.  
  462.  
  463.         }
  464.         else {        /* check to see if it hit lightsource */
  465.         SV (ss.cent, ls.cent, r -> org);
  466.         ss.rad = ls.rad;
  467.         if (find (&trans, &(ss.cent)) > 0.0)
  468.             lght = 255;
  469.         else
  470.             lght = 0;
  471.         }
  472.     }
  473.     }
  474. /* to many levels return 0 cause it shouldn't matter */
  475.     else
  476.     lght = 0;
  477.     level--;
  478.     if (lght < 0.0)
  479.     lght = 0.0;
  480.     if (lght > 255.0)
  481.     lght = 255.0;
  482.     return ((int) lght);
  483. }
  484. @//E*O*F shade.c//
  485. chmod u=rw,g=r,o=r shade.c
  486.  
  487. echo x - support.c
  488. sed 's/^@//' > "support.c" <<'@//E*O*F support.c//'
  489. /*
  490.  *    supportive subroutines...
  491.  */
  492.  
  493. #include <math.h>
  494. #include <stdio.h>
  495. #include "rtd.h"
  496. #include "extern.h"
  497.  
  498.  
  499. mt (vec, trans)
  500. struct vector  *vec;
  501. struct mat *trans;
  502. {
  503.     if (vec -> xzl == 0.0) {
  504.     trans -> x.x = 0.0;
  505.     trans -> x.y = 1.0;
  506.     trans -> x.z = 0.0;
  507.     trans -> y.x = -1.0;
  508.     trans -> y.y = 0.0;
  509.     trans -> y.z = 0.0;
  510.     trans -> z.x = 0.0;
  511.     trans -> z.y = 0.0;
  512.     trans -> z.z = 1.0;
  513.     }
  514.     else {
  515.     trans -> x.x = (vec -> x) / (vec -> l);
  516.     trans -> x.y = (vec -> y) / (vec -> l);
  517.     trans -> x.z = (vec -> z) / (vec -> l);
  518.     trans -> y.x = -(vec -> x) * (vec -> y) / ((vec -> l) * (vec -> xzl));
  519.     trans -> y.y = (vec -> xzl) / (vec -> l);
  520.     trans -> y.z = -(vec -> z) * (vec -> y) / ((vec -> l) * (vec -> xzl));
  521.     trans -> z.x = -(vec -> z) / (vec -> xzl);
  522.     trans -> z.y = 0;
  523.     trans -> z.z = (vec -> x) / (vec -> xzl);
  524.     }
  525. }
  526. @//E*O*F support.c//
  527. chmod u=rw,g=r,o=r support.c
  528.  
  529. echo x - tracer.c
  530. sed 's/^@//' > "tracer.c" <<'@//E*O*F tracer.c//'
  531.  
  532.  
  533. /* tracer version 2.1 */
  534. #include <stdio.h>
  535. #include <math.h>
  536. #include "rtd.h"
  537. #include "macros.h"
  538.  
  539.  
  540. FILE * fp;
  541. double  suzie[300][300],
  542.         sam = 1.0;
  543. int     xsue,
  544.         ysue;
  545. struct ball *bl[150];
  546. int     level,
  547.         nob;
  548. struct sphere   ls;
  549.  
  550. main (argc, argv)
  551. int     argc;
  552. char  **argv;
  553. {
  554.     FILE * df, *texfile;
  555.     static double   xco,
  556.                     yco;
  557.     struct ray  rr;
  558.     struct vector   vp;
  559.     double  x,
  560.             y,
  561.             z;
  562.     int     i,
  563.             in = 0,
  564.             out = 0,
  565.             tex = 0;
  566.     int     c;
  567.  
  568. /* command interp */
  569.  
  570.     for (i = 1; i < argc; i++) {
  571.     if (argv[i][0] != '-')
  572.         booboo ("Options strt with a '-' bozo");
  573.     c = argv[i][1];
  574.  
  575.     switch (c) {
  576.         case ('i'): 
  577.         if (in)
  578.             booboo ("Sorry, but you may only have one input file");
  579.         in = 1;
  580.         if ((i + 1) >= argc || argv[i + 1][0] == '-')/* no arg */
  581.             df = stdin;
  582.         else
  583.             if ((df = fopen (argv[++i], "r")) == NULL)
  584.             booboo ("input file not found");
  585.         break;
  586.         case ('o'): 
  587.         if (out)
  588.             booboo ("Sorry, but you may have only one output file");
  589.         out = 1;
  590.         if ((i + 1) >= argc || argv[i + 1][0] == '-')/* no arg */
  591.             fp = stdout;
  592.         else
  593.             fp = fopen (argv[++i], "w");
  594.         break;
  595.         case ('s'): 
  596.         if (tex)
  597.             booboo ("Sorry, but you may have only one image file");
  598.         if ((i + 1) >= argc || argv[i + 1][0] == '-')/* no arg */
  599.             booboo ("-s requires an argument");
  600.         tex = 1;
  601.         if ((texfile = fopen (argv[++i], "r")) == NULL)
  602.             booboo ("image file not found");
  603.         break;
  604.         booboo ("this line shouldn't do anything");
  605.         case ('S'): 
  606.         if (argv[i][2] < '0' || argv[i][2] > '9'){
  607. printf("%c\n",argv[i][2]);
  608.             booboo ("-S needs a numerical argument");}
  609.         sam = atof (&(argv[i][2]));
  610.         break;
  611.         default: 
  612.         booboo ("Unrecognized option. Better try again");
  613.     }
  614.     }
  615.  
  616.  
  617.     if (!in)
  618.     if ((df = fopen ("bdata.i", "r")) == NULL)
  619.         booboo ("bdata.i not found");
  620.     if (!out)
  621.     fp = fopen ("data.dis", "w");
  622.     if (!tex)
  623.     if ((texfile = fopen ("pat.def", "r")) == NULL)
  624.         booboo ("pat.def not found");
  625.  
  626.  
  627.  
  628.     nob = g_bal (df);
  629.     g_bod (texfile);
  630.  
  631.  
  632.  
  633.     MV (95.0, 140.0, -200.0, vp);
  634.     MV (0.0, 900.0, 0.0, ls.cent);
  635.     ls.rad = 40;
  636.     fprintf (fp, "%d %d\n", (int) ((XMAX - XMIN) * SCALE +0.9999999), (int) ((YMAX - YMIN) * SCALE +0.9999999));
  637.  
  638.     for (yco = YMAX * SCALE; yco > YMIN * SCALE; yco--)
  639.     for (xco = XMIN * SCALE; xco < XMAX * SCALE; xco++) {
  640.         MV (xco / SCALE, yco / SCALE, 0.0, rr.org);
  641.         SV (rr.dir, rr.org, vp);
  642.         fprintf (fp, "%c", shade (&rr));
  643.     }
  644. }
  645.  
  646. booboo (str)
  647. char   *str; {
  648.     printf ("%s\n", str);
  649.     exit (-1);
  650. }
  651. @//E*O*F tracer.c//
  652. chmod u=rw,g=r,o=r tracer.c
  653.  
  654. echo x - extern.h
  655. sed 's/^@//' > "extern.h" <<'@//E*O*F extern.h//'
  656. extern double suzie[300][300],sam;
  657. extern struct ball *bl[];
  658. extern struct sphere ls;
  659. extern int level,nob;
  660. extern int xsue,ysue;
  661. @//E*O*F extern.h//
  662. chmod u=rw,g=r,o=r extern.h
  663.  
  664. echo x - macros.h
  665. sed 's/^@//' > "macros.h" <<'@//E*O*F macros.h//'
  666. /* some of the most important stuff in the program */
  667. #define DOT(v1,v2) (v1.x*v2.x+v1.y*v2.y+v1.z*v2.z)
  668. /* returns dot product of two vectors */
  669. #define LN2(v)       (DOT(v,v))
  670. /* returns the square of the length of a vector */
  671. #define LEN(v)       sqrt(LN2(v))
  672. /* guess */
  673. #define XZL(v)       sqrt(v.x*v.x+v.z*v.z)
  674. /* returns the component in the xz plane of a vector */
  675. #define SCMLT(sc,vct) vct.x*= sc;vct.y*= sc;vct.z*= sc;vct.l*= sc;
  676. /* multiplies a vetor by a scalar */
  677. #define MV(a,b,c,v)   v.x= a;v.y= b;v.z= c;
  678. /* makes a vector. wouldn't need this with c++ */
  679. #define SV(t,u,v)  t.x=u.x-v.x;t.y=u.y-v.y;t.z=u.z-v.z;
  680. /*subtract vector t=u-v */
  681. #define AV(t,u,v)  t.x=u.x+v.x;t.y=u.y+v.y;t.z=u.z+v.z;
  682. /* add vector t=u+v */
  683. #define MTV(v1,m,v2) MV(DOT(m.x,v2),DOT(m.y,v2),DOT(m.z,v2),v1)
  684. /* multiply transpose matrix by vector. v1=m*v2 */
  685.  
  686. #define LEVEL 5/* levels of recursion */
  687. #define RLEV  3/*don't want as many inside the ball, takes forever as it is*/
  688.  
  689. #define XMIN 10.0
  690. #define XMAX 220.0
  691. #define YMIN 10.0
  692. #define YMAX 170.0
  693. /* window size,  virtual units */
  694. #define SCALE  2.0
  695. /* maginification factor */
  696. @//E*O*F macros.h//
  697. chmod u=rw,g=r,o=r macros.h
  698.  
  699. echo x - rtd.h
  700. sed 's/^@//' > "rtd.h" <<'@//E*O*F rtd.h//'
  701. struct color {
  702. int r;int g;int b;};
  703.  
  704. struct vector {
  705. double x;
  706. double y;
  707. double z;
  708. double l;
  709. double xzl;} ;
  710.  
  711. struct ray {
  712. struct vector org;
  713. struct vector dir;} ;
  714.  
  715. struct sphere {
  716. struct vector cent;
  717. double rad;} ;
  718.  
  719. struct ball {
  720. struct sphere s;
  721. double ior;
  722. double rfr;
  723. double rfl;
  724. double dif;
  725. double amb;
  726. };
  727.  
  728. struct mat {
  729. struct vector x;  /* first !row! */
  730. struct vector y;  /*second !row! */
  731. struct vector z;}; /* third !row! */
  732.  
  733. @//E*O*F rtd.h//
  734. chmod u=rw,g=r,o=r rtd.h
  735.  
  736. exit 0
  737.  
  738.  
  739.