ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
(Generate patch)

Comparing ray/src/rt/rpict.c (file contents):
Revision 1.1 by greg, Thu Feb 2 10:41:36 1989 UTC vs.
Revision 1.26 by greg, Tue Jun 18 10:27:16 1991 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 15 | Line 15 | static char SCCSid[] = "$SunId$ LBL";
15   #ifdef BSD
16   #include  <sys/time.h>
17   #include  <sys/resource.h>
18 + #else
19 + #include  <signal.h>
20   #endif
21 + #include  <fcntl.h>
22  
23   #include  "view.h"
24  
25   #include  "random.h"
26  
27 < VIEW  ourview = STDVIEW(512);           /* view parameters */
27 > int  dimlist[MAXDIM];                   /* sampling dimensions */
28 > int  ndims = 0;                         /* number of sampling dimensions */
29 > int  samplendx;                         /* sample index number */
30  
31 < int  psample = 4;                       /* pixel sample size */
31 > VIEW  ourview = STDVIEW;                /* view parameters */
32 > int  hresolu = 512;                     /* horizontal resolution */
33 > int  vresolu = 512;                     /* vertical resolution */
34 > double  pixaspect = 1.0;                /* pixel aspect ratio */
35  
36 + int  psample = 4;                       /* pixel sample size */
37   double  maxdiff = .05;                  /* max. difference for interpolation */
29
38   double  dstrpix = 0.67;                 /* square pixel distribution */
39 +
40   double  dstrsrc = 0.0;                  /* square source distribution */
41 + double  shadthresh = .05;               /* shadow threshold */
42 + double  shadcert = .5;                  /* shadow certainty */
43  
44   int  maxdepth = 6;                      /* maximum recursion depth */
45   double  minweight = 5e-3;               /* minimum ray weight */
46  
47   COLOR  ambval = BLKCOLOR;               /* ambient value */
48   double  ambacc = 0.2;                   /* ambient accuracy */
49 < int  ambres = 128;                      /* ambient resolution */
49 > int  ambres = 32;                       /* ambient resolution */
50   int  ambdiv = 128;                      /* ambient divisions */
51   int  ambssamp = 0;                      /* ambient super-samples */
52   int  ambounce = 0;                      /* ambient bounces */
# Line 46 | Line 57 | int  ralrm = 0;                                /* seconds between reports */
57  
58   double  pctdone = 0.0;                  /* percentage done */
59  
60 + long  tlastrept = 0L;                   /* time at last report */
61 +
62 + extern long  time();
63 + extern long  tstart;                    /* starting time */
64 +
65   extern long  nrays;                     /* number of rays traced */
66  
67 < #define  MAXDIV         32              /* maximum sample size */
67 > #define  MAXDIV         15              /* maximum sample size */
68  
69   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
70  
71 + double  pixvalue();
72  
73 +
74   quit(code)                      /* quit program */
75   int  code;
76   {
# Line 63 | Line 81 | int  code;
81   }
82  
83  
84 + #ifdef BSD
85   report()                /* report progress */
86   {
68 #ifdef BSD
87          struct rusage  rubuf;
88          double  t;
89  
# Line 78 | Line 96 | report()               /* report progress */
96  
97          sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
98                          nrays, pctdone, t/3600.0);
99 +        eputs(errmsg);
100 +        tlastrept = time((long *)0);
101 + }
102   #else
103 <        sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
104 < #endif
103 > report()                /* report progress */
104 > {
105 >        signal(SIGALRM, report);
106 >        tlastrept = time((long *)0);
107 >        sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
108 >                        nrays, pctdone, (tlastrept-tstart)/3600.0);
109          eputs(errmsg);
85
86        if (ralrm > 0)
87                alarm(ralrm);
110   }
111 + #endif
112  
113  
114 < render(oldfile)                         /* render the scene */
115 < char  *oldfile;
114 > render(zfile, oldfile)                          /* render the scene */
115 > char  *zfile, *oldfile;
116   {
117 +        extern long  lseek();
118          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
119 +        float  *zbar[MAXDIV+1];         /* z values */
120          int  ypos;                      /* current scanline */
121 +        int  ystep;                     /* current y step size */
122 +        int  zfd;
123          COLOR  *colptr;
124 +        float  *zptr;
125          register int  i;
126 <
126 >                                        /* check sampling */
127          if (psample < 1)
128                  psample = 1;
129 <        else if (psample > MAXDIV)
129 >        else if (psample > MAXDIV) {
130 >                sprintf(errmsg, "pixel sampling reduced from %d to %d",
131 >                                psample, MAXDIV);
132 >                error(WARNING, errmsg);
133                  psample = MAXDIV;
134 <
135 <        ourview.hresolu -= ourview.hresolu % psample;
105 <        ourview.vresolu -= ourview.vresolu % psample;
106 <                
134 >        }
135 >                                        /* allocate scanlines */
136          for (i = 0; i <= psample; i++) {
137 <                scanbar[i] = (COLOR *)malloc((ourview.hresolu+1)*sizeof(COLOR));
137 >                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
138                  if (scanbar[i] == NULL)
139 <                        error(SYSTEM, "out of memory in render");
139 >                        goto memerr;
140          }
141 <        
141 >                                        /* open z file */
142 >        if (zfile != NULL) {
143 >                if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
144 >                        sprintf(errmsg, "cannot open z file \"%s\"", zfile);
145 >                        error(SYSTEM, errmsg);
146 >                }
147 >                for (i = 0; i <= psample; i++) {
148 >                        zbar[i] = (float *)malloc(hresolu*sizeof(float));
149 >                        if (zbar[i] == NULL)
150 >                                goto memerr;
151 >                }
152 >        } else {
153 >                zfd = -1;
154 >                for (i = 0; i <= psample; i++)
155 >                        zbar[i] = NULL;
156 >        }
157                                          /* write out boundaries */
158 <        printf("-Y %d +X %d\n", ourview.vresolu, ourview.hresolu);
159 <
160 <        ypos = ourview.vresolu - salvage(oldfile);      /* find top line */
161 <        fillscanline(scanbar[0], ypos, psample);        /* top scan */
162 <
163 <        for (ypos -= psample; ypos > -psample; ypos -= psample) {
164 <        
165 <                colptr = scanbar[psample];              /* get last scanline */
166 <                scanbar[psample] = scanbar[0];
158 >        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
159 >                                        /* recover file and compute first */
160 >        i = salvage(oldfile);
161 >        if (zfd != -1 && i > 0 &&
162 >                        lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
163 >                error(SYSTEM, "z file seek error in render");
164 >        pctdone = 100.0*i/vresolu;
165 >        if (ralrm > 0)                  /* report init stats */
166 >                report();
167 >        ypos = vresolu-1 - i;
168 >        fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
169 >        ystep = psample;
170 >                                                /* compute scanlines */
171 >        for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
172 >                                                        /* bottom adjust? */
173 >                if (ypos < 0) {
174 >                        ystep += ypos;
175 >                        ypos = 0;
176 >                }
177 >                colptr = scanbar[ystep];                /* move base to top */
178 >                scanbar[ystep] = scanbar[0];
179                  scanbar[0] = colptr;
180 <
181 <                fillscanline(scanbar[0], ypos, psample);        /* base scan */
182 <        
183 <                fillscanbar(scanbar, ypos, psample);
184 <                
185 <                for (i = psample-1; i >= 0; i--)
186 <                        if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0)
180 >                zptr = zbar[ystep];
181 >                zbar[ystep] = zbar[0];
182 >                zbar[0] = zptr;
183 >                                                        /* fill base line */
184 >                fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
185 >                                                        /* fill bar */
186 >                fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
187 >                                                        /* write it out */
188 >                for (i = ystep; i > 0; i--) {
189 >                        if (zfd != -1 && write(zfd, (char *)zbar[i],
190 >                                        hresolu*sizeof(float))
191 >                                        < hresolu*sizeof(float))
192                                  goto writerr;
193 +                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
194 +                                goto writerr;
195 +                }
196                  if (fflush(stdout) == EOF)
197                          goto writerr;
198 <                pctdone = 100.0*(ourview.vresolu-ypos)/ourview.vresolu;
198 >                                                        /* record progress */
199 >                pctdone = 100.0*(vresolu-1-ypos)/vresolu;
200 >                if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
201 >                        report();
202          }
203 <                
203 >                                                /* clean up */
204 >        if (zfd != -1) {
205 >                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
206 >                                < hresolu*sizeof(float))
207 >                        goto writerr;
208 >                if (close(zfd) == -1)
209 >                        goto writerr;
210 >                for (i = 0; i <= psample; i++)
211 >                        free((char *)zbar[i]);
212 >        }
213 >        fwritescan(scanbar[0], hresolu, stdout);
214 >        if (fflush(stdout) == EOF)
215 >                goto writerr;
216          for (i = 0; i <= psample; i++)
217                  free((char *)scanbar[i]);
218 +        pctdone = 100.0;
219          return;
220   writerr:
221          error(SYSTEM, "write error in render");
222 + memerr:
223 +        error(SYSTEM, "out of memory in render");
224   }
225  
226  
227 < fillscanline(scanline, y, xstep)                /* fill scan line at y */
227 > fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
228   register COLOR  *scanline;
229 < int  y, xstep;
229 > register float  *zline;
230 > int  xres, y, xstep;
231   {
232          int  b = xstep;
233 +        double  z;
234          register int  i;
235          
236 <        pixvalue(scanline[0], 0, y);
236 >        z = pixvalue(scanline[0], 0, y);
237 >        if (zline) zline[0] = z;
238  
239 <        for (i = xstep; i <= ourview.hresolu; i += xstep) {
240 <        
241 <                pixvalue(scanline[i], i, y);
239 >        for (i = xstep; i < xres-1+xstep; i += xstep) {
240 >                if (i >= xres) {
241 >                        xstep += xres-1-i;
242 >                        i = xres-1;
243 >                }
244 >                z = pixvalue(scanline[i], i, y);
245 >                if (zline) zline[i] = z;
246                  
247 <                b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2);
247 >                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
248 >                                i-xstep, y, xstep, 0, b/2);
249          }
250   }
251  
252  
253 < fillscanbar(scanbar, y, ysize)          /* fill interior */
253 > fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
254   register COLOR  *scanbar[];
255 < int  y, ysize;
255 > register float  *zbar[];
256 > int  xres, y, ysize;
257   {
258          COLOR  vline[MAXDIV+1];
259 +        float  zline[MAXDIV+1];
260          int  b = ysize;
261          register int  i, j;
262          
263 <        for (i = 0; i < ourview.hresolu; i++) {
263 >        for (i = 0; i < xres; i++) {
264                  
265                  copycolor(vline[0], scanbar[0][i]);
266                  copycolor(vline[ysize], scanbar[ysize][i]);
267 +                if (zbar[0]) {
268 +                        zline[0] = zbar[0][i];
269 +                        zline[ysize] = zbar[ysize][i];
270 +                }
271                  
272 <                b = fillsample(vline, i, y, 0, ysize, b/2);
272 >                b = fillsample(vline, zbar[0] ? zline : NULL,
273 >                                i, y, 0, ysize, b/2);
274                  
275                  for (j = 1; j < ysize; j++)
276                          copycolor(scanbar[j][i], vline[j]);
277 +                if (zbar[0])
278 +                        for (j = 1; j < ysize; j++)
279 +                                zbar[j][i] = zline[j];
280          }
281   }
282  
283  
284   int
285 < fillsample(colline, x, y, xlen, ylen, b)        /* fill interior points */
285 > fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
286   register COLOR  *colline;
287 + register float  *zline;
288   int  x, y;
289   int  xlen, ylen;
290   int  b;
291   {
292 +        extern double  fabs();
293          double  ratio;
294 +        double  z;
295          COLOR  ctmp;
296          int  ncut;
297          register int  len;
# Line 201 | Line 304 | int  b;
304          if (len <= 1)                   /* limit recursion */
305                  return(0);
306          
307 <        if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) {
307 >        if (b > 0
308 >        || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
309 >                        || bigdiff(colline[0], colline[len], maxdiff)) {
310          
311 <                pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
311 >                z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
312 >                if (zline) zline[len>>1] = z;
313                  ncut = 1;
314                  
315          } else {                                        /* interpolate */
# Line 211 | Line 317 | int  b;
317                  copycolor(colline[len>>1], colline[len]);
318                  ratio = (double)(len>>1) / len;
319                  scalecolor(colline[len>>1], ratio);
320 <                copycolor(ctmp, colline[0]);
320 >                if (zline) zline[len>>1] = zline[len] * ratio;
321                  ratio = 1.0 - ratio;
322 +                copycolor(ctmp, colline[0]);
323                  scalecolor(ctmp, ratio);
324                  addcolor(colline[len>>1], ctmp);
325 +                if (zline) zline[len>>1] += zline[0] * ratio;
326                  ncut = 0;
327          }
328                                                          /* recurse */
329 <        ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2);
329 >        ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
330          
331 <        ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1),
332 <                                xlen - (xlen>>1), ylen - (ylen>>1), b/2);
331 >        ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
332 >                        x+(xlen>>1), y+(ylen>>1),
333 >                        xlen-(xlen>>1), ylen-(ylen>>1), b/2);
334  
335          return(ncut);
336   }
337  
338  
339 + double
340   pixvalue(col, x, y)             /* compute pixel value */
341   COLOR  col;                     /* returned color */
342   int  x, y;                      /* pixel position */
343   {
344 <        static RAY  thisray;    /* our ray for this pixel */
344 >        static RAY  thisray;
345  
346 <        rayview(thisray.rorg, thisray.rdir, &ourview,
347 <                        x + pixjitter(), y + pixjitter());
346 >        if (viewray(thisray.rorg, thisray.rdir, &ourview,
347 >                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) {
348 >                setcolor(col, 0.0, 0.0, 0.0);
349 >                return(0.0);
350 >        }
351  
352          rayorigin(&thisray, NULL, PRIMARY, 1.0);
353 <        
353 >
354 >        samplendx = 3*y + x;                    /* set pixel index */
355 >
356          rayvalue(&thisray);                     /* trace ray */
357  
358          copycolor(col, thisray.rcol);           /* return color */
359 +        
360 +        return(thisray.rt);                     /* return distance */
361   }
362  
363  
# Line 254 | Line 371 | char  *oldfile;
371  
372          if (oldfile == NULL)
373                  return(0);
374 <        else if ((fp = fopen(oldfile, "r")) == NULL) {
374 >        
375 >        if ((fp = fopen(oldfile, "r")) == NULL) {
376                  sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
377                  error(WARNING, errmsg);
378                  return(0);
# Line 262 | Line 380 | char  *oldfile;
380                                  /* discard header */
381          getheader(fp, NULL);
382                                  /* get picture size */
383 <        if (fscanf(fp, "-Y %d +X %d\n", &y, &x) != 2) {
383 >        if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
384 >                sprintf(errmsg, "bad recover file \"%s\"", oldfile);
385 >                error(WARNING, errmsg);
386                  fclose(fp);
387                  return(0);
388          }
389  
390 <        if (x != ourview.hresolu || y != ourview.vresolu) {
390 >        if (x != hresolu || y != vresolu) {
391                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
392                                  oldfile);
393                  error(USER, errmsg);
274                return(0);
394          }
395  
396 <        scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR));
396 >        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
397          if (scanline == NULL)
398                  error(SYSTEM, "out of memory in salvage");
399 <        for (y = 0; y < ourview.vresolu; y++) {
400 <                if (freadcolrs(scanline, ourview.hresolu, fp) < 0)
399 >        for (y = 0; y < vresolu; y++) {
400 >                if (freadcolrs(scanline, hresolu, fp) < 0)
401                          break;
402 <                if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0)
402 >                if (fwritecolrs(scanline, hresolu, stdout) < 0)
403                          goto writerr;
404          }
405          if (fflush(stdout) == EOF)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines