home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vectoper.zip / RTSupSam.cpp < prev    next >
C/C++ Source or Header  |  1996-09-15  |  6KB  |  226 lines

  1.  
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. #include "RTTypes.h"
  7. #include "RTVecOps.h"
  8.  
  9. #include "RTObjOps.h"
  10. #include "RTIMOps.h"
  11. #include "RayTrace.h"
  12.  
  13.  
  14.  
  15.  
  16.  
  17. int RT_CompareI( Vector Iul, Vector Iur, Vector Ill, Vector Ilr);
  18.  
  19. /*
  20.  *******************************************************************************
  21.  *
  22.  *  Name:  RT_SuperSample
  23.  *
  24.  *  Purpose:  this routine super samples a pixel, or (in a recursive call)
  25.  *            a subarea of an already super sampled pixel, according to
  26.  *            the algorithm in section 15.10.4 of:
  27.  *
  28.  *               Computer Graphics (Principles and Practice)
  29.  *
  30.  *            This routine is called recursively for each subarea of the
  31.  *            area currently being super sampled.  If the subarea does
  32.  *            not require super sampling or if the recursive limit of
  33.  *            super sampling has been reached, the corner intensities for
  34.  *            the subarea are averaged.
  35.  *
  36.  *
  37.  *
  38.  *  Input Parameters
  39.  *
  40.  *     Iul, Iur, Ill, Ilr - intensities at corners of super sample area
  41.  *     Rul, Rur, Rll, Rlr - rays through corners of super sample area
  42.  *     scan - scan line containing "Rlr"
  43.  *     pix - pixel containing "Rlr"
  44.  *     d - current recursive depth in super sampling
  45.  *
  46.  *
  47.  *  Output Parameters
  48.  *
  49.  *     none
  50.  *
  51.  *******************************************************************************
  52.  */
  53. Vector
  54.    RT_SuperSample(Vector Iul, Vector Iur, Vector Ill, Vector Ilr,
  55.                   Ray    Rul, Ray    Rur, Ray    Rll, Ray    Rlr,
  56.                   float scan, float pix, int d)
  57.       {
  58.        Vector Imt, Imb;
  59.        Vector Iml, Imr;
  60.        Vector Ic;
  61.  
  62. /*
  63.  *     Ray Rul, Rur;
  64.  */
  65.        Ray Rmt, Rmb;
  66.        Ray Rml, Rmr;
  67.        Ray Rc;
  68.  
  69.        Vector pixval;
  70.        Vector newpix;
  71.  
  72.        float dsp;
  73.  
  74.  
  75. /*
  76.  *     initialization
  77.  */
  78.        dsp =((float) 1.0) / (float) (d * 2);
  79.        vset(&pixval, (float) 0.0,  (float) 0.0,  (float) 0.0);
  80.  
  81. /*
  82.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  83.  +  if ss is not needed - average I values ar corners
  84.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  85.  */
  86.        if ( (RT_CompareI(Iul, Iur, Ill, Ilr) == 0)  || (d >= 4) )  {
  87.            pixval = Vector_Sum(&Iul, &Iur);
  88.            pixval = Vector_Sum(&pixval, &Ill);
  89.            pixval = Vector_Sum(&pixval, &Ilr);
  90.  
  91.            pixval = VectorScaler_Division(&pixval, (float)4.0);
  92.           }
  93.  
  94. /*
  95.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  96.  +  if ss is needed - subdivide current area for ss
  97.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  98.  */
  99.        else  {
  100.  
  101. /*
  102.  *         determine additional rays for super sampling
  103.  */
  104.            Rmt.origin = Rlr.origin;
  105.            Rmt.dir = RT_DirectionVector(pix-dsp,       scan+( ((float)2.0)*dsp));
  106.  
  107.            Rmb.origin = Rlr.origin;
  108.            Rmb.dir = RT_DirectionVector(pix-dsp,       scan);
  109.  
  110.            Rml.origin = Rlr.origin;
  111.            Rml.dir = RT_DirectionVector(pix-( ((float)2.0)*dsp), scan+dsp);
  112.  
  113.            Rmr.origin = Rlr.origin;
  114.            Rmr.dir = RT_DirectionVector(pix,           scan+dsp);
  115.  
  116.            Rc.origin =  Rlr.origin;
  117.            Rc.dir =  RT_DirectionVector(pix-dsp,       scan+dsp);
  118.  
  119. /*
  120.  *         determine intensities for super sample rays
  121.  */
  122.            Imt = RT_Trace(Rmt, (float) -1.0, 1);
  123.            Imb = RT_Trace(Rmb, (float) -1.0, 1);
  124.            Iml = RT_Trace(Rml, (float) -1.0, 1);
  125.            Imr = RT_Trace(Rmr, (float) -1.0, 1);
  126.            Ic  = RT_Trace(Rc , (float) -1.0, 1);
  127.  
  128.  
  129. /*
  130.  *         super sample each subarea in super sample area & average results
  131.  */
  132.            pixval = RT_SuperSample(Ic,  Imr, Imb, Ilr, Rc,  Rmr, Rmb, Rlr,
  133.                                    scan,     pix,      d+1);
  134.  
  135.  
  136.            newpix = RT_SuperSample(Iml, Ic,  Ill, Imb, Rml, Rc,  Rll, Rmb,
  137.                                    scan,     pix-dsp,  d+1);
  138.            pixval = Vector_Sum(&pixval, &newpix);
  139.  
  140.  
  141.            newpix = RT_SuperSample(Imt, Iur, Ic,  Imr, Rmt, Rur, Rc,  Rmr,
  142.                                    scan+dsp, pix,      d+1);
  143.            pixval = Vector_Sum(&pixval, &newpix);
  144.  
  145.  
  146.            newpix = RT_SuperSample(Iul, Imt, Iml, Ic,  Rul, Rmt, Rml, Rc,
  147.                                    scan+dsp, pix-dsp,  d+1);
  148.            pixval = Vector_Sum(&pixval, &newpix);
  149.  
  150.  
  151.            pixval = VectorScaler_Division(&pixval, (float)4.0);
  152.           }
  153. /*
  154.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  155.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  156.  */
  157.  
  158.        return(pixval);
  159.       }
  160.  
  161.  
  162.  
  163. /*
  164.  *******************************************************************************
  165.  *
  166.  *  Name:  RT_CompareI
  167.  *
  168.  *  Purpose:  this routine compares the intensities at the corners of a
  169.  *            super sampled area to see if it should be subdivided.  The
  170.  *            comparison done on a component by component basis and deter-
  171.  *            mines if any of the components differ significantly from the
  172.  *            the component average.
  173.  *            
  174.  *
  175.  *
  176.  *
  177.  *  Input Parameters
  178.  *
  179.  *     Iul, Iur, Ill, Ilr - intensities at corners of super sampled area
  180.  *
  181.  *
  182.  *  Output Parameters
  183.  *
  184.  *     none
  185.  *
  186.  *******************************************************************************
  187.  */
  188. int
  189.    RT_CompareI( Vector Iul, Vector Iur, Vector Ill, Vector Ilr)
  190.       {
  191.        float avg;
  192.  
  193.  
  194. /*
  195.  *     compare Red components of intensity
  196.  */
  197.        avg = (Iul.x + Iur.x + Ill.x + Ilr.x) / ((float) 4.0);
  198.  
  199.        if ( fabs(avg - Iul.x) > 0.05 || fabs(avg - Iur.x) > 0.05 ||
  200.             fabs(avg - Ill.x) > 0.05 || fabs(avg - Ilr.x) > 0.05    ) 
  201.           return(1);
  202.  
  203.  
  204. /*
  205.  *     compare Green components of intensity
  206.  */
  207.        avg = (Iul.y + Iur.y + Ill.y + Ilr.y) / ((float) 4.0);
  208.  
  209.        if ( fabs(avg - Iul.y) > 0.05 || fabs(avg - Iur.y) > 0.05 ||
  210.             fabs(avg - Ill.y) > 0.05 || fabs(avg - Ilr.y) > 0.05    ) 
  211.           return(1);
  212.  
  213.  
  214. /*
  215.  *     compare Blue components of intensity
  216.  */
  217.        if ( fabs(avg - Iul.z) > 0.05 || fabs(avg - Iur.z) > 0.05 ||
  218.             fabs(avg - Ill.z) > 0.05 || fabs(avg - Ilr.z) > 0.05    ) 
  219.           return(1);
  220.  
  221.        return(0);
  222.       }
  223.  
  224.  
  225.  
  226.