home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vectoper.zip / RayTrace.cpp next >
C/C++ Source or Header  |  1996-09-23  |  7KB  |  218 lines

  1.  
  2.  
  3. #include <stdio.h>
  4.  
  5. #include "RTTypes.h"
  6. #include "RTMatMac.h"
  7. #include "RTVecOps.h"
  8.  
  9. #include "RTObjOps.h"
  10. #include "RTIMOps.h"
  11. #include "RayTrace.h"
  12.  
  13. #include <calypso.H>
  14.  
  15.  
  16.  
  17. SharedType *shared;  // Pointer to shared memory segment
  18.  
  19. int npix, nscan;     // Parameters that define image size
  20. FILE  *ofp;             // Output file pointer
  21.  
  22.  
  23.  
  24.  
  25. /*
  26.  *******************************************************************************
  27.  *
  28.  *  Name:  RT_ThreadSegment
  29.  *
  30.  *  Purpose:  this routine contains the code governing the behavior of each
  31.  *            thread in a parallel step.  Its execution is controled by its two
  32.  *            input parameters.  The parameter "numThreads" defines the total
  33.  *            numberof thread segments to be executed, while the parameter
  34.  *            "ThreadId"indicates which thread segment is currently being
  35.  *            executed.
  36.  *
  37.  *            Each parallel step is responsible for rendering a specified scan
  38.  *            set (from shared->FirstScan to shared->ScanLimit).  A given thread
  39.  *            segment is responsible for rendering some contiguous interval of
  40.  *            this scan set.  It determines this interval based on the scan set
  41.  *            information and input parameters.  All scan intervals are of equal
  42.  *            size except the last.  the size of the final interval may vary if
  43.  *            the number of thread segments does not evenly divide the number of
  44.  *            scan lines in the scan set. 
  45.  *
  46.  *            TECHNICAL NOTES
  47.  *                 It should noted that each thread segment executes an
  48.  *            additional scan line beyond the specified scan interval.  The
  49.  *            reason for this is that the algorithm actually calculates
  50.  *            intensities at pixel corners.  The additional scan line is
  51.  *            necessary for the bottom corners of the last row of pixels.
  52.  *                 It should also be noted that the output buffer in shared
  53.  *            memory (pix_buf) is overwritten in each parallel step.  Therefore
  54.  *            the first scan of each parallel step is placed in scan zero of the
  55.  *            output buffer.
  56.  *            
  57.  *
  58.  *
  59.  *
  60.  *  Input Parameters
  61.  *
  62.  *     NumThreads -    Number of thread segments which use this routine
  63.  *     ThreadId - thread segment currently executing this routine
  64.  *
  65.  *
  66.  *  Output Parameters
  67.  *
  68.  *     none
  69.  *
  70.  *******************************************************************************
  71.  */
  72. static void RT_ThreadSegment (int numThreads, int ThreadId)
  73.    {
  74.     Ray curr_ray;
  75.     int pix, scan, scanline;
  76.  
  77.  
  78. /*
  79.  *  Calculate scan interval for this thread segment
  80.  *  (if this is the last thread segment, the interval may be modified)
  81.  */
  82.  
  83.     int from = shared->FirstScan + (ThreadId * (shared->ScanSetSize/numThreads));
  84.     int to = from + (shared->ScanSetSize/numThreads);
  85.  
  86.     if (ThreadId == numThreads-1)
  87.         to = shared->ScanLimit + 1;
  88.  
  89. /*
  90.  *  initialize ray origin and index into output buffer
  91.  */
  92.     curr_ray.origin = shared->vp.F;
  93.     scanline = from - shared->FirstScan;
  94.  
  95. /*
  96.  *  Determine intensities for pixel corners in specified scan interval
  97.  */
  98.     for (scan=from; scan<to; scan++)  {
  99.  
  100.         for (pix=0; pix<shared->npix+1; pix++)  {
  101.             curr_ray.dir = RT_DirectionVector((float) pix, (float) scan);
  102.             shared->pix_buf[scanline][pix] = RT_Trace(curr_ray, (float)-1.0, 1);
  103.            }  /* end -- for pix */
  104.  
  105.          scanline++;
  106.  
  107.         }  /* end -- for scan */
  108.  
  109.    }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. /*
  118.  *******************************************************************************
  119.  *
  120.  *  Name:  RayTrace
  121.  *
  122.  *  Purpose:  this is the main routine for the parallelized ray tracing
  123.  *            algorithm.  It is designed to render an image in a specified
  124.  *            number (nIter) of parallel steps.  Each parallel step renders a
  125.  *            contiguous scan line set of an image.  All scan lines sets, except
  126.  *            the last, are of are of equal size.  The final set may vary in
  127.  *            size if the number of parallel steps does not evenly divide the
  128.  *            number of scan lines in the image.
  129.  *
  130.  *            The algorithm used here determines intensities at pixel corners
  131.  *            rather than at pixel centers.  Therefore, once a given parallel
  132.  *            step has been completed, it is necessary to average the four
  133.  *            values in the result buffer (pix_buf) that represent the corners
  134.  *            of a pixel in order to determine its intensity.  These intensity
  135.  *            values are then converted to RGB values by the routine RT_SetPixel
  136.  *            which also determines whether the results should be displayed or
  137.  *            written to an output file.
  138.  *
  139.  *            TECHNICAL NOTES
  140.  *                 It should be noted that the output buffer in shared memory
  141.  *            (pix_buf) is overwritten in each parallel step.  Therefore the
  142.  *            first scan of each parallel step is placed in scan zero of the
  143.  *            output buffer.
  144.  *
  145.  *
  146.  *
  147.  *  Input Parameters
  148.  *
  149.  *     none
  150.  *
  151.  *
  152.  *  Output Parameters
  153.  *
  154.  *     none
  155.  *
  156.  *******************************************************************************
  157.  */
  158. void
  159.    RayTrace ()
  160.       {
  161.        Vector Ipix[1030];
  162.  
  163.        int iter, scan, pix;
  164.   
  165. /*
  166.  *       Initialization of parameters for scene to be rendered
  167.  */
  168.        RT_SetViewParams();
  169.  
  170. /*
  171.  ++++++++++++++++++++++++++++++++++++++++++++++++++
  172.  +  Render image in nIter steps
  173.  ++++++++++++++++++++++++++++++++++++++++++++++++++
  174.  */
  175.        for (iter=0; iter<nIter; iter++)  {
  176.  
  177. /*
  178.  *           calculate scan set parameters for parallel step 
  179.  *        (if this is the last parallel step, the set size may be modified)
  180.  */
  181.            shared->FirstScan = iter * (nscan/nIter);
  182.            shared->ScanLimit = shared->FirstScan + (nscan/nIter);
  183.            
  184.            if (iter == nIter - 1)
  185.                shared->ScanLimit = nscan;
  186.  
  187.            shared->ScanSetSize = shared->ScanLimit - shared->FirstScan;
  188.  
  189. /*
  190.  *         calypso library call to handel execution of parallel step
  191.  */
  192.            ParallelExec(RT_ThreadSegment, num_T, NULL);
  193.  
  194. /*
  195.  *          average pixel corner intensities to get intensity for pixel 
  196.  */
  197.            for (scan=0; scan<shared->ScanSetSize; scan++)  {
  198.                for (pix=0; pix<npix; pix++)  {
  199.                    Ipix[pix] = shared->pix_buf[scan][pix];
  200.                    Ipix[pix] = Vector_Sum(&Ipix[pix], &shared->pix_buf[scan][pix+1]);
  201.                    Ipix[pix] = Vector_Sum(&Ipix[pix], &shared->pix_buf[scan+1][pix]);
  202.                    Ipix[pix] = Vector_Sum(&Ipix[pix], &shared->pix_buf[scan+1][pix+1]);
  203.  
  204.                    Ipix[pix] = VectorScaler_Division(&Ipix[pix], (float)4.0);
  205.                   }  /* end -- for pix */
  206.  
  207. /*
  208.  *                 convert pixel intensities to RGB values and display or write to a file
  209.  */
  210.                    RT_SetPixels (Ipix, shared->FirstScan+scan, npix);
  211.  
  212.               }  /* end -- for scan */
  213.                  
  214.           }  /* end -- for iter */ 
  215.  
  216.       }
  217.  
  218.