home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libcommon / sampling.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  3.4 KB  |  133 lines

  1. /*
  2.  * sampling.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.  * sampling.c,v 4.1 1994/08/09 07:55:05 explorer Exp
  17.  *
  18.  * sampling.c,v
  19.  * Revision 4.1  1994/08/09  07:55:05  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:01  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:31:55  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "common.h"
  30. #include "sampling.h"
  31.  
  32. SampleInfo    Sampling;    /* sampling information */
  33.  
  34. /*
  35.  * Set sampling options.
  36.  */
  37. void
  38. SamplingSetOptions(n, gaussian, width)
  39. int n, gaussian;
  40. Float width;
  41. {
  42.     Float norm, u, v;
  43.     int x, y;
  44.  
  45.     Sampling.sidesamples = n;
  46.     Sampling.totsamples = n*n;
  47.     Sampling.weight = 1. / (Float)Sampling.totsamples;
  48.     Sampling.spacing = 1. / (Float)Sampling.sidesamples;
  49.     Sampling.filterwidth = width;
  50.     Sampling.filterdelta = Sampling.filterwidth * Sampling.spacing;
  51.     Sampling.gaussian = gaussian;
  52.  
  53.     Sampling.filter = (Float **)Malloc(Sampling.sidesamples
  54.         *sizeof(Float *));
  55.     for (y = 0; y < Sampling.sidesamples; y++) {
  56.         Sampling.filter[y] = (Float *)Malloc(Sampling.sidesamples *
  57.             sizeof(Float));
  58.     }
  59.     if (Sampling.gaussian) {
  60.         norm = 0.;
  61.         u = -0.5*Sampling.filterwidth + 
  62.              0.5*Sampling.filterwidth*Sampling.spacing;
  63.         for (x = 0; x < Sampling.sidesamples; x++) {
  64.             v = -0.5*Sampling.filterwidth +
  65.                  0.5*Sampling.filterwidth*Sampling.spacing;
  66.             for (y = 0; y < Sampling.sidesamples; y++) {
  67.                 Sampling.filter[x][y] = exp(-0.5*(u*u+v*v));
  68.                 norm += Sampling.filter[x][y];
  69.                 v += Sampling.spacing *
  70.                     Sampling.filterwidth;
  71.             }
  72.             u += Sampling.spacing * Sampling.filterwidth;
  73.         }
  74.         
  75.         for (x = 0; x < Sampling.sidesamples; x++)
  76.             for (y = 0; y < Sampling.sidesamples; y++)
  77.                 Sampling.filter[x][y] /= norm;
  78.     } else {
  79.         /* Box filter.  Yawn. */
  80.         for (x = 0; x < Sampling.sidesamples; x++)
  81.             for (y = 0; y < Sampling.sidesamples; y++)
  82.                 Sampling.filter[x][y] = Sampling.weight;
  83.     }
  84. }
  85.  
  86. /*
  87.  * Set start time and duration of frame.
  88.  */
  89. void
  90. SamplingSetTime(starttime, shutter, frame)
  91. Float starttime, shutter;
  92. int frame;
  93. {
  94.     Sampling.starttime = starttime;
  95.     Sampling.shutter = shutter;
  96.     Sampling.framenum = frame;
  97.     TimeSet(Sampling.starttime);
  98.     FrameSet((Float)frame);
  99. }
  100.  
  101. /*
  102.  * Find a point on a unit circle that is separated from other random
  103.  * points by some jitter spacing.
  104.  *
  105.  * It should do the above, but the temporary hack below just finds a
  106.  * jittered point in a unit square.
  107.  */
  108. void
  109. UnitCirclePoint(pnt, sample)
  110. Vector *pnt;
  111. {
  112.     /*
  113.      * This picks a random point on a -1 to 1 square.  The jitter stuff
  114.      * is correct enough to avoid excessive noise.  An extremely blurry
  115.      * bright highlight will look squarish, not roundish.  Sorry.
  116.      */
  117.     Float jit;
  118.  
  119.     if (sample >= 0) {
  120.         jit = 2. * Sampling.spacing;
  121.  
  122.         pnt->x = nrand()*jit - 1.0 +
  123.             (sample % Sampling.sidesamples) * jit;
  124.         pnt->y = nrand()*jit - 1.0 +
  125.             (sample / Sampling.sidesamples) * jit;
  126.         pnt->z = 0.0;
  127.     } else {
  128.         pnt->x = nrand() * 2.0 - 1.0;
  129.         pnt->y = nrand() * 2.0 - 1.0;
  130.         pnt->z = 0.0;
  131.     }
  132. }
  133.