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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines