ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 1.24
Committed: Tue Apr 9 09:31:01 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.23: +23 -9 lines
Log Message:
changed reporting so that alarm() is not used.

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