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

  1. /*
  2.  * trace.c - This files contains the code which does the actuall raytracing.
  3.  * 
  4.  * Copyright (C) 1990, Kory Hamzeh
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9.  
  10. #include "rt.h"
  11. #include "externs.h"
  12.  
  13.  
  14. COLOR           Trace_a_ray(), Background_color(), Illuminate();
  15.  
  16.  
  17. /*
  18.  * Raytrace()
  19.  * 
  20.  * Raytrace the entire picture.
  21.  */
  22.  
  23. Raytrace()
  24. {
  25.     RAY             ray;
  26.     double          xr, yr, x_step, y_step, x_pw, y_pw;
  27.     double          x_rand, y_rand;
  28.     int             x, y;
  29.     VECTOR          hor, ver;
  30.     COLOR           col, lcol, scol;
  31.     long            ts, te;
  32.     int             l_pr, l_int, l_shad, l_refl, l_refr, s;
  33.  
  34.     /* calculate the viewing frustrum. */
  35.     VecSub(view.look_at, view.from, view.look_at);
  36.     VecNormalize(&view.look_at);
  37.     VecNormalize(&view.up);
  38.     VecCross(view.up, view.look_at, hor);
  39.     VecNormalize(&hor);    /* horizontal screen vector */
  40.     VecCross(view.look_at, hor, ver);
  41.     VecNormalize(&ver);    /* vertical screen vector     */
  42.  
  43.  
  44.     x_pw = x_step = 2.0 / view.x_res;
  45.     y_pw = y_step = 2.0 / view.y_res;
  46.  
  47.     VecCopy(view.from, ray.pos);
  48.  
  49.     view.angle = tan(view.angle * M_PI / 180) / sqrt(2.0);
  50.  
  51.     l_pr = l_int = l_shad = l_refl = l_refr = 0;
  52.     time(&ts);
  53.  
  54.     /* OK, start tracing */
  55.     yr = 1 - (y_step * (double) y_start);
  56.     y_step = y_step * (double) y_inc;
  57.  
  58.     for (y = y_start; y < view.y_res; y += y_inc)
  59.     {
  60.         xr = 1;
  61.         for (x = 0; x < view.x_res; x++)
  62.         {
  63.             /*
  64.              * Setup the ray
  65.              */
  66.             if (sample_cnt == 1)
  67.             {
  68.                 VecComb(xr * view.angle, hor, yr * view.angle, ver, ray.dir);
  69.                 VecAdd(ray.dir, view.look_at, ray.dir);
  70.                 VecNormalize(&ray.dir);
  71.  
  72.                 /*
  73.                  * Trace that Ray!!
  74.                  */
  75.  
  76.                 col = Trace_a_ray(&ray, 0);
  77.             }
  78.             else
  79.             {
  80.                 col.r = col.g = col.b = 0.0;
  81.                 for (s = 1; s < sample_cnt; s++)
  82.                 {
  83.                     x_rand = (xr * view.angle) + (x_pw * RAND());
  84.                     y_rand = (yr * view.angle) + (y_pw * RAND());
  85.  
  86.                     VecComb(x_rand, hor, y_rand, ver, ray.dir);
  87.                     VecAdd(ray.dir, view.look_at, ray.dir);
  88.                     VecNormalize(&ray.dir);
  89.  
  90.                     /*
  91.                      * printf("ray.dir = (%lg %lg
  92.                      * %lg)\n", ray.dir.x, ray.dir.y,
  93.                      * ray.dir.z);
  94.                      */
  95.  
  96.                     scol = Trace_a_ray(&ray, 0);
  97.  
  98.                     col.r += scol.r;
  99.                     col.g += scol.g;
  100.                     col.b += scol.b;
  101.                 }
  102.  
  103.                 col.r /= sample_cnt;
  104.                 col.g /= sample_cnt;
  105.                 col.b /= sample_cnt;
  106.             }
  107.  
  108.             /*
  109.              * Write pixel to output file
  110.              */
  111.  
  112.             Write_pixel(&col);
  113.  
  114.             xr -= x_step;
  115.         }
  116.  
  117.         Flush_output_file();
  118.  
  119.         yr -= y_step;
  120.  
  121.         if (verbose)
  122.         {
  123.             time(&te);
  124.             fprintf(stderr, "\r%s: scan %d -- %d:%02d  i:%d  s:%d   rl:%d  rr:%d ",
  125.                 my_name, y, (te - ts) / 60, (te - ts) % 60,
  126.                 n_intersects - l_int, n_shadinter - l_shad,
  127.                 n_reflect - l_refl, n_refract - l_refr);
  128.  
  129.             l_int = n_intersects;
  130.             l_shad = n_shadinter;
  131.             l_refl = n_reflect;
  132.             l_refr = n_refract;
  133.  
  134.             ts = te;
  135.         }
  136.     }
  137.  
  138. }
  139.  
  140.  
  141. /*
  142.  * Trace_a_ray()
  143.  * 
  144.  * Trace the given ray and return the resulting color.
  145.  */
  146.  
  147. COLOR 
  148. Trace_a_ray(ray, n)
  149. RAY            *ray;
  150. int             n;
  151. {
  152.     OBJECT         *obj;
  153.     INTERSECT       inter;
  154.     COLOR           col;
  155.     VECTOR          ip;
  156.     double          mc, t;
  157.  
  158.     ++n_rays;
  159.  
  160.     /*
  161.      * Check to see if this ray will intersect anything. If not, then
  162.      * return a proper background color. Else, apply the proper
  163.      * illumination model to get the color of the object.
  164.      */
  165.  
  166.     if (!Intersect(ray, &inter))
  167.         return (Background_color(ray));
  168.  
  169.     ++n_intersects;
  170.  
  171.     /*
  172.      * calculate the point of intersection and pass it to the shad
  173.      * function
  174.      */
  175.  
  176.     t = inter.t;
  177.     VecAddS(t, ray->dir, ray->pos, ip);
  178.  
  179.     col = Illuminate(&inter, ray, &ip, n);
  180.  
  181.     /*
  182.      * If colors have overflown, normalize it.
  183.      */
  184.  
  185.     return (col);
  186.  
  187. }
  188.  
  189.  
  190. /*
  191.  * Background_color()
  192.  * 
  193.  * Determin what color the background should be at the given point.
  194.  */
  195.  
  196. COLOR 
  197. Background_color(ray)
  198. RAY            *ray;
  199. {
  200.     return (bkgnd.col);
  201. }
  202.