ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.7
Committed: Wed Mar 11 09:32:40 1992 UTC (32 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +1 -0 lines
Log Message:
forgot to disable alarm signal before final writes

File Contents

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