13 |
|
#include "source.h" |
14 |
|
#include "paths.h" |
15 |
|
|
16 |
< |
#ifndef NBSDFSAMPS |
17 |
< |
#define NBSDFSAMPS 256 /* BSDF resampling count */ |
16 |
> |
#ifndef R_EPS |
17 |
> |
#define R_EPS 0.005 /* relative epsilon for ray origin */ |
18 |
|
#endif |
19 |
|
|
20 |
|
COLORV * distarr = NULL; /* distribution array */ |
21 |
|
int distsiz = 0; |
22 |
– |
COLORV * direct_discount = NULL; /* amount to take off direct */ |
22 |
|
|
23 |
|
|
24 |
|
void |
45 |
|
} |
46 |
|
|
47 |
|
|
49 |
– |
static void |
50 |
– |
new_discount() /* allocate space for direct contrib. record */ |
51 |
– |
{ |
52 |
– |
if (distsiz <= 0) |
53 |
– |
return; |
54 |
– |
direct_discount = (COLORV *)calloc(distsiz, sizeof(COLOR)); |
55 |
– |
if (direct_discount == NULL) |
56 |
– |
error(SYSTEM, "out of memory in new_discount"); |
57 |
– |
} |
58 |
– |
|
59 |
– |
|
60 |
– |
static void |
61 |
– |
done_discount() /* clear off direct contrib. record */ |
62 |
– |
{ |
63 |
– |
if (direct_discount == NULL) |
64 |
– |
return; |
65 |
– |
free(direct_discount); |
66 |
– |
direct_discount = NULL; |
67 |
– |
} |
68 |
– |
|
69 |
– |
|
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); |
87 |
– |
if (r->rsrc >= 0 && /* remember source contrib. */ |
88 |
– |
direct_discount != NULL) { |
89 |
– |
colp = &direct_discount[r->rno * 3]; |
90 |
– |
addcolor(colp, r->rcol); |
91 |
– |
} |
65 |
|
return(1); |
66 |
|
} |
67 |
|
|
92 |
|
srcsamps( /* sample sources from this surface position */ |
93 |
|
struct illum_args *il, |
94 |
|
FVECT org, |
95 |
< |
FVECT nrm, |
95 |
> |
double eps, |
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) { |
134 |
< |
nalt = nazi = 1; |
135 |
< |
} 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 */ |
143 |
– |
if (il->sd != NULL) { |
144 |
– |
int sn = si.sn; |
145 |
– |
if (si.sp+1 >= si.np) ++sn; |
146 |
– |
if (sn >= nsources) break; |
147 |
– |
if (source[sn].sflags & SDISTANT) |
148 |
– |
d = DOT(source[sn].sloc, nrm); |
149 |
– |
else { |
150 |
– |
VSUB(v, source[sn].sloc, org); |
151 |
– |
d = DOT(v, nrm); |
152 |
– |
} |
153 |
– |
} else |
154 |
– |
d = 1.0; /* only transmission */ |
155 |
– |
if (d < 0.0) |
156 |
– |
d = -1.0001*il->thick - 5.*FTINY; |
157 |
– |
else |
158 |
– |
d = 5.*FTINY; |
159 |
– |
for (i = 3; i--; ) |
160 |
– |
sr.rorg[i] += d*nrm[i]; |
114 |
|
samplendx++; /* increment sample counter */ |
115 |
|
if (!srcray(&sr, NULL, &si)) |
116 |
|
break; /* end of sources */ |
119 |
|
multv3(v, sr.rdir, ixfm); |
120 |
|
else |
121 |
|
VCOPY(v, sr.rdir); |
122 |
< |
if (il->sd != NULL) { |
123 |
< |
i = getBSDF_incndx(il->sd, v); |
124 |
< |
if (i < 0) |
125 |
< |
continue; /* must not be important */ |
126 |
< |
sr.rno = i; |
174 |
< |
d = 1.0/getBSDF_incohm(il->sd, i); |
175 |
< |
} else { |
176 |
< |
if (v[2] >= -FTINY) |
177 |
< |
continue; /* only sample transmission */ |
178 |
< |
v[0] = -v[0]; v[1] = -v[1]; v[2] = -v[2]; |
179 |
< |
sr.rno = flatindex(v, nalt, nazi); |
180 |
< |
d = nalt*nazi*(1./PI) * v[2]; |
181 |
< |
} |
122 |
> |
if (v[2] >= -FTINY) |
123 |
> |
continue; /* only sample transmission */ |
124 |
> |
v[0] = -v[0]; v[1] = -v[1]; v[2] = -v[2]; |
125 |
> |
sr.rno = flatindex(v, nalt, nazi); |
126 |
> |
d = nalt*nazi*(1./PI) * v[2]; |
127 |
|
d *= si.dom; /* solid angle correction */ |
128 |
|
scalecolor(sr.rcoef, d); |
129 |
+ |
VSUM(sr.rorg, sr.rorg, sr.rdir, -eps); |
130 |
|
process_ray(&sr, ray_pqueue(&sr)); |
131 |
|
} |
132 |
|
} |
149 |
|
FVECT n |
150 |
|
) |
151 |
|
{ |
152 |
< |
register int i; |
207 |
< |
|
208 |
< |
v[0] = v[1] = v[2] = 0.0; |
209 |
< |
for (i = 0; i < 3; i++) |
210 |
< |
if (n[i] < 0.6 && n[i] > -0.6) |
211 |
< |
break; |
212 |
< |
v[i] = 1.0; |
213 |
< |
fcross(u, v, n); |
214 |
< |
normalize(u); |
152 |
> |
getperpendicular(u, n, 1); |
153 |
|
fcross(v, n, u); |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
static void |
158 |
|
rounddir( /* compute uniform spherical direction */ |
159 |
< |
register FVECT dv, |
159 |
> |
FVECT dv, |
160 |
|
double alt, |
161 |
|
double azi |
162 |
|
) |
209 |
|
|
210 |
|
|
211 |
|
int |
274 |
– |
printgeom( /* print out detailed geometry for BSDF */ |
275 |
– |
struct BSDF_data *sd, |
276 |
– |
char *xfrot, |
277 |
– |
FVECT ctr, |
278 |
– |
double s1, |
279 |
– |
double s2 |
280 |
– |
) |
281 |
– |
{ |
282 |
– |
static char mgftemp[] = TEMPLATE; |
283 |
– |
char cmdbuf[64]; |
284 |
– |
FILE *fp; |
285 |
– |
double sca; |
286 |
– |
|
287 |
– |
if (sd == NULL || sd->mgf == NULL) |
288 |
– |
return(0); |
289 |
– |
if (sd->dim[0] <= FTINY || sd->dim[1] <= FTINY) |
290 |
– |
return(0); |
291 |
– |
if ((s1 > s2) ^ (sd->dim[0] > sd->dim[1])) { |
292 |
– |
sca = s1; s1 = s2; s2 = sca; |
293 |
– |
} |
294 |
– |
s1 /= sd->dim[0]; |
295 |
– |
s2 /= sd->dim[1]; |
296 |
– |
sca = s1 > s2 ? s1 : s2; |
297 |
– |
strcpy(mgftemp, TEMPLATE); |
298 |
– |
if ((fp = fopen(mktemp(mgftemp), "w")) == NULL) |
299 |
– |
error(SYSTEM, "cannot create temporary file for MGF"); |
300 |
– |
/* prepend our transform */ |
301 |
– |
fprintf(fp, "xf%s -s %.5f -t %.5g %.5g %.5g\n", |
302 |
– |
xfrot, sca, ctr[0], ctr[1], ctr[2]); |
303 |
– |
/* output given MGF description */ |
304 |
– |
fputs(sd->mgf, fp); |
305 |
– |
fputs("\nxf\n", fp); |
306 |
– |
if (fclose(fp) == EOF) |
307 |
– |
error(SYSTEM, "error writing MGF temporary file"); |
308 |
– |
/* execute mgf2rad to convert MGF */ |
309 |
– |
strcpy(cmdbuf, "mgf2rad "); |
310 |
– |
strcpy(cmdbuf+8, mgftemp); |
311 |
– |
fflush(stdout); |
312 |
– |
system(cmdbuf); |
313 |
– |
unlink(mgftemp); /* clean up */ |
314 |
– |
return(1); |
315 |
– |
} |
316 |
– |
|
317 |
– |
|
318 |
– |
int |
212 |
|
my_default( /* default illum action */ |
213 |
|
OBJREC *ob, |
214 |
|
struct illum_args *il, |
237 |
|
FVECT dn, org, dir; |
238 |
|
FVECT u, v; |
239 |
|
double ur[2], vr[2]; |
240 |
+ |
double epsilon; |
241 |
|
MAT4 xfm; |
242 |
|
char xfrot[64]; |
243 |
|
int nallow; |
250 |
|
return(my_default(ob, il, nm)); |
251 |
|
} |
252 |
|
/* set up sampling */ |
253 |
< |
if (il->sd != NULL) { |
254 |
< |
if (!getBSDF_xfm(xfm, fa->norm, il->udir, xfrot)) { |
361 |
< |
objerror(ob, WARNING, "illegal up direction"); |
362 |
< |
freeface(ob); |
363 |
< |
return(my_default(ob, il, nm)); |
364 |
< |
} |
365 |
< |
n = il->sd->ninc; |
253 |
> |
if (il->sampdens <= 0) { |
254 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
255 |
|
} else { |
256 |
< |
if (il->sampdens <= 0) { |
257 |
< |
nalt = nazi = 1; /* diffuse assumption */ |
258 |
< |
} else { |
370 |
< |
n = PI * il->sampdens; |
371 |
< |
nalt = sqrt(n/PI) + .5; |
372 |
< |
nazi = PI*nalt + .5; |
373 |
< |
} |
374 |
< |
n = nazi*nalt; |
256 |
> |
n = PI * il->sampdens; |
257 |
> |
nalt = sqrt(n/PI) + .5; |
258 |
> |
nazi = PI*nalt + .5; |
259 |
|
} |
260 |
+ |
n = nazi*nalt; |
261 |
|
newdist(n); |
262 |
|
/* take first edge >= sqrt(area) */ |
263 |
|
for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) { |
284 |
|
if (r2 < vr[0]) vr[0] = r2; |
285 |
|
if (r2 > vr[1]) vr[1] = r2; |
286 |
|
} |
402 |
– |
/* output detailed geometry? */ |
403 |
– |
if (!(il->flags & IL_LIGHT) && il->sd != NULL && il->sd->mgf != NULL && |
404 |
– |
il->thick <= FTINY) { |
405 |
– |
for (j = 3; j--; ) |
406 |
– |
org[j] = .5*(ur[0]+ur[1])*u[j] + |
407 |
– |
.5*(vr[0]+vr[1])*v[j] + |
408 |
– |
fa->offset*fa->norm[j]; |
409 |
– |
printgeom(il->sd, xfrot, org, ur[1]-ur[0], vr[1]-vr[0]); |
410 |
– |
} |
287 |
|
dim[0] = random(); |
288 |
|
/* sample polygon */ |
289 |
|
nallow = 5*n*il->nsamps; |
290 |
+ |
epsilon = R_EPS*sqrt(fa->area); |
291 |
|
for (dim[1] = 0; dim[1] < n; dim[1]++) |
292 |
|
for (i = 0; i < il->nsamps; i++) { |
293 |
|
/* randomize direction */ |
294 |
|
h = ilhash(dim, 2) + i; |
295 |
< |
if (il->sd != NULL) { |
296 |
< |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
297 |
< |
} else { |
298 |
< |
multisamp(sp, 2, urand(h)); |
299 |
< |
alti = dim[1]/nazi; |
300 |
< |
r1 = (alti + sp[0])/nalt; |
424 |
< |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
425 |
< |
flatdir(dn, r1, r2); |
426 |
< |
for (j = 0; j < 3; j++) |
295 |
> |
multisamp(sp, 2, urand(h)); |
296 |
> |
alti = dim[1]/nazi; |
297 |
> |
r1 = (alti + sp[0])/nalt; |
298 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
299 |
> |
flatdir(dn, r1, r2); |
300 |
> |
for (j = 0; j < 3; j++) |
301 |
|
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - |
302 |
|
dn[2]*fa->norm[j]; |
429 |
– |
} |
303 |
|
/* randomize location */ |
304 |
|
do { |
305 |
|
multisamp(sp, 2, urand(h+4862+nallow)); |
315 |
|
freeface(ob); |
316 |
|
return(my_default(ob, il, nm)); |
317 |
|
} |
318 |
< |
if (il->sd != NULL && DOT(dir, fa->norm) < -FTINY) |
446 |
< |
r1 = -1.0001*il->thick - 5.*FTINY; |
447 |
< |
else |
448 |
< |
r1 = 5.*FTINY; |
449 |
< |
for (j = 0; j < 3; j++) |
450 |
< |
org[j] += r1*fa->norm[j]; |
318 |
> |
VSUM(org, org, dir, -epsilon); |
319 |
|
/* send sample */ |
320 |
|
raysamp(dim[1], org, dir); |
321 |
|
} |
322 |
|
/* add in direct component? */ |
323 |
< |
if (il->flags & IL_LIGHT || il->sd != NULL) { |
323 |
> |
if (il->flags & IL_LIGHT) { |
324 |
|
MAT4 ixfm; |
325 |
< |
if (il->sd == NULL) { |
326 |
< |
for (i = 3; i--; ) { |
327 |
< |
ixfm[i][0] = u[i]; |
328 |
< |
ixfm[i][1] = v[i]; |
329 |
< |
ixfm[i][2] = fa->norm[i]; |
462 |
< |
ixfm[i][3] = 0.; |
463 |
< |
} |
464 |
< |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
465 |
< |
ixfm[3][3] = 1.; |
466 |
< |
} else { |
467 |
< |
if (!invmat4(ixfm, xfm)) |
468 |
< |
objerror(ob, INTERNAL, |
469 |
< |
"cannot invert BSDF transform"); |
470 |
< |
if (!(il->flags & IL_LIGHT)) |
471 |
< |
new_discount(); |
325 |
> |
for (i = 3; i--; ) { |
326 |
> |
ixfm[i][0] = u[i]; |
327 |
> |
ixfm[i][1] = v[i]; |
328 |
> |
ixfm[i][2] = fa->norm[i]; |
329 |
> |
ixfm[i][3] = 0.; |
330 |
|
} |
331 |
+ |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
332 |
+ |
ixfm[3][3] = 1.; |
333 |
|
dim[0] = random(); |
334 |
|
nallow = 10*il->nsamps; |
335 |
|
for (i = 0; i < il->nsamps; i++) { |
350 |
|
return(my_default(ob, il, nm)); |
351 |
|
} |
352 |
|
/* sample source rays */ |
353 |
< |
srcsamps(il, org, fa->norm, ixfm); |
353 |
> |
srcsamps(il, org, epsilon, ixfm); |
354 |
|
} |
355 |
|
} |
356 |
|
/* wait for all rays to finish */ |
357 |
|
rayclean(); |
498 |
– |
if (il->sd != NULL) { /* run distribution through BSDF */ |
499 |
– |
nalt = sqrt(il->sd->nout/PI) + .5; |
500 |
– |
nazi = PI*nalt + .5; |
501 |
– |
redistribute(il->sd, nalt, nazi, u, v, fa->norm, xfm); |
502 |
– |
done_discount(); |
503 |
– |
if (!il->sampdens) |
504 |
– |
il->sampdens = nalt*nazi/PI + .999; |
505 |
– |
} |
358 |
|
/* write out the face and its distribution */ |
359 |
|
if (average(il, distarr, n)) { |
360 |
|
if (il->sampdens > 0) |
370 |
|
|
371 |
|
int |
372 |
|
my_sphere( /* make an illum sphere */ |
373 |
< |
register OBJREC *ob, |
373 |
> |
OBJREC *ob, |
374 |
|
struct illum_args *il, |
375 |
|
char *nm |
376 |
|
) |
380 |
|
double sp[4], r1, r2, r3; |
381 |
|
FVECT org, dir; |
382 |
|
FVECT u, v; |
383 |
< |
register int i, j; |
383 |
> |
int i, j; |
384 |
|
/* check arguments */ |
385 |
|
if (ob->oargs.nfargs != 4) |
386 |
|
objerror(ob, USER, "bad # of arguments"); |
392 |
|
nalt = sqrt(2./PI*n) + .5; |
393 |
|
nazi = PI/2.*nalt + .5; |
394 |
|
} |
543 |
– |
if (il->sd != NULL) |
544 |
– |
objerror(ob, WARNING, "BSDF ignored"); |
395 |
|
n = nalt*nazi; |
396 |
|
newdist(n); |
397 |
|
dim[0] = random(); |
446 |
|
int dim[2]; |
447 |
|
int n, nalt, nazi, alti; |
448 |
|
double sp[2], r1, r2, r3; |
449 |
+ |
double epsilon; |
450 |
|
int h; |
451 |
|
FVECT dn, org, dir; |
452 |
|
FVECT u, v; |
453 |
|
MAT4 xfm; |
454 |
|
CONE *co; |
455 |
|
int i, j; |
456 |
< |
/* get/check arguments */ |
456 |
> |
/* get/check arguments */ |
457 |
|
co = getcone(ob, 0); |
458 |
< |
/* set up sampling */ |
459 |
< |
if (il->sd != NULL) { |
460 |
< |
if (!getBSDF_xfm(xfm, co->ad, il->udir, NULL)) { |
461 |
< |
objerror(ob, WARNING, "illegal up direction"); |
462 |
< |
freecone(ob); |
612 |
< |
return(my_default(ob, il, nm)); |
613 |
< |
} |
614 |
< |
n = il->sd->ninc; |
458 |
> |
if (co == NULL) |
459 |
> |
objerror(ob, USER, "cannot create illum"); |
460 |
> |
/* set up sampling */ |
461 |
> |
if (il->sampdens <= 0) { |
462 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
463 |
|
} else { |
464 |
< |
if (il->sampdens <= 0) { |
465 |
< |
nalt = nazi = 1; /* diffuse assumption */ |
466 |
< |
} else { |
619 |
< |
n = PI * il->sampdens; |
620 |
< |
nalt = sqrt(n/PI) + .5; |
621 |
< |
nazi = PI*nalt + .5; |
622 |
< |
} |
623 |
< |
n = nazi*nalt; |
464 |
> |
n = PI * il->sampdens; |
465 |
> |
nalt = sqrt(n/PI) + .5; |
466 |
> |
nazi = PI*nalt + .5; |
467 |
|
} |
468 |
+ |
epsilon = R_EPS*CO_R1(co); |
469 |
+ |
n = nazi*nalt; |
470 |
|
newdist(n); |
471 |
|
mkaxes(u, v, co->ad); |
472 |
|
dim[0] = random(); |
476 |
|
/* next sample point */ |
477 |
|
h = ilhash(dim,2) + i; |
478 |
|
/* randomize direction */ |
479 |
< |
if (il->sd != NULL) { |
480 |
< |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
481 |
< |
} else { |
482 |
< |
multisamp(sp, 2, urand(h)); |
483 |
< |
alti = dim[1]/nazi; |
484 |
< |
r1 = (alti + sp[0])/nalt; |
485 |
< |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
641 |
< |
flatdir(dn, r1, r2); |
642 |
< |
for (j = 0; j < 3; j++) |
643 |
< |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
644 |
< |
} |
479 |
> |
multisamp(sp, 2, urand(h)); |
480 |
> |
alti = dim[1]/nazi; |
481 |
> |
r1 = (alti + sp[0])/nalt; |
482 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
483 |
> |
flatdir(dn, r1, r2); |
484 |
> |
for (j = 0; j < 3; j++) |
485 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
486 |
|
/* randomize location */ |
487 |
|
multisamp(sp, 2, urand(h+8371)); |
488 |
|
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
490 |
|
r2 = 2.*PI*sp[1]; |
491 |
|
r1 = r3*cos(r2); |
492 |
|
r2 = r3*sin(r2); |
652 |
– |
if (il->sd != NULL && DOT(dir, co->ad) < -FTINY) |
653 |
– |
r3 = -1.0001*il->thick - 5.*FTINY; |
654 |
– |
else |
655 |
– |
r3 = 5.*FTINY; |
493 |
|
for (j = 0; j < 3; j++) |
494 |
|
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + |
495 |
< |
r3*co->ad[j]; |
495 |
> |
epsilon*co->ad[j]; |
496 |
|
/* send sample */ |
497 |
|
raysamp(dim[1], org, dir); |
498 |
|
} |
499 |
|
/* add in direct component? */ |
500 |
< |
if (il->flags & IL_LIGHT || il->sd != NULL) { |
500 |
> |
if (il->flags & IL_LIGHT) { |
501 |
|
MAT4 ixfm; |
502 |
< |
if (il->sd == NULL) { |
503 |
< |
for (i = 3; i--; ) { |
504 |
< |
ixfm[i][0] = u[i]; |
505 |
< |
ixfm[i][1] = v[i]; |
506 |
< |
ixfm[i][2] = co->ad[i]; |
670 |
< |
ixfm[i][3] = 0.; |
671 |
< |
} |
672 |
< |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
673 |
< |
ixfm[3][3] = 1.; |
674 |
< |
} else { |
675 |
< |
if (!invmat4(ixfm, xfm)) |
676 |
< |
objerror(ob, INTERNAL, |
677 |
< |
"cannot invert BSDF transform"); |
678 |
< |
if (!(il->flags & IL_LIGHT)) |
679 |
< |
new_discount(); |
502 |
> |
for (i = 3; i--; ) { |
503 |
> |
ixfm[i][0] = u[i]; |
504 |
> |
ixfm[i][1] = v[i]; |
505 |
> |
ixfm[i][2] = co->ad[i]; |
506 |
> |
ixfm[i][3] = 0.; |
507 |
|
} |
508 |
+ |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
509 |
+ |
ixfm[3][3] = 1.; |
510 |
|
dim[0] = random(); |
511 |
|
for (i = 0; i < il->nsamps; i++) { |
512 |
|
/* randomize location */ |
520 |
|
for (j = 0; j < 3; j++) |
521 |
|
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j]; |
522 |
|
/* sample source rays */ |
523 |
< |
srcsamps(il, org, co->ad, ixfm); |
523 |
> |
srcsamps(il, org, epsilon, ixfm); |
524 |
|
} |
525 |
|
} |
526 |
|
/* wait for all rays to finish */ |
527 |
|
rayclean(); |
699 |
– |
if (il->sd != NULL) { /* run distribution through BSDF */ |
700 |
– |
nalt = sqrt(il->sd->nout/PI) + .5; |
701 |
– |
nazi = PI*nalt + .5; |
702 |
– |
redistribute(il->sd, nalt, nazi, u, v, co->ad, xfm); |
703 |
– |
done_discount(); |
704 |
– |
if (!il->sampdens) |
705 |
– |
il->sampdens = nalt*nazi/PI + .999; |
706 |
– |
} |
528 |
|
/* write out the ring and its distribution */ |
529 |
|
if (average(il, distarr, n)) { |
530 |
|
if (il->sampdens > 0) |
535 |
|
/* clean up */ |
536 |
|
freecone(ob); |
537 |
|
return(1); |
717 |
– |
} |
718 |
– |
|
719 |
– |
|
720 |
– |
void |
721 |
– |
redistribute( /* pass distarr ray sums through BSDF */ |
722 |
– |
struct BSDF_data *b, |
723 |
– |
int nalt, |
724 |
– |
int nazi, |
725 |
– |
FVECT u, |
726 |
– |
FVECT v, |
727 |
– |
FVECT w, |
728 |
– |
MAT4 xm |
729 |
– |
) |
730 |
– |
{ |
731 |
– |
int nout = 0; |
732 |
– |
MAT4 mymat, inmat; |
733 |
– |
COLORV *idist; |
734 |
– |
COLORV *cp; |
735 |
– |
FVECT dv; |
736 |
– |
double wt; |
737 |
– |
int i, j, k, c, o; |
738 |
– |
COLOR col, cinc; |
739 |
– |
/* copy incoming distribution */ |
740 |
– |
if (b->ninc > distsiz) |
741 |
– |
error(INTERNAL, "error 1 in redistribute"); |
742 |
– |
idist = (COLORV *)malloc(sizeof(COLOR)*b->ninc); |
743 |
– |
if (idist == NULL) |
744 |
– |
error(SYSTEM, "out of memory in redistribute"); |
745 |
– |
memcpy(idist, distarr, sizeof(COLOR)*b->ninc); |
746 |
– |
/* compose direction transform */ |
747 |
– |
for (i = 3; i--; ) { |
748 |
– |
mymat[i][0] = u[i]; |
749 |
– |
mymat[i][1] = v[i]; |
750 |
– |
mymat[i][2] = w[i]; |
751 |
– |
mymat[i][3] = 0.; |
752 |
– |
} |
753 |
– |
mymat[3][0] = mymat[3][1] = mymat[3][2] = 0.; |
754 |
– |
mymat[3][3] = 1.; |
755 |
– |
if (xm != NULL) |
756 |
– |
multmat4(mymat, xm, mymat); |
757 |
– |
for (i = 3; i--; ) { /* make sure it's normalized */ |
758 |
– |
wt = 1./sqrt( mymat[0][i]*mymat[0][i] + |
759 |
– |
mymat[1][i]*mymat[1][i] + |
760 |
– |
mymat[2][i]*mymat[2][i] ); |
761 |
– |
for (j = 3; j--; ) |
762 |
– |
mymat[j][i] *= wt; |
763 |
– |
} |
764 |
– |
if (!invmat4(inmat, mymat)) /* need inverse as well */ |
765 |
– |
error(INTERNAL, "cannot invert BSDF transform"); |
766 |
– |
newdist(nalt*nazi); /* resample distribution */ |
767 |
– |
for (i = b->ninc; i--; ) { |
768 |
– |
int direct_out = -1; |
769 |
– |
COLOR cdir; |
770 |
– |
getBSDF_incvec(dv, b, i); /* compute incident irrad. */ |
771 |
– |
multv3(dv, dv, mymat); |
772 |
– |
if (dv[2] < 0.0) { |
773 |
– |
dv[0] = -dv[0]; dv[1] = -dv[1]; dv[2] = -dv[2]; |
774 |
– |
direct_out += (direct_discount != NULL); |
775 |
– |
} |
776 |
– |
wt = getBSDF_incohm(b, i); |
777 |
– |
wt *= dv[2]; /* solid_angle*cosine(theta) */ |
778 |
– |
cp = &idist[3*i]; |
779 |
– |
copycolor(cinc, cp); |
780 |
– |
scalecolor(cinc, wt); |
781 |
– |
if (!direct_out) { /* discount direct contr. */ |
782 |
– |
cp = &direct_discount[3*i]; |
783 |
– |
copycolor(cdir, cp); |
784 |
– |
scalecolor(cdir, -wt); |
785 |
– |
if (b->nout != b->ninc) |
786 |
– |
direct_out = flatindex(dv, nalt, nazi); |
787 |
– |
else |
788 |
– |
direct_out = i; /* assumes dist. mirroring */ |
789 |
– |
} |
790 |
– |
for (k = nalt; k--; ) /* loop over distribution */ |
791 |
– |
for (j = nazi; j--; ) { |
792 |
– |
int rstart = random(); |
793 |
– |
for (c = NBSDFSAMPS; c--; ) { |
794 |
– |
double sp[2]; |
795 |
– |
multisamp(sp, 2, urand(rstart+c)); |
796 |
– |
flatdir(dv, (k + sp[0])/nalt, |
797 |
– |
(j + .5 - sp[1])/nazi); |
798 |
– |
multv3(dv, dv, inmat); |
799 |
– |
/* evaluate BSDF @ outgoing */ |
800 |
– |
o = getBSDF_outndx(b, dv); |
801 |
– |
if (o < 0) { |
802 |
– |
nout++; |
803 |
– |
continue; |
804 |
– |
} |
805 |
– |
wt = BSDF_value(b, i, o) * (1./NBSDFSAMPS); |
806 |
– |
copycolor(col, cinc); |
807 |
– |
if (b->nout != b->ninc) |
808 |
– |
o = k*nazi + j; |
809 |
– |
if (o == direct_out) |
810 |
– |
addcolor(col, cdir); /* minus direct */ |
811 |
– |
scalecolor(col, wt); |
812 |
– |
cp = &distarr[3*(k*nazi + j)]; |
813 |
– |
addcolor(cp, col); /* sum into distribution */ |
814 |
– |
} |
815 |
– |
} |
816 |
– |
} |
817 |
– |
free(idist); /* free temp space */ |
818 |
– |
if (nout) { |
819 |
– |
sprintf(errmsg, "missing %.1f%% of BSDF directions", |
820 |
– |
100.*nout/(b->ninc*nalt*nazi*NBSDFSAMPS)); |
821 |
– |
error(WARNING, errmsg); |
822 |
– |
} |
538 |
|
} |