1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
6 |
|
|
7 |
|
/* |
8 |
|
* rpict.c - routines and variables for picture generation. |
9 |
– |
* |
10 |
– |
* 8/14/85 |
9 |
|
*/ |
10 |
|
|
11 |
|
#include "ray.h" |
18 |
|
#include <sys/resource.h> |
19 |
|
#else |
20 |
|
#include <sys/times.h> |
23 |
– |
#include <sys/utsname.h> |
21 |
|
#include <unistd.h> |
22 |
|
#endif |
23 |
|
#endif |
53 |
|
double maxdiff = .05; /* max. difference for interpolation */ |
54 |
|
double dstrpix = 0.67; /* square pixel distribution */ |
55 |
|
|
56 |
+ |
double mblur = 0.; /* motion blur parameter */ |
57 |
+ |
|
58 |
|
double dstrsrc = 0.0; /* square source distribution */ |
59 |
|
double shadthresh = .05; /* shadow threshold */ |
60 |
|
double shadcert = .5; /* shadow certainty */ |
63 |
|
int directvis = 1; /* sources visible? */ |
64 |
|
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
65 |
|
|
66 |
+ |
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ |
67 |
+ |
COLOR salbedo = BLKCOLOR; /* 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 |
+ |
int ambvwt = 0; /* initial weight for ambient value */ |
81 |
|
double ambacc = 0.2; /* ambient accuracy */ |
82 |
|
int ambres = 32; /* ambient resolution */ |
83 |
|
int ambdiv = 128; /* ambient divisions */ |
104 |
|
|
105 |
|
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
106 |
|
|
107 |
< |
static int hres, vres; /* resolution for this frame */ |
107 |
> |
int hres, vres; /* resolution for this frame */ |
108 |
|
|
109 |
+ |
static VIEW lastview; /* the previous view input */ |
110 |
+ |
|
111 |
|
extern char *mktemp(); |
112 |
|
|
113 |
|
double pixvalue(); |
142 |
|
#ifndef NIX |
143 |
|
report() /* report progress */ |
144 |
|
{ |
145 |
+ |
extern char *myhostname(); |
146 |
|
double u, s; |
147 |
|
#ifdef BSD |
138 |
– |
char hostname[257]; |
148 |
|
struct rusage rubuf; |
149 |
|
#else |
150 |
|
struct tms tbuf; |
142 |
– |
struct utsname nambuf; |
151 |
|
double period; |
144 |
– |
#define hostname nambuf.nodename |
152 |
|
#endif |
153 |
|
|
154 |
|
tlastrept = time((time_t *)NULL); |
159 |
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
160 |
|
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
161 |
|
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
155 |
– |
gethostname(hostname, sizeof(hostname)); |
162 |
|
#else |
163 |
|
times(&tbuf); |
164 |
+ |
#ifdef _SC_CLK_TCK |
165 |
|
period = 1.0 / sysconf(_SC_CLK_TCK); |
166 |
+ |
#else |
167 |
+ |
period = 1.0 / 60.0; |
168 |
+ |
#endif |
169 |
|
u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; |
170 |
|
s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; |
161 |
– |
uname(&nambuf); |
171 |
|
#endif |
172 |
|
|
173 |
|
sprintf(errmsg, |
174 |
|
"%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", |
175 |
|
nrays, pctdone, u/3600., s/3600., |
176 |
< |
(tlastrept-tstart)/3600., hostname); |
176 |
> |
(tlastrept-tstart)/3600., myhostname()); |
177 |
|
eputs(errmsg); |
178 |
|
#ifndef BSD |
179 |
|
signal(SIGCONT, report); |
171 |
– |
#undef hostname |
180 |
|
#endif |
181 |
|
} |
182 |
|
#else |
232 |
|
for ( ; seq < rn; seq++) |
233 |
|
if (nextview(stdin) == EOF) |
234 |
|
error(USER, "unexpected EOF on view input"); |
235 |
+ |
setview(&ourview); |
236 |
|
prvr = fbuf; /* mark for renaming */ |
237 |
|
} |
238 |
|
if (pout != NULL & prvr != NULL) { |
274 |
|
fbuf); |
275 |
|
error(USER, errmsg); |
276 |
|
} |
277 |
+ |
setview(&ourview); |
278 |
|
continue; /* don't clobber */ |
279 |
|
} |
280 |
|
if (freopen(fbuf, "w", stdout) == NULL) { |
337 |
|
{ |
338 |
|
char linebuf[256]; |
339 |
|
|
340 |
+ |
copystruct(&lastview, &ourview); |
341 |
|
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) |
342 |
|
if (isview(linebuf) && sscanview(&ourview, linebuf) > 0) |
343 |
|
return(0); |
348 |
|
render(zfile, oldfile) /* render the scene */ |
349 |
|
char *zfile, *oldfile; |
350 |
|
{ |
340 |
– |
extern long lseek(); |
351 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
352 |
|
float *zbar[MAXDIV+1]; /* z values */ |
353 |
|
char *sampdens; /* previous sample density */ |
397 |
|
fprtresolu(hres, vres, stdout); |
398 |
|
/* recover file and compute first */ |
399 |
|
i = salvage(oldfile); |
400 |
+ |
if (i >= vres) |
401 |
+ |
goto alldone; |
402 |
|
if (zfd != -1 && i > 0 && |
403 |
< |
lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) |
403 |
> |
lseek(zfd, (long)i*hres*sizeof(float), 0) < 0) |
404 |
|
error(SYSTEM, "z-file seek error in render"); |
405 |
|
pctdone = 100.0*i/vres; |
406 |
|
if (ralrm > 0) /* report init stats */ |
409 |
|
else |
410 |
|
#endif |
411 |
|
signal(SIGCONT, report); |
412 |
< |
ypos = vres-1 - i; |
412 |
> |
ypos = vres-1 - i; /* initialize sampling */ |
413 |
> |
if (directvis) |
414 |
> |
init_drawsources(psample); |
415 |
|
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
416 |
|
/* compute scanlines */ |
417 |
|
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
431 |
|
hres, ypos, hstep); |
432 |
|
/* fill bar */ |
433 |
|
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
434 |
+ |
if (directvis) /* add bitty sources */ |
435 |
+ |
drawsources(scanbar, zbar, 0, hres, ypos, ystep); |
436 |
|
/* write it out */ |
437 |
|
#ifndef BSD |
438 |
|
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
458 |
|
} |
459 |
|
/* clean up */ |
460 |
|
signal(SIGCONT, SIG_IGN); |
461 |
< |
if (zfd != -1) { |
446 |
< |
if (write(zfd, (char *)zbar[0], hres*sizeof(float)) |
461 |
> |
if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float)) |
462 |
|
< hres*sizeof(float)) |
463 |
< |
goto writerr; |
463 |
> |
goto writerr; |
464 |
> |
fwritescan(scanbar[0], hres, stdout); |
465 |
> |
if (fflush(stdout) == EOF) |
466 |
> |
goto writerr; |
467 |
> |
alldone: |
468 |
> |
if (zfd != -1) { |
469 |
|
if (close(zfd) == -1) |
470 |
|
goto writerr; |
471 |
|
for (i = 0; i <= psample; i++) |
472 |
|
free((char *)zbar[i]); |
473 |
|
} |
454 |
– |
fwritescan(scanbar[0], hres, stdout); |
455 |
– |
if (fflush(stdout) == EOF) |
456 |
– |
goto writerr; |
474 |
|
for (i = 0; i <= psample; i++) |
475 |
|
free((char *)scanbar[i]); |
476 |
|
if (sampdens != NULL) |
497 |
|
int bl = xstep, b = xstep; |
498 |
|
double z; |
499 |
|
register int i; |
500 |
< |
|
500 |
> |
|
501 |
|
z = pixvalue(scanline[0], 0, y); |
502 |
|
if (zline) zline[0] = z; |
503 |
|
/* zig-zag start for quincunx pattern */ |
513 |
|
b = fillsample(scanline, zline, 0, y, i, 0, b/2); |
514 |
|
else |
515 |
|
b = fillsample(scanline+i-xstep, |
516 |
< |
zline ? zline+i-xstep : NULL, |
516 |
> |
zline ? zline+i-xstep : (float *)NULL, |
517 |
|
i-xstep, y, xstep, 0, b/2); |
518 |
|
if (sd) *sd++ = nc & 1 ? bl : b; |
519 |
|
bl = b; |
531 |
|
float zline[MAXDIV+1]; |
532 |
|
int b = ysize; |
533 |
|
register int i, j; |
534 |
< |
|
534 |
> |
|
535 |
|
for (i = 0; i < xres; i++) { |
519 |
– |
|
536 |
|
copycolor(vline[0], scanbar[0][i]); |
537 |
|
copycolor(vline[ysize], scanbar[ysize][i]); |
538 |
|
if (zbar[0]) { |
539 |
|
zline[0] = zbar[0][i]; |
540 |
|
zline[ysize] = zbar[ysize][i]; |
541 |
|
} |
542 |
< |
|
527 |
< |
b = fillsample(vline, zbar[0] ? zline : NULL, |
542 |
> |
b = fillsample(vline, zbar[0] ? zline : (float *)NULL, |
543 |
|
i, y, 0, ysize, b/2); |
544 |
< |
|
544 |
> |
|
545 |
|
for (j = 1; j < ysize; j++) |
546 |
|
copycolor(scanbar[j][i], vline[j]); |
547 |
|
if (zbar[0]) |
564 |
|
COLOR ctmp; |
565 |
|
int ncut; |
566 |
|
register int len; |
567 |
< |
|
567 |
> |
|
568 |
|
if (xlen > 0) /* x or y length is zero */ |
569 |
|
len = xlen; |
570 |
|
else |
571 |
|
len = ylen; |
572 |
< |
|
572 |
> |
|
573 |
|
if (len <= 1) /* limit recursion */ |
574 |
|
return(0); |
575 |
< |
|
576 |
< |
if (b > 0 |
577 |
< |
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
575 |
> |
|
576 |
> |
if (b > 0 || |
577 |
> |
(zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
578 |
|
|| bigdiff(colline[0], colline[len], maxdiff)) { |
579 |
< |
|
579 |
> |
|
580 |
|
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
581 |
|
if (zline) zline[len>>1] = z; |
582 |
|
ncut = 1; |
568 |
– |
|
583 |
|
} else { /* interpolate */ |
570 |
– |
|
584 |
|
copycolor(colline[len>>1], colline[len]); |
585 |
|
ratio = (double)(len>>1) / len; |
586 |
|
scalecolor(colline[len>>1], ratio); |
594 |
|
} |
595 |
|
/* recurse */ |
596 |
|
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
597 |
< |
|
598 |
< |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
597 |
> |
|
598 |
> |
ncut += fillsample(colline+(len>>1), |
599 |
> |
zline ? zline+(len>>1) : (float *)NULL, |
600 |
|
x+(xlen>>1), y+(ylen>>1), |
601 |
|
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
602 |
|
|
609 |
|
COLOR col; /* returned color */ |
610 |
|
int x, y; /* pixel position */ |
611 |
|
{ |
612 |
< |
static RAY thisray; |
613 |
< |
|
614 |
< |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
615 |
< |
(x+pixjitter())/hres, (y+pixjitter())/vres) < 0) { |
612 |
> |
RAY thisray; |
613 |
> |
FVECT lorg, ldir; |
614 |
> |
double hpos, vpos, lmax, d; |
615 |
> |
/* compute view ray */ |
616 |
> |
hpos = (x+pixjitter())/hres; |
617 |
> |
vpos = (y+pixjitter())/vres; |
618 |
> |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
619 |
> |
&ourview, hpos, vpos)) < -FTINY) { |
620 |
|
setcolor(col, 0.0, 0.0, 0.0); |
621 |
|
return(0.0); |
622 |
|
} |
623 |
|
|
606 |
– |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
607 |
– |
|
624 |
|
samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ |
625 |
|
|
626 |
+ |
/* optional motion blur */ |
627 |
+ |
if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, |
628 |
+ |
&lastview, hpos, vpos)) >= -FTINY) { |
629 |
+ |
register int i; |
630 |
+ |
register double d = mblur*(.5-urand(281+samplendx)); |
631 |
+ |
|
632 |
+ |
thisray.rmax = (1.-d)*thisray.rmax + d*lmax; |
633 |
+ |
for (i = 3; i--; ) { |
634 |
+ |
thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i]; |
635 |
+ |
thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i]; |
636 |
+ |
} |
637 |
+ |
if (normalize(thisray.rdir) == 0.0) |
638 |
+ |
return(0.0); |
639 |
+ |
} |
640 |
+ |
|
641 |
+ |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
642 |
+ |
|
643 |
|
rayvalue(&thisray); /* trace ray */ |
644 |
|
|
645 |
|
copycolor(col, thisray.rcol); /* return color */ |
646 |
< |
|
646 |
> |
|
647 |
|
return(thisray.rt); /* return distance */ |
648 |
|
} |
649 |
|
|
657 |
|
int x, y; |
658 |
|
|
659 |
|
if (oldfile == NULL) |
660 |
< |
return(0); |
661 |
< |
|
660 |
> |
goto gotzip; |
661 |
> |
|
662 |
|
if ((fp = fopen(oldfile, "r")) == NULL) { |
663 |
|
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); |
664 |
|
error(WARNING, errmsg); |
665 |
< |
return(0); |
665 |
> |
goto gotzip; |
666 |
|
} |
667 |
|
#ifdef MSDOS |
668 |
|
setmode(fileno(fp), O_BINARY); |
675 |
|
oldfile); |
676 |
|
error(WARNING, errmsg); |
677 |
|
fclose(fp); |
678 |
< |
return(0); |
678 |
> |
goto gotzip; |
679 |
|
} |
680 |
|
|
681 |
|
if (x != hres || y != vres) { |
699 |
|
fclose(fp); |
700 |
|
unlink(oldfile); |
701 |
|
return(y); |
702 |
+ |
gotzip: |
703 |
+ |
if (fflush(stdout) == EOF) |
704 |
+ |
error(SYSTEM, "error writing picture header"); |
705 |
+ |
return(0); |
706 |
|
writerr: |
707 |
|
sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); |
708 |
|
error(SYSTEM, errmsg); |