home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / rayshade / src / viewing.c < prev    next >
C/C++ Source or Header  |  1992-04-30  |  9KB  |  294 lines

  1. /*
  2.  * viewing.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb, Rod G. Bogart
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: viewing.c,v 4.0.1.1 91/09/29 15:53:09 cek Exp Locker: cek $
  17.  *
  18.  * $Log:    viewing.c,v $
  19.  * Revision 4.0.1.1  91/09/29  15:53:09  cek
  20.  * patch1: Added support for window and crop commands.
  21.  * 
  22.  * Revision 4.0  91/07/17  14:48:18  kolb
  23.  * Initial version.
  24.  * 
  25.  */
  26. #include "rayshade.h"
  27. #include "viewing.h"
  28. #include "sampling.h"
  29. #include "options.h"
  30. #include "defaults.h"
  31. #include "picture.h"
  32. #include "stats.h"
  33.  
  34. RSCamera    Camera;
  35. RSScreen    Screen;
  36.  
  37. void SampleScreen(), SampleScreenFiltered();
  38.  
  39. void
  40. RSViewing()
  41. {
  42.     Float magnitude;
  43.  
  44.     VecSub(Camera.lookp, Camera.pos, &Camera.dir);
  45.     Screen.firstray = Camera.dir;
  46.  
  47.     Camera.lookdist = VecNormalize(&Camera.dir);
  48.     if (VecNormCross(&Camera.dir, &Camera.up, &Screen.scrni) == 0.)
  49.         RLerror(RL_PANIC,
  50.             "The view and up directions are identical?\n");
  51.     (void)VecNormCross(&Screen.scrni, &Camera.dir, &Screen.scrnj);
  52.  
  53.     /*
  54.      * Add stereo separation if desired.
  55.      */
  56.     if (Options.stereo) {
  57.         if (Options.stereo == LEFT)
  58.             magnitude = -.5 * Options.eyesep;
  59.         else
  60.             magnitude =  .5 * Options.eyesep;
  61.         Camera.pos.x += magnitude * Screen.scrni.x;
  62.         Camera.pos.y += magnitude * Screen.scrni.y;
  63.         Camera.pos.z += magnitude * Screen.scrni.z;
  64.         VecSub(Camera.lookp, Camera.pos, &Screen.firstray);
  65.         Camera.dir = Screen.firstray;
  66.         Camera.lookdist = VecNormalize(&Camera.dir);
  67.         (void)VecNormCross(&Camera.dir, &Camera.up, &Screen.scrni);
  68.         (void)VecNormCross(&Screen.scrni, &Camera.dir, &Screen.scrnj);
  69.     }
  70.  
  71.     magnitude = 2.*Camera.lookdist * tan(deg2rad(0.5*Camera.hfov)) /
  72.                 Screen.xres;
  73.  
  74.     VecScale(magnitude, Screen.scrni, &Screen.scrnx);
  75.     magnitude = 2.*Camera.lookdist * tan(deg2rad(0.5*Camera.vfov)) /
  76.                 Screen.yres;
  77. #ifndef URT
  78.     /*
  79.      * If using "generic" file format, render top-to-bottom (yick).
  80.      */
  81.     magnitude *= -1;
  82. #endif
  83.     VecScale(magnitude, Screen.scrnj, &Screen.scrny);
  84.  
  85.     Screen.firstray.x -= 0.5*Screen.yres*Screen.scrny.x +
  86.                  0.5*Screen.xres*Screen.scrnx.x;
  87.     Screen.firstray.y -= 0.5*Screen.yres*Screen.scrny.y +
  88.                  0.5*Screen.xres*Screen.scrnx.y;
  89.     Screen.firstray.z -= 0.5*Screen.yres*Screen.scrny.z +
  90.                  0.5*Screen.xres*Screen.scrnx.z;
  91.  
  92.     if (Camera.focaldist == UNSET)
  93.         Camera.focaldist = Camera.lookdist;
  94. }
  95.  
  96. /*
  97.  * Adjust the initial ray to account for an aperture and a focal
  98.  * distance.  The ray argument is assumed to be an initial ray, and
  99.  * always reset to the eye point.  It is assumed to be unit length.
  100.  */
  101. void
  102. focus_blur_ray(ray)
  103. Ray *ray;
  104. {
  105.     Vector circle_point, aperture_inc;
  106.     extern void UnitCirclePoint();
  107.     /*
  108.      * Find a point on a unit circle and scale by aperture size.
  109.      * This simulates rays passing thru different parts of the aperture.
  110.      * Treat the point as a vector and rotate it so the circle lies
  111.      * in the plane of the screen.  Add the aperture increment to the
  112.      * starting position of the ray.  Stretch the ray to be focaldist 
  113.      * in length.  Subtract the aperture increment from the end of the
  114.      * long ray.  This insures that the ray heads toward a point at
  115.      * the specified focus distance, so that point will be in focus.
  116.      * Normalize the ray, and that's it.  Really.
  117.      */
  118.     UnitCirclePoint(&circle_point, ray->sample);
  119.     VecComb(Camera.aperture * circle_point.x, Screen.scrni,
  120.             Camera.aperture * circle_point.y, Screen.scrnj,
  121.             &aperture_inc);
  122.     VecAdd(aperture_inc, Camera.pos, &(ray->pos));
  123.     VecScale(Camera.focaldist, ray->dir, &(ray->dir));
  124.     VecSub(ray->dir, aperture_inc, &(ray->dir));
  125.     (void)VecNormalize(&ray->dir);
  126. }
  127.  
  128. void
  129. ViewingSetup()
  130. {
  131. #define SWAP(a,b)    (tmp = (a), (a) = (b), (b) = tmp)
  132.  
  133.     Float tmp;
  134.     int xwidth, ywidth;
  135.  
  136.     if (Options.stereo && Options.eyesep == UNSET)
  137.         RLerror(RL_PANIC,
  138.             "No eye separation specified for stereo rendering.\n");
  139.     /*
  140.      * Because we want the user to be able to override the input file
  141.      * through the command line, we have to initialize some variables to
  142.      * bogus values so that when the file is being parsed, it is
  143.      * possible to tell if a given variable has been set on the
  144.      * command line.
  145.      *
  146.      * If such variables are not set to legal values on the command
  147.      * line or in the input file, we must do it here.
  148.      */
  149.     if (Screen.xres == UNSET)
  150.         Screen.xres = XRESOLUTION;
  151.     if (Screen.yres == UNSET)
  152.         Screen.yres = YRESOLUTION;
  153.  
  154.     /*
  155.      * The window to be rendered is defined by applying
  156.      * the crop window to the sub window.  The subwindow
  157.      * is defined using pixel numbers, and must be within
  158.      * [0, xres -1],[0, yres -1].  The default is the entire
  159.      * screen.  The crop window is specified using normalized
  160.      * coordinates.
  161.      */
  162.  
  163.     if (!Options.window_set) {
  164.         /* If no window set, set equal to entire screen. */    
  165.         Options.window[LOW][X] = Options.window[LOW][Y] = 0;
  166.         Options.window[HIGH][X] = Screen.xres -1;
  167.         Options.window[HIGH][Y] = Screen.yres -1;
  168.     }
  169.  
  170.     /* Truncate crop window to legal limits. */
  171.     if (Options.crop[LOW][X] > Options.crop[HIGH][X])
  172.         SWAP(Options.crop[LOW][X], Options.crop[HIGH][X]);
  173.     if (Options.crop[LOW][Y] > Options.crop[HIGH][Y])
  174.         SWAP(Options.crop[LOW][Y], Options.crop[HIGH][Y]);
  175.     if (Options.crop[LOW][X] < 0.) Options.crop[LOW][X] = 0.;
  176.     if (Options.crop[LOW][Y] < 0.) Options.crop[LOW][Y] = 0.;
  177.     if (Options.crop[HIGH][X] > 1.) Options.crop[HIGH][X] = 1.;
  178.     if (Options.crop[HIGH][Y] > 1.) Options.crop[HIGH][Y] = 1.;
  179.  
  180.     xwidth = Options.window[HIGH][X] - Options.window[LOW][X];
  181.     ywidth = Options.window[HIGH][Y] - Options.window[LOW][Y];
  182.  
  183.     /* Compute x and y extents of window to be renered. */
  184.     Screen.minx = (int)(Options.window[LOW][X] +
  185.             Options.crop[LOW][X] * xwidth);
  186.     Screen.maxx = (int)(Options.window[LOW][X] +
  187.             Options.crop[HIGH][X] * xwidth);
  188.     Screen.miny = (int)(Options.window[LOW][Y] +
  189.             Options.crop[LOW][Y] * ywidth);
  190.     Screen.maxy = (int)(Options.window[LOW][Y] +
  191.             Options.crop[HIGH][Y] * ywidth);
  192.  
  193. #ifdef URT
  194.     /*
  195.      * If using the URT, we should use the RLE file header to
  196.      * determine cropped window size.  Screen size
  197.      * (Screen.xres, Screen.yres) is determined from command
  198.      * line or input file, as usual.
  199.      *
  200.      * If the cropped window computed in PictureSetWindow()
  201.      * is not equal the cropped window computed above,
  202.      * a warning message is issued.
  203.      */
  204.     if (Options.appending) {
  205.         /*
  206.          * Read image header to determine window size.
  207.          */
  208.         PictureSetWindow();
  209.     }
  210. #endif
  211.  
  212.     Screen.xsize = Screen.maxx - Screen.minx + 1;
  213.     Screen.ysize = Screen.maxy - Screen.miny + 1;
  214.  
  215.     /*
  216.      * Sanity check.
  217.      */
  218.     if (Screen.minx < 0 || Screen.miny < 0 ||
  219.         Screen.maxx >= Screen.xres || Screen.maxy >= Screen.yres)
  220.         RLerror(RL_PANIC, "Invalid window specification.\n");
  221.  
  222.     /*
  223.      * If not defined in the input file, calculate VFOV
  224.      * by hand.  This assumes that pixels are square, which is
  225.      * probably a bad idea.  ("aspect" option?)
  226.      */
  227.     if (Camera.vfov == UNSET)
  228.         Camera.vfov = Camera.hfov * Screen.yres / Screen.xres;
  229. }
  230.  
  231. void
  232. SampleScreenFiltered(x, y, u, v, ray, color, sample)
  233. Float x, y;
  234. Ray *ray;
  235. Pixel *color;
  236. int sample, u, v;
  237. {
  238.     SampleScreen(x, y, ray, color, sample);
  239.     color->r *= Sampling.filter[u][v];
  240.     color->g *= Sampling.filter[u][v];
  241.     color->b *= Sampling.filter[u][v];
  242.     color->alpha *= Sampling.filter[u][v];
  243. }    
  244.  
  245. void
  246. SampleScreen(x, y, ray, color, sample)
  247. Float x, y;        /* Screen position to sample */
  248. Ray *ray;        /* ray, with origin and medium properly set */
  249. Pixel *color;        /* resulting color */
  250. int sample;        /* sample number */
  251. {
  252.     Float dist;
  253.     HitList hitlist;
  254.     Color ctmp, fullintens;
  255.     extern void focus_blur_ray(), ShadeRay();
  256.  
  257.     /*
  258.      * Calculate ray direction.
  259.      */
  260.     Stats.EyeRays++;
  261.     ray->dir.x = Screen.firstray.x + x*Screen.scrnx.x + y*Screen.scrny.x;
  262.     ray->dir.y = Screen.firstray.y + x*Screen.scrnx.y + y*Screen.scrny.y;
  263.     ray->dir.z = Screen.firstray.z + x*Screen.scrnx.z + y*Screen.scrny.z;
  264.  
  265.     (void)VecNormalize(&ray->dir);
  266.  
  267.     ray->sample = sample;
  268.  
  269.     if (Camera.aperture > 0.0) {
  270.         /*
  271.          * If the aperture is open, adjust the initial ray
  272.          * to account for depth of field.  
  273.          */
  274.         focus_blur_ray(ray);
  275.     }
  276.  
  277.     /*
  278.      * Do the actual ray trace.
  279.      */
  280.     fullintens.r = fullintens.g = fullintens.b = 1.;
  281.     dist = FAR_AWAY;
  282.     hitlist.nodes = 0;
  283.     (void)TraceRay(ray, &hitlist, EPSILON, &dist);
  284.     ShadeRay(&hitlist, ray, dist, &Screen.background, &ctmp, &fullintens);
  285.     color->r = ctmp.r;
  286.     color->g = ctmp.g;
  287.     color->b = ctmp.b;
  288.     if (hitlist.nodes != 0) {
  289.         color->alpha = 1.;
  290.     } else {
  291.         color->alpha = 0.;
  292.     }
  293. }
  294.