12 |
|
|
13 |
|
#include "ray.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> |
21 |
+ |
#include <sys/times.h> |
22 |
+ |
#include <limits.h> |
23 |
|
#endif |
24 |
+ |
#endif |
25 |
|
|
26 |
|
#include <signal.h> |
21 |
– |
#include <fcntl.h> |
27 |
|
|
28 |
|
#include "view.h" |
29 |
|
|
33 |
|
|
34 |
|
#include "paths.h" |
35 |
|
|
36 |
+ |
#define RFTEMPLATE "rfXXXXXX" |
37 |
+ |
|
38 |
+ |
#ifndef SIGCONT |
39 |
+ |
#define SIGCONT SIGIO |
40 |
+ |
#endif |
41 |
+ |
|
42 |
|
int dimlist[MAXDIM]; /* sampling dimensions */ |
43 |
|
int ndims = 0; /* number of sampling dimensions */ |
44 |
|
int samplendx; /* sample index number */ |
46 |
|
VIEW ourview = STDVIEW; /* view parameters */ |
47 |
|
int hresolu = 512; /* horizontal resolution */ |
48 |
|
int vresolu = 512; /* vertical resolution */ |
49 |
< |
double pixaspect = 1.0; /* pixel aspect ratio */ |
49 |
> |
double pixaspect = 1.0; /* pixel aspect ratio */ |
50 |
|
|
51 |
|
int psample = 4; /* pixel sample size */ |
52 |
< |
double maxdiff = .05; /* max. difference for interpolation */ |
53 |
< |
double dstrpix = 0.67; /* square pixel distribution */ |
52 |
> |
double maxdiff = .05; /* max. difference for interpolation */ |
53 |
> |
double dstrpix = 0.67; /* square pixel distribution */ |
54 |
|
|
55 |
< |
double dstrsrc = 0.0; /* square source distribution */ |
56 |
< |
double shadthresh = .05; /* shadow threshold */ |
57 |
< |
double shadcert = .5; /* shadow certainty */ |
55 |
> |
double dstrsrc = 0.0; /* square source distribution */ |
56 |
> |
double shadthresh = .05; /* shadow threshold */ |
57 |
> |
double shadcert = .5; /* shadow certainty */ |
58 |
|
int directrelay = 1; /* number of source relays */ |
59 |
|
int vspretest = 512; /* virtual source pretest density */ |
60 |
< |
int directinvis = 0; /* sources invisible? */ |
61 |
< |
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
60 |
> |
int directvis = 1; /* sources visible? */ |
61 |
> |
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
62 |
|
|
63 |
< |
double specthresh = .15; /* specular sampling threshold */ |
64 |
< |
double specjitter = 1.; /* specular sampling jitter */ |
63 |
> |
double specthresh = .15; /* specular sampling threshold */ |
64 |
> |
double specjitter = 1.; /* specular sampling jitter */ |
65 |
|
|
66 |
|
int maxdepth = 6; /* maximum recursion depth */ |
67 |
< |
double minweight = 5e-3; /* minimum ray weight */ |
67 |
> |
double minweight = 5e-3; /* minimum ray weight */ |
68 |
|
|
69 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
70 |
< |
double ambacc = 0.2; /* ambient accuracy */ |
70 |
> |
double ambacc = 0.2; /* ambient accuracy */ |
71 |
|
int ambres = 32; /* ambient resolution */ |
72 |
|
int ambdiv = 128; /* ambient divisions */ |
73 |
|
int ambssamp = 0; /* ambient super-samples */ |
75 |
|
char *amblist[128]; /* ambient include/exclude list */ |
76 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
77 |
|
|
78 |
+ |
#ifdef MSDOS |
79 |
+ |
int ralrm = 60; /* seconds between reports */ |
80 |
+ |
#else |
81 |
|
int ralrm = 0; /* seconds between reports */ |
82 |
+ |
#endif |
83 |
|
|
84 |
< |
double pctdone = 0.0; /* percentage done */ |
84 |
> |
double pctdone = 0.0; /* percentage done */ |
85 |
|
|
86 |
|
long tlastrept = 0L; /* time at last report */ |
87 |
|
|
88 |
|
extern long time(); |
89 |
|
extern long tstart; /* starting time */ |
90 |
|
|
91 |
< |
extern long nrays; /* number of rays traced */ |
91 |
> |
extern unsigned long nrays; /* number of rays traced */ |
92 |
|
|
93 |
< |
#define MAXDIV 16 /* maximum sample size */ |
93 |
> |
#define MAXDIV 16 /* maximum sample size */ |
94 |
|
|
95 |
< |
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
95 |
> |
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
96 |
|
|
82 |
– |
#define RFTEMPLATE "rfXXXXXX" |
83 |
– |
#define HFTEMPLATE TEMPLATE |
84 |
– |
|
85 |
– |
static char *hfname = NULL; /* header file name */ |
86 |
– |
static FILE *hfp = NULL; /* header file pointer */ |
87 |
– |
|
97 |
|
static int hres, vres; /* resolution for this frame */ |
98 |
|
|
99 |
|
extern char *mktemp(); |
100 |
|
|
101 |
< |
double pixvalue(); |
101 |
> |
double pixvalue(); |
102 |
|
|
103 |
+ |
#ifdef NIX |
104 |
+ |
#define file_exists(f) (access(f,F_OK)==0) |
105 |
+ |
#else |
106 |
+ |
#include <sys/types.h> |
107 |
+ |
#include <sys/stat.h> |
108 |
+ |
int |
109 |
+ |
file_exists(fname) /* ordinary file exists? */ |
110 |
+ |
char *fname; |
111 |
+ |
{ |
112 |
+ |
struct stat sbuf; |
113 |
+ |
if (stat(fname, &sbuf) < 0) return(0); |
114 |
+ |
return((sbuf.st_mode & S_IFREG) != 0); |
115 |
+ |
} |
116 |
+ |
#endif |
117 |
|
|
118 |
+ |
|
119 |
|
quit(code) /* quit program */ |
120 |
|
int code; |
121 |
|
{ |
122 |
|
if (code) /* report status */ |
123 |
|
report(); |
124 |
< |
if (hfname != NULL) { /* delete header file */ |
125 |
< |
if (hfp != NULL) |
102 |
< |
fclose(hfp); |
103 |
< |
unlink(hfname); |
104 |
< |
} |
124 |
> |
headclean(); /* delete header file */ |
125 |
> |
pfclean(); /* clean up persist files */ |
126 |
|
exit(code); |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
< |
#ifdef BSD |
130 |
> |
#ifndef NIX |
131 |
|
report() /* report progress */ |
132 |
|
{ |
133 |
+ |
char hostname[128]; |
134 |
+ |
double u, s; |
135 |
+ |
#ifdef BSD |
136 |
|
struct rusage rubuf; |
137 |
< |
double t; |
137 |
> |
#else |
138 |
> |
struct tms tbuf; |
139 |
> |
#endif |
140 |
|
|
141 |
+ |
tlastrept = time((long *)0); |
142 |
+ |
#ifdef BSD |
143 |
|
getrusage(RUSAGE_SELF, &rubuf); |
144 |
< |
t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
145 |
< |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
144 |
> |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
145 |
> |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
146 |
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
147 |
< |
t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
148 |
< |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
147 |
> |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
148 |
> |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
149 |
> |
#else |
150 |
> |
times(&tbuf); |
151 |
> |
u = ( tbuf.tms_utime + tbuf.tms_cutime ) / CLK_TCK; |
152 |
> |
s = ( tbuf.tms_stime + tbuf.tms_cstime ) / CLK_TCK; |
153 |
> |
#endif |
154 |
> |
gethostname(hostname, sizeof(hostname)); |
155 |
|
|
156 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
157 |
< |
nrays, pctdone, t/3600.0); |
156 |
> |
sprintf(errmsg, |
157 |
> |
"%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", |
158 |
> |
nrays, pctdone, u/3600., s/3600., |
159 |
> |
(tlastrept-tstart)/3600., hostname); |
160 |
|
eputs(errmsg); |
125 |
– |
tlastrept = time((long *)0); |
161 |
|
} |
162 |
|
#else |
163 |
|
report() /* report progress */ |
164 |
|
{ |
165 |
|
tlastrept = time((long *)0); |
166 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", |
166 |
> |
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
167 |
|
nrays, pctdone, (tlastrept-tstart)/3600.0); |
168 |
|
eputs(errmsg); |
169 |
< |
signal(SIGALRM, report); |
169 |
> |
signal(SIGCONT, report); |
170 |
|
} |
171 |
|
#endif |
172 |
|
|
173 |
|
|
139 |
– |
openheader() /* save standard output to header file */ |
140 |
– |
{ |
141 |
– |
hfname = mktemp(HFTEMPLATE); |
142 |
– |
if (freopen(hfname, "w", stdout) == NULL) { |
143 |
– |
sprintf(errmsg, "cannot open header file \"%s\"", hfname); |
144 |
– |
error(SYSTEM, errmsg); |
145 |
– |
} |
146 |
– |
} |
147 |
– |
|
148 |
– |
|
149 |
– |
closeheader() /* done with header output */ |
150 |
– |
{ |
151 |
– |
if (hfname == NULL) |
152 |
– |
return; |
153 |
– |
if (fflush(stdout) == EOF || (hfp = fopen(hfname, "r")) == NULL) |
154 |
– |
error(SYSTEM, "error reopening header file"); |
155 |
– |
} |
156 |
– |
|
157 |
– |
|
158 |
– |
dupheader() /* repeat header on standard output */ |
159 |
– |
{ |
160 |
– |
register int c; |
161 |
– |
|
162 |
– |
if (fseek(hfp, 0L, 0) < 0) |
163 |
– |
error(SYSTEM, "seek error on header file"); |
164 |
– |
while ((c = getc(hfp)) != EOF) |
165 |
– |
putchar(c); |
166 |
– |
} |
167 |
– |
|
168 |
– |
|
174 |
|
rpict(seq, pout, zout, prvr) /* generate image(s) */ |
175 |
|
int seq; |
176 |
|
char *pout, *zout, *prvr; |
186 |
|
* sequenced file naming. |
187 |
|
*/ |
188 |
|
{ |
189 |
< |
extern char *rindex(), *strncpy(), *strcat(); |
189 |
> |
extern char *rindex(), *strncpy(), *strcat(), *strcpy(); |
190 |
|
char fbuf[128], fbuf2[128]; |
191 |
+ |
int npicts; |
192 |
|
register char *cp; |
193 |
< |
RESOLU rs; |
194 |
< |
double pa; |
189 |
< |
/* finished writing header */ |
190 |
< |
closeheader(); |
193 |
> |
RESOLU rs; |
194 |
> |
double pa; |
195 |
|
/* check sampling */ |
196 |
|
if (psample < 1) |
197 |
|
psample = 1; |
215 |
|
error(USER, "unexpected EOF on view input"); |
216 |
|
prvr = fbuf; /* mark for renaming */ |
217 |
|
} |
218 |
< |
if (pout != NULL) { |
218 |
> |
if (pout != NULL & prvr != NULL) { |
219 |
|
sprintf(fbuf, pout, seq); |
220 |
< |
if (prvr != NULL && !strcmp(prvr, fbuf)) { /* rename */ |
221 |
< |
fbuf2[0] = '\0'; |
222 |
< |
if ((cp = rindex(fbuf, '/')) != NULL) |
223 |
< |
strncpy(fbuf2, fbuf, cp-fbuf+1); |
224 |
< |
strcat(fbuf2, RFTEMPLATE); |
220 |
> |
if (!strcmp(prvr, fbuf)) { /* rename */ |
221 |
> |
strcpy(fbuf2, fbuf); |
222 |
> |
for (cp = fbuf2; *cp; cp++) |
223 |
> |
; |
224 |
> |
while (cp > fbuf2 && !ISDIRSEP(cp[-1])) |
225 |
> |
cp--; |
226 |
> |
strcpy(cp, RFTEMPLATE); |
227 |
|
prvr = mktemp(fbuf2); |
228 |
< |
if (rename(fbuf, prvr) < 0 && errno != ENOENT) { |
229 |
< |
sprintf(errmsg, |
228 |
> |
if (rename(fbuf, prvr) < 0) |
229 |
> |
if (errno == ENOENT) { /* ghost file */ |
230 |
> |
sprintf(errmsg, |
231 |
> |
"new output file \"%s\"", |
232 |
> |
fbuf); |
233 |
> |
error(WARNING, errmsg); |
234 |
> |
prvr = NULL; |
235 |
> |
} else { /* serious error */ |
236 |
> |
sprintf(errmsg, |
237 |
|
"cannot rename \"%s\" to \"%s\"", |
238 |
|
fbuf, prvr); |
239 |
< |
error(SYSTEM, errmsg); |
240 |
< |
} |
239 |
> |
error(SYSTEM, errmsg); |
240 |
> |
} |
241 |
|
} |
242 |
|
} |
243 |
< |
/* render sequence */ |
243 |
> |
npicts = 0; /* render sequence */ |
244 |
|
do { |
245 |
|
if (seq && nextview(stdin) == EOF) |
246 |
|
break; |
247 |
+ |
pctdone = 0.0; |
248 |
|
if (pout != NULL) { |
249 |
|
sprintf(fbuf, pout, seq); |
250 |
+ |
if (file_exists(fbuf)) { |
251 |
+ |
if (prvr != NULL || !strcmp(fbuf, pout)) { |
252 |
+ |
sprintf(errmsg, |
253 |
+ |
"output file \"%s\" exists", |
254 |
+ |
fbuf); |
255 |
+ |
error(USER, errmsg); |
256 |
+ |
} |
257 |
+ |
continue; /* don't clobber */ |
258 |
+ |
} |
259 |
|
if (freopen(fbuf, "w", stdout) == NULL) { |
260 |
|
sprintf(errmsg, |
261 |
|
"cannot open output file \"%s\"", fbuf); |
262 |
|
error(SYSTEM, errmsg); |
263 |
|
} |
264 |
+ |
#ifdef MSDOS |
265 |
+ |
setmode(fileno(stdout), O_BINARY); |
266 |
+ |
#endif |
267 |
|
dupheader(); |
268 |
|
} |
269 |
|
hres = hresolu; vres = vresolu; pa = pixaspect; |
303 |
|
cp = NULL; |
304 |
|
render(cp, prvr); |
305 |
|
prvr = NULL; |
306 |
+ |
npicts++; |
307 |
|
} while (seq++); |
308 |
+ |
/* check that we did something */ |
309 |
+ |
if (npicts == 0) |
310 |
+ |
error(WARNING, "no output produced"); |
311 |
|
} |
312 |
|
|
313 |
|
|
353 |
|
sampdens[i] = hstep; |
354 |
|
} else |
355 |
|
sampdens = NULL; |
356 |
< |
/* open z file */ |
356 |
> |
/* open z-file */ |
357 |
|
if (zfile != NULL) { |
358 |
|
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
359 |
< |
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
359 |
> |
sprintf(errmsg, "cannot open z-file \"%s\"", zfile); |
360 |
|
error(SYSTEM, errmsg); |
361 |
|
} |
362 |
+ |
#ifdef MSDOS |
363 |
+ |
setmode(zfd, O_BINARY); |
364 |
+ |
#endif |
365 |
|
for (i = 0; i <= psample; i++) { |
366 |
|
zbar[i] = (float *)malloc(hres*sizeof(float)); |
367 |
|
if (zbar[i] == NULL) |
378 |
|
i = salvage(oldfile); |
379 |
|
if (zfd != -1 && i > 0 && |
380 |
|
lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) |
381 |
< |
error(SYSTEM, "z file seek error in render"); |
381 |
> |
error(SYSTEM, "z-file seek error in render"); |
382 |
|
pctdone = 100.0*i/vres; |
383 |
|
if (ralrm > 0) /* report init stats */ |
384 |
|
report(); |
385 |
< |
#ifndef BSD |
385 |
> |
#ifndef BSD |
386 |
|
else |
387 |
|
#endif |
388 |
< |
signal(SIGALRM, report); |
388 |
> |
signal(SIGCONT, report); |
389 |
|
ypos = vres-1 - i; |
390 |
|
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
391 |
|
/* compute scanlines */ |
407 |
|
/* fill bar */ |
408 |
|
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
409 |
|
/* write it out */ |
410 |
< |
#ifndef BSD |
411 |
< |
signal(SIGALRM, SIG_IGN); /* don't interrupt writes */ |
410 |
> |
#ifndef BSD |
411 |
> |
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
412 |
|
#endif |
413 |
|
for (i = ystep; i > 0; i--) { |
414 |
|
if (zfd != -1 && write(zfd, (char *)zbar[i], |
424 |
|
pctdone = 100.0*(vres-1-ypos)/vres; |
425 |
|
if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) |
426 |
|
report(); |
427 |
< |
#ifndef BSD |
427 |
> |
#ifndef BSD |
428 |
|
else |
429 |
< |
signal(SIGALRM, report); |
429 |
> |
signal(SIGCONT, report); |
430 |
|
#endif |
431 |
|
} |
432 |
|
/* clean up */ |
433 |
< |
signal(SIGALRM, SIG_IGN); |
433 |
> |
signal(SIGCONT, SIG_IGN); |
434 |
|
if (zfd != -1) { |
435 |
|
if (write(zfd, (char *)zbar[0], hres*sizeof(float)) |
436 |
|
< hres*sizeof(float)) |
448 |
|
if (sampdens != NULL) |
449 |
|
free(sampdens); |
450 |
|
pctdone = 100.0; |
451 |
+ |
if (ralrm > 0) |
452 |
+ |
report(); |
453 |
+ |
signal(SIGCONT, SIG_DFL); |
454 |
|
return; |
455 |
|
writerr: |
456 |
|
error(SYSTEM, "write error in render"); |
460 |
|
|
461 |
|
|
462 |
|
fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */ |
463 |
< |
register COLOR *scanline; |
464 |
< |
register float *zline; |
463 |
> |
register COLOR *scanline; |
464 |
> |
register float *zline; |
465 |
|
register char *sd; |
466 |
|
int xres, y, xstep; |
467 |
|
{ |
468 |
|
static int nc = 0; /* number of calls */ |
469 |
|
int bl = xstep, b = xstep; |
470 |
< |
double z; |
470 |
> |
double z; |
471 |
|
register int i; |
472 |
|
|
473 |
|
z = pixvalue(scanline[0], 0, y); |
495 |
|
|
496 |
|
|
497 |
|
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
498 |
< |
register COLOR *scanbar[]; |
499 |
< |
register float *zbar[]; |
498 |
> |
register COLOR *scanbar[]; |
499 |
> |
register float *zbar[]; |
500 |
|
int xres, y, ysize; |
501 |
|
{ |
502 |
|
COLOR vline[MAXDIV+1]; |
526 |
|
|
527 |
|
|
528 |
|
int |
529 |
< |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
530 |
< |
register COLOR *colline; |
531 |
< |
register float *zline; |
529 |
> |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
530 |
> |
register COLOR *colline; |
531 |
> |
register float *zline; |
532 |
|
int x, y; |
533 |
|
int xlen, ylen; |
534 |
|
int b; |
535 |
|
{ |
536 |
< |
extern double fabs(); |
537 |
< |
double ratio; |
502 |
< |
double z; |
536 |
> |
double ratio; |
537 |
> |
double z; |
538 |
|
COLOR ctmp; |
539 |
|
int ncut; |
540 |
|
register int len; |
620 |
|
error(WARNING, errmsg); |
621 |
|
return(0); |
622 |
|
} |
623 |
+ |
#ifdef MSDOS |
624 |
+ |
setmode(fileno(fp), O_BINARY); |
625 |
+ |
#endif |
626 |
|
/* discard header */ |
627 |
< |
getheader(fp, NULL); |
627 |
> |
getheader(fp, NULL, NULL); |
628 |
|
/* get picture size */ |
629 |
|
if (!fscnresolu(&x, &y, fp)) { |
630 |
< |
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
630 |
> |
sprintf(errmsg, "bad recover file \"%s\" - not removed", |
631 |
> |
oldfile); |
632 |
|
error(WARNING, errmsg); |
633 |
|
fclose(fp); |
634 |
|
return(0); |