1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
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" |
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 |
+ |
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 |
< |
long tlastrept = 0L; /* time at last report */ |
97 |
> |
time_t tlastrept = 0L; /* time at last report */ |
98 |
|
|
99 |
< |
extern long time(); |
83 |
< |
extern long tstart; /* starting time */ |
99 |
> |
extern time_t tstart; /* starting time */ |
100 |
|
|
101 |
|
extern unsigned long nrays; /* number of rays traced */ |
102 |
|
|
110 |
|
|
111 |
|
double pixvalue(); |
112 |
|
|
113 |
+ |
#ifdef NIX |
114 |
+ |
#define file_exists(f) (access(f,F_OK)==0) |
115 |
+ |
#else |
116 |
+ |
#include <sys/types.h> |
117 |
+ |
#include <sys/stat.h> |
118 |
+ |
int |
119 |
+ |
file_exists(fname) /* ordinary file exists? */ |
120 |
+ |
char *fname; |
121 |
+ |
{ |
122 |
+ |
struct stat sbuf; |
123 |
+ |
if (stat(fname, &sbuf) < 0) return(0); |
124 |
+ |
return((sbuf.st_mode & S_IFREG) != 0); |
125 |
+ |
} |
126 |
+ |
#endif |
127 |
|
|
128 |
+ |
|
129 |
|
quit(code) /* quit program */ |
130 |
|
int code; |
131 |
|
{ |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
< |
#ifdef BSD |
140 |
> |
#ifndef NIX |
141 |
|
report() /* report progress */ |
142 |
|
{ |
143 |
+ |
extern char *myhostname(); |
144 |
+ |
double u, s; |
145 |
+ |
#ifdef BSD |
146 |
|
struct rusage rubuf; |
147 |
< |
double t; |
147 |
> |
#else |
148 |
> |
struct tms tbuf; |
149 |
> |
double period; |
150 |
> |
#endif |
151 |
|
|
152 |
+ |
tlastrept = time((time_t *)NULL); |
153 |
+ |
#ifdef BSD |
154 |
|
getrusage(RUSAGE_SELF, &rubuf); |
155 |
< |
t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
156 |
< |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
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 |
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
158 |
< |
t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
159 |
< |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
158 |
> |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
159 |
> |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
160 |
> |
#else |
161 |
> |
times(&tbuf); |
162 |
> |
#ifdef _SC_CLK_TCK |
163 |
> |
period = 1.0 / sysconf(_SC_CLK_TCK); |
164 |
> |
#else |
165 |
> |
period = 1.0 / 60.0; |
166 |
> |
#endif |
167 |
> |
u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; |
168 |
> |
s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; |
169 |
> |
#endif |
170 |
|
|
171 |
< |
sprintf(errmsg, "%lu rays, %4.2f%% done after %5.4f CPU hours\n", |
172 |
< |
nrays, pctdone, t/3600.0); |
171 |
> |
sprintf(errmsg, |
172 |
> |
"%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", |
173 |
> |
nrays, pctdone, u/3600., s/3600., |
174 |
> |
(tlastrept-tstart)/3600., myhostname()); |
175 |
|
eputs(errmsg); |
176 |
< |
tlastrept = time((long *)0); |
176 |
> |
#ifndef BSD |
177 |
> |
signal(SIGCONT, report); |
178 |
> |
#endif |
179 |
|
} |
180 |
|
#else |
181 |
|
report() /* report progress */ |
182 |
|
{ |
183 |
< |
tlastrept = time((long *)0); |
184 |
< |
sprintf(errmsg, "%lu rays, %4.2f%% done after %5.4f hours\n", |
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); |
134 |
– |
signal(SIGCONT, report); |
187 |
|
} |
188 |
|
#endif |
189 |
|
|
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; |
242 |
|
cp--; |
243 |
|
strcpy(cp, RFTEMPLATE); |
244 |
|
prvr = mktemp(fbuf2); |
245 |
< |
if (rename(fbuf, prvr) < 0 && errno != ENOENT) { |
246 |
< |
sprintf(errmsg, |
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 |
< |
} |
256 |
> |
error(SYSTEM, errmsg); |
257 |
> |
} |
258 |
|
} |
259 |
|
} |
260 |
< |
/* render sequence */ |
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); |
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 |
|
|
370 |
|
sampdens[i] = hstep; |
371 |
|
} else |
372 |
|
sampdens = NULL; |
373 |
< |
/* open z file */ |
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); |
376 |
> |
sprintf(errmsg, "cannot open z-file \"%s\"", zfile); |
377 |
|
error(SYSTEM, errmsg); |
378 |
|
} |
379 |
|
#ifdef MSDOS |
393 |
|
fprtresolu(hres, vres, stdout); |
394 |
|
/* recover file and compute first */ |
395 |
|
i = salvage(oldfile); |
396 |
+ |
if (i >= vres) |
397 |
+ |
goto alldone; |
398 |
|
if (zfd != -1 && i > 0 && |
399 |
|
lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) |
400 |
< |
error(SYSTEM, "z file seek error in render"); |
400 |
> |
error(SYSTEM, "z-file seek error in render"); |
401 |
|
pctdone = 100.0*i/vres; |
402 |
|
if (ralrm > 0) /* report init stats */ |
403 |
|
report(); |
441 |
|
goto writerr; |
442 |
|
/* record progress */ |
443 |
|
pctdone = 100.0*(vres-1-ypos)/vres; |
444 |
< |
if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) |
444 |
> |
if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) |
445 |
|
report(); |
446 |
|
#ifndef BSD |
447 |
|
else |
450 |
|
} |
451 |
|
/* clean up */ |
452 |
|
signal(SIGCONT, SIG_IGN); |
453 |
< |
if (zfd != -1) { |
378 |
< |
if (write(zfd, (char *)zbar[0], hres*sizeof(float)) |
453 |
> |
if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float)) |
454 |
|
< hres*sizeof(float)) |
455 |
< |
goto writerr; |
455 |
> |
goto writerr; |
456 |
> |
fwritescan(scanbar[0], hres, stdout); |
457 |
> |
if (fflush(stdout) == EOF) |
458 |
> |
goto writerr; |
459 |
> |
alldone: |
460 |
> |
if (zfd != -1) { |
461 |
|
if (close(zfd) == -1) |
462 |
|
goto writerr; |
463 |
|
for (i = 0; i <= psample; i++) |
464 |
|
free((char *)zbar[i]); |
465 |
|
} |
386 |
– |
fwritescan(scanbar[0], hres, stdout); |
387 |
– |
if (fflush(stdout) == EOF) |
388 |
– |
goto writerr; |
466 |
|
for (i = 0; i <= psample; i++) |
467 |
|
free((char *)scanbar[i]); |
468 |
|
if (sampdens != NULL) |
505 |
|
b = fillsample(scanline, zline, 0, y, i, 0, b/2); |
506 |
|
else |
507 |
|
b = fillsample(scanline+i-xstep, |
508 |
< |
zline ? zline+i-xstep : NULL, |
508 |
> |
zline ? zline+i-xstep : (float *)NULL, |
509 |
|
i-xstep, y, xstep, 0, b/2); |
510 |
|
if (sd) *sd++ = nc & 1 ? bl : b; |
511 |
|
bl = b; |
533 |
|
zline[ysize] = zbar[ysize][i]; |
534 |
|
} |
535 |
|
|
536 |
< |
b = fillsample(vline, zbar[0] ? zline : NULL, |
536 |
> |
b = fillsample(vline, zbar[0] ? zline : (float *)NULL, |
537 |
|
i, y, 0, ysize, b/2); |
538 |
|
|
539 |
|
for (j = 1; j < ysize; j++) |
591 |
|
/* recurse */ |
592 |
|
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
593 |
|
|
594 |
< |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
594 |
> |
ncut += fillsample(colline+(len>>1), |
595 |
> |
zline ? zline+(len>>1) : (float *)NULL, |
596 |
|
x+(xlen>>1), y+(ylen>>1), |
597 |
|
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
598 |
|
|
607 |
|
{ |
608 |
|
static RAY thisray; |
609 |
|
|
610 |
< |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
611 |
< |
(x+pixjitter())/hres, (y+pixjitter())/vres) < 0) { |
610 |
> |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview, |
611 |
> |
(x+pixjitter())/hres, (y+pixjitter())/vres)) < -FTINY) { |
612 |
|
setcolor(col, 0.0, 0.0, 0.0); |
613 |
|
return(0.0); |
614 |
|
} |
634 |
|
int x, y; |
635 |
|
|
636 |
|
if (oldfile == NULL) |
637 |
< |
return(0); |
638 |
< |
|
637 |
> |
goto gotzip; |
638 |
> |
|
639 |
|
if ((fp = fopen(oldfile, "r")) == NULL) { |
640 |
|
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); |
641 |
|
error(WARNING, errmsg); |
642 |
< |
return(0); |
642 |
> |
goto gotzip; |
643 |
|
} |
644 |
|
#ifdef MSDOS |
645 |
|
setmode(fileno(fp), O_BINARY); |
648 |
|
getheader(fp, NULL, NULL); |
649 |
|
/* get picture size */ |
650 |
|
if (!fscnresolu(&x, &y, fp)) { |
651 |
< |
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
651 |
> |
sprintf(errmsg, "bad recover file \"%s\" - not removed", |
652 |
> |
oldfile); |
653 |
|
error(WARNING, errmsg); |
654 |
|
fclose(fp); |
655 |
< |
return(0); |
655 |
> |
goto gotzip; |
656 |
|
} |
657 |
|
|
658 |
|
if (x != hres || y != vres) { |
676 |
|
fclose(fp); |
677 |
|
unlink(oldfile); |
678 |
|
return(y); |
679 |
+ |
gotzip: |
680 |
+ |
if (fflush(stdout) == EOF) |
681 |
+ |
error(SYSTEM, "error writing picture header"); |
682 |
+ |
return(0); |
683 |
|
writerr: |
684 |
|
sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); |
685 |
|
error(SYSTEM, errmsg); |