home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / mtvraytrace / part02 / screen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-26  |  5.2 KB  |  254 lines

  1. /***********************************************************************
  2.  * $Author: markv $
  3.  * $Revision: 1.3 $
  4.  * $Date: 88/10/04 14:33:37 $
  5.  * $Log:    screen.c,v $
  6.  * Revision 1.3  88/10/04  14:33:37  markv
  7.  * Revamped most of this code.  Added code for no antialiasing, jittering, 
  8.  * filtered, and statistically optimized antialiasing.   Statistically
  9.  * optimized anti aliasing still doesn't work properly.
  10.  * 
  11.  * Revision 1.2  88/09/12  10:00:54  markv
  12.  * Fixed lingering (possible) bug with curbuf memory allocation.  
  13.  * Size returned by malloc is now correct in both spots.
  14.  * Thanks tim@ben.
  15.  * 
  16.  * Revision 1.1  88/09/11  11:00:43  markv
  17.  * Initial revision
  18.  * 
  19.  ***********************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <math.h>
  23. #include <assert.h>
  24. #include "defs.h"
  25. #include "pic.h"
  26. #include "extern.h"
  27.  
  28. Screen(view, picfile, xres, yres)
  29.  Viewpoint *view ;
  30.  char *picfile ;
  31.  int xres, yres ;
  32. {
  33.     Pixel     *buf, *oldbuf, *curbuf, *tmp ;
  34.     Pic      *pic, *PicOpen();
  35.     Point     viewvec, leftvec, upvec ;
  36.     int red, green, blue ;
  37.     Point    dir ;
  38.     Ray     ray ;
  39.     Color    color;
  40.     Flt    frustrumwidth ;
  41.     int     x, y ;
  42.  
  43.     pic = PicOpen(picfile, xres, yres) ;
  44.  
  45.     /*
  46.      * calculate the "up" vector...
  47.      */
  48.  
  49.     VecCopy(view -> view_up, upvec) ;
  50.     VecNormalize(upvec) ;
  51.  
  52.     /*
  53.      * and the "view" vector....
  54.      */
  55.  
  56.     VecSub(view -> view_at, view-> view_from, viewvec);
  57.     VecNormalize(viewvec);
  58.  
  59.     /*
  60.      * And the "left" vector...
  61.      */
  62.  
  63.     VecCross(view -> view_up, viewvec, leftvec);
  64.     VecNormalize(leftvec);
  65.  
  66.     /*
  67.      * Calculate the width of the view frustrum in world coordinates.
  68.      * and then scale the left and up vectors appropriately.
  69.      */
  70.  
  71.     frustrumwidth = (view -> view_dist) * ((Flt) tan(view -> view_angle)) ;
  72.     VecScale(-frustrumwidth, upvec) ;
  73.     VecScale(-frustrumwidth, leftvec) ;
  74.  
  75.     if (filtflag) {
  76.         FilterScan( pic,
  77.             view -> view_from,
  78.             viewvec,
  79.             upvec,
  80.             leftvec, 
  81.             xres, yres) ;
  82.     } else if (jitterflag) {
  83.         JitterScan(   pic,
  84.             view -> view_from,
  85.             viewvec,
  86.             upvec,
  87.             leftvec, 
  88.              xres, yres) ;
  89.     } else {
  90.         Scan(   pic,
  91.             view -> view_from,
  92.             viewvec,
  93.             upvec,
  94.             leftvec, 
  95.              xres, yres) ;
  96.     }
  97.  
  98.     PicClose(pic) ;
  99. }
  100.  
  101.  
  102.  
  103.  
  104. Scan(pic, eye, viewvec, upvec, leftvec, xres, yres)
  105.  Pic * pic ;
  106.  Vec eye ;
  107.  Vec viewvec ;
  108.  Vec upvec ;
  109.  Vec leftvec ;
  110.  int xres, yres ;
  111. {
  112.     Ray ray ;
  113.     int x, y ;
  114.     Flt xlen, ylen ;
  115.     Color color ;
  116.  
  117.     /*
  118.      * Generate the image...
  119.      */
  120.  
  121.     VecCopy(eye, ray.P) ;
  122.     for (y = 0 ; y < yres ; y++) {
  123.         for (x = 0 ; x < xres ; x++) {
  124.             xlen = ((Flt) (2 * x) / (Flt) xres) - 1.0 ;
  125.             ylen = ((Flt) (2 * y) / (Flt) yres) - 1.0 ;
  126.             VecComb(xlen, leftvec, ylen, upvec, ray.D) ;
  127.             VecAdd(ray.D, viewvec, ray.D) ;
  128.             VecNormalize(ray.D);
  129.             Trace(0, 1.0, &ray, color);
  130.             color[0] = min(1.0, color[0]) ;
  131.             color[1] = min(1.0, color[1]) ;
  132.             color[2] = min(1.0, color[2]) ;
  133.             PicWritePixel(pic, color) ;
  134.         }
  135.         if (tickflag)
  136.             fprintf(stderr, ".") ;
  137.     }
  138.     if (tickflag)
  139.         fprintf(stderr, "\n") ;
  140. }
  141.  
  142. FilterScan(pic, eye, viewvec, upvec, leftvec, xres, yres)
  143.  Pic * pic ;
  144.  Vec eye ;
  145.  Vec viewvec ;
  146.  Vec upvec ;
  147.  Vec leftvec ;
  148.  int xres, yres ;
  149. {
  150.     Ray ray ;
  151.     int x, y, i ;
  152.     Flt xlen, ylen ;
  153.     Color color ;
  154.     Color * nbuf, *obuf, *tmp  ;    /* new and old buffers, resp.    */
  155.     Color avg ;
  156.  
  157.     /*
  158.      * allocate enough memory for the filter buffer
  159.      */
  160.  
  161.     nbuf = (Color *) calloc ((xres + 1), sizeof(Color)) ;
  162.     obuf = NULL ;
  163.  
  164.     VecCopy(eye, ray.P) ;
  165.  
  166.     for (y = 0 ; y <= yres ; y++) {
  167.         for (x = 0 ; x <= xres ; x++) {
  168.             xlen = ((Flt) (2 * x) / (Flt) xres) - 1.0 ;
  169.             ylen = ((Flt) (2 * y) / (Flt) yres) - 1.0 ;
  170.             VecComb(xlen, leftvec, ylen, upvec, ray.D) ;
  171.             VecAdd(ray.D, viewvec, ray.D) ;
  172.             VecNormalize(ray.D);
  173.             Trace(0, 1.0, &ray, color);
  174.             color[0] = min(1.0, color[0]) ;
  175.             color[1] = min(1.0, color[1]) ;
  176.             color[2] = min(1.0, color[2]) ;
  177.             VecCopy(color, nbuf[x]) ;
  178.         }
  179.         if (obuf) {
  180.             for (i = 0 ; i < xres ; i++) {
  181.                 VecAdd(nbuf[i], nbuf[i+1], avg) ;
  182.                 VecAdd(obuf[i], avg, avg) ;
  183.                 VecAdd(obuf[i+1], avg, avg) ;
  184.                 VecScale(0.25, avg) ;
  185.                 PicWritePixel(pic, avg) ;
  186.             }
  187.             tmp = obuf ;
  188.             obuf = nbuf ;
  189.             nbuf = tmp ;
  190.         } else {
  191.             /* 
  192.              * first scan line, set it up wierdly...
  193.              */
  194.             obuf = nbuf ;
  195.             nbuf = (Color *) calloc ((xres + 1), sizeof(Color)) ;
  196.         }
  197.         if (tickflag)
  198.             fprintf(stderr, ".") ;
  199.     }
  200.     if (tickflag)
  201.         fprintf(stderr, "\n") ;
  202. }
  203.  
  204. JitterScan(pic, eye, viewvec, upvec, leftvec, xres, yres)
  205.  Pic * pic ;
  206.  Vec eye ; 
  207.  Vec viewvec ; 
  208.  Vec upvec ; 
  209.  Vec leftvec ;
  210.  int xres, yres ;
  211. {
  212.     Ray ray ;
  213.     int x, y, i ;
  214.     Flt xlen, ylen ;
  215.     Flt xwidth, ywidth ;
  216.     Color color, avg ;
  217.  
  218.     /*
  219.      * Generate the image...
  220.      */
  221.  
  222.     xwidth = 2.0 / (Flt) xres ;
  223.     ywidth = 2.0 / (Flt) xres ;
  224.  
  225.     VecCopy(eye, ray.P) ;
  226.     for (y = 0 ; y < yres ; y++) {
  227.         ylen = ((Flt) (2 * y) / (Flt) yres) - 1.0 ;
  228.         for (x = 0 ; x < xres ; x++) {
  229.             xlen = ((Flt) (2 * x) / (Flt) xres) - 1.0 ;
  230.  
  231.             avg[0] = avg[1] = avg[2] = 0.0 ;
  232.  
  233.             for (i = 0 ; i < maxsamples ; i++) {
  234.                 VecComb(xlen + rnd() * xwidth, leftvec, 
  235.                     ylen + rnd() * ywidth, upvec, ray.D) ;
  236.                 VecAdd(ray.D, viewvec, ray.D) ;
  237.                 VecNormalize(ray.D);
  238.                 Trace(0, 1.0, &ray, color);
  239.                 VecAdd(color, avg, avg) ;
  240.             }
  241.  
  242.             VecScale(1.0 / (Flt) maxsamples, avg) ;
  243.             avg[0] = min(1.0, avg[0]) ;
  244.             avg[1] = min(1.0, avg[1]) ;
  245.             avg[2] = min(1.0, avg[2]) ;
  246.             PicWritePixel(pic, avg) ;
  247.         }
  248.         if (tickflag)
  249.             fprintf(stderr, ".") ;
  250.     }
  251.     if (tickflag)
  252.         fprintf(stderr, "\n") ;
  253. }
  254.