home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / rayshade.lzh / viewing.c < prev    next >
Text File  |  1990-10-01  |  5KB  |  166 lines

  1. /*
  2.  * viewing.c
  3.  *
  4.  * Copyright (C) 1989, Craig E. Kolb
  5.  *
  6.  * This software may be freely copied, modified, and redistributed,
  7.  * provided that this copyright notice is preserved on all copies.
  8.  *
  9.  * There is no warranty or other guarantee of fitness for this software,
  10.  * it is provided solely .  Bug reports or fixes may be sent
  11.  * to the author, who may or may not act on them as he desires.
  12.  *
  13.  * You may not include this software in a program or other software product
  14.  * without supplying the source, or without informing the end-user that the
  15.  * source is available for no extra charge.
  16.  *
  17.  * If you modify this software, you should include a notice giving the
  18.  * name of the person performing the modification, the date of modification,
  19.  * and the reason for such modification.
  20.  *
  21.  * $Id: viewing.c,v 3.0.1.4 90/04/04 18:59:54 craig Exp $
  22.  *
  23.  * $Log:    viewing.c,v $
  24.  * Revision 3.0.1.4  90/04/04  18:59:54  craig
  25.  * patch5: Lint removal.
  26.  * 
  27.  * Revision 3.0.1.3  89/12/02  14:38:37  craig
  28.  * patch2: Added depth of field code, courtesy of Rodney G. Bogart.
  29.  * 
  30.  * Revision 3.0.1.2  89/11/27  19:07:49  craig
  31.  * patch2: Changed calculation of scrny when NORLE is defined so images
  32.  * patch2: will be rendered top-to-bottom.
  33.  * 
  34.  * Revision 3.0.1.1  89/11/16  18:24:30  craig
  35.  * patch1: Fixed calculation of dist in Stereo mode.
  36.  * 
  37.  * Revision 3.0  89/10/27  02:06:08  craig
  38.  * Baseline for first official release.
  39.  * 
  40.  */
  41. #include <math.h>
  42. #include <stdio.h>
  43. #include "constants.h"
  44. #include "typedefs.h"
  45. #include "funcdefs.h"
  46.  
  47. Vector scrni, scrnj;        /* Unit vectors of screen plane. */
  48. Vector lookp, eyep, up, firstray, scrnx, scrny;
  49. double vfov, hfov, Separation;
  50. double aperture = 0., focaldist = UNSET;
  51. int Xres = UNSET, Yres = UNSET, Stereo;
  52.  
  53. viewing()
  54. {
  55.     Vector gaze;
  56.     double magnitude, dist;
  57.  
  58.     vecsub(lookp, eyep, &gaze);
  59.     firstray = gaze;
  60.  
  61.     dist = normalize(&gaze);
  62.     (void)crossp(&scrni, &gaze, &up);
  63.     (void)crossp(&scrnj, &scrni, &gaze);
  64.  
  65.     /*
  66.      * Add stereo separation if desired.
  67.      */
  68.     if (Stereo) {
  69.         if (Stereo == LEFT)
  70.             magnitude = -.5 * Separation;
  71.         else
  72.             magnitude = .5 * Separation;
  73.         eyep.x += magnitude * scrni.x;
  74.         eyep.y += magnitude * scrni.y;
  75.         eyep.z += magnitude * scrni.z;
  76.         vecsub(lookp, eyep, &firstray);
  77.         gaze = firstray;
  78.         dist = normalize(&gaze);
  79.         (void)crossp(&scrni, &gaze, &up);
  80.         (void)crossp(&scrnj, &scrni, &gaze);
  81.     }
  82.  
  83.     magnitude = 2. * dist * tan(deg2rad(0.5*hfov)) / Xres;
  84.     scrnx.x = scrni.x * magnitude;
  85.     scrnx.y = scrni.y * magnitude;
  86.     scrnx.z = scrni.z * magnitude;
  87.     magnitude = 2. * dist * tan(deg2rad(0.5*vfov)) / Yres;
  88. #ifdef NORLE
  89.     magnitude *= -1;
  90. #endif
  91.     scrny.x = scrnj.x * magnitude;
  92.     scrny.y = scrnj.y * magnitude;
  93.     scrny.z = scrnj.z * magnitude;
  94.  
  95.     firstray.x += 0.5*Yres*scrny.x - 0.5*Xres*scrnx.x;
  96.     firstray.y += 0.5*Yres*scrny.y - 0.5*Xres*scrnx.y;
  97.     firstray.z += 0.5*Yres*scrny.z - 0.5*Xres*scrnx.z;
  98.  
  99.     if (focaldist == UNSET)
  100.         focaldist = dist;
  101. }
  102.  
  103. /*
  104.  * Depth of field code courtesy of Rodney G. Bogart.
  105.  *
  106.  * Adjust the initial ray to account for an aperture and a focal
  107.  * distance.  The ray argument is assumed to be an initial ray, and
  108.  * always reset to the eye point.  It is assumed to be unit length.
  109.  */
  110. focus_blur_ray(ray)
  111. Ray *ray;
  112. {
  113.     Vector circle_point, aperture_inc;
  114.     /*
  115.      * Find a point on a unit circle and scale by aperture size.
  116.      * This simulates rays passing thru different parts of the aperture.
  117.      * Treat the point as a vector and rotate it so the circle lies
  118.      * in the plane of the screen.  Add the aperture increment to the
  119.      * starting position of the ray.  Stretch the ray to be focaldist 
  120.      * in length.  Subtract the aperture increment from the end of the
  121.      * long ray.  This insures that the ray heads toward a point at
  122.      * the specified focus distance, so that point will be in focus.
  123.      * Normalize the ray, and that's it.  Really.
  124.      */
  125.     unit_circle_point(&circle_point);
  126.     veccomb(aperture * circle_point.x, scrni,
  127.             aperture * circle_point.y, scrnj,
  128.             &aperture_inc);
  129.     vecadd(aperture_inc, eyep, &(ray->pos));
  130.     scalar_prod(focaldist, ray->dir, &(ray->dir));
  131.     vecsub(ray->dir, aperture_inc, &(ray->dir));
  132.     (void)normalize(&ray->dir);
  133. }
  134.  
  135. /*
  136.  * Find a point on a unit circle which is separated from other random
  137.  * points by some jitter spacing.
  138.  *
  139.  * It should do the above, but the temporary hack below just finds a
  140.  * jittered point in a unit square.
  141.  */
  142. unit_circle_point(pnt)
  143. Vector *pnt;
  144. {
  145.     /*
  146.      * This picks a random point on a -1 to 1 square.  The jitter stuff
  147.      * is correct enough to avoid excessive noise.  An extremely blurry
  148.      * bright highlight will look squarish, not roundish.  Sorry.
  149.      */
  150.     double jit;
  151.     extern double SampleSpacing;
  152.     extern int Jittered, JitSamples, SampleNumber;
  153.  
  154.     if (Jittered) {
  155.         jit = 2. * SampleSpacing;
  156.  
  157.         pnt->x = nrand()*jit - 1.0 + (SampleNumber % JitSamples) * jit;
  158.         pnt->y = nrand()*jit - 1.0 + (SampleNumber / JitSamples) * jit;
  159.         pnt->z = 0.0;
  160.     } else {
  161.         pnt->x = nrand() * 2.0 - 1.0;
  162.         pnt->y = nrand() * 2.0 - 1.0;
  163.         pnt->z = 0.0;
  164.     }
  165. }
  166.