25 |
|
|
26 |
|
#include "platform.h" |
27 |
|
#include "ray.h" |
28 |
+ |
#include "ambient.h" |
29 |
+ |
#include "source.h" |
30 |
|
#include "otypes.h" |
31 |
|
#include "resolu.h" |
32 |
|
|
46 |
|
|
47 |
|
int do_irrad = 0; /* compute irradiance? */ |
48 |
|
|
49 |
+ |
int rand_samp = 0; /* pure Monte Carlo sampling? */ |
50 |
+ |
|
51 |
|
void (*trace)() = NULL; /* trace call */ |
52 |
|
|
49 |
– |
extern void ambnotify(), tranotify(); |
50 |
– |
void (*addobjnotify[])() = {ambnotify, tranotify, NULL}; |
53 |
|
char *tralist[128]; /* list of modifers to trace (or no) */ |
54 |
|
int traincl = -1; /* include == 1, exclude == 0 */ |
55 |
< |
#define MAXTSET 511 /* maximum number in trace set */ |
55 |
> |
#ifndef MAXTSET |
56 |
> |
#define MAXTSET 1024 /* maximum number in trace set */ |
57 |
> |
#endif |
58 |
|
OBJECT traset[MAXTSET+1]={0}; /* trace include/exclude set */ |
59 |
|
|
60 |
|
int hresolu = 0; /* horizontal (scan) size */ |
61 |
|
int vresolu = 0; /* vertical resolution */ |
62 |
|
|
63 |
|
double dstrsrc = 0.0; /* square source distribution */ |
64 |
< |
double shadthresh = .05; /* shadow threshold */ |
65 |
< |
double shadcert = .5; /* shadow certainty */ |
64 |
> |
double shadthresh = .03; /* shadow threshold */ |
65 |
> |
double shadcert = .75; /* shadow certainty */ |
66 |
|
int directrelay = 2; /* number of source relays */ |
67 |
|
int vspretest = 512; /* virtual source pretest density */ |
68 |
|
int directvis = 1; /* sources visible? */ |
78 |
|
|
79 |
|
int backvis = 1; /* back face visibility */ |
80 |
|
|
81 |
< |
int maxdepth = 6; /* maximum recursion depth */ |
82 |
< |
double minweight = 4e-3; /* minimum ray weight */ |
81 |
> |
int maxdepth = -10; /* maximum recursion depth */ |
82 |
> |
double minweight = 2e-3; /* minimum ray weight */ |
83 |
|
|
84 |
|
char *ambfile = NULL; /* ambient file name */ |
85 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
86 |
|
int ambvwt = 0; /* initial weight for ambient value */ |
87 |
< |
double ambacc = 0.2; /* ambient accuracy */ |
88 |
< |
int ambres = 128; /* ambient resolution */ |
89 |
< |
int ambdiv = 512; /* ambient divisions */ |
90 |
< |
int ambssamp = 0; /* ambient super-samples */ |
87 |
> |
double ambacc = 0.15; /* ambient accuracy */ |
88 |
> |
int ambres = 256; /* ambient resolution */ |
89 |
> |
int ambdiv = 1024; /* ambient divisions */ |
90 |
> |
int ambssamp = 512; /* ambient super-samples */ |
91 |
|
int ambounce = 0; /* ambient bounces */ |
92 |
< |
char *amblist[128]; /* ambient include/exclude list */ |
92 |
> |
char *amblist[AMBLLEN]; /* ambient include/exclude list */ |
93 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
94 |
|
|
95 |
+ |
static int castonly = 0; |
96 |
|
|
97 |
|
static RAY thisray; /* for our convenience */ |
98 |
|
|
99 |
< |
static void oputo(), oputd(), oputv(), oputl(), oputL(), oputc(), |
100 |
< |
oputp(), oputn(), oputN(), oputs(), oputw(), oputm(); |
99 |
> |
typedef void putf_t(double v); |
100 |
> |
static putf_t puta, putd, putf; |
101 |
|
|
102 |
< |
static void ourtrace(), tabin(); |
103 |
< |
static void (*ray_out[16])(), (*every_out[16])(); |
104 |
< |
static int castonly = 0; |
102 |
> |
typedef void oputf_t(RAY *r); |
103 |
> |
static oputf_t oputo, oputd, oputv, oputl, oputL, oputc, oputp, |
104 |
> |
oputn, oputN, oputs, oputw, oputW, oputm, oputM, oputtilde; |
105 |
|
|
106 |
< |
static void puta(), putf(), putd(); |
106 |
> |
static void setoutput(char *vs); |
107 |
> |
static void tranotify(OBJECT obj); |
108 |
> |
static void bogusray(void); |
109 |
> |
static void rad(FVECT org, FVECT dir, double dmax); |
110 |
> |
static void irrad(FVECT org, FVECT dir); |
111 |
> |
static void printvals(RAY *r); |
112 |
> |
static int getvec(FVECT vec, int fmt, FILE *fp); |
113 |
> |
static void tabin(RAY *r); |
114 |
> |
static void ourtrace(RAY *r); |
115 |
|
|
116 |
< |
static void (*putreal)(); |
116 |
> |
static oputf_t *ray_out[16], *every_out[16]; |
117 |
> |
static putf_t *putreal; |
118 |
|
|
119 |
< |
void bogusray(), rad(), irrad(), printvals(); |
119 |
> |
void (*addobjnotify[])() = {ambnotify, tranotify, NULL}; |
120 |
|
|
121 |
|
|
122 |
|
void |
123 |
< |
quit(code) /* quit program */ |
124 |
< |
int code; |
123 |
> |
quit( /* quit program */ |
124 |
> |
int code |
125 |
> |
) |
126 |
|
{ |
127 |
|
#ifndef NON_POSIX /* XXX we don't clean up elsewhere? */ |
128 |
|
headclean(); /* delete header file */ |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
< |
char * |
136 |
< |
formstr(f) /* return format identifier */ |
137 |
< |
int f; |
135 |
> |
extern char * |
136 |
> |
formstr( /* return format identifier */ |
137 |
> |
int f |
138 |
> |
) |
139 |
|
{ |
140 |
|
switch (f) { |
141 |
|
case 'a': return("ascii"); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
< |
void |
151 |
< |
rtrace(fname) /* trace rays from file */ |
152 |
< |
char *fname; |
150 |
> |
extern void |
151 |
> |
rtrace( /* trace rays from file */ |
152 |
> |
char *fname |
153 |
> |
) |
154 |
|
{ |
155 |
< |
long vcount = hresolu>1 ? hresolu*vresolu : vresolu; |
155 |
> |
unsigned long vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu |
156 |
> |
: vresolu; |
157 |
|
long nextflush = hresolu; |
158 |
|
FILE *fp; |
159 |
|
double d; |
165 |
|
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
166 |
|
error(SYSTEM, errmsg); |
167 |
|
} |
150 |
– |
#ifdef _WIN32 |
168 |
|
if (inform != 'a') |
169 |
|
SET_FILE_BINARY(fp); |
153 |
– |
#endif |
170 |
|
/* set up output */ |
171 |
|
setoutput(outvals); |
172 |
|
switch (outform) { |
192 |
|
d = normalize(direc); |
193 |
|
if (d == 0.0) { /* zero ==> flush */ |
194 |
|
bogusray(); |
195 |
< |
if (--nextflush <= 0 || vcount <= 0) { |
195 |
> |
if (--nextflush <= 0 || !vcount) { |
196 |
|
fflush(stdout); |
197 |
|
nextflush = hresolu; |
198 |
|
} |
199 |
|
} else { |
200 |
< |
samplendx++; |
200 |
> |
samplendx = rand_samp ? random() : samplendx+1; |
201 |
|
/* compute and print */ |
202 |
|
if (imm_irrad) |
203 |
|
irrad(orig, direc); |
204 |
|
else |
205 |
|
rad(orig, direc, lim_dist ? d : 0.0); |
206 |
|
/* flush if time */ |
207 |
< |
if (--nextflush == 0) { |
207 |
> |
if (!--nextflush) { |
208 |
|
fflush(stdout); |
209 |
|
nextflush = hresolu; |
210 |
|
} |
211 |
|
} |
212 |
|
if (ferror(stdout)) |
213 |
|
error(SYSTEM, "write error"); |
214 |
< |
if (--vcount == 0) /* check for end */ |
214 |
> |
if (vcount && !--vcount) /* check for end */ |
215 |
|
break; |
216 |
|
} |
217 |
|
fflush(stdout); |
218 |
< |
if (vcount > 0) |
219 |
< |
error(USER, "read error"); |
218 |
> |
if (vcount) |
219 |
> |
error(USER, "unexpected EOF on input"); |
220 |
|
if (fname != NULL) |
221 |
|
fclose(fp); |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
< |
setoutput(vs) /* set up output tables */ |
226 |
< |
register char *vs; |
225 |
> |
static void |
226 |
> |
trace_sources(void) /* trace rays to light sources, also */ |
227 |
|
{ |
228 |
< |
extern void (*trace)(); |
229 |
< |
register void (**table)() = ray_out; |
228 |
> |
int sn; |
229 |
> |
|
230 |
> |
for (sn = 0; sn < nsources; sn++) |
231 |
> |
source[sn].sflags |= SFOLLOW; |
232 |
> |
} |
233 |
|
|
234 |
+ |
|
235 |
+ |
static void |
236 |
+ |
setoutput( /* set up output tables */ |
237 |
+ |
register char *vs |
238 |
+ |
) |
239 |
+ |
{ |
240 |
+ |
register oputf_t **table = ray_out; |
241 |
+ |
|
242 |
|
castonly = 1; |
243 |
|
while (*vs) |
244 |
|
switch (*vs++) { |
245 |
+ |
case 'T': /* trace sources */ |
246 |
+ |
if (!*vs) break; |
247 |
+ |
trace_sources(); |
248 |
+ |
/* fall through */ |
249 |
|
case 't': /* trace */ |
250 |
+ |
if (!*vs) break; |
251 |
|
*table = NULL; |
252 |
|
table = every_out; |
253 |
|
trace = ourtrace; |
289 |
|
case 'w': /* weight */ |
290 |
|
*table++ = oputw; |
291 |
|
break; |
292 |
+ |
case 'W': /* coefficient */ |
293 |
+ |
*table++ = oputW; |
294 |
+ |
if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0)) |
295 |
+ |
error(WARNING, |
296 |
+ |
"-otW accuracy depends on -aa 0 -as 0"); |
297 |
+ |
break; |
298 |
|
case 'm': /* modifier */ |
299 |
|
*table++ = oputm; |
300 |
|
break; |
301 |
+ |
case 'M': /* material */ |
302 |
+ |
*table++ = oputM; |
303 |
+ |
break; |
304 |
+ |
case '~': /* tilde */ |
305 |
+ |
*table++ = oputtilde; |
306 |
+ |
break; |
307 |
|
} |
308 |
|
*table = NULL; |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
< |
void |
313 |
< |
bogusray() /* print out empty record */ |
312 |
> |
static void |
313 |
> |
bogusray(void) /* print out empty record */ |
314 |
|
{ |
315 |
|
thisray.rorg[0] = thisray.rorg[1] = thisray.rorg[2] = |
316 |
|
thisray.rdir[0] = thisray.rdir[1] = thisray.rdir[2] = 0.0; |
317 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
317 |
> |
thisray.rmax = 0.0; |
318 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
319 |
|
printvals(&thisray); |
320 |
|
} |
321 |
|
|
322 |
|
|
323 |
< |
void |
324 |
< |
rad(org, dir, dmax) /* compute and print ray value(s) */ |
325 |
< |
FVECT org, dir; |
326 |
< |
double dmax; |
323 |
> |
static void |
324 |
> |
rad( /* compute and print ray value(s) */ |
325 |
> |
FVECT org, |
326 |
> |
FVECT dir, |
327 |
> |
double dmax |
328 |
> |
) |
329 |
|
{ |
330 |
|
VCOPY(thisray.rorg, org); |
331 |
|
VCOPY(thisray.rdir, dir); |
332 |
|
thisray.rmax = dmax; |
333 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
333 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
334 |
|
if (castonly) { |
335 |
|
if (!localhit(&thisray, &thescene)) { |
336 |
|
if (thisray.ro == &Aftplane) { /* clipped */ |
345 |
|
} |
346 |
|
|
347 |
|
|
348 |
< |
void |
349 |
< |
irrad(org, dir) /* compute immediate irradiance value */ |
350 |
< |
FVECT org, dir; |
348 |
> |
static void |
349 |
> |
irrad( /* compute immediate irradiance value */ |
350 |
> |
FVECT org, |
351 |
> |
FVECT dir |
352 |
> |
) |
353 |
|
{ |
354 |
|
register int i; |
355 |
|
|
357 |
|
thisray.rorg[i] = org[i] + dir[i]; |
358 |
|
thisray.rdir[i] = -dir[i]; |
359 |
|
} |
360 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
360 |
> |
thisray.rmax = 0.0; |
361 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
362 |
|
/* pretend we hit surface */ |
363 |
|
thisray.rot = 1.0-1e-4; |
364 |
|
thisray.rod = 1.0; |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
< |
void |
375 |
< |
printvals(r) /* print requested ray values */ |
376 |
< |
RAY *r; |
374 |
> |
static void |
375 |
> |
printvals( /* print requested ray values */ |
376 |
> |
RAY *r |
377 |
> |
) |
378 |
|
{ |
379 |
< |
register void (**tp)(); |
379 |
> |
register oputf_t **tp; |
380 |
|
|
381 |
|
if (ray_out[0] == NULL) |
382 |
|
return; |
387 |
|
} |
388 |
|
|
389 |
|
|
390 |
< |
int |
391 |
< |
getvec(vec, fmt, fp) /* get a vector from fp */ |
392 |
< |
register FVECT vec; |
393 |
< |
int fmt; |
394 |
< |
FILE *fp; |
390 |
> |
static int |
391 |
> |
getvec( /* get a vector from fp */ |
392 |
> |
register FVECT vec, |
393 |
> |
int fmt, |
394 |
> |
FILE *fp |
395 |
> |
) |
396 |
|
{ |
397 |
|
static float vf[3]; |
398 |
|
static double vd[3]; |
425 |
|
} |
426 |
|
|
427 |
|
|
428 |
< |
void |
429 |
< |
tranotify(obj) /* record new modifier */ |
430 |
< |
OBJECT obj; |
428 |
> |
static void |
429 |
> |
tranotify( /* record new modifier */ |
430 |
> |
OBJECT obj |
431 |
> |
) |
432 |
|
{ |
433 |
|
static int hitlimit = 0; |
434 |
|
register OBJREC *o = objptr(obj); |
455 |
|
|
456 |
|
|
457 |
|
static void |
458 |
< |
ourtrace(r) /* print ray values */ |
459 |
< |
RAY *r; |
458 |
> |
ourtrace( /* print ray values */ |
459 |
> |
RAY *r |
460 |
> |
) |
461 |
|
{ |
462 |
< |
register void (**tp)(); |
462 |
> |
register oputf_t **tp; |
463 |
|
|
464 |
|
if (every_out[0] == NULL) |
465 |
|
return; |
471 |
|
tabin(r); |
472 |
|
for (tp = every_out; *tp != NULL; tp++) |
473 |
|
(**tp)(r); |
474 |
< |
putchar('\n'); |
474 |
> |
if (outform == 'a') |
475 |
> |
putchar('\n'); |
476 |
|
} |
477 |
|
|
478 |
|
|
479 |
|
static void |
480 |
< |
tabin(r) /* tab in appropriate amount */ |
481 |
< |
RAY *r; |
480 |
> |
tabin( /* tab in appropriate amount */ |
481 |
> |
RAY *r |
482 |
> |
) |
483 |
|
{ |
484 |
< |
register RAY *rp; |
484 |
> |
const RAY *rp; |
485 |
|
|
486 |
|
for (rp = r->parent; rp != NULL; rp = rp->parent) |
487 |
|
putchar('\t'); |
489 |
|
|
490 |
|
|
491 |
|
static void |
492 |
< |
oputo(r) /* print origin */ |
493 |
< |
RAY *r; |
492 |
> |
oputo( /* print origin */ |
493 |
> |
RAY *r |
494 |
> |
) |
495 |
|
{ |
496 |
|
(*putreal)(r->rorg[0]); |
497 |
|
(*putreal)(r->rorg[1]); |
500 |
|
|
501 |
|
|
502 |
|
static void |
503 |
< |
oputd(r) /* print direction */ |
504 |
< |
RAY *r; |
503 |
> |
oputd( /* print direction */ |
504 |
> |
RAY *r |
505 |
> |
) |
506 |
|
{ |
507 |
|
(*putreal)(r->rdir[0]); |
508 |
|
(*putreal)(r->rdir[1]); |
511 |
|
|
512 |
|
|
513 |
|
static void |
514 |
< |
oputv(r) /* print value */ |
515 |
< |
RAY *r; |
514 |
> |
oputv( /* print value */ |
515 |
> |
RAY *r |
516 |
> |
) |
517 |
|
{ |
459 |
– |
COLR cout; |
460 |
– |
|
518 |
|
if (outform == 'c') { |
519 |
+ |
COLR cout; |
520 |
|
setcolr(cout, colval(r->rcol,RED), |
521 |
|
colval(r->rcol,GRN), |
522 |
|
colval(r->rcol,BLU)); |
530 |
|
|
531 |
|
|
532 |
|
static void |
533 |
< |
oputl(r) /* print effective distance */ |
534 |
< |
RAY *r; |
533 |
> |
oputl( /* print effective distance */ |
534 |
> |
RAY *r |
535 |
> |
) |
536 |
|
{ |
537 |
|
(*putreal)(r->rt); |
538 |
|
} |
539 |
|
|
540 |
|
|
541 |
|
static void |
542 |
< |
oputL(r) /* print single ray length */ |
543 |
< |
RAY *r; |
542 |
> |
oputL( /* print single ray length */ |
543 |
> |
RAY *r |
544 |
> |
) |
545 |
|
{ |
546 |
|
(*putreal)(r->rot); |
547 |
|
} |
548 |
|
|
549 |
|
|
550 |
|
static void |
551 |
< |
oputc(r) /* print local coordinates */ |
552 |
< |
RAY *r; |
551 |
> |
oputc( /* print local coordinates */ |
552 |
> |
RAY *r |
553 |
> |
) |
554 |
|
{ |
555 |
|
(*putreal)(r->uv[0]); |
556 |
|
(*putreal)(r->uv[1]); |
558 |
|
|
559 |
|
|
560 |
|
static void |
561 |
< |
oputp(r) /* print point */ |
562 |
< |
RAY *r; |
561 |
> |
oputp( /* print point */ |
562 |
> |
RAY *r |
563 |
> |
) |
564 |
|
{ |
565 |
|
if (r->rot < FHUGE) { |
566 |
|
(*putreal)(r->rop[0]); |
575 |
|
|
576 |
|
|
577 |
|
static void |
578 |
< |
oputN(r) /* print unperturbed normal */ |
579 |
< |
RAY *r; |
578 |
> |
oputN( /* print unperturbed normal */ |
579 |
> |
RAY *r |
580 |
> |
) |
581 |
|
{ |
582 |
|
if (r->rot < FHUGE) { |
583 |
|
(*putreal)(r->ron[0]); |
592 |
|
|
593 |
|
|
594 |
|
static void |
595 |
< |
oputn(r) /* print perturbed normal */ |
596 |
< |
RAY *r; |
595 |
> |
oputn( /* print perturbed normal */ |
596 |
> |
RAY *r |
597 |
> |
) |
598 |
|
{ |
599 |
|
FVECT pnorm; |
600 |
|
|
612 |
|
|
613 |
|
|
614 |
|
static void |
615 |
< |
oputs(r) /* print name */ |
616 |
< |
RAY *r; |
615 |
> |
oputs( /* print name */ |
616 |
> |
RAY *r |
617 |
> |
) |
618 |
|
{ |
619 |
|
if (r->ro != NULL) |
620 |
|
fputs(r->ro->oname, stdout); |
625 |
|
|
626 |
|
|
627 |
|
static void |
628 |
< |
oputw(r) /* print weight */ |
629 |
< |
RAY *r; |
628 |
> |
oputw( /* print weight */ |
629 |
> |
RAY *r |
630 |
> |
) |
631 |
|
{ |
632 |
|
(*putreal)(r->rweight); |
633 |
|
} |
634 |
|
|
635 |
|
|
636 |
|
static void |
637 |
< |
oputm(r) /* print modifier */ |
638 |
< |
RAY *r; |
637 |
> |
oputW( /* print contribution */ |
638 |
> |
RAY *r |
639 |
> |
) |
640 |
|
{ |
641 |
+ |
double contr[3]; |
642 |
+ |
|
643 |
+ |
raycontrib(contr, r, PRIMARY); |
644 |
+ |
(*putreal)(contr[RED]); |
645 |
+ |
(*putreal)(contr[GRN]); |
646 |
+ |
(*putreal)(contr[BLU]); |
647 |
+ |
} |
648 |
+ |
|
649 |
+ |
|
650 |
+ |
static void |
651 |
+ |
oputm( /* print modifier */ |
652 |
+ |
RAY *r |
653 |
+ |
) |
654 |
+ |
{ |
655 |
|
if (r->ro != NULL) |
656 |
|
if (r->ro->omod != OVOID) |
657 |
|
fputs(objptr(r->ro->omod)->oname, stdout); |
664 |
|
|
665 |
|
|
666 |
|
static void |
667 |
< |
puta(v) /* print ascii value */ |
668 |
< |
double v; |
667 |
> |
oputM( /* print material */ |
668 |
> |
RAY *r |
669 |
> |
) |
670 |
> |
{ |
671 |
> |
OBJREC *mat; |
672 |
> |
|
673 |
> |
if (r->ro != NULL) { |
674 |
> |
if ((mat = findmaterial(r->ro)) != NULL) |
675 |
> |
fputs(mat->oname, stdout); |
676 |
> |
else |
677 |
> |
fputs(VOIDID, stdout); |
678 |
> |
} else |
679 |
> |
putchar('*'); |
680 |
> |
putchar('\t'); |
681 |
> |
} |
682 |
> |
|
683 |
> |
|
684 |
> |
static void |
685 |
> |
oputtilde( /* output tilde (spacer) */ |
686 |
> |
RAY *r |
687 |
> |
) |
688 |
> |
{ |
689 |
> |
fputs("~\t", stdout); |
690 |
> |
} |
691 |
> |
|
692 |
> |
|
693 |
> |
static void |
694 |
> |
puta( /* print ascii value */ |
695 |
> |
double v |
696 |
> |
) |
697 |
|
{ |
698 |
|
printf("%e\t", v); |
699 |
|
} |