ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.40
Committed: Wed Dec 21 09:51:56 1994 UTC (29 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.39: +2 -0 lines
Log Message:
added -bv option for back face visibility (normally on)

File Contents

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