ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.21
Committed: Wed Jan 20 15:19:47 1993 UTC (31 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.20: +5 -39 lines
Log Message:
added -P and -PP persist options

File Contents

# Content
1 /* Copyright (c) 1992 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 #endif
19
20 #include <signal.h>
21
22 #include "view.h"
23
24 #include "resolu.h"
25
26 #include "random.h"
27
28 #include "paths.h"
29
30 #define RFTEMPLATE "rfXXXXXX"
31
32 int dimlist[MAXDIM]; /* sampling dimensions */
33 int ndims = 0; /* number of sampling dimensions */
34 int samplendx; /* sample index number */
35
36 VIEW ourview = STDVIEW; /* view parameters */
37 int hresolu = 512; /* horizontal resolution */
38 int vresolu = 512; /* vertical resolution */
39 double pixaspect = 1.0; /* pixel aspect ratio */
40
41 int psample = 4; /* pixel sample size */
42 double maxdiff = .05; /* max. difference for interpolation */
43 double dstrpix = 0.67; /* square pixel distribution */
44
45 double dstrsrc = 0.0; /* square source distribution */
46 double shadthresh = .05; /* shadow threshold */
47 double shadcert = .5; /* shadow certainty */
48 int directrelay = 1; /* number of source relays */
49 int vspretest = 512; /* virtual source pretest density */
50 int directvis = 1; /* sources visible? */
51 double srcsizerat = .25; /* maximum ratio source size/dist. */
52
53 double specthresh = .15; /* specular sampling threshold */
54 double specjitter = 1.; /* specular sampling jitter */
55
56 int maxdepth = 6; /* maximum recursion depth */
57 double minweight = 5e-3; /* minimum ray weight */
58
59 COLOR ambval = BLKCOLOR; /* ambient value */
60 double ambacc = 0.2; /* ambient accuracy */
61 int ambres = 32; /* ambient resolution */
62 int ambdiv = 128; /* ambient divisions */
63 int ambssamp = 0; /* ambient super-samples */
64 int ambounce = 0; /* ambient bounces */
65 char *amblist[128]; /* ambient include/exclude list */
66 int ambincl = -1; /* include == 1, exclude == 0 */
67
68 #ifdef MSDOS
69 int ralrm = 60; /* seconds between reports */
70 #else
71 int ralrm = 0; /* seconds between reports */
72 #endif
73
74 double pctdone = 0.0; /* percentage done */
75
76 long tlastrept = 0L; /* time at last report */
77
78 extern long time();
79 extern long tstart; /* starting time */
80
81 extern long nrays; /* number of rays traced */
82
83 #define MAXDIV 16 /* maximum sample size */
84
85 #define pixjitter() (.5+dstrpix*(.5-frandom()))
86
87 static int hres, vres; /* resolution for this frame */
88
89 extern char *mktemp();
90
91 double pixvalue();
92
93
94 quit(code) /* quit program */
95 int code;
96 {
97 if (code) /* report status */
98 report();
99 headclean(); /* delete header file */
100 pfclean(); /* clean up persist files */
101 exit(code);
102 }
103
104
105 #ifdef BSD
106 report() /* report progress */
107 {
108 struct rusage rubuf;
109 double t;
110
111 getrusage(RUSAGE_SELF, &rubuf);
112 t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
113 t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
114 getrusage(RUSAGE_CHILDREN, &rubuf);
115 t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
116 t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
117
118 sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
119 nrays, pctdone, t/3600.0);
120 eputs(errmsg);
121 tlastrept = time((long *)0);
122 }
123 #else
124 report() /* report progress */
125 {
126 tlastrept = time((long *)0);
127 sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
128 nrays, pctdone, (tlastrept-tstart)/3600.0);
129 eputs(errmsg);
130 signal(SIGALRM, report);
131 }
132 #endif
133
134
135 rpict(seq, pout, zout, prvr) /* generate image(s) */
136 int seq;
137 char *pout, *zout, *prvr;
138 /*
139 * If seq is greater than zero, then we will render a sequence of
140 * images based on view parameter strings read from the standard input.
141 * If pout is NULL, then all images will be sent to the standard ouput.
142 * If seq is greater than zero and prvr is an integer, then it is the
143 * frame number at which rendering should begin. Preceeding view parameter
144 * strings will be skipped in the input.
145 * If pout and prvr are the same, prvr is renamed to avoid overwriting.
146 * Note that pout and zout should contain %d format specifications for
147 * sequenced file naming.
148 */
149 {
150 extern char *rindex(), *strncpy(), *strcat(), *strcpy();
151 char fbuf[128], fbuf2[128];
152 register char *cp;
153 RESOLU rs;
154 double pa;
155 /* check sampling */
156 if (psample < 1)
157 psample = 1;
158 else if (psample > MAXDIV) {
159 sprintf(errmsg, "pixel sampling reduced from %d to %d",
160 psample, MAXDIV);
161 error(WARNING, errmsg);
162 psample = MAXDIV;
163 }
164 /* get starting frame */
165 if (seq <= 0)
166 seq = 0;
167 else if (prvr != NULL && isint(prvr)) {
168 register int rn; /* skip to specified view */
169 if ((rn = atoi(prvr)) < seq)
170 error(USER, "recover frame less than start frame");
171 if (pout == NULL)
172 error(USER, "missing output file specification");
173 for ( ; seq < rn; seq++)
174 if (nextview(stdin) == EOF)
175 error(USER, "unexpected EOF on view input");
176 prvr = fbuf; /* mark for renaming */
177 }
178 if (pout != NULL & prvr != NULL) {
179 sprintf(fbuf, pout, seq);
180 if (!strcmp(prvr, fbuf)) { /* rename */
181 strcpy(fbuf2, fbuf);
182 for (cp = fbuf2; *cp; cp++)
183 ;
184 while (cp > fbuf2 && !ISDIRSEP(cp[-1]))
185 cp--;
186 strcpy(cp, RFTEMPLATE);
187 prvr = mktemp(fbuf2);
188 if (rename(fbuf, prvr) < 0 && errno != ENOENT) {
189 sprintf(errmsg,
190 "cannot rename \"%s\" to \"%s\"",
191 fbuf, prvr);
192 error(SYSTEM, errmsg);
193 }
194 }
195 }
196 /* render sequence */
197 do {
198 if (seq && nextview(stdin) == EOF)
199 break;
200 if (pout != NULL) {
201 sprintf(fbuf, pout, seq);
202 if (freopen(fbuf, "w", stdout) == NULL) {
203 sprintf(errmsg,
204 "cannot open output file \"%s\"", fbuf);
205 error(SYSTEM, errmsg);
206 }
207 #ifdef MSDOS
208 setmode(fileno(stdout), O_BINARY);
209 #endif
210 dupheader();
211 }
212 hres = hresolu; vres = vresolu; pa = pixaspect;
213 if (prvr != NULL)
214 if (viewfile(prvr, &ourview, &rs) <= 0
215 || rs.or != PIXSTANDARD) {
216 sprintf(errmsg,
217 "cannot recover view parameters from \"%s\"", prvr);
218 error(WARNING, errmsg);
219 } else {
220 pa = 0.0;
221 hres = scanlen(&rs);
222 vres = numscans(&rs);
223 }
224 if ((cp = setview(&ourview)) != NULL)
225 error(USER, cp);
226 normaspect(viewaspect(&ourview), &pa, &hres, &vres);
227 if (seq) {
228 if (ralrm > 0) {
229 fprintf(stderr, "FRAME %d:", seq);
230 fprintview(&ourview, stderr);
231 putc('\n', stderr);
232 fflush(stderr);
233 }
234 printf("FRAME=%d\n", seq);
235 }
236 fputs(VIEWSTR, stdout);
237 fprintview(&ourview, stdout);
238 putchar('\n');
239 if (pa < .99 || pa > 1.01)
240 fputaspect(pa, stdout);
241 fputformat(COLRFMT, stdout);
242 putchar('\n');
243 if (zout != NULL)
244 sprintf(cp=fbuf, zout, seq);
245 else
246 cp = NULL;
247 render(cp, prvr);
248 prvr = NULL;
249 } while (seq++);
250 }
251
252
253 nextview(fp) /* get next view from fp */
254 FILE *fp;
255 {
256 char linebuf[256];
257
258 while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
259 if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
260 return(0);
261 return(EOF);
262 }
263
264
265 render(zfile, oldfile) /* render the scene */
266 char *zfile, *oldfile;
267 {
268 extern long lseek();
269 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
270 float *zbar[MAXDIV+1]; /* z values */
271 char *sampdens; /* previous sample density */
272 int ypos; /* current scanline */
273 int ystep; /* current y step size */
274 int hstep; /* h step size */
275 int zfd;
276 COLOR *colptr;
277 float *zptr;
278 register int i;
279 /* allocate scanlines */
280 for (i = 0; i <= psample; i++) {
281 scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
282 if (scanbar[i] == NULL)
283 goto memerr;
284 }
285 hstep = (psample*140+49)/99; /* quincunx sampling */
286 ystep = (psample*99+70)/140;
287 if (hstep > 2) {
288 i = hres/hstep + 2;
289 if ((sampdens = malloc(i)) == NULL)
290 goto memerr;
291 while (i--)
292 sampdens[i] = hstep;
293 } else
294 sampdens = NULL;
295 /* open z file */
296 if (zfile != NULL) {
297 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
298 sprintf(errmsg, "cannot open z file \"%s\"", zfile);
299 error(SYSTEM, errmsg);
300 }
301 #ifdef MSDOS
302 setmode(zfd, O_BINARY);
303 #endif
304 for (i = 0; i <= psample; i++) {
305 zbar[i] = (float *)malloc(hres*sizeof(float));
306 if (zbar[i] == NULL)
307 goto memerr;
308 }
309 } else {
310 zfd = -1;
311 for (i = 0; i <= psample; i++)
312 zbar[i] = NULL;
313 }
314 /* write out boundaries */
315 fprtresolu(hres, vres, stdout);
316 /* recover file and compute first */
317 i = salvage(oldfile);
318 if (zfd != -1 && i > 0 &&
319 lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
320 error(SYSTEM, "z file seek error in render");
321 pctdone = 100.0*i/vres;
322 if (ralrm > 0) /* report init stats */
323 report();
324 #ifndef BSD
325 else
326 #endif
327 signal(SIGALRM, report);
328 ypos = vres-1 - i;
329 fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
330 /* compute scanlines */
331 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
332 /* bottom adjust? */
333 if (ypos < 0) {
334 ystep += ypos;
335 ypos = 0;
336 }
337 colptr = scanbar[ystep]; /* move base to top */
338 scanbar[ystep] = scanbar[0];
339 scanbar[0] = colptr;
340 zptr = zbar[ystep];
341 zbar[ystep] = zbar[0];
342 zbar[0] = zptr;
343 /* fill base line */
344 fillscanline(scanbar[0], zbar[0], sampdens,
345 hres, ypos, hstep);
346 /* fill bar */
347 fillscanbar(scanbar, zbar, hres, ypos, ystep);
348 /* write it out */
349 #ifndef BSD
350 signal(SIGALRM, SIG_IGN); /* don't interrupt writes */
351 #endif
352 for (i = ystep; i > 0; i--) {
353 if (zfd != -1 && write(zfd, (char *)zbar[i],
354 hres*sizeof(float))
355 < hres*sizeof(float))
356 goto writerr;
357 if (fwritescan(scanbar[i], hres, stdout) < 0)
358 goto writerr;
359 }
360 if (fflush(stdout) == EOF)
361 goto writerr;
362 /* record progress */
363 pctdone = 100.0*(vres-1-ypos)/vres;
364 if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
365 report();
366 #ifndef BSD
367 else
368 signal(SIGALRM, report);
369 #endif
370 }
371 /* clean up */
372 signal(SIGALRM, SIG_IGN);
373 if (zfd != -1) {
374 if (write(zfd, (char *)zbar[0], hres*sizeof(float))
375 < hres*sizeof(float))
376 goto writerr;
377 if (close(zfd) == -1)
378 goto writerr;
379 for (i = 0; i <= psample; i++)
380 free((char *)zbar[i]);
381 }
382 fwritescan(scanbar[0], hres, stdout);
383 if (fflush(stdout) == EOF)
384 goto writerr;
385 for (i = 0; i <= psample; i++)
386 free((char *)scanbar[i]);
387 if (sampdens != NULL)
388 free(sampdens);
389 pctdone = 100.0;
390 if (ralrm > 0)
391 report();
392 return;
393 writerr:
394 error(SYSTEM, "write error in render");
395 memerr:
396 error(SYSTEM, "out of memory in render");
397 }
398
399
400 fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */
401 register COLOR *scanline;
402 register float *zline;
403 register char *sd;
404 int xres, y, xstep;
405 {
406 static int nc = 0; /* number of calls */
407 int bl = xstep, b = xstep;
408 double z;
409 register int i;
410
411 z = pixvalue(scanline[0], 0, y);
412 if (zline) zline[0] = z;
413 /* zig-zag start for quincunx pattern */
414 for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
415 if (i >= xres) {
416 xstep += xres-1-i;
417 i = xres-1;
418 }
419 z = pixvalue(scanline[i], i, y);
420 if (zline) zline[i] = z;
421 if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
422 if (i <= xstep)
423 b = fillsample(scanline, zline, 0, y, i, 0, b/2);
424 else
425 b = fillsample(scanline+i-xstep,
426 zline ? zline+i-xstep : NULL,
427 i-xstep, y, xstep, 0, b/2);
428 if (sd) *sd++ = nc & 1 ? bl : b;
429 bl = b;
430 }
431 if (sd && nc & 1) *sd = bl;
432 }
433
434
435 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
436 register COLOR *scanbar[];
437 register float *zbar[];
438 int xres, y, ysize;
439 {
440 COLOR vline[MAXDIV+1];
441 float zline[MAXDIV+1];
442 int b = ysize;
443 register int i, j;
444
445 for (i = 0; i < xres; i++) {
446
447 copycolor(vline[0], scanbar[0][i]);
448 copycolor(vline[ysize], scanbar[ysize][i]);
449 if (zbar[0]) {
450 zline[0] = zbar[0][i];
451 zline[ysize] = zbar[ysize][i];
452 }
453
454 b = fillsample(vline, zbar[0] ? zline : NULL,
455 i, y, 0, ysize, b/2);
456
457 for (j = 1; j < ysize; j++)
458 copycolor(scanbar[j][i], vline[j]);
459 if (zbar[0])
460 for (j = 1; j < ysize; j++)
461 zbar[j][i] = zline[j];
462 }
463 }
464
465
466 int
467 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
468 register COLOR *colline;
469 register float *zline;
470 int x, y;
471 int xlen, ylen;
472 int b;
473 {
474 double ratio;
475 double z;
476 COLOR ctmp;
477 int ncut;
478 register int len;
479
480 if (xlen > 0) /* x or y length is zero */
481 len = xlen;
482 else
483 len = ylen;
484
485 if (len <= 1) /* limit recursion */
486 return(0);
487
488 if (b > 0
489 || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
490 || bigdiff(colline[0], colline[len], maxdiff)) {
491
492 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
493 if (zline) zline[len>>1] = z;
494 ncut = 1;
495
496 } else { /* interpolate */
497
498 copycolor(colline[len>>1], colline[len]);
499 ratio = (double)(len>>1) / len;
500 scalecolor(colline[len>>1], ratio);
501 if (zline) zline[len>>1] = zline[len] * ratio;
502 ratio = 1.0 - ratio;
503 copycolor(ctmp, colline[0]);
504 scalecolor(ctmp, ratio);
505 addcolor(colline[len>>1], ctmp);
506 if (zline) zline[len>>1] += zline[0] * ratio;
507 ncut = 0;
508 }
509 /* recurse */
510 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
511
512 ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
513 x+(xlen>>1), y+(ylen>>1),
514 xlen-(xlen>>1), ylen-(ylen>>1), b/2);
515
516 return(ncut);
517 }
518
519
520 double
521 pixvalue(col, x, y) /* compute pixel value */
522 COLOR col; /* returned color */
523 int x, y; /* pixel position */
524 {
525 static RAY thisray;
526
527 if (viewray(thisray.rorg, thisray.rdir, &ourview,
528 (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
529 setcolor(col, 0.0, 0.0, 0.0);
530 return(0.0);
531 }
532
533 rayorigin(&thisray, NULL, PRIMARY, 1.0);
534
535 samplendx = pixnumber(x,y,hres,vres); /* set pixel index */
536
537 rayvalue(&thisray); /* trace ray */
538
539 copycolor(col, thisray.rcol); /* return color */
540
541 return(thisray.rt); /* return distance */
542 }
543
544
545 int
546 salvage(oldfile) /* salvage scanlines from killed program */
547 char *oldfile;
548 {
549 COLR *scanline;
550 FILE *fp;
551 int x, y;
552
553 if (oldfile == NULL)
554 return(0);
555
556 if ((fp = fopen(oldfile, "r")) == NULL) {
557 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
558 error(WARNING, errmsg);
559 return(0);
560 }
561 #ifdef MSDOS
562 setmode(fileno(fp), O_BINARY);
563 #endif
564 /* discard header */
565 getheader(fp, NULL, NULL);
566 /* get picture size */
567 if (!fscnresolu(&x, &y, fp)) {
568 sprintf(errmsg, "bad recover file \"%s\"", oldfile);
569 error(WARNING, errmsg);
570 fclose(fp);
571 return(0);
572 }
573
574 if (x != hres || y != vres) {
575 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
576 oldfile);
577 error(USER, errmsg);
578 }
579
580 scanline = (COLR *)malloc(hres*sizeof(COLR));
581 if (scanline == NULL)
582 error(SYSTEM, "out of memory in salvage");
583 for (y = 0; y < vres; y++) {
584 if (freadcolrs(scanline, hres, fp) < 0)
585 break;
586 if (fwritecolrs(scanline, hres, stdout) < 0)
587 goto writerr;
588 }
589 if (fflush(stdout) == EOF)
590 goto writerr;
591 free((char *)scanline);
592 fclose(fp);
593 unlink(oldfile);
594 return(y);
595 writerr:
596 sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
597 error(SYSTEM, errmsg);
598 }
599
600
601 int
602 pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */
603 register int x, y;
604 int xres, yres;
605 {
606 x -= y;
607 while (x < 0)
608 x += xres;
609 return((((x>>2)*yres + y) << 2) + (x & 3));
610 }