11 |
|
#include "face.h" |
12 |
|
#include "cone.h" |
13 |
|
#include "source.h" |
14 |
+ |
#include "paths.h" |
15 |
|
|
16 |
+ |
#ifndef NBSDFSAMPS |
17 |
+ |
#define NBSDFSAMPS 256 /* BSDF resampling count */ |
18 |
+ |
#endif |
19 |
|
|
20 |
|
COLORV * distarr = NULL; /* distribution array */ |
21 |
|
int distsiz = 0; |
18 |
– |
COLORV * direct_discount = NULL; /* amount to take off direct */ |
22 |
|
|
23 |
+ |
|
24 |
|
void |
25 |
|
newdist( /* allocate & clear distribution array */ |
26 |
|
int siz |
28 |
|
{ |
29 |
|
if (siz <= 0) { |
30 |
|
if (distsiz > 0) |
31 |
< |
free((void *)distarr); |
31 |
> |
free(distarr); |
32 |
|
distarr = NULL; |
33 |
|
distsiz = 0; |
34 |
|
return; |
35 |
|
} |
36 |
|
if (distsiz < siz) { |
37 |
|
if (distsiz > 0) |
38 |
< |
free((void *)distarr); |
38 |
> |
free(distarr); |
39 |
|
distarr = (COLORV *)malloc(sizeof(COLOR)*siz); |
40 |
|
if (distarr == NULL) |
41 |
|
error(SYSTEM, "out of memory in newdist"); |
45 |
|
} |
46 |
|
|
47 |
|
|
44 |
– |
static void |
45 |
– |
new_discount() /* allocate space for direct contrib. record */ |
46 |
– |
{ |
47 |
– |
if (distsiz <= 0) |
48 |
– |
return; |
49 |
– |
direct_discount = (COLORV *)calloc(distsiz, sizeof(COLOR)); |
50 |
– |
if (direct_discount == NULL) |
51 |
– |
error(SYSTEM, "out of memory in new_discount"); |
52 |
– |
} |
53 |
– |
|
54 |
– |
|
55 |
– |
static void |
56 |
– |
done_discount() /* clear off direct contrib. record */ |
57 |
– |
{ |
58 |
– |
if (direct_discount == NULL) |
59 |
– |
return; |
60 |
– |
free((void *)direct_discount); |
61 |
– |
direct_discount = NULL; |
62 |
– |
} |
63 |
– |
|
64 |
– |
|
48 |
|
int |
49 |
|
process_ray( /* process a ray result or report error */ |
50 |
|
RAY *r, |
62 |
|
multcolor(r->rcol, r->rcoef); /* in case it's a source ray */ |
63 |
|
colp = &distarr[r->rno * 3]; |
64 |
|
addcolor(colp, r->rcol); |
82 |
– |
if (r->rsrc >= 0 && /* remember source contrib. */ |
83 |
– |
direct_discount != NULL) { |
84 |
– |
colp = &direct_discount[r->rno * 3]; |
85 |
– |
addcolor(colp, r->rcol); |
86 |
– |
} |
65 |
|
return(1); |
66 |
|
} |
67 |
|
|
81 |
|
VCOPY(myRay.rorg, org); |
82 |
|
VCOPY(myRay.rdir, dir); |
83 |
|
myRay.rmax = .0; |
84 |
< |
rayorigin(&myRay, PRIMARY, NULL, NULL); |
84 |
> |
rayorigin(&myRay, PRIMARY|SPECULAR, NULL, NULL); |
85 |
|
myRay.rno = ndx; |
86 |
|
/* queue ray, check result */ |
87 |
|
process_ray(&myRay, ray_pqueue(&myRay)); |
96 |
|
MAT4 ixfm |
97 |
|
) |
98 |
|
{ |
99 |
< |
int nalt, nazi; |
99 |
> |
int nalt=1, nazi=1; |
100 |
|
SRCINDEX si; |
101 |
|
RAY sr; |
102 |
|
FVECT v; |
103 |
|
double d; |
104 |
|
int i, j; |
105 |
|
/* get sampling density */ |
106 |
< |
if (il->sampdens <= 0) { |
129 |
< |
nalt = nazi = 1; |
130 |
< |
} else { |
106 |
> |
if (il->sampdens > 0) { |
107 |
|
i = PI * il->sampdens; |
108 |
|
nalt = sqrt(i/PI) + .5; |
109 |
|
nazi = PI*nalt + .5; |
111 |
|
initsrcindex(&si); /* loop over (sub)sources */ |
112 |
|
for ( ; ; ) { |
113 |
|
VCOPY(sr.rorg, org); /* pick side to shoot from */ |
114 |
< |
if (il->sd != NULL) { |
115 |
< |
int sn = si.sn; |
116 |
< |
if (si.sp+1 >= si.np) ++sn; |
141 |
< |
if (sn >= nsources) break; |
142 |
< |
if (source[sn].sflags & SDISTANT) |
143 |
< |
d = DOT(source[sn].sloc, nrm); |
144 |
< |
else { |
145 |
< |
VSUB(v, source[sn].sloc, org); |
146 |
< |
d = DOT(v, nrm); |
147 |
< |
} |
148 |
< |
} else |
149 |
< |
d = 1.0; /* only transmission */ |
150 |
< |
if (d < 0.0) |
151 |
< |
d = -1.0001*il->thick - 5.*FTINY; |
152 |
< |
else |
153 |
< |
d = 5.*FTINY; |
154 |
< |
for (i = 3; i--; ) |
155 |
< |
sr.rorg[i] += d*nrm[i]; |
114 |
> |
d = 5.*FTINY; |
115 |
> |
VSUM(sr.rorg, sr.rorg, nrm, d); |
116 |
> |
samplendx++; /* increment sample counter */ |
117 |
|
if (!srcray(&sr, NULL, &si)) |
118 |
|
break; /* end of sources */ |
119 |
|
/* index direction */ |
121 |
|
multv3(v, sr.rdir, ixfm); |
122 |
|
else |
123 |
|
VCOPY(v, sr.rdir); |
124 |
< |
if (il->sd != NULL) { |
125 |
< |
i = getBSDF_incndx(il->sd, v); |
126 |
< |
if (i < 0) |
127 |
< |
continue; /* must not be important */ |
128 |
< |
sr.rno = i; |
168 |
< |
d = 1.0/getBSDF_incohm(il->sd, i); |
169 |
< |
} else { |
170 |
< |
if (v[2] >= -FTINY) |
171 |
< |
continue; /* only sample transmission */ |
172 |
< |
v[0] = -v[0]; v[1] = -v[1]; v[2] = -v[2]; |
173 |
< |
sr.rno = flatindex(v, nalt, nazi); |
174 |
< |
d = nalt*nazi*(1./PI) * v[2]; |
175 |
< |
} |
124 |
> |
if (v[2] >= -FTINY) |
125 |
> |
continue; /* only sample transmission */ |
126 |
> |
v[0] = -v[0]; v[1] = -v[1]; v[2] = -v[2]; |
127 |
> |
sr.rno = flatindex(v, nalt, nazi); |
128 |
> |
d = nalt*nazi*(1./PI) * v[2]; |
129 |
|
d *= si.dom; /* solid angle correction */ |
130 |
|
scalecolor(sr.rcoef, d); |
131 |
|
process_ray(&sr, ray_pqueue(&sr)); |
196 |
|
dv[2] = sqrt(1. - alt); |
197 |
|
} |
198 |
|
|
199 |
+ |
|
200 |
|
int |
201 |
|
flatindex( /* compute index for hemispherical direction */ |
202 |
|
FVECT dv, |
247 |
|
FVECT u, v; |
248 |
|
double ur[2], vr[2]; |
249 |
|
MAT4 xfm; |
250 |
+ |
char xfrot[64]; |
251 |
|
int nallow; |
252 |
|
FACE *fa; |
253 |
|
int i, j; |
258 |
|
return(my_default(ob, il, nm)); |
259 |
|
} |
260 |
|
/* set up sampling */ |
261 |
< |
if (il->sd != NULL) { |
262 |
< |
if (!getBSDF_xfm(xfm, fa->norm, il->udir)) { |
308 |
< |
objerror(ob, WARNING, "illegal up direction"); |
309 |
< |
freeface(ob); |
310 |
< |
return(my_default(ob, il, nm)); |
311 |
< |
} |
312 |
< |
n = il->sd->ninc; |
261 |
> |
if (il->sampdens <= 0) { |
262 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
263 |
|
} else { |
264 |
< |
if (il->sampdens <= 0) { |
265 |
< |
nalt = nazi = 1; /* diffuse assumption */ |
266 |
< |
} else { |
317 |
< |
n = PI * il->sampdens; |
318 |
< |
nalt = sqrt(n/PI) + .5; |
319 |
< |
nazi = PI*nalt + .5; |
320 |
< |
} |
321 |
< |
n = nazi*nalt; |
264 |
> |
n = PI * il->sampdens; |
265 |
> |
nalt = sqrt(n/PI) + .5; |
266 |
> |
nazi = PI*nalt + .5; |
267 |
|
} |
268 |
+ |
n = nazi*nalt; |
269 |
|
newdist(n); |
270 |
|
/* take first edge >= sqrt(area) */ |
271 |
|
for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) { |
299 |
|
for (i = 0; i < il->nsamps; i++) { |
300 |
|
/* randomize direction */ |
301 |
|
h = ilhash(dim, 2) + i; |
302 |
< |
if (il->sd != NULL) { |
303 |
< |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
304 |
< |
} else { |
305 |
< |
multisamp(sp, 2, urand(h)); |
306 |
< |
alti = dim[1]/nazi; |
307 |
< |
r1 = (alti + sp[0])/nalt; |
362 |
< |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
363 |
< |
flatdir(dn, r1, r2); |
364 |
< |
for (j = 0; j < 3; j++) |
302 |
> |
multisamp(sp, 2, urand(h)); |
303 |
> |
alti = dim[1]/nazi; |
304 |
> |
r1 = (alti + sp[0])/nalt; |
305 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
306 |
> |
flatdir(dn, r1, r2); |
307 |
> |
for (j = 0; j < 3; j++) |
308 |
|
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - |
309 |
|
dn[2]*fa->norm[j]; |
367 |
– |
} |
310 |
|
/* randomize location */ |
311 |
|
do { |
312 |
|
multisamp(sp, 2, urand(h+4862+nallow)); |
322 |
|
freeface(ob); |
323 |
|
return(my_default(ob, il, nm)); |
324 |
|
} |
325 |
< |
if (il->sd != NULL && DOT(dir, fa->norm) < -FTINY) |
384 |
< |
r1 = -1.0001*il->thick - 5.*FTINY; |
385 |
< |
else |
386 |
< |
r1 = 5.*FTINY; |
325 |
> |
r1 = 5.*FTINY; |
326 |
|
for (j = 0; j < 3; j++) |
327 |
|
org[j] += r1*fa->norm[j]; |
328 |
|
/* send sample */ |
329 |
|
raysamp(dim[1], org, dir); |
330 |
|
} |
331 |
|
/* add in direct component? */ |
332 |
< |
if (!directvis && (il->flags & IL_LIGHT || il->sd != NULL)) { |
332 |
> |
if (il->flags & IL_LIGHT) { |
333 |
|
MAT4 ixfm; |
334 |
< |
if (il->sd == NULL) { |
335 |
< |
for (i = 3; i--; ) { |
336 |
< |
ixfm[i][0] = u[i]; |
337 |
< |
ixfm[i][1] = v[i]; |
338 |
< |
ixfm[i][2] = fa->norm[i]; |
400 |
< |
ixfm[i][3] = 0.; |
401 |
< |
} |
402 |
< |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
403 |
< |
ixfm[3][3] = 1.; |
404 |
< |
} else { |
405 |
< |
if (!invmat4(ixfm, xfm)) |
406 |
< |
objerror(ob, INTERNAL, |
407 |
< |
"cannot invert BSDF transform"); |
408 |
< |
if (!(il->flags & IL_LIGHT)) |
409 |
< |
new_discount(); |
334 |
> |
for (i = 3; i--; ) { |
335 |
> |
ixfm[i][0] = u[i]; |
336 |
> |
ixfm[i][1] = v[i]; |
337 |
> |
ixfm[i][2] = fa->norm[i]; |
338 |
> |
ixfm[i][3] = 0.; |
339 |
|
} |
340 |
+ |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
341 |
+ |
ixfm[3][3] = 1.; |
342 |
|
dim[0] = random(); |
343 |
|
nallow = 10*il->nsamps; |
344 |
|
for (i = 0; i < il->nsamps; i++) { |
364 |
|
} |
365 |
|
/* wait for all rays to finish */ |
366 |
|
rayclean(); |
436 |
– |
if (il->sd != NULL) { /* run distribution through BSDF */ |
437 |
– |
nalt = sqrt(il->sd->nout/PI) + .5; |
438 |
– |
nazi = PI*nalt + .5; |
439 |
– |
redistribute(il->sd, nalt, nazi, u, v, fa->norm, xfm); |
440 |
– |
done_discount(); |
441 |
– |
if (!il->sampdens) |
442 |
– |
il->sampdens = nalt*nazi/PI + .999; |
443 |
– |
} |
367 |
|
/* write out the face and its distribution */ |
368 |
|
if (average(il, distarr, n)) { |
369 |
|
if (il->sampdens > 0) |
401 |
|
nalt = sqrt(2./PI*n) + .5; |
402 |
|
nazi = PI/2.*nalt + .5; |
403 |
|
} |
481 |
– |
if (il->sd != NULL) |
482 |
– |
objerror(ob, WARNING, "BSDF ignored"); |
404 |
|
n = nalt*nazi; |
405 |
|
newdist(n); |
406 |
|
dim[0] = random(); |
464 |
|
/* get/check arguments */ |
465 |
|
co = getcone(ob, 0); |
466 |
|
/* set up sampling */ |
467 |
< |
if (il->sd != NULL) { |
468 |
< |
if (!getBSDF_xfm(xfm, co->ad, il->udir)) { |
548 |
< |
objerror(ob, WARNING, "illegal up direction"); |
549 |
< |
freecone(ob); |
550 |
< |
return(my_default(ob, il, nm)); |
551 |
< |
} |
552 |
< |
n = il->sd->ninc; |
467 |
> |
if (il->sampdens <= 0) { |
468 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
469 |
|
} else { |
470 |
< |
if (il->sampdens <= 0) { |
471 |
< |
nalt = nazi = 1; /* diffuse assumption */ |
472 |
< |
} else { |
557 |
< |
n = PI * il->sampdens; |
558 |
< |
nalt = sqrt(n/PI) + .5; |
559 |
< |
nazi = PI*nalt + .5; |
560 |
< |
} |
561 |
< |
n = nazi*nalt; |
470 |
> |
n = PI * il->sampdens; |
471 |
> |
nalt = sqrt(n/PI) + .5; |
472 |
> |
nazi = PI*nalt + .5; |
473 |
|
} |
474 |
+ |
n = nazi*nalt; |
475 |
|
newdist(n); |
476 |
|
mkaxes(u, v, co->ad); |
477 |
|
dim[0] = random(); |
481 |
|
/* next sample point */ |
482 |
|
h = ilhash(dim,2) + i; |
483 |
|
/* randomize direction */ |
484 |
< |
if (il->sd != NULL) { |
485 |
< |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
486 |
< |
} else { |
487 |
< |
multisamp(sp, 2, urand(h)); |
488 |
< |
alti = dim[1]/nazi; |
489 |
< |
r1 = (alti + sp[0])/nalt; |
490 |
< |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
579 |
< |
flatdir(dn, r1, r2); |
580 |
< |
for (j = 0; j < 3; j++) |
581 |
< |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
582 |
< |
} |
484 |
> |
multisamp(sp, 2, urand(h)); |
485 |
> |
alti = dim[1]/nazi; |
486 |
> |
r1 = (alti + sp[0])/nalt; |
487 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
488 |
> |
flatdir(dn, r1, r2); |
489 |
> |
for (j = 0; j < 3; j++) |
490 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
491 |
|
/* randomize location */ |
492 |
|
multisamp(sp, 2, urand(h+8371)); |
493 |
|
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
495 |
|
r2 = 2.*PI*sp[1]; |
496 |
|
r1 = r3*cos(r2); |
497 |
|
r2 = r3*sin(r2); |
498 |
< |
if (il->sd != NULL && DOT(dir, co->ad) < -FTINY) |
591 |
< |
r3 = -1.0001*il->thick - 5.*FTINY; |
592 |
< |
else |
593 |
< |
r3 = 5.*FTINY; |
498 |
> |
r3 = 5.*FTINY; |
499 |
|
for (j = 0; j < 3; j++) |
500 |
|
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + |
501 |
|
r3*co->ad[j]; |
503 |
|
raysamp(dim[1], org, dir); |
504 |
|
} |
505 |
|
/* add in direct component? */ |
506 |
< |
if (!directvis && (il->flags & IL_LIGHT || il->sd != NULL)) { |
506 |
> |
if (il->flags & IL_LIGHT) { |
507 |
|
MAT4 ixfm; |
508 |
< |
if (il->sd == NULL) { |
509 |
< |
for (i = 3; i--; ) { |
510 |
< |
ixfm[i][0] = u[i]; |
511 |
< |
ixfm[i][1] = v[i]; |
512 |
< |
ixfm[i][2] = co->ad[i]; |
608 |
< |
ixfm[i][3] = 0.; |
609 |
< |
} |
610 |
< |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
611 |
< |
ixfm[3][3] = 1.; |
612 |
< |
} else { |
613 |
< |
if (!invmat4(ixfm, xfm)) |
614 |
< |
objerror(ob, INTERNAL, |
615 |
< |
"cannot invert BSDF transform"); |
616 |
< |
if (!(il->flags & IL_LIGHT)) |
617 |
< |
new_discount(); |
508 |
> |
for (i = 3; i--; ) { |
509 |
> |
ixfm[i][0] = u[i]; |
510 |
> |
ixfm[i][1] = v[i]; |
511 |
> |
ixfm[i][2] = co->ad[i]; |
512 |
> |
ixfm[i][3] = 0.; |
513 |
|
} |
514 |
+ |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
515 |
+ |
ixfm[3][3] = 1.; |
516 |
|
dim[0] = random(); |
517 |
|
for (i = 0; i < il->nsamps; i++) { |
518 |
|
/* randomize location */ |
531 |
|
} |
532 |
|
/* wait for all rays to finish */ |
533 |
|
rayclean(); |
637 |
– |
if (il->sd != NULL) { /* run distribution through BSDF */ |
638 |
– |
nalt = sqrt(il->sd->nout/PI) + .5; |
639 |
– |
nazi = PI*nalt + .5; |
640 |
– |
redistribute(il->sd, nalt, nazi, u, v, co->ad, xfm); |
641 |
– |
done_discount(); |
642 |
– |
if (!il->sampdens) |
643 |
– |
il->sampdens = nalt*nazi/PI + .999; |
644 |
– |
} |
534 |
|
/* write out the ring and its distribution */ |
535 |
|
if (average(il, distarr, n)) { |
536 |
|
if (il->sampdens > 0) |