home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 015.lha / tracer_source / tracer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-10  |  6.5 KB  |  310 lines

  1. /************************************************************************
  2.  *        basic ray tracing package tracer2.1            *
  3.  *                                    *
  4.  *        version 0.0 done 6/1/86-6/10/86                *
  5.  *        version 1.0 released 6/29/86                *
  6.  *        version 2.0 released 7/5/86                *
  7.  *        version 2.1 released 8/27/86                *
  8.  *        by friedrich knauss (one tired programmer)        *
  9.  *                                    *
  10.  *        Fast Amiga math    9/86     Mark Reichert            *
  11.  *        -p, -a options    9/86    Steve Williams & Jim Horn    *
  12.  *        IFF file output    12/86:    Mark Reichert            *
  13.  ************************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <math.h>
  17. #include "MyMath.h"
  18. #include "rtd.h"
  19. #include "macros.h"
  20.  
  21.  
  22. #define    CHECK_IT    if ( Chk_Abort() ) {\
  23.                 printf("Aborting, save picture. [y/n] ");\
  24.                 gets(tmp);\
  25.                 if( tmp[0] == 'y' )\
  26.                     save_screen(gfx_screen(), out_name);\
  27.                 gfx_close();\
  28.                 close_math();\
  29.                 exit(104);\
  30.             }
  31.  
  32. struct    ball    *bl[150];
  33. struct    sphere    ls;
  34.  
  35. int    xsue,
  36.     ysue,
  37.     level,
  38.     nob;
  39.  
  40. char    suzie[300][300],
  41.     tmp[10],
  42.     out_name[132];
  43.  
  44. FFP    sam;
  45.  
  46. long    gfx_screen();    /* really struct Screen, but ... */
  47.  
  48. main(argc, argv)
  49. int    argc;
  50. char    *argv[];
  51. {
  52.     FILE    *df,
  53.         *texfile,
  54.         *parmsf;
  55.  
  56.     static    double    xco, xco1,
  57.             yco, yco1;
  58.  
  59.     struct    ray    rr;
  60.     struct    vector    vp;
  61.     int    i, i1,
  62.         j, j1,
  63.         pixelev,
  64.         in = 0,
  65.         out = 0,
  66.         parms = 0,
  67.         tex = 0,
  68.         alias = 1,
  69.         a2 = 1,
  70.         c,
  71.         xoffset, yoffset;    /* for centered display */
  72.  
  73.     double    xmin, xmax,
  74.         ymin, ymax,
  75.         xpix, ypix,
  76.         aspect_ratio,
  77.         xc, yc,
  78.         dx, dx1,
  79.         dy, dy1,
  80.         vpx, vpy, vpz,
  81.         lsx, lsy, lsz,
  82.         lsrad;
  83.  
  84.  
  85.     if( !open_math() ) {
  86.         printf("couldn't open math library\n");
  87.         exit(103);
  88.     }
  89.  
  90.     if( !gfx_open() ) {
  91.         printf("couldn't open gfx libraries\n");
  92.         close_math();
  93.         exit(103);
  94.     }
  95.  
  96.     sam = SPFlt(1);
  97.  
  98.     /* command interp */
  99.  
  100.     for (i = 1; i < argc; i++) {
  101.         if (argv[i][0] != '-')
  102.             booboo ("Options start with a '-' ");
  103.         c = argv[i][1];
  104.  
  105.         switch (c) {
  106.  
  107.             case ('i'): /* input file */
  108.  
  109.                 if (in)
  110.                     booboo("Sorry, but you may only have one input file");
  111.                 in = 1;
  112.                 if(
  113.                     (i + 1) >= argc ||
  114.                     argv[i + 1][0] == '-'
  115.                 )    /* no arg */
  116.                     df = stdin;
  117.                 else if( ! ( df = fopen (argv[++i], "r") ) )
  118.                     booboo ("input file not found");
  119.                 break;
  120.  
  121.             case ('o'): /* output file */
  122.                 if (out)
  123.                     booboo ("Sorry, but you may have only one output file");
  124.                 out = 1;
  125.                 if (
  126.                     (i + 1) >= argc ||
  127.                     argv[i + 1][0] == '-'
  128.                 )    /* no arg */
  129.                     booboo("-o stdout option no longer supported");
  130.                 else
  131.                     strcpy( out_name, argv[++i]);
  132.                 break;
  133.  
  134.             case ('p'): /* parms file */
  135.                 if (parms)
  136.                     booboo ("Sorry, but you may only have one parameters file");
  137.                 parms = 1;
  138.                 if (
  139.                     (i + 1) >= argc ||
  140.                     argv[i + 1][0] == '-'
  141.                 )    /* no arg */
  142.                     parmsf = stdin;
  143.                 else if(! (parmsf = fopen(argv[++i], "r") ) )
  144.                     booboo ("parameters file not found");
  145.                 break;
  146.  
  147.             case ('r'): /* rotate results */
  148.                 fprintf(
  149.                     stderr,
  150.                     "ignoring -r, option no longer supported\n"
  151.                 );
  152.                 break;
  153.  
  154.             case ('s'): /* susie file */
  155.                 if (tex)
  156.                     booboo ("Sorry, but you may have only one image file");
  157.                 if (
  158.                     (i + 1) >= argc ||
  159.                     argv[i + 1][0] == '-'
  160.                 )    /* no arg */
  161.                     booboo ("-s requires an argument");
  162.                 tex = 1;
  163.                 if (!(texfile = fopen (argv[++i], "r") ) )
  164.                     booboo ("image file not found");
  165.                 break;
  166.  
  167.             case ('a'): /* anti-aliasing value */
  168.                 if (argv[i][2] < '1' || argv[i][2] > '9') {
  169.                     printf("%c\n",argv[i][2]);
  170.                     booboo ("-a needs a numerical argument");}
  171.                     alias = (int) atof ( &(argv[i][2]) );
  172.                     /* Find number of subcells per pixel*/
  173.                     a2  = alias * alias;
  174.                     break;
  175.  
  176.             case ('S'): /* amount of susie */
  177.                 if (argv[i][2] < '0' || argv[i][2] > '9') {
  178.                     printf("%c\n",argv[i][2]);
  179.                     booboo ("-S needs a numerical argument");
  180.                 }
  181.                     sam = ieee_to_ffp( atof (&(argv[i][2]) ) );
  182.                 break;
  183.             default:
  184.                 booboo ("Unrecognized option. Better try again");
  185.         } /* switch */
  186.     } /* for */
  187.  
  188.  
  189.     if (!in)
  190.         if ( ! (df = fopen ("tracer.ball", "r") ) )
  191.             booboo ("tracer.ball not found");
  192.  
  193.     if (!out)
  194.         strcpy( out_name, "tracer.pic");
  195.  
  196.     if (!parms)
  197.         if ( ! (parmsf = fopen ("tracer.param", "r") ) )
  198.             booboo ("tracer.param not found");
  199.  
  200.     if (!tex)
  201.         if ( ! (texfile = fopen ("tracer.pat", "r") ) )
  202.             booboo ("tracer.pat not found");
  203.  
  204.     /* read parameters from file */
  205.     fscanf(
  206.         parmsf,
  207.         "%lf %lf %lf %lf %lf %lf %lf",
  208.         &xmin, &xmax,
  209.         &ymin, &ymax,
  210.         &xpix, &ypix,
  211.         &aspect_ratio
  212.     );
  213.     fscanf(
  214.         parmsf,
  215.         "%lf %lf %lf %lf %lf %lf %lf",
  216.         &vpx, &vpy, &vpz,
  217.         &lsx, &lsy, &lsz,
  218.         &lsrad
  219.     );
  220.  
  221.     fclose (parmsf);
  222.  
  223.  
  224.     nob = g_bal (df);        /* Read ball data file.        */
  225.     g_bod (texfile);        /* Read & normalize background. */
  226.  
  227.     /* Set view point location.    */
  228.     MV (ieee_to_ffp(vpx), ieee_to_ffp(vpy), ieee_to_ffp(vpz), vp);
  229.  
  230.     /* Set light source location    */
  231.     MV (ieee_to_ffp(lsx), ieee_to_ffp(lsy), ieee_to_ffp(lsz), ls.cent);
  232.  
  233.     /* and radius.        */
  234.     ls.rad = ieee_to_ffp(lsrad);
  235.  
  236. #ifdef    FOOIE
  237.     /* just so that I remember this line was. ok to delete it */
  238.     fprintf (filep, "%d %d\n", (int) (xpix), (int) (ypix));
  239. #endif
  240.  
  241.     dx = xmax - xmin;            /* Find size of viewport   */
  242.     dy = ymax - ymin;
  243.  
  244.     if ( aspect_ratio != 0.0 ) {
  245.         xc = (xmax + xmin) / 2.0;    /* Find center of viewport */
  246.         yc = (ymax + ymin) / 2.0;
  247.         if (aspect_ratio < 0.0)
  248.             aspect_ratio *= -xpix / ypix;
  249.  
  250.         /* Adjust viewport to fit display?  */
  251.         if (dx < (dy * aspect_ratio)) {
  252.  
  253.             /* Yes - make a bigger x axis range. */
  254.             dx = dy * aspect_ratio;
  255.             xmin = xc - dx / 2.0;
  256.             xmax = xmin + dx;
  257.  
  258.         } else if ( dx > ( dy * aspect_ratio) ) {
  259.  
  260.             /* Yes - make a bigger y axis range. */
  261.             dy = dx / aspect_ratio;
  262.             ymin = yc - dy / 2.0;
  263.             ymax = ymin + dy;
  264.  
  265.         };
  266.     };
  267.  
  268.     dx /= xpix;        /* Find the size of each pixel in x */
  269.     dy /= ypix;        /* and y units. */
  270.     dx1 = dx / alias;    /* Find the size of each subcell in */
  271.     dy1 = dy / alias;    /* x and y units. */
  272.  
  273.     /* for centering in amiga specific window dimensions */
  274.     xoffset = (640 - (int) xpix) >> 1;
  275.     yoffset = (400 - (int) ypix) >> 1;
  276.  
  277.     yco = ymin + (dy / 2.0);
  278.     for( j=ypix; j > 0; yco += dy, j-- ) {
  279.  
  280.         xco = xmax - (dx / 2.0);
  281.         for ( i=xpix; i > 0; xco -= dx, i-- ) {
  282.  
  283.             pixelev = 0;
  284.             for (yco1 = yco, i1 = alias; i1 > 0; yco1 -= dy1, i1--)
  285.                 for (xco1 = xco, j1 = alias; j1 > 0; xco1 += dx1, j1--) {
  286.                     MV (ieee_to_ffp(xco1), ieee_to_ffp(yco1), SPFlt(0), rr.org);
  287.                     SV (rr.dir, rr.org, vp);
  288.                     pixelev += shade (&rr);
  289.                 };
  290.             do_pixel( (i+xoffset), (j-yoffset), (pixelev/a2) );
  291.             CHECK_IT;
  292.         }
  293.         printf(".");
  294.     }
  295.  
  296.     save_screen( gfx_screen(), out_name );
  297.  
  298.     gfx_close();
  299.     close_math();
  300. }
  301.  
  302. booboo(str)
  303. char    *str;
  304. {
  305.     printf ("%s\n", str);
  306.     gfx_close();
  307.     close_math();
  308.     exit (-1);
  309. }
  310.