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

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
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 #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; /* view parameters */
28 int hresolu = 512; /* horizontal resolution */
29 int vresolu = 512; /* vertical resolution */
30 double pixaspect = 1.0; /* pixel aspect ratio */
31
32 int psample = 4; /* pixel sample size */
33 double maxdiff = .05; /* max. difference for interpolation */
34 double dstrpix = 0.67; /* square pixel distribution */
35
36 double dstrsrc = 0.0; /* square source distribution */
37 double shadthresh = .05; /* shadow threshold */
38 double shadcert = .5; /* shadow certainty */
39
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 int ambres = 32; /* ambient resolution */
46 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 long tlastrept = 0L; /* time at last report */
57
58 extern long time();
59 extern long tstart; /* starting time */
60
61 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 double pixvalue();
68
69
70 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 #ifdef BSD
81 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 eputs(errmsg);
96 tlastrept = time((long *)0);
97 }
98 #else
99 report() /* report progress */
100 {
101 signal(SIGALRM, report);
102 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 eputs(errmsg);
106 }
107 #endif
108
109
110 render(zfile, oldfile) /* render the scene */
111 char *zfile, *oldfile;
112 {
113 extern long lseek();
114 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
115 float *zbar[MAXDIV+1]; /* z values */
116 int ypos; /* current scanline */
117 int ystep; /* current y step size */
118 int zfd;
119 COLOR *colptr;
120 float *zptr;
121 register int i;
122 /* check sampling */
123 if (psample < 1)
124 psample = 1;
125 else if (psample > MAXDIV)
126 psample = MAXDIV;
127 /* allocate scanlines */
128 for (i = 0; i <= psample; i++) {
129 scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
130 if (scanbar[i] == NULL)
131 goto memerr;
132 }
133 /* open z file */
134 if (zfile != NULL) {
135 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
136 sprintf(errmsg, "cannot open z file \"%s\"", zfile);
137 error(SYSTEM, errmsg);
138 }
139 for (i = 0; i <= psample; i++) {
140 zbar[i] = (float *)malloc(hresolu*sizeof(float));
141 if (zbar[i] == NULL)
142 goto memerr;
143 }
144 } else {
145 zfd = -1;
146 for (i = 0; i <= psample; i++)
147 zbar[i] = NULL;
148 }
149 /* write out boundaries */
150 fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
151 /* recover file and compute first */
152 i = salvage(oldfile);
153 if (zfd != -1 && i > 0 &&
154 lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
155 error(SYSTEM, "z file seek error in render");
156 pctdone = 100.0*i/vresolu;
157 if (ralrm > 0) /* report init stats */
158 report();
159 ypos = vresolu-1 - i;
160 fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
161 ystep = psample;
162 /* compute scanlines */
163 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 scanbar[0] = colptr;
172 zptr = zbar[ystep];
173 zbar[ystep] = zbar[0];
174 zbar[0] = zptr;
175 /* fill base line */
176 fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
177 /* fill bar */
178 fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
179 /* write it out */
180 for (i = ystep; i > 0; i--) {
181 if (zfd != -1 && write(zfd, (char *)zbar[i],
182 hresolu*sizeof(float))
183 < hresolu*sizeof(float))
184 goto writerr;
185 if (fwritescan(scanbar[i], hresolu, stdout) < 0)
186 goto writerr;
187 }
188 if (fflush(stdout) == EOF)
189 goto writerr;
190 /* record progress */
191 pctdone = 100.0*(vresolu-1-ypos)/vresolu;
192 if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
193 report();
194 }
195 /* clean up */
196 if (zfd != -1) {
197 if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
198 < hresolu*sizeof(float))
199 goto writerr;
200 if (close(zfd) == -1)
201 goto writerr;
202 for (i = 0; i <= psample; i++)
203 free((char *)zbar[i]);
204 }
205 fwritescan(scanbar[0], hresolu, stdout);
206 if (fflush(stdout) == EOF)
207 goto writerr;
208 for (i = 0; i <= psample; i++)
209 free((char *)scanbar[i]);
210 pctdone = 100.0;
211 return;
212 writerr:
213 error(SYSTEM, "write error in render");
214 memerr:
215 error(SYSTEM, "out of memory in render");
216 }
217
218
219 fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */
220 register COLOR *scanline;
221 register float *zline;
222 int xres, y, xstep;
223 {
224 int b = xstep;
225 double z;
226 register int i;
227
228 z = pixvalue(scanline[0], 0, y);
229 if (zline) zline[0] = z;
230
231 for (i = xstep; i < xres-1+xstep; i += xstep) {
232 if (i >= xres) {
233 xstep += xres-1-i;
234 i = xres-1;
235 }
236 z = pixvalue(scanline[i], i, y);
237 if (zline) zline[i] = z;
238
239 b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
240 i-xstep, y, xstep, 0, b/2);
241 }
242 }
243
244
245 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
246 register COLOR *scanbar[];
247 register float *zbar[];
248 int xres, y, ysize;
249 {
250 COLOR vline[MAXDIV+1];
251 float zline[MAXDIV+1];
252 int b = ysize;
253 register int i, j;
254
255 for (i = 0; i < xres; i++) {
256
257 copycolor(vline[0], scanbar[0][i]);
258 copycolor(vline[ysize], scanbar[ysize][i]);
259 if (zbar[0]) {
260 zline[0] = zbar[0][i];
261 zline[ysize] = zbar[ysize][i];
262 }
263
264 b = fillsample(vline, zbar[0] ? zline : NULL,
265 i, y, 0, ysize, b/2);
266
267 for (j = 1; j < ysize; j++)
268 copycolor(scanbar[j][i], vline[j]);
269 if (zbar[0])
270 for (j = 1; j < ysize; j++)
271 zbar[j][i] = zline[j];
272 }
273 }
274
275
276 int
277 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
278 register COLOR *colline;
279 register float *zline;
280 int x, y;
281 int xlen, ylen;
282 int b;
283 {
284 extern double fabs();
285 double ratio;
286 double z;
287 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 if (b > 0
300 || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
301 || bigdiff(colline[0], colline[len], maxdiff)) {
302
303 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
304 if (zline) zline[len>>1] = z;
305 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 if (zline) zline[len>>1] = zline[len] * ratio;
313 ratio = 1.0 - ratio;
314 copycolor(ctmp, colline[0]);
315 scalecolor(ctmp, ratio);
316 addcolor(colline[len>>1], ctmp);
317 if (zline) zline[len>>1] += zline[0] * ratio;
318 ncut = 0;
319 }
320 /* recurse */
321 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
322
323 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
327 return(ncut);
328 }
329
330
331 double
332 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 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
344 rayorigin(&thisray, NULL, PRIMARY, 1.0);
345
346 rayvalue(&thisray); /* trace ray */
347
348 copycolor(col, thisray.rcol); /* return color */
349
350 return(thisray.rt); /* return distance */
351 }
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
365 if ((fp = fopen(oldfile, "r")) == NULL) {
366 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 if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
374 sprintf(errmsg, "bad recover file \"%s\"", oldfile);
375 error(WARNING, errmsg);
376 fclose(fp);
377 return(0);
378 }
379
380 if (x != hresolu || y != vresolu) {
381 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
382 oldfile);
383 error(USER, errmsg);
384 }
385
386 scanline = (COLR *)malloc(hresolu*sizeof(COLR));
387 if (scanline == NULL)
388 error(SYSTEM, "out of memory in salvage");
389 for (y = 0; y < vresolu; y++) {
390 if (freadcolrs(scanline, hresolu, fp) < 0)
391 break;
392 if (fwritecolrs(scanline, hresolu, stdout) < 0)
393 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 }