10 |
|
#include "mkillum.h" |
11 |
|
#include "face.h" |
12 |
|
#include "cone.h" |
13 |
< |
#include "random.h" |
13 |
> |
#include "source.h" |
14 |
|
|
15 |
|
|
16 |
|
COLORV * distarr = NULL; /* distribution array */ |
42 |
|
|
43 |
|
|
44 |
|
int |
45 |
< |
process_ray(RAY *r, int rv) |
45 |
> |
process_ray( /* process a ray result or report error */ |
46 |
> |
RAY *r, |
47 |
> |
int rv |
48 |
> |
) |
49 |
|
{ |
50 |
|
COLORV *colp; |
51 |
|
|
55 |
|
error(USER, "ray tracing process died"); |
56 |
|
if (r->rno >= distsiz) |
57 |
|
error(INTERNAL, "bad returned index in process_ray"); |
58 |
+ |
multcolor(r->rcol, r->rcoef); /* in case it's a source ray */ |
59 |
|
colp = &distarr[r->rno * 3]; |
60 |
|
addcolor(colp, r->rcol); |
61 |
|
return(1); |
63 |
|
|
64 |
|
|
65 |
|
void |
66 |
< |
raysamp( /* queue a ray sample */ |
66 |
> |
raysamp( /* queue a ray sample */ |
67 |
|
int ndx, |
68 |
|
FVECT org, |
69 |
|
FVECT dir |
85 |
|
|
86 |
|
|
87 |
|
void |
88 |
+ |
srcsamps( /* sample sources from this surface position */ |
89 |
+ |
struct illum_args *il, |
90 |
+ |
FVECT org, |
91 |
+ |
FVECT nrm, |
92 |
+ |
MAT4 ixfm |
93 |
+ |
) |
94 |
+ |
{ |
95 |
+ |
int nalt, nazi; |
96 |
+ |
SRCINDEX si; |
97 |
+ |
RAY sr; |
98 |
+ |
FVECT v; |
99 |
+ |
double d; |
100 |
+ |
int i, j; |
101 |
+ |
/* get sampling density */ |
102 |
+ |
if (il->sampdens <= 0) { |
103 |
+ |
nalt = nazi = 1; |
104 |
+ |
} else { |
105 |
+ |
i = PI * il->sampdens; |
106 |
+ |
nalt = sqrt(i/PI) + .5; |
107 |
+ |
nazi = PI*nalt + .5; |
108 |
+ |
} |
109 |
+ |
initsrcindex(&si); /* loop over (sub)sources */ |
110 |
+ |
for ( ; ; ) { |
111 |
+ |
VCOPY(sr.rorg, org); /* pick side to shoot from */ |
112 |
+ |
if (il->sd != NULL) { |
113 |
+ |
int sn = si.sn; |
114 |
+ |
if (si.sp+1 >= si.np) ++sn; |
115 |
+ |
if (sn >= nsources) break; |
116 |
+ |
if (source[sn].sflags & SDISTANT) |
117 |
+ |
d = DOT(source[sn].sloc, nrm); |
118 |
+ |
else { |
119 |
+ |
VSUB(v, source[sn].sloc, org); |
120 |
+ |
d = DOT(v, nrm); |
121 |
+ |
} |
122 |
+ |
} else |
123 |
+ |
d = 1.0; /* only transmission */ |
124 |
+ |
if (d < 0.0) |
125 |
+ |
d = -1.0001*il->thick - 5.*FTINY; |
126 |
+ |
else |
127 |
+ |
d = 5.*FTINY; |
128 |
+ |
for (i = 3; i--; ) |
129 |
+ |
sr.rorg[i] += d*nrm[i]; |
130 |
+ |
if (!srcray(&sr, NULL, &si)) |
131 |
+ |
break; /* end of sources */ |
132 |
+ |
/* index direction */ |
133 |
+ |
if (ixfm != NULL) |
134 |
+ |
multv3(v, sr.rdir, ixfm); |
135 |
+ |
else |
136 |
+ |
VCOPY(v, sr.rdir); |
137 |
+ |
if (il->sd != NULL) { |
138 |
+ |
i = getBSDF_incndx(il->sd, v); |
139 |
+ |
if (i < 0) |
140 |
+ |
continue; /* must not be important */ |
141 |
+ |
sr.rno = i; |
142 |
+ |
d = getBSDF_incrad(il->sd, i); |
143 |
+ |
d = 1.0/(PI*d*d); |
144 |
+ |
} else { |
145 |
+ |
if (v[2] >= -FTINY) |
146 |
+ |
continue; /* only sample transmission */ |
147 |
+ |
d = 1.0 - v[2]*v[2]; |
148 |
+ |
i = d*nalt; |
149 |
+ |
d = atan2(-v[1], -v[0])/(2.*PI); |
150 |
+ |
if (d < 0.0) d += 1.0; |
151 |
+ |
j = d*nazi + 0.5; |
152 |
+ |
if (j >= nazi) j = 0; |
153 |
+ |
sr.rno = i*nazi + j; |
154 |
+ |
d = nalt*nazi/PI; |
155 |
+ |
} |
156 |
+ |
d *= si.dom; /* solid angle correction */ |
157 |
+ |
scalecolor(sr.rcoef, d); |
158 |
+ |
process_ray(&sr, ray_pqueue(&sr)); |
159 |
+ |
} |
160 |
+ |
} |
161 |
+ |
|
162 |
+ |
|
163 |
+ |
void |
164 |
|
rayclean() /* finish all pending rays */ |
165 |
|
{ |
166 |
|
RAY myRay; |
246 |
|
char *nm |
247 |
|
) |
248 |
|
{ |
169 |
– |
#define MAXMISS (5*n*il->nsamps) |
249 |
|
int dim[2]; |
250 |
< |
int n, nalt, nazi, h, alti; |
250 |
> |
int n, nalt, nazi, alti; |
251 |
|
double sp[2], r1, r2; |
252 |
+ |
int h; |
253 |
|
FVECT dn, org, dir; |
254 |
|
FVECT u, v; |
255 |
|
double ur[2], vr[2]; |
256 |
|
MAT4 xfm; |
257 |
< |
int nmisses; |
257 |
> |
int nallow; |
258 |
|
FACE *fa; |
259 |
< |
register int i, j; |
259 |
> |
int i, j; |
260 |
|
/* get/check arguments */ |
261 |
|
fa = getface(ob); |
262 |
|
if (fa->area == 0.0) { |
309 |
|
} |
310 |
|
dim[0] = random(); |
311 |
|
/* sample polygon */ |
312 |
< |
nmisses = 0; |
312 |
> |
nallow = 5*n*il->nsamps; |
313 |
|
for (dim[1] = 0; dim[1] < n; dim[1]++) |
314 |
|
for (i = 0; i < il->nsamps; i++) { |
315 |
|
/* randomize direction */ |
328 |
|
} |
329 |
|
/* randomize location */ |
330 |
|
do { |
331 |
< |
multisamp(sp, 2, urand(h+4862+nmisses)); |
331 |
> |
multisamp(sp, 2, urand(h+4862+nallow)); |
332 |
|
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
333 |
|
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
334 |
|
for (j = 0; j < 3; j++) |
335 |
|
org[j] = r1*u[j] + r2*v[j] |
336 |
|
+ fa->offset*fa->norm[j]; |
337 |
< |
} while (!inface(org, fa) && nmisses++ < MAXMISS); |
338 |
< |
if (nmisses > MAXMISS) { |
337 |
> |
} while (!inface(org, fa) && nallow-- > 0); |
338 |
> |
if (nallow < 0) { |
339 |
|
objerror(ob, WARNING, "bad aspect"); |
340 |
|
rayclean(); |
341 |
|
freeface(ob); |
342 |
|
return(my_default(ob, il, nm)); |
343 |
|
} |
344 |
|
if (il->sd != NULL && DOT(dir, fa->norm) < -FTINY) |
345 |
< |
r1 = -1.0001*il->thick - .0001; |
345 |
> |
r1 = -1.0001*il->thick - 5.*FTINY; |
346 |
|
else |
347 |
< |
r1 = .0001; |
347 |
> |
r1 = 5.*FTINY; |
348 |
|
for (j = 0; j < 3; j++) |
349 |
|
org[j] += r1*fa->norm[j]; |
350 |
|
/* send sample */ |
351 |
|
raysamp(dim[1], org, dir); |
352 |
|
} |
353 |
+ |
/* add in direct component? */ |
354 |
+ |
if (!directvis && il->flags & IL_LIGHT) { |
355 |
+ |
MAT4 ixfm; |
356 |
+ |
if (il->sd == NULL) { |
357 |
+ |
for (i = 3; i--; ) { |
358 |
+ |
ixfm[i][0] = u[i]; |
359 |
+ |
ixfm[i][1] = v[i]; |
360 |
+ |
ixfm[i][2] = fa->norm[i]; |
361 |
+ |
ixfm[i][3] = 0.; |
362 |
+ |
} |
363 |
+ |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
364 |
+ |
ixfm[3][3] = 1.; |
365 |
+ |
} else if (!invmat4(ixfm, xfm)) |
366 |
+ |
objerror(ob, INTERNAL, "cannot invert BSDF transform"); |
367 |
+ |
dim[0] = random(); |
368 |
+ |
nallow = 10*il->nsamps; |
369 |
+ |
for (i = 0; i < il->nsamps; i++) { |
370 |
+ |
/* randomize location */ |
371 |
+ |
h = dim[0] + samplendx++; |
372 |
+ |
do { |
373 |
+ |
multisamp(sp, 2, urand(h+nallow)); |
374 |
+ |
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
375 |
+ |
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
376 |
+ |
for (j = 0; j < 3; j++) |
377 |
+ |
org[j] = r1*u[j] + r2*v[j] |
378 |
+ |
+ fa->offset*fa->norm[j]; |
379 |
+ |
} while (!inface(org, fa) && nallow-- > 0); |
380 |
+ |
if (nallow < 0) { |
381 |
+ |
objerror(ob, WARNING, "bad aspect"); |
382 |
+ |
rayclean(); |
383 |
+ |
freeface(ob); |
384 |
+ |
return(my_default(ob, il, nm)); |
385 |
+ |
} |
386 |
+ |
/* sample source rays */ |
387 |
+ |
srcsamps(il, org, fa->norm, ixfm); |
388 |
+ |
} |
389 |
+ |
} |
390 |
+ |
/* wait for all rays to finish */ |
391 |
|
rayclean(); |
392 |
|
if (il->sd != NULL) { /* run distribution through BSDF */ |
393 |
|
nalt = sqrt(il->sd->nout/PI) + .5; |
404 |
|
/* clean up */ |
405 |
|
freeface(ob); |
406 |
|
return(0); |
289 |
– |
#undef MAXMISS |
407 |
|
} |
408 |
|
|
409 |
|
|
461 |
|
/* send sample */ |
462 |
|
raysamp(dim[1]*nazi+dim[2], org, dir); |
463 |
|
} |
464 |
+ |
/* wait for all rays to finish */ |
465 |
|
rayclean(); |
466 |
|
/* write out the sphere and its distribution */ |
467 |
|
if (average(il, distarr, n)) { |
492 |
|
FVECT u, v; |
493 |
|
MAT4 xfm; |
494 |
|
CONE *co; |
495 |
< |
register int i, j; |
495 |
> |
int i, j; |
496 |
|
/* get/check arguments */ |
497 |
|
co = getcone(ob, 0); |
498 |
|
/* set up sampling */ |
541 |
|
r1 = r3*cos(r2); |
542 |
|
r2 = r3*sin(r2); |
543 |
|
if (il->sd != NULL && DOT(dir, co->ad) < -FTINY) |
544 |
< |
r3 = -1.0001*il->thick - .0001; |
544 |
> |
r3 = -1.0001*il->thick - 5.*FTINY; |
545 |
|
else |
546 |
< |
r3 = .0001; |
546 |
> |
r3 = 5.*FTINY; |
547 |
|
for (j = 0; j < 3; j++) |
548 |
|
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + |
549 |
|
r3*co->ad[j]; |
550 |
|
/* send sample */ |
551 |
|
raysamp(dim[1], org, dir); |
552 |
|
} |
553 |
+ |
/* add in direct component? */ |
554 |
+ |
if (!directvis && il->flags & IL_LIGHT) { |
555 |
+ |
MAT4 ixfm; |
556 |
+ |
if (il->sd == NULL) { |
557 |
+ |
for (i = 3; i--; ) { |
558 |
+ |
ixfm[i][0] = u[i]; |
559 |
+ |
ixfm[i][1] = v[i]; |
560 |
+ |
ixfm[i][2] = co->ad[i]; |
561 |
+ |
ixfm[i][3] = 0.; |
562 |
+ |
} |
563 |
+ |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
564 |
+ |
ixfm[3][3] = 1.; |
565 |
+ |
} else if (!invmat4(ixfm, xfm)) |
566 |
+ |
objerror(ob, INTERNAL, "cannot invert BSDF transform"); |
567 |
+ |
dim[0] = random(); |
568 |
+ |
for (i = 0; i < il->nsamps; i++) { |
569 |
+ |
/* randomize location */ |
570 |
+ |
h = dim[0] + samplendx++; |
571 |
+ |
multisamp(sp, 2, urand(h)); |
572 |
+ |
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
573 |
+ |
sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
574 |
+ |
r2 = 2.*PI*sp[1]; |
575 |
+ |
r1 = r3*cos(r2); |
576 |
+ |
r2 = r3*sin(r2); |
577 |
+ |
for (j = 0; j < 3; j++) |
578 |
+ |
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j]; |
579 |
+ |
/* sample source rays */ |
580 |
+ |
srcsamps(il, org, co->ad, ixfm); |
581 |
+ |
} |
582 |
+ |
} |
583 |
+ |
/* wait for all rays to finish */ |
584 |
|
rayclean(); |
585 |
|
if (il->sd != NULL) { /* run distribution through BSDF */ |
586 |
|
nalt = sqrt(il->sd->nout/PI) + .5; |