ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.39
Committed: Tue Dec 20 20:18:30 1994 UTC (29 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.38: +3 -3 lines
Log Message:
added -vo and -va (fore and aft clipping plane) view options

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