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 = 0; /* pure Monte Carlo sampling? */ |
51 |
+ |
|
52 |
|
void (*trace)() = NULL; /* trace call */ |
53 |
|
|
54 |
|
char *tralist[128]; /* list of modifers to trace (or no) */ |
55 |
|
int traincl = -1; /* include == 1, exclude == 0 */ |
56 |
< |
#define MAXTSET 511 /* maximum number in trace set */ |
56 |
> |
#ifndef MAXTSET |
57 |
> |
#define MAXTSET 8192 /* maximum number in trace set */ |
58 |
> |
#endif |
59 |
|
OBJECT traset[MAXTSET+1]={0}; /* trace include/exclude set */ |
60 |
|
|
61 |
|
int hresolu = 0; /* horizontal (scan) size */ |
79 |
|
|
80 |
|
int backvis = 1; /* back face visibility */ |
81 |
|
|
82 |
< |
int maxdepth = 8; /* maximum recursion depth */ |
82 |
> |
int maxdepth = -10; /* maximum recursion depth */ |
83 |
|
double minweight = 2e-3; /* minimum ray weight */ |
84 |
|
|
85 |
|
char *ambfile = NULL; /* ambient file name */ |
101 |
|
static putf_t puta, putd, putf; |
102 |
|
|
103 |
|
typedef void oputf_t(RAY *r); |
104 |
< |
static oputf_t oputo, oputd, oputv, oputl, oputL, oputc, |
105 |
< |
oputp, oputn, oputN, oputs, oputw, oputm, oputM; |
104 |
> |
static oputf_t oputo, oputd, oputv, oputV, oputl, oputL, oputc, oputp, |
105 |
> |
oputn, oputN, oputs, oputw, oputW, oputm, oputM, oputtilde; |
106 |
|
|
107 |
|
static void setoutput(char *vs); |
108 |
|
static void tranotify(OBJECT obj); |
153 |
|
char *fname |
154 |
|
) |
155 |
|
{ |
156 |
< |
long vcount = hresolu>1 ? hresolu*vresolu : vresolu; |
156 |
> |
unsigned long vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu |
157 |
> |
: vresolu; |
158 |
|
long nextflush = hresolu; |
159 |
|
FILE *fp; |
160 |
|
double d; |
166 |
|
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
167 |
|
error(SYSTEM, errmsg); |
168 |
|
} |
163 |
– |
#ifdef _WIN32 |
169 |
|
if (inform != 'a') |
170 |
|
SET_FILE_BINARY(fp); |
166 |
– |
#endif |
171 |
|
/* set up output */ |
172 |
|
setoutput(outvals); |
173 |
|
switch (outform) { |
193 |
|
d = normalize(direc); |
194 |
|
if (d == 0.0) { /* zero ==> flush */ |
195 |
|
bogusray(); |
196 |
< |
if (--nextflush <= 0 || vcount <= 0) { |
196 |
> |
if (--nextflush <= 0 || !vcount) { |
197 |
|
fflush(stdout); |
198 |
|
nextflush = hresolu; |
199 |
|
} |
200 |
|
} else { |
201 |
< |
samplendx++; |
201 |
> |
samplendx = rand_samp ? random() : samplendx+1; |
202 |
|
/* compute and print */ |
203 |
|
if (imm_irrad) |
204 |
|
irrad(orig, direc); |
205 |
|
else |
206 |
|
rad(orig, direc, lim_dist ? d : 0.0); |
207 |
|
/* flush if time */ |
208 |
< |
if (--nextflush == 0) { |
208 |
> |
if (!--nextflush) { |
209 |
|
fflush(stdout); |
210 |
|
nextflush = hresolu; |
211 |
|
} |
212 |
|
} |
213 |
|
if (ferror(stdout)) |
214 |
|
error(SYSTEM, "write error"); |
215 |
< |
if (--vcount == 0) /* check for end */ |
215 |
> |
if (vcount && !--vcount) /* check for end */ |
216 |
|
break; |
217 |
|
} |
218 |
< |
fflush(stdout); |
219 |
< |
if (vcount > 0) |
220 |
< |
error(USER, "read error"); |
218 |
> |
if (fflush(stdout) < 0) |
219 |
> |
error(SYSTEM, "write error"); |
220 |
> |
if (vcount) |
221 |
> |
error(USER, "unexpected EOF on input"); |
222 |
|
if (fname != NULL) |
223 |
|
fclose(fp); |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
|
static void |
228 |
+ |
trace_sources(void) /* trace rays to light sources, also */ |
229 |
+ |
{ |
230 |
+ |
int sn; |
231 |
+ |
|
232 |
+ |
for (sn = 0; sn < nsources; sn++) |
233 |
+ |
source[sn].sflags |= SFOLLOW; |
234 |
+ |
} |
235 |
+ |
|
236 |
+ |
|
237 |
+ |
static void |
238 |
|
setoutput( /* set up output tables */ |
239 |
|
register char *vs |
240 |
|
) |
244 |
|
castonly = 1; |
245 |
|
while (*vs) |
246 |
|
switch (*vs++) { |
247 |
+ |
case 'T': /* trace sources */ |
248 |
+ |
if (!*vs) break; |
249 |
+ |
trace_sources(); |
250 |
+ |
/* fall through */ |
251 |
|
case 't': /* trace */ |
252 |
+ |
if (!*vs) break; |
253 |
|
*table = NULL; |
254 |
|
table = every_out; |
255 |
|
trace = ourtrace; |
265 |
|
*table++ = oputv; |
266 |
|
castonly = 0; |
267 |
|
break; |
268 |
+ |
case 'V': /* contribution */ |
269 |
+ |
*table++ = oputV; |
270 |
+ |
if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0)) |
271 |
+ |
error(WARNING, |
272 |
+ |
"-otV accuracy depends on -aa 0 -as 0"); |
273 |
+ |
break; |
274 |
|
case 'l': /* effective distance */ |
275 |
|
*table++ = oputl; |
276 |
|
castonly = 0; |
297 |
|
case 'w': /* weight */ |
298 |
|
*table++ = oputw; |
299 |
|
break; |
300 |
+ |
case 'W': /* coefficient */ |
301 |
+ |
*table++ = oputW; |
302 |
+ |
if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0)) |
303 |
+ |
error(WARNING, |
304 |
+ |
"-otW accuracy depends on -aa 0 -as 0"); |
305 |
+ |
break; |
306 |
|
case 'm': /* modifier */ |
307 |
|
*table++ = oputm; |
308 |
|
break; |
309 |
|
case 'M': /* material */ |
310 |
|
*table++ = oputM; |
311 |
|
break; |
312 |
+ |
case '~': /* tilde */ |
313 |
+ |
*table++ = oputtilde; |
314 |
+ |
break; |
315 |
|
} |
316 |
|
*table = NULL; |
317 |
|
} |
322 |
|
{ |
323 |
|
thisray.rorg[0] = thisray.rorg[1] = thisray.rorg[2] = |
324 |
|
thisray.rdir[0] = thisray.rdir[1] = thisray.rdir[2] = 0.0; |
325 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
325 |
> |
thisray.rmax = 0.0; |
326 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
327 |
|
printvals(&thisray); |
328 |
|
} |
329 |
|
|
338 |
|
VCOPY(thisray.rorg, org); |
339 |
|
VCOPY(thisray.rdir, dir); |
340 |
|
thisray.rmax = dmax; |
341 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
341 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
342 |
|
if (castonly) { |
343 |
|
if (!localhit(&thisray, &thescene)) { |
344 |
|
if (thisray.ro == &Aftplane) { /* clipped */ |
365 |
|
thisray.rorg[i] = org[i] + dir[i]; |
366 |
|
thisray.rdir[i] = -dir[i]; |
367 |
|
} |
368 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
368 |
> |
thisray.rmax = 0.0; |
369 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
370 |
|
/* pretend we hit surface */ |
371 |
|
thisray.rot = 1.0-1e-4; |
372 |
|
thisray.rod = 1.0; |
479 |
|
tabin(r); |
480 |
|
for (tp = every_out; *tp != NULL; tp++) |
481 |
|
(**tp)(r); |
482 |
< |
putchar('\n'); |
482 |
> |
if (outform == 'a') |
483 |
> |
putchar('\n'); |
484 |
|
} |
485 |
|
|
486 |
|
|
489 |
|
RAY *r |
490 |
|
) |
491 |
|
{ |
492 |
< |
register RAY *rp; |
492 |
> |
const RAY *rp; |
493 |
|
|
494 |
|
for (rp = r->parent; rp != NULL; rp = rp->parent) |
495 |
|
putchar('\t'); |
523 |
|
RAY *r |
524 |
|
) |
525 |
|
{ |
488 |
– |
COLR cout; |
489 |
– |
|
526 |
|
if (outform == 'c') { |
527 |
+ |
COLR cout; |
528 |
|
setcolr(cout, colval(r->rcol,RED), |
529 |
|
colval(r->rcol,GRN), |
530 |
|
colval(r->rcol,BLU)); |
538 |
|
|
539 |
|
|
540 |
|
static void |
541 |
+ |
oputV( /* print value contribution */ |
542 |
+ |
RAY *r |
543 |
+ |
) |
544 |
+ |
{ |
545 |
+ |
double contr[3]; |
546 |
+ |
|
547 |
+ |
raycontrib(contr, r, PRIMARY); |
548 |
+ |
multcolor(contr, r->rcol); |
549 |
+ |
(*putreal)(contr[RED]); |
550 |
+ |
(*putreal)(contr[GRN]); |
551 |
+ |
(*putreal)(contr[BLU]); |
552 |
+ |
} |
553 |
+ |
|
554 |
+ |
|
555 |
+ |
static void |
556 |
|
oputl( /* print effective distance */ |
557 |
|
RAY *r |
558 |
|
) |
657 |
|
|
658 |
|
|
659 |
|
static void |
660 |
+ |
oputW( /* print coefficient */ |
661 |
+ |
RAY *r |
662 |
+ |
) |
663 |
+ |
{ |
664 |
+ |
double contr[3]; |
665 |
+ |
|
666 |
+ |
raycontrib(contr, r, PRIMARY); |
667 |
+ |
(*putreal)(contr[RED]); |
668 |
+ |
(*putreal)(contr[GRN]); |
669 |
+ |
(*putreal)(contr[BLU]); |
670 |
+ |
} |
671 |
+ |
|
672 |
+ |
|
673 |
+ |
static void |
674 |
|
oputm( /* print modifier */ |
675 |
|
RAY *r |
676 |
|
) |
701 |
|
} else |
702 |
|
putchar('*'); |
703 |
|
putchar('\t'); |
704 |
+ |
} |
705 |
+ |
|
706 |
+ |
|
707 |
+ |
static void |
708 |
+ |
oputtilde( /* output tilde (spacer) */ |
709 |
+ |
RAY *r |
710 |
+ |
) |
711 |
+ |
{ |
712 |
+ |
fputs("~\t", stdout); |
713 |
|
} |
714 |
|
|
715 |
|
|