ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.43
Committed: Wed Feb 7 16:35:18 1996 UTC (28 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.42: +2 -8 lines
Log Message:
moved host name stuff to lib routine myhostname()

File Contents

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