home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2213 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  4.2 KB  |  238 lines

  1.  
  2. /*
  3.  * * main.c - Main module for raytracer *
  4.  * 
  5.  * Copyright (C) 1990, Kory Hamzeh
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <math.h>
  10.  
  11. #include "rt.h"
  12. #include "externs.h"
  13.  
  14.  
  15.  
  16. main(argc, argv)
  17. int             argc;
  18. char           *argv[];
  19. {
  20.     long            timest, timeend;
  21.     int             i;
  22.  
  23.     time(×t);
  24.  
  25.     my_name = argv[0];
  26.  
  27.     /*
  28.      * check command line options
  29.      */
  30.  
  31.     ++argv;
  32.     --argc;
  33.     while (argc > 0 && *argv[0] == '-')
  34.     {
  35.         if (!strcmp(*argv, "-v"))    /* verbose mode         */
  36.         {
  37.             verbose = 1;
  38.             --argc;
  39.             ++argv;
  40.         }
  41.         else if (!strcmp(*argv, "-s"))    /* shadows off         */
  42.         {
  43.             shadow = 0;
  44.             --argc;
  45.             ++argv;
  46.         }
  47.         else if (!strcmp(*argv, "-l"))    /* disable reflections     */
  48.         {
  49.             reflect = 0;
  50.             --argc;
  51.             ++argv;
  52.         }
  53.         else if (!strcmp(*argv, "-r"))    /* disable refractions     */
  54.         {
  55.             refract = 0;
  56.             ++argv;
  57.             --argc;
  58.         }
  59.         else if (!strcmp(*argv, "-d"))    /* disable sampling     */
  60.         {
  61.             sample_cnt = 0;
  62.             ++argv;
  63.             --argc;
  64.         }
  65.         else if (!strcmp(*argv, "-z"))    /* disable image size     */
  66.         {
  67.             do_image_size = 0;
  68.             ++argv;
  69.             --argc;
  70.         }
  71.         else if (!strcmp(*argv, "-c"))    /* sample count         */
  72.         {
  73.             ++argv;
  74.             sample_cnt = atoi(*argv);
  75.             ++argv;
  76.             argc -= 2;
  77.             if (sample_cnt < 0)
  78.             {
  79.                 fprintf(stderr, "%s: sample count must be > 0\n", my_name);
  80.                 exit(1);
  81.             }
  82.         }
  83.         else if (!strcmp(*argv, "-y"))    /* y start and inc */
  84.         {
  85.             ++argv;
  86.             y_start = atoi(*argv);
  87.             ++argv;
  88.             y_inc = atoi(*argv);
  89.             ++argv;
  90.             argc -= 3;
  91.             if (y_start < 0 || y_inc < 1)
  92.             {
  93.                 fprintf(stderr, "%s: bad y_start and y_inc\n", my_name);
  94.                 exit(1);
  95.             }
  96.         }
  97.         else
  98.         {
  99.             Usage();
  100.         }
  101.     }
  102.  
  103.     if (argc == 0)
  104.     {
  105.         strcpy(input_file, "STDIN");
  106.         strcpy(output_file, "STDOUT");
  107.     }
  108.     else if (argc == 2)
  109.     {
  110.         strcpy(input_file, argv[0]);
  111.         strcpy(output_file, argv[1]);
  112.     }
  113.     else
  114.         Usage();
  115.  
  116.     /*
  117.      * Read the input file. Will exit on error.
  118.      */
  119.  
  120.     if (argc == 0)
  121.         Read_input_file(NULL);
  122.     else
  123.         Read_input_file(input_file);
  124.  
  125.     /*
  126.      * Check to make sure that there was at least one object and one
  127.      * light source specified.
  128.      */
  129.  
  130.     if (nlights == 0)
  131.     {
  132.         fprintf(stderr, "%s: no light sources were specified.\n", my_name);
  133.         exit(1);
  134.     }
  135.  
  136.     if (nobjects == 0)
  137.     {
  138.         fprintf(stderr, "%s: no objects were specified.\n", my_name);
  139.         exit(1);
  140.     }
  141.  
  142.     /*
  143.      * Adjust the intensity of each light
  144.      */
  145.  
  146.     for (i = 0; i < nlights; i++)
  147.     {
  148.         lights[i]->intensity = sqrt((double) nlights) / (double) nlights;
  149.     }
  150.  
  151.     /*
  152.      * Open the output file.
  153.      */
  154.  
  155.     if (argc == 0)
  156.         Init_output_file(NULL);
  157.     else
  158.         Init_output_file(output_file);
  159.  
  160.     /*
  161.      * If verbose flag is on, print some info.
  162.      */
  163.  
  164.     if (verbose)
  165.     {
  166.         fprintf(stderr, "%s: input file = %s\n", my_name, input_file);
  167.         fprintf(stderr, "%s: output file = %s\n", my_name, output_file);
  168.         fprintf(stderr, "%s: %d objects were specified\n", my_name, nobjects);
  169.         fprintf(stderr, "%s: %d lights were specified\n", my_name, nlights);
  170.         fprintf(stderr, "%s: output image is %d x %d\n", my_name,
  171.             view.x_res, view.y_res);
  172.     }
  173.  
  174.     /*
  175.      * Build the bounding box structures.
  176.      */
  177.  
  178.     Build_bounding_slabs();
  179.  
  180.     if (verbose)
  181.     {
  182.         fprintf(stderr, "%s: %d objects after adding bounding volumes\n", my_name,
  183.             nobjects);
  184.     }
  185.  
  186.     /*
  187.      * Raytrace the picture.
  188.      */
  189.  
  190.     Raytrace();
  191.  
  192.     /*
  193.      * Close output file and exit
  194.      */
  195.  
  196.     if(argc == 0)
  197.         Close_output_file(NULL);
  198.     else
  199.         Close_output_file(output_file);
  200.  
  201.     if(verbose)
  202.         fprintf(stderr, "\n");
  203.  
  204.     /*
  205.      * If verbose mode is on, then print some stats.
  206.      * 
  207.      */
  208.  
  209.     if (verbose)
  210.     {
  211.         time(&timeend);
  212.         fprintf(stderr, "%s: total execution time: %d:%02d\n", my_name,
  213.             (timeend - timest) / 60, (timeend - timest) % 60);
  214.         fprintf(stderr, "%s: number of rays traced: %d\n", my_name, n_rays);
  215.         fprintf(stderr, "%s: number of non-shadow intersections: %d\n", my_name,
  216.             n_intersects);
  217.         fprintf(stderr, "%s: number of shadow rays: %d\n", my_name, n_shadows);
  218.         fprintf(stderr, "%s: number of shadow hits: %d\n", my_name, n_shadinter);
  219.         fprintf(stderr, "%s: number of reflected rays: %d\n", my_name, n_reflect);
  220.         fprintf(stderr, "%s: number of refracted rays: %d\n", my_name, n_refract);
  221.     }
  222.  
  223.     exit(0);
  224. }
  225.  
  226. /*
  227.  * Usage() - Print usage and exit.
  228.  */
  229.  
  230. Usage()
  231. {
  232.     fprintf(stderr, 
  233.         "Usage: %s: [-v] [-s] [-l] [-r] [-d] [-z] [-c sample_count]\n", my_name);
  234.     fprintf(stderr, 
  235.         "           [-y y_start y_inc]  [input-file output-file]\n");
  236.     exit(1);
  237. }
  238.