29 |
|
#include "source.h" |
30 |
|
#include "otypes.h" |
31 |
|
#include "resolu.h" |
32 |
+ |
#include "random.h" |
33 |
|
|
34 |
|
CUBE thescene; /* our scene */ |
35 |
|
OBJECT nsceneobjs; /* number of objects in our scene */ |
47 |
|
|
48 |
|
int do_irrad = 0; /* compute irradiance? */ |
49 |
|
|
50 |
+ |
int rand_samp = 1; /* pure Monte Carlo sampling? */ |
51 |
+ |
|
52 |
|
void (*trace)() = NULL; /* trace call */ |
53 |
|
|
54 |
< |
char *tralist[128]; /* list of modifers to trace (or no) */ |
54 |
> |
#ifndef MAXMODLIST |
55 |
> |
#define MAXMODLIST 1024 /* maximum modifiers we'll track */ |
56 |
> |
#endif |
57 |
> |
|
58 |
> |
char *tralist[MAXMODLIST]; /* list of modifers to trace (or no) */ |
59 |
|
int traincl = -1; /* include == 1, exclude == 0 */ |
60 |
< |
#define MAXTSET 511 /* maximum number in trace set */ |
60 |
> |
#ifndef MAXTSET |
61 |
> |
#define MAXTSET 8191 /* maximum number in trace set */ |
62 |
> |
#endif |
63 |
|
OBJECT traset[MAXTSET+1]={0}; /* trace include/exclude set */ |
64 |
|
|
65 |
|
int hresolu = 0; /* horizontal (scan) size */ |
83 |
|
|
84 |
|
int backvis = 1; /* back face visibility */ |
85 |
|
|
86 |
< |
int maxdepth = 8; /* maximum recursion depth */ |
86 |
> |
int maxdepth = -10; /* maximum recursion depth */ |
87 |
|
double minweight = 2e-3; /* minimum ray weight */ |
88 |
|
|
89 |
|
char *ambfile = NULL; /* ambient file name */ |
94 |
|
int ambdiv = 1024; /* ambient divisions */ |
95 |
|
int ambssamp = 512; /* ambient super-samples */ |
96 |
|
int ambounce = 0; /* ambient bounces */ |
97 |
< |
char *amblist[128]; /* ambient include/exclude list */ |
97 |
> |
char *amblist[AMBLLEN]; /* ambient include/exclude list */ |
98 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
99 |
|
|
100 |
|
static int castonly = 0; |
105 |
|
static putf_t puta, putd, putf; |
106 |
|
|
107 |
|
typedef void oputf_t(RAY *r); |
108 |
< |
static oputf_t oputo, oputd, oputv, oputl, oputL, oputc, |
109 |
< |
oputp, oputn, oputN, oputs, oputw, oputm; |
108 |
> |
static oputf_t oputo, oputd, oputv, oputV, oputl, oputL, oputc, oputp, |
109 |
> |
oputn, oputN, oputs, oputw, oputW, oputm, oputM, oputtilde; |
110 |
|
|
111 |
|
static void setoutput(char *vs); |
112 |
|
static void tranotify(OBJECT obj); |
157 |
|
char *fname |
158 |
|
) |
159 |
|
{ |
160 |
< |
long vcount = hresolu>1 ? hresolu*vresolu : vresolu; |
160 |
> |
unsigned long vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu |
161 |
> |
: vresolu; |
162 |
|
long nextflush = hresolu; |
163 |
|
FILE *fp; |
164 |
|
double d; |
170 |
|
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
171 |
|
error(SYSTEM, errmsg); |
172 |
|
} |
163 |
– |
#ifdef _WIN32 |
173 |
|
if (inform != 'a') |
174 |
|
SET_FILE_BINARY(fp); |
166 |
– |
#endif |
175 |
|
/* set up output */ |
176 |
|
setoutput(outvals); |
177 |
|
switch (outform) { |
197 |
|
d = normalize(direc); |
198 |
|
if (d == 0.0) { /* zero ==> flush */ |
199 |
|
bogusray(); |
200 |
< |
if (--nextflush <= 0 || vcount <= 0) { |
200 |
> |
if (--nextflush <= 0 || !vcount) { |
201 |
|
fflush(stdout); |
202 |
|
nextflush = hresolu; |
203 |
|
} |
209 |
|
else |
210 |
|
rad(orig, direc, lim_dist ? d : 0.0); |
211 |
|
/* flush if time */ |
212 |
< |
if (--nextflush == 0) { |
212 |
> |
if (!--nextflush) { |
213 |
|
fflush(stdout); |
214 |
|
nextflush = hresolu; |
215 |
|
} |
216 |
|
} |
217 |
|
if (ferror(stdout)) |
218 |
|
error(SYSTEM, "write error"); |
219 |
< |
if (--vcount == 0) /* check for end */ |
219 |
> |
if (vcount && !--vcount) /* check for end */ |
220 |
|
break; |
221 |
|
} |
222 |
< |
fflush(stdout); |
223 |
< |
if (vcount > 0) |
224 |
< |
error(USER, "read error"); |
222 |
> |
if (fflush(stdout) < 0) |
223 |
> |
error(SYSTEM, "write error"); |
224 |
> |
if (vcount) |
225 |
> |
error(USER, "unexpected EOF on input"); |
226 |
|
if (fname != NULL) |
227 |
|
fclose(fp); |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
static void |
232 |
+ |
trace_sources(void) /* trace rays to light sources, also */ |
233 |
+ |
{ |
234 |
+ |
int sn; |
235 |
+ |
|
236 |
+ |
for (sn = 0; sn < nsources; sn++) |
237 |
+ |
source[sn].sflags |= SFOLLOW; |
238 |
+ |
} |
239 |
+ |
|
240 |
+ |
|
241 |
+ |
static void |
242 |
|
setoutput( /* set up output tables */ |
243 |
|
register char *vs |
244 |
|
) |
248 |
|
castonly = 1; |
249 |
|
while (*vs) |
250 |
|
switch (*vs++) { |
251 |
+ |
case 'T': /* trace sources */ |
252 |
+ |
if (!*vs) break; |
253 |
+ |
trace_sources(); |
254 |
+ |
/* fall through */ |
255 |
|
case 't': /* trace */ |
256 |
+ |
if (!*vs) break; |
257 |
|
*table = NULL; |
258 |
|
table = every_out; |
259 |
|
trace = ourtrace; |
269 |
|
*table++ = oputv; |
270 |
|
castonly = 0; |
271 |
|
break; |
272 |
+ |
case 'V': /* contribution */ |
273 |
+ |
*table++ = oputV; |
274 |
+ |
if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0)) |
275 |
+ |
error(WARNING, |
276 |
+ |
"-otV accuracy depends on -aa 0 -as 0"); |
277 |
+ |
break; |
278 |
|
case 'l': /* effective distance */ |
279 |
|
*table++ = oputl; |
280 |
|
castonly = 0; |
301 |
|
case 'w': /* weight */ |
302 |
|
*table++ = oputw; |
303 |
|
break; |
304 |
+ |
case 'W': /* coefficient */ |
305 |
+ |
*table++ = oputW; |
306 |
+ |
if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0)) |
307 |
+ |
error(WARNING, |
308 |
+ |
"-otW accuracy depends on -aa 0 -as 0"); |
309 |
+ |
break; |
310 |
|
case 'm': /* modifier */ |
311 |
|
*table++ = oputm; |
312 |
|
break; |
313 |
+ |
case 'M': /* material */ |
314 |
+ |
*table++ = oputM; |
315 |
+ |
break; |
316 |
+ |
case '~': /* tilde */ |
317 |
+ |
*table++ = oputtilde; |
318 |
+ |
break; |
319 |
|
} |
320 |
|
*table = NULL; |
321 |
|
} |
326 |
|
{ |
327 |
|
thisray.rorg[0] = thisray.rorg[1] = thisray.rorg[2] = |
328 |
|
thisray.rdir[0] = thisray.rdir[1] = thisray.rdir[2] = 0.0; |
329 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
329 |
> |
thisray.rmax = 0.0; |
330 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
331 |
|
printvals(&thisray); |
332 |
|
} |
333 |
|
|
342 |
|
VCOPY(thisray.rorg, org); |
343 |
|
VCOPY(thisray.rdir, dir); |
344 |
|
thisray.rmax = dmax; |
345 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
345 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
346 |
|
if (castonly) { |
347 |
|
if (!localhit(&thisray, &thescene)) { |
348 |
|
if (thisray.ro == &Aftplane) { /* clipped */ |
369 |
|
thisray.rorg[i] = org[i] + dir[i]; |
370 |
|
thisray.rdir[i] = -dir[i]; |
371 |
|
} |
372 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
372 |
> |
thisray.rmax = 0.0; |
373 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
374 |
|
/* pretend we hit surface */ |
375 |
|
thisray.rot = 1.0-1e-4; |
376 |
|
thisray.rod = 1.0; |
483 |
|
tabin(r); |
484 |
|
for (tp = every_out; *tp != NULL; tp++) |
485 |
|
(**tp)(r); |
486 |
< |
putchar('\n'); |
486 |
> |
if (outform == 'a') |
487 |
> |
putchar('\n'); |
488 |
|
} |
489 |
|
|
490 |
|
|
493 |
|
RAY *r |
494 |
|
) |
495 |
|
{ |
496 |
< |
register RAY *rp; |
496 |
> |
const RAY *rp; |
497 |
|
|
498 |
|
for (rp = r->parent; rp != NULL; rp = rp->parent) |
499 |
|
putchar('\t'); |
527 |
|
RAY *r |
528 |
|
) |
529 |
|
{ |
485 |
– |
COLR cout; |
486 |
– |
|
530 |
|
if (outform == 'c') { |
531 |
+ |
COLR cout; |
532 |
|
setcolr(cout, colval(r->rcol,RED), |
533 |
|
colval(r->rcol,GRN), |
534 |
|
colval(r->rcol,BLU)); |
542 |
|
|
543 |
|
|
544 |
|
static void |
545 |
+ |
oputV( /* print value contribution */ |
546 |
+ |
RAY *r |
547 |
+ |
) |
548 |
+ |
{ |
549 |
+ |
double contr[3]; |
550 |
+ |
|
551 |
+ |
raycontrib(contr, r, PRIMARY); |
552 |
+ |
multcolor(contr, r->rcol); |
553 |
+ |
(*putreal)(contr[RED]); |
554 |
+ |
(*putreal)(contr[GRN]); |
555 |
+ |
(*putreal)(contr[BLU]); |
556 |
+ |
} |
557 |
+ |
|
558 |
+ |
|
559 |
+ |
static void |
560 |
|
oputl( /* print effective distance */ |
561 |
|
RAY *r |
562 |
|
) |
661 |
|
|
662 |
|
|
663 |
|
static void |
664 |
+ |
oputW( /* print coefficient */ |
665 |
+ |
RAY *r |
666 |
+ |
) |
667 |
+ |
{ |
668 |
+ |
double contr[3]; |
669 |
+ |
|
670 |
+ |
raycontrib(contr, r, PRIMARY); |
671 |
+ |
(*putreal)(contr[RED]); |
672 |
+ |
(*putreal)(contr[GRN]); |
673 |
+ |
(*putreal)(contr[BLU]); |
674 |
+ |
} |
675 |
+ |
|
676 |
+ |
|
677 |
+ |
static void |
678 |
|
oputm( /* print modifier */ |
679 |
|
RAY *r |
680 |
|
) |
687 |
|
else |
688 |
|
putchar('*'); |
689 |
|
putchar('\t'); |
690 |
+ |
} |
691 |
+ |
|
692 |
+ |
|
693 |
+ |
static void |
694 |
+ |
oputM( /* print material */ |
695 |
+ |
RAY *r |
696 |
+ |
) |
697 |
+ |
{ |
698 |
+ |
OBJREC *mat; |
699 |
+ |
|
700 |
+ |
if (r->ro != NULL) { |
701 |
+ |
if ((mat = findmaterial(r->ro)) != NULL) |
702 |
+ |
fputs(mat->oname, stdout); |
703 |
+ |
else |
704 |
+ |
fputs(VOIDID, stdout); |
705 |
+ |
} else |
706 |
+ |
putchar('*'); |
707 |
+ |
putchar('\t'); |
708 |
+ |
} |
709 |
+ |
|
710 |
+ |
|
711 |
+ |
static void |
712 |
+ |
oputtilde( /* output tilde (spacer) */ |
713 |
+ |
RAY *r |
714 |
+ |
) |
715 |
+ |
{ |
716 |
+ |
fputs("~\t", stdout); |
717 |
|
} |
718 |
|
|
719 |
|
|