home *** CD-ROM | disk | FTP | other *** search
/ Altsys Virtuoso 2.0K / virtuoso_20k.iso / DemoApps / Graphics / Viewers / raytracers / ohta / Source / scan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-08  |  3.2 KB  |  174 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #include "ray.h"
  5.  
  6. int res = 128, xres, yres;
  7. #define MAXRES 2048
  8.  
  9. typedef struct {int xres, yres;} pichead;
  10. typedef unsigned char icolor[3];
  11.  
  12. char aaflag[5][4*MAXRES+1];
  13. icolor aabuf[5][4*MAXRES+1];
  14.  
  15. pixel(ic,x,y)
  16. icolor ic;
  17. register double x,y;
  18. {register double t;
  19. struct ray r;
  20. struct color c;
  21. register double m;
  22.     x*=fovf;
  23.     y*=fovf;
  24.     r.obj=0;
  25.     r.a.x=r.a.y=r.a.z=0;
  26.     t=1/sqrt(x*x+y*y+1);
  27.     r.l.x=x*t;
  28.     r.l.y=y*t;
  29.     r.l.z=t;
  30.     c=trace(0,r);
  31.     m=c.r;
  32.     if(m<c.g)
  33.         m=c.g;
  34.     if(m<c.b)
  35.         m=c.b;
  36.     if(m>1.0)
  37.     {    c.r/=m;
  38.         c.g/=m;
  39.         c.b/=m;
  40.     }
  41.     ic[0]=c.r*255;
  42.     ic[1]=c.g*255;
  43.     ic[2]=c.b*255;
  44. }
  45.  
  46. aapixel2(ic,i,j)
  47. icolor ic;
  48. register int i,j;
  49. {register double x,y;
  50.     x=((double)(j-2*xres))/(2*xres);
  51.     y=((double)(2*yres-i))/(2*yres);
  52.     pixel(ic,x,y);
  53. }
  54.  
  55. aapixel(ic,i,j,ai,s)
  56. icolor ic;
  57. register int i,j,ai,s;
  58. {register int k;
  59. icolor c,c00,c01,c10,c11;
  60.     if(!aaflag[ai][j])
  61.     {    aapixel2(aabuf[ai][j],i,j);
  62.         aaflag[ai][j]=1;
  63.     }
  64.     if(!aaflag[ai+s][j])
  65.     {    aapixel2(aabuf[ai+s][j],i+s,j);
  66.         aaflag[ai+s][j]=1;
  67.     }
  68.     if(!aaflag[ai][j+s])
  69.     {    aapixel2(aabuf[ai][j+s],i,j+s);
  70.         aaflag[ai][j+s]=1;
  71.     }
  72.     if(!aaflag[ai+s][j+s])
  73.     {    aapixel2(aabuf[ai+s][j+s],i+s,j+s);
  74.         aaflag[ai+s][j+s]=1;
  75.     }
  76.     for(k=0;k<3;k++)
  77.     {    c00[k]=aabuf[ai][j][k];
  78.         c10[k]=aabuf[ai+s][j][k];
  79.         c01[k]=aabuf[ai][j+s][k];
  80.         c11[k]=aabuf[ai+s][j+s][k];
  81.     }
  82.     c[0]=(c00[0]+c01[0]+c10[0]+c11[0])/4;
  83.     c[1]=(c00[1]+c01[1]+c10[1]+c11[1])/4;
  84.     c[2]=(c00[2]+c01[2]+c10[2]+c11[2])/4;
  85.     if(s==1||nearc(c,c00)&&nearc(c,c01)&&nearc(c,c10)&&nearc(c,c11))
  86.     {    ic[0]=c[0];
  87.         ic[1]=c[1];
  88.         ic[2]=c[2];
  89.         return;
  90.     }
  91.     s/=2;
  92.     aapixel(c00,i,j,ai,s);
  93.     aapixel(c01,i+s,j,ai+s,s);
  94.     aapixel(c10,i,j+s,ai,s);
  95.     aapixel(c11,i+s,j+s,ai+s,s);
  96.     ic[0]=(c00[0]+c01[0]+c10[0]+c11[0])/4;
  97.     ic[1]=(c00[1]+c01[1]+c10[1]+c11[1])/4;
  98.     ic[2]=(c00[2]+c01[2]+c10[2]+c11[2])/4;
  99. }
  100.  
  101. scan(f)
  102. register int f;
  103. {register int i,j;
  104. register double x,y;
  105. icolor buf[MAXRES];
  106.     scaninit(f);
  107.     for(i=0;i<yres;i++)
  108.     {    for(j=0;j<xres;j++)
  109.         {    x=(2*j-xres+0.5)/xres;
  110.             y=(yres-2*i-0.5)/yres;
  111.             pixel(buf[j],x,y);
  112.         }
  113.         (void) write(f,(char *)buf,xres*sizeof(icolor));
  114.     }
  115. }
  116.  
  117. ascan(f)
  118. register int f;
  119. {icolor buf[MAXRES];
  120. register double x,y;
  121. register int i,j,ai;
  122.     scaninit(f);
  123.     for(ai=0;ai<5;ai++)
  124.         for(j=0;j<=4*xres;j++)
  125.             aaflag[ai][j]=0;
  126.     for(j=0;j<=4*xres;j+=4)
  127.     {    x=((double)(j-2*xres))/(2*xres);
  128.         y=1;
  129.         pixel(aabuf[0][j],x,y);
  130.         aaflag[0][j]=1;
  131.     }
  132.     for(i=0;i<4*yres;i+=4)
  133.     {    for(j=0;j<=4*xres;j+=4)
  134.         {    x=((double)(j-2*xres))/(2*xres);
  135.             y=((double)(2*yres-(i+4)))/(2*yres);
  136.             pixel(aabuf[4][j],x,y);
  137.             aaflag[4][j]=1;
  138.         }
  139.         for(j=0;j<4*xres;j+=4)
  140.             aapixel(buf[j/4],i,j,0,4);
  141.         (void) write(f,(char *)buf,xres*sizeof(icolor));
  142.         for(j=0;j<=4*xres;j++)
  143.         {    aabuf[0][j][0]=aabuf[4][j][0];
  144.             aabuf[0][j][1]=aabuf[4][j][1];
  145.             aabuf[0][j][2]=aabuf[4][j][2];
  146.             aaflag[0][j]=aaflag[4][j];
  147.         }
  148.         for(ai=1;ai<5;ai++)
  149.         {    for(j=0;j<=4*xres;j++)
  150.                 aaflag[ai][j]=0;
  151.         }
  152.     }
  153. }
  154.  
  155. scaninit(f)
  156. int f;
  157. {pichead head;
  158.     if (res>MAXRES)
  159.     {    fprintf(stderr, "res=%d too large, must be less than %d\n",
  160.             res, MAXRES);
  161.         exit(1);
  162.     }
  163.     head.xres = xres = res;
  164.     head.yres = yres = res;
  165.     (void) write(f,(char *)&head,sizeof head);
  166. }
  167.  
  168. nearc(c0,c1)
  169. icolor c0,c1;
  170. {register int d;
  171.     d=abs(c0[0]-c1[0])+abs(c0[1]-c1[1])+abs(c0[2]-c1[2]);
  172.     return d<15;
  173. }
  174.