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" |
12 |
|
|
13 |
+ |
#include <sys/types.h> |
14 |
+ |
|
15 |
|
#ifndef NIX |
16 |
|
#ifdef BSD |
17 |
|
#include <sys/time.h> |
18 |
|
#include <sys/resource.h> |
19 |
|
#else |
20 |
– |
#include <sys/types.h> |
20 |
|
#include <sys/times.h> |
21 |
< |
#include <limits.h> |
23 |
< |
extern time_t time(); |
21 |
> |
#include <unistd.h> |
22 |
|
#endif |
25 |
– |
#else |
26 |
– |
extern unsigned long time(); |
23 |
|
#endif |
24 |
|
|
25 |
+ |
extern time_t time(); |
26 |
+ |
|
27 |
|
#include <signal.h> |
28 |
|
|
29 |
|
#include "view.h" |
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 */ |
94 |
|
|
95 |
|
double pctdone = 0.0; /* percentage done */ |
96 |
|
|
97 |
< |
unsigned long tlastrept = 0L; /* time at last report */ |
97 |
> |
time_t tlastrept = 0L; /* time at last report */ |
98 |
|
|
99 |
< |
extern unsigned long tstart; /* starting time */ |
99 |
> |
extern time_t tstart; /* starting time */ |
100 |
|
|
101 |
|
extern unsigned long nrays; /* number of rays traced */ |
102 |
|
|
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 |
< |
char hostname[128]; |
145 |
> |
extern char *myhostname(); |
146 |
|
double u, s; |
147 |
|
#ifdef BSD |
148 |
|
struct rusage rubuf; |
149 |
|
#else |
150 |
|
struct tms tbuf; |
151 |
+ |
double period; |
152 |
|
#endif |
153 |
|
|
154 |
< |
tlastrept = time((unsigned long *)NULL); |
154 |
> |
tlastrept = time((time_t *)NULL); |
155 |
|
#ifdef BSD |
156 |
|
getrusage(RUSAGE_SELF, &rubuf); |
157 |
|
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; |
162 |
|
#else |
163 |
|
times(&tbuf); |
164 |
< |
u = ( tbuf.tms_utime + tbuf.tms_cutime ) / CLK_TCK; |
165 |
< |
s = ( tbuf.tms_stime + tbuf.tms_cstime ) / CLK_TCK; |
164 |
> |
#ifdef _SC_CLK_TCK |
165 |
> |
period = 1.0 / sysconf(_SC_CLK_TCK); |
166 |
> |
#else |
167 |
> |
period = 1.0 / 60.0; |
168 |
|
#endif |
169 |
< |
gethostname(hostname, sizeof(hostname)); |
169 |
> |
u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; |
170 |
> |
s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; |
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); |
180 |
+ |
#endif |
181 |
|
} |
182 |
|
#else |
183 |
|
report() /* report progress */ |
184 |
|
{ |
185 |
< |
tlastrept = time((unsigned long *)NULL); |
185 |
> |
tlastrept = time((time_t *)NULL); |
186 |
|
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
187 |
|
nrays, pctdone, (tlastrept-tstart)/3600.0); |
188 |
|
eputs(errmsg); |
171 |
– |
signal(SIGCONT, report); |
189 |
|
} |
190 |
|
#endif |
191 |
|
|
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); |
398 |
|
fprtresolu(hres, vres, stdout); |
399 |
|
/* recover file and compute first */ |
400 |
|
i = salvage(oldfile); |
401 |
+ |
if (i >= vres) |
402 |
+ |
goto alldone; |
403 |
|
if (zfd != -1 && i > 0 && |
404 |
|
lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) |
405 |
|
error(SYSTEM, "z-file seek error in render"); |
410 |
|
else |
411 |
|
#endif |
412 |
|
signal(SIGCONT, report); |
413 |
< |
ypos = vres-1 - i; |
413 |
> |
ypos = vres-1 - i; /* initialize sampling */ |
414 |
> |
if (directvis) |
415 |
> |
init_drawsources(psample); |
416 |
|
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
417 |
|
/* compute scanlines */ |
418 |
|
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
432 |
|
hres, ypos, hstep); |
433 |
|
/* fill bar */ |
434 |
|
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
435 |
+ |
if (directvis) /* add bitty sources */ |
436 |
+ |
drawsources(scanbar, zbar, 0, hres, ypos, ystep); |
437 |
|
/* write it out */ |
438 |
|
#ifndef BSD |
439 |
|
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
450 |
|
goto writerr; |
451 |
|
/* record progress */ |
452 |
|
pctdone = 100.0*(vres-1-ypos)/vres; |
453 |
< |
if (ralrm > 0 && time((unsigned long *)NULL) >= tlastrept+ralrm) |
453 |
> |
if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) |
454 |
|
report(); |
455 |
|
#ifndef BSD |
456 |
|
else |
459 |
|
} |
460 |
|
/* clean up */ |
461 |
|
signal(SIGCONT, SIG_IGN); |
462 |
< |
if (zfd != -1) { |
437 |
< |
if (write(zfd, (char *)zbar[0], hres*sizeof(float)) |
462 |
> |
if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float)) |
463 |
|
< hres*sizeof(float)) |
464 |
< |
goto writerr; |
464 |
> |
goto writerr; |
465 |
> |
fwritescan(scanbar[0], hres, stdout); |
466 |
> |
if (fflush(stdout) == EOF) |
467 |
> |
goto writerr; |
468 |
> |
alldone: |
469 |
> |
if (zfd != -1) { |
470 |
|
if (close(zfd) == -1) |
471 |
|
goto writerr; |
472 |
|
for (i = 0; i <= psample; i++) |
473 |
|
free((char *)zbar[i]); |
474 |
|
} |
445 |
– |
fwritescan(scanbar[0], hres, stdout); |
446 |
– |
if (fflush(stdout) == EOF) |
447 |
– |
goto writerr; |
475 |
|
for (i = 0; i <= psample; i++) |
476 |
|
free((char *)scanbar[i]); |
477 |
|
if (sampdens != NULL) |
498 |
|
int bl = xstep, b = xstep; |
499 |
|
double z; |
500 |
|
register int i; |
501 |
< |
|
501 |
> |
|
502 |
|
z = pixvalue(scanline[0], 0, y); |
503 |
|
if (zline) zline[0] = z; |
504 |
|
/* zig-zag start for quincunx pattern */ |
514 |
|
b = fillsample(scanline, zline, 0, y, i, 0, b/2); |
515 |
|
else |
516 |
|
b = fillsample(scanline+i-xstep, |
517 |
< |
zline ? zline+i-xstep : NULL, |
517 |
> |
zline ? zline+i-xstep : (float *)NULL, |
518 |
|
i-xstep, y, xstep, 0, b/2); |
519 |
|
if (sd) *sd++ = nc & 1 ? bl : b; |
520 |
|
bl = b; |
532 |
|
float zline[MAXDIV+1]; |
533 |
|
int b = ysize; |
534 |
|
register int i, j; |
535 |
< |
|
535 |
> |
|
536 |
|
for (i = 0; i < xres; i++) { |
510 |
– |
|
537 |
|
copycolor(vline[0], scanbar[0][i]); |
538 |
|
copycolor(vline[ysize], scanbar[ysize][i]); |
539 |
|
if (zbar[0]) { |
540 |
|
zline[0] = zbar[0][i]; |
541 |
|
zline[ysize] = zbar[ysize][i]; |
542 |
|
} |
543 |
< |
|
518 |
< |
b = fillsample(vline, zbar[0] ? zline : NULL, |
543 |
> |
b = fillsample(vline, zbar[0] ? zline : (float *)NULL, |
544 |
|
i, y, 0, ysize, b/2); |
545 |
< |
|
545 |
> |
|
546 |
|
for (j = 1; j < ysize; j++) |
547 |
|
copycolor(scanbar[j][i], vline[j]); |
548 |
|
if (zbar[0]) |
565 |
|
COLOR ctmp; |
566 |
|
int ncut; |
567 |
|
register int len; |
568 |
< |
|
568 |
> |
|
569 |
|
if (xlen > 0) /* x or y length is zero */ |
570 |
|
len = xlen; |
571 |
|
else |
572 |
|
len = ylen; |
573 |
< |
|
573 |
> |
|
574 |
|
if (len <= 1) /* limit recursion */ |
575 |
|
return(0); |
576 |
< |
|
577 |
< |
if (b > 0 |
578 |
< |
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
576 |
> |
|
577 |
> |
if (b > 0 || |
578 |
> |
(zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
579 |
|
|| bigdiff(colline[0], colline[len], maxdiff)) { |
580 |
< |
|
580 |
> |
|
581 |
|
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
582 |
|
if (zline) zline[len>>1] = z; |
583 |
|
ncut = 1; |
559 |
– |
|
584 |
|
} else { /* interpolate */ |
561 |
– |
|
585 |
|
copycolor(colline[len>>1], colline[len]); |
586 |
|
ratio = (double)(len>>1) / len; |
587 |
|
scalecolor(colline[len>>1], ratio); |
595 |
|
} |
596 |
|
/* recurse */ |
597 |
|
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
598 |
< |
|
599 |
< |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
598 |
> |
|
599 |
> |
ncut += fillsample(colline+(len>>1), |
600 |
> |
zline ? zline+(len>>1) : (float *)NULL, |
601 |
|
x+(xlen>>1), y+(ylen>>1), |
602 |
|
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
603 |
|
|
610 |
|
COLOR col; /* returned color */ |
611 |
|
int x, y; /* pixel position */ |
612 |
|
{ |
613 |
< |
static RAY thisray; |
614 |
< |
|
615 |
< |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
616 |
< |
(x+pixjitter())/hres, (y+pixjitter())/vres) < 0) { |
613 |
> |
RAY thisray; |
614 |
> |
FVECT lorg, ldir; |
615 |
> |
double hpos, vpos, lmax, d; |
616 |
> |
/* compute view ray */ |
617 |
> |
hpos = (x+pixjitter())/hres; |
618 |
> |
vpos = (y+pixjitter())/vres; |
619 |
> |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
620 |
> |
&ourview, hpos, vpos)) < -FTINY) { |
621 |
|
setcolor(col, 0.0, 0.0, 0.0); |
622 |
|
return(0.0); |
623 |
|
} |
624 |
|
|
597 |
– |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
598 |
– |
|
625 |
|
samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ |
626 |
|
|
627 |
+ |
/* optional motion blur */ |
628 |
+ |
if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, |
629 |
+ |
&lastview, hpos, vpos)) >= -FTINY) { |
630 |
+ |
register int i; |
631 |
+ |
register double d = mblur*(.5-urand(281+samplendx)); |
632 |
+ |
|
633 |
+ |
thisray.rmax = (1.-d)*thisray.rmax + d*lmax; |
634 |
+ |
for (i = 3; i--; ) { |
635 |
+ |
thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i]; |
636 |
+ |
thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i]; |
637 |
+ |
} |
638 |
+ |
if (normalize(thisray.rdir) == 0.0) |
639 |
+ |
return(0.0); |
640 |
+ |
} |
641 |
+ |
|
642 |
+ |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
643 |
+ |
|
644 |
|
rayvalue(&thisray); /* trace ray */ |
645 |
|
|
646 |
|
copycolor(col, thisray.rcol); /* return color */ |
647 |
< |
|
647 |
> |
|
648 |
|
return(thisray.rt); /* return distance */ |
649 |
|
} |
650 |
|
|
658 |
|
int x, y; |
659 |
|
|
660 |
|
if (oldfile == NULL) |
661 |
< |
return(0); |
662 |
< |
|
661 |
> |
goto gotzip; |
662 |
> |
|
663 |
|
if ((fp = fopen(oldfile, "r")) == NULL) { |
664 |
|
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); |
665 |
|
error(WARNING, errmsg); |
666 |
< |
return(0); |
666 |
> |
goto gotzip; |
667 |
|
} |
668 |
|
#ifdef MSDOS |
669 |
|
setmode(fileno(fp), O_BINARY); |
676 |
|
oldfile); |
677 |
|
error(WARNING, errmsg); |
678 |
|
fclose(fp); |
679 |
< |
return(0); |
679 |
> |
goto gotzip; |
680 |
|
} |
681 |
|
|
682 |
|
if (x != hres || y != vres) { |
700 |
|
fclose(fp); |
701 |
|
unlink(oldfile); |
702 |
|
return(y); |
703 |
+ |
gotzip: |
704 |
+ |
if (fflush(stdout) == EOF) |
705 |
+ |
error(SYSTEM, "error writing picture header"); |
706 |
+ |
return(0); |
707 |
|
writerr: |
708 |
|
sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); |
709 |
|
error(SYSTEM, errmsg); |