ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.30
Committed: Wed Sep 2 18:59:01 2015 UTC (8 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R0
Changes since 2.29: +8 -1 lines
Log Message:
Had to reinstate ambRayInPmap() macro to avoid over-counting bug

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: m_bsdf.c,v 2.29 2015/08/01 23:27:04 greg Exp $";
3 #endif
4 /*
5 * Shading for materials with BSDFs taken from XML data files
6 */
7
8 #include "copyright.h"
9
10 #include "ray.h"
11 #include "ambient.h"
12 #include "source.h"
13 #include "func.h"
14 #include "bsdf.h"
15 #include "random.h"
16 #include "pmapmat.h"
17
18 /*
19 * Arguments to this material include optional diffuse colors.
20 * String arguments include the BSDF and function files.
21 * A non-zero thickness causes the strange but useful behavior
22 * of translating transmitted rays this distance beneath the surface
23 * (opposite the surface normal) to bypass any intervening geometry.
24 * Translation only affects scattered, non-source-directed samples.
25 * A non-zero thickness has the further side-effect that an unscattered
26 * (view) ray will pass right through our material if it has any
27 * non-diffuse transmission, making the BSDF surface invisible. This
28 * shows the proxied geometry instead. Thickness has the further
29 * effect of turning off reflection on the hidden side so that rays
30 * heading in the opposite direction pass unimpeded through the BSDF
31 * surface. A paired surface may be placed on the opposide side of
32 * the detail geometry, less than this thickness away, if a two-way
33 * proxy is desired. Note that the sign of the thickness is important.
34 * A positive thickness hides geometry behind the BSDF surface and uses
35 * front reflectance and transmission properties. A negative thickness
36 * hides geometry in front of the surface when rays hit from behind,
37 * and applies only the transmission and backside reflectance properties.
38 * Reflection is ignored on the hidden side, as those rays pass through.
39 * The "up" vector for the BSDF is given by three variables, defined
40 * (along with the thickness) by the named function file, or '.' if none.
41 * Together with the surface normal, this defines the local coordinate
42 * system for the BSDF.
43 * We do not reorient the surface, so if the BSDF has no back-side
44 * reflectance and none is given in the real arguments, a BSDF surface
45 * with zero thickness will appear black when viewed from behind
46 * unless backface visibility is off.
47 * The diffuse arguments are added to components in the BSDF file,
48 * not multiplied. However, patterns affect this material as a multiplier
49 * on everything except non-diffuse reflection.
50 *
51 * Arguments for MAT_BSDF are:
52 * 6+ thick BSDFfile ux uy uz funcfile transform
53 * 0
54 * 0|3|6|9 rdf gdf bdf
55 * rdb gdb bdb
56 * rdt gdt bdt
57 */
58
59 /*
60 * Note that our reverse ray-tracing process means that the positions
61 * of incoming and outgoing vectors may be reversed in our calls
62 * to the BSDF library. This is fine, since the bidirectional nature
63 * of the BSDF (that's what the 'B' stands for) means it all works out.
64 */
65
66 typedef struct {
67 OBJREC *mp; /* material pointer */
68 RAY *pr; /* intersected ray */
69 FVECT pnorm; /* perturbed surface normal */
70 FVECT vray; /* local outgoing (return) vector */
71 double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */
72 RREAL toloc[3][3]; /* world to local BSDF coords */
73 RREAL fromloc[3][3]; /* local BSDF coords to world */
74 double thick; /* surface thickness */
75 SDData *sd; /* loaded BSDF data */
76 COLOR runsamp; /* BSDF hemispherical reflection */
77 COLOR rdiff; /* added diffuse reflection */
78 COLOR tunsamp; /* BSDF hemispherical transmission */
79 COLOR tdiff; /* added diffuse transmission */
80 } BSDFDAT; /* BSDF material data */
81
82 #define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
83
84 /* Jitter ray sample according to projected solid angle and specjitter */
85 static void
86 bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
87 {
88 VCOPY(vres, ndp->vray);
89 if (specjitter < 1.)
90 sr_psa *= specjitter;
91 if (sr_psa <= FTINY)
92 return;
93 vres[0] += sr_psa*(.5 - frandom());
94 vres[1] += sr_psa*(.5 - frandom());
95 normalize(vres);
96 }
97
98 /* Evaluate BSDF for direct component, returning true if OK to proceed */
99 static int
100 direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
101 {
102 int nsamp, ok = 0;
103 FVECT vsrc, vsmp, vjit;
104 double tomega;
105 double sf, tsr, sd[2];
106 COLOR csmp;
107 SDValue sv;
108 SDError ec;
109 int i;
110 /* transform source direction */
111 if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
112 return(0);
113 /* assign number of samples */
114 ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
115 if (ec)
116 goto baderror;
117 /* check indirect over-counting */
118 if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
119 && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
120 double dx = vsrc[0] + ndp->vray[0];
121 double dy = vsrc[1] + ndp->vray[1];
122 if (dx*dx + dy*dy <= omega+tomega)
123 return(0);
124 }
125 sf = specjitter * ndp->pr->rweight;
126 if (tomega <= .0)
127 nsamp = 1;
128 else if (25.*tomega <= omega)
129 nsamp = 100.*sf + .5;
130 else
131 nsamp = 4.*sf*omega/tomega + .5;
132 nsamp += !nsamp;
133 setcolor(cval, .0, .0, .0); /* sample our source area */
134 sf = sqrt(omega);
135 tsr = sqrt(tomega);
136 for (i = nsamp; i--; ) {
137 VCOPY(vsmp, vsrc); /* jitter query directions */
138 if (nsamp > 1) {
139 multisamp(sd, 2, (i + frandom())/(double)nsamp);
140 vsmp[0] += (sd[0] - .5)*sf;
141 vsmp[1] += (sd[1] - .5)*sf;
142 if (normalize(vsmp) == 0) {
143 --nsamp;
144 continue;
145 }
146 }
147 bsdf_jitter(vjit, ndp, tsr);
148 /* compute BSDF */
149 ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
150 if (ec)
151 goto baderror;
152 if (sv.cieY <= FTINY) /* worth using? */
153 continue;
154 cvt_sdcolor(csmp, &sv);
155 addcolor(cval, csmp); /* average it in */
156 ++ok;
157 }
158 sf = 1./(double)nsamp;
159 scalecolor(cval, sf);
160 return(ok);
161 baderror:
162 objerror(ndp->mp, USER, transSDError(ec));
163 return(0); /* gratis return */
164 }
165
166 /* Compute source contribution for BSDF (reflected & transmitted) */
167 static void
168 dir_bsdf(
169 COLOR cval, /* returned coefficient */
170 void *nnp, /* material data */
171 FVECT ldir, /* light source direction */
172 double omega /* light source size */
173 )
174 {
175 BSDFDAT *np = (BSDFDAT *)nnp;
176 double ldot;
177 double dtmp;
178 COLOR ctmp;
179
180 setcolor(cval, .0, .0, .0);
181
182 ldot = DOT(np->pnorm, ldir);
183 if ((-FTINY <= ldot) & (ldot <= FTINY))
184 return;
185
186 if (ldot > 0 && bright(np->rdiff) > FTINY) {
187 /*
188 * Compute added diffuse reflected component.
189 */
190 copycolor(ctmp, np->rdiff);
191 dtmp = ldot * omega * (1./PI);
192 scalecolor(ctmp, dtmp);
193 addcolor(cval, ctmp);
194 }
195 if (ldot < 0 && bright(np->tdiff) > FTINY) {
196 /*
197 * Compute added diffuse transmission.
198 */
199 copycolor(ctmp, np->tdiff);
200 dtmp = -ldot * omega * (1.0/PI);
201 scalecolor(ctmp, dtmp);
202 addcolor(cval, ctmp);
203 }
204 if (ambRayInPmap(np->pr))
205 return; /* specular already in photon map */
206 /*
207 * Compute scattering coefficient using BSDF.
208 */
209 if (!direct_bsdf_OK(ctmp, ldir, omega, np))
210 return;
211 if (ldot > 0) { /* pattern only diffuse reflection */
212 COLOR ctmp1, ctmp2;
213 dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
214 : np->sd->rLambBack.cieY;
215 /* diffuse fraction */
216 dtmp /= PI * bright(ctmp);
217 copycolor(ctmp2, np->pr->pcol);
218 scalecolor(ctmp2, dtmp);
219 setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
220 addcolor(ctmp1, ctmp2);
221 multcolor(ctmp, ctmp1); /* apply derated pattern */
222 dtmp = ldot * omega;
223 } else { /* full pattern on transmission */
224 multcolor(ctmp, np->pr->pcol);
225 dtmp = -ldot * omega;
226 }
227 scalecolor(ctmp, dtmp);
228 addcolor(cval, ctmp);
229 }
230
231 /* Compute source contribution for BSDF (reflected only) */
232 static void
233 dir_brdf(
234 COLOR cval, /* returned coefficient */
235 void *nnp, /* material data */
236 FVECT ldir, /* light source direction */
237 double omega /* light source size */
238 )
239 {
240 BSDFDAT *np = (BSDFDAT *)nnp;
241 double ldot;
242 double dtmp;
243 COLOR ctmp, ctmp1, ctmp2;
244
245 setcolor(cval, .0, .0, .0);
246
247 ldot = DOT(np->pnorm, ldir);
248
249 if (ldot <= FTINY)
250 return;
251
252 if (bright(np->rdiff) > FTINY) {
253 /*
254 * Compute added diffuse reflected component.
255 */
256 copycolor(ctmp, np->rdiff);
257 dtmp = ldot * omega * (1./PI);
258 scalecolor(ctmp, dtmp);
259 addcolor(cval, ctmp);
260 }
261 if (ambRayInPmap(np->pr))
262 return; /* specular already in photon map */
263 /*
264 * Compute reflection coefficient using BSDF.
265 */
266 if (!direct_bsdf_OK(ctmp, ldir, omega, np))
267 return;
268 /* pattern only diffuse reflection */
269 dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
270 : np->sd->rLambBack.cieY;
271 dtmp /= PI * bright(ctmp); /* diffuse fraction */
272 copycolor(ctmp2, np->pr->pcol);
273 scalecolor(ctmp2, dtmp);
274 setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
275 addcolor(ctmp1, ctmp2);
276 multcolor(ctmp, ctmp1); /* apply derated pattern */
277 dtmp = ldot * omega;
278 scalecolor(ctmp, dtmp);
279 addcolor(cval, ctmp);
280 }
281
282 /* Compute source contribution for BSDF (transmitted only) */
283 static void
284 dir_btdf(
285 COLOR cval, /* returned coefficient */
286 void *nnp, /* material data */
287 FVECT ldir, /* light source direction */
288 double omega /* light source size */
289 )
290 {
291 BSDFDAT *np = (BSDFDAT *)nnp;
292 double ldot;
293 double dtmp;
294 COLOR ctmp;
295
296 setcolor(cval, .0, .0, .0);
297
298 ldot = DOT(np->pnorm, ldir);
299
300 if (ldot >= -FTINY)
301 return;
302
303 if (bright(np->tdiff) > FTINY) {
304 /*
305 * Compute added diffuse transmission.
306 */
307 copycolor(ctmp, np->tdiff);
308 dtmp = -ldot * omega * (1.0/PI);
309 scalecolor(ctmp, dtmp);
310 addcolor(cval, ctmp);
311 }
312 if (ambRayInPmap(np->pr))
313 return; /* specular already in photon map */
314 /*
315 * Compute scattering coefficient using BSDF.
316 */
317 if (!direct_bsdf_OK(ctmp, ldir, omega, np))
318 return;
319 /* full pattern on transmission */
320 multcolor(ctmp, np->pr->pcol);
321 dtmp = -ldot * omega;
322 scalecolor(ctmp, dtmp);
323 addcolor(cval, ctmp);
324 }
325
326 /* Sample separate BSDF component */
327 static int
328 sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
329 {
330 int nstarget = 1;
331 int nsent;
332 SDError ec;
333 SDValue bsv;
334 double xrand;
335 FVECT vsmp;
336 RAY sr;
337 /* multiple samples? */
338 if (specjitter > 1.5) {
339 nstarget = specjitter*ndp->pr->rweight + .5;
340 nstarget += !nstarget;
341 }
342 /* run through our samples */
343 for (nsent = 0; nsent < nstarget; nsent++) {
344 if (nstarget == 1) { /* stratify random variable */
345 xrand = urand(ilhash(dimlist,ndims)+samplendx);
346 if (specjitter < 1.)
347 xrand = .5 + specjitter*(xrand-.5);
348 } else {
349 xrand = (nsent + frandom())/(double)nstarget;
350 }
351 SDerrorDetail[0] = '\0'; /* sample direction & coef. */
352 bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
353 ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
354 if (ec)
355 objerror(ndp->mp, USER, transSDError(ec));
356 if (bsv.cieY <= FTINY) /* zero component? */
357 break;
358 /* map vector to world */
359 if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
360 break;
361 /* spawn a specular ray */
362 if (nstarget > 1)
363 bsv.cieY /= (double)nstarget;
364 cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */
365 if (usepat) /* apply pattern? */
366 multcolor(sr.rcoef, ndp->pr->pcol);
367 if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
368 if (maxdepth > 0)
369 break;
370 continue; /* Russian roulette victim */
371 }
372 /* need to offset origin? */
373 if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
374 VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
375 rayvalue(&sr); /* send & evaluate sample */
376 multcolor(sr.rcol, sr.rcoef);
377 addcolor(ndp->pr->rcol, sr.rcol);
378 }
379 return(nsent);
380 }
381
382 /* Sample non-diffuse components of BSDF */
383 static int
384 sample_sdf(BSDFDAT *ndp, int sflags)
385 {
386 int n, ntotal = 0;
387 SDSpectralDF *dfp;
388 COLORV *unsc;
389
390 if (sflags == SDsampSpT) {
391 unsc = ndp->tunsamp;
392 if (ndp->pr->rod > 0)
393 dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
394 else
395 dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
396 cvt_sdcolor(unsc, &ndp->sd->tLamb);
397 } else /* sflags == SDsampSpR */ {
398 unsc = ndp->runsamp;
399 if (ndp->pr->rod > 0) {
400 dfp = ndp->sd->rf;
401 cvt_sdcolor(unsc, &ndp->sd->rLambFront);
402 } else {
403 dfp = ndp->sd->rb;
404 cvt_sdcolor(unsc, &ndp->sd->rLambBack);
405 }
406 }
407 multcolor(unsc, ndp->pr->pcol);
408 if (dfp == NULL) /* no specular component? */
409 return(0);
410 /* below sampling threshold? */
411 if (dfp->maxHemi <= specthresh+FTINY) {
412 if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */
413 FVECT vjit;
414 double d;
415 COLOR ctmp;
416 bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
417 d = SDdirectHemi(vjit, sflags, ndp->sd);
418 if (sflags == SDsampSpT) {
419 copycolor(ctmp, ndp->pr->pcol);
420 scalecolor(ctmp, d);
421 } else /* no pattern on reflection */
422 setcolor(ctmp, d, d, d);
423 addcolor(unsc, ctmp);
424 }
425 return(0);
426 }
427 /* else need to sample */
428 dimlist[ndims++] = (int)(size_t)ndp->mp;
429 ndims++;
430 for (n = dfp->ncomp; n--; ) { /* loop over components */
431 dimlist[ndims-1] = n + 9438;
432 ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
433 }
434 ndims -= 2;
435 return(ntotal);
436 }
437
438 /* Color a ray that hit a BSDF material */
439 int
440 m_bsdf(OBJREC *m, RAY *r)
441 {
442 int hitfront;
443 COLOR ctmp;
444 SDError ec;
445 FVECT upvec, vtmp;
446 MFUNC *mf;
447 BSDFDAT nd;
448 /* check arguments */
449 if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
450 (m->oargs.nfargs % 3))
451 objerror(m, USER, "bad # arguments");
452 /* record surface struck */
453 hitfront = (r->rod > 0);
454 /* load cal file */
455 mf = getfunc(m, 5, 0x1d, 1);
456 setfunc(m, r);
457 /* get thickness */
458 nd.thick = evalue(mf->ep[0]);
459 if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
460 nd.thick = .0;
461 /* check shadow */
462 if (r->crtype & SHADOW) {
463 if (nd.thick != 0)
464 raytrans(r); /* pass-through */
465 return(1); /* or shadow */
466 }
467 /* check backface visibility */
468 if (!hitfront & !backvis) {
469 raytrans(r);
470 return(1);
471 }
472 /* check other rays to pass */
473 if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
474 (nd.thick > 0) ^ hitfront)) {
475 raytrans(r); /* hide our proxy */
476 return(1);
477 }
478 /* get BSDF data */
479 nd.sd = loadBSDF(m->oargs.sarg[1]);
480 /* diffuse reflectance */
481 if (hitfront) {
482 if (m->oargs.nfargs < 3)
483 setcolor(nd.rdiff, .0, .0, .0);
484 else
485 setcolor(nd.rdiff, m->oargs.farg[0],
486 m->oargs.farg[1],
487 m->oargs.farg[2]);
488 } else {
489 if (m->oargs.nfargs < 6)
490 setcolor(nd.rdiff, .0, .0, .0);
491 else
492 setcolor(nd.rdiff, m->oargs.farg[3],
493 m->oargs.farg[4],
494 m->oargs.farg[5]);
495 }
496 /* diffuse transmittance */
497 if (m->oargs.nfargs < 9)
498 setcolor(nd.tdiff, .0, .0, .0);
499 else
500 setcolor(nd.tdiff, m->oargs.farg[6],
501 m->oargs.farg[7],
502 m->oargs.farg[8]);
503 nd.mp = m;
504 nd.pr = r;
505 /* get modifiers */
506 raytexture(r, m->omod);
507 /* modify diffuse values */
508 multcolor(nd.rdiff, r->pcol);
509 multcolor(nd.tdiff, r->pcol);
510 /* get up vector */
511 upvec[0] = evalue(mf->ep[1]);
512 upvec[1] = evalue(mf->ep[2]);
513 upvec[2] = evalue(mf->ep[3]);
514 /* return to world coords */
515 if (mf->fxp != &unitxf) {
516 multv3(upvec, upvec, mf->fxp->xfm);
517 nd.thick *= mf->fxp->sca;
518 }
519 if (r->rox != NULL) {
520 multv3(upvec, upvec, r->rox->f.xfm);
521 nd.thick *= r->rox->f.sca;
522 }
523 raynormal(nd.pnorm, r);
524 /* compute local BSDF xform */
525 ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
526 if (!ec) {
527 nd.vray[0] = -r->rdir[0];
528 nd.vray[1] = -r->rdir[1];
529 nd.vray[2] = -r->rdir[2];
530 ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
531 }
532 if (!ec)
533 ec = SDinvXform(nd.fromloc, nd.toloc);
534 if (ec) {
535 objerror(m, WARNING, "Illegal orientation vector");
536 return(1);
537 }
538 /* determine BSDF resolution */
539 ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
540 if (ec)
541 objerror(m, USER, transSDError(ec));
542
543 nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
544 nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
545 if (!hitfront) { /* perturb normal towards hit */
546 nd.pnorm[0] = -nd.pnorm[0];
547 nd.pnorm[1] = -nd.pnorm[1];
548 nd.pnorm[2] = -nd.pnorm[2];
549 }
550 /* sample reflection */
551 sample_sdf(&nd, SDsampSpR);
552 /* sample transmission */
553 sample_sdf(&nd, SDsampSpT);
554 /* compute indirect diffuse */
555 copycolor(ctmp, nd.rdiff);
556 addcolor(ctmp, nd.runsamp);
557 if (bright(ctmp) > FTINY) { /* ambient from reflection */
558 if (!hitfront)
559 flipsurface(r);
560 multambient(ctmp, r, nd.pnorm);
561 addcolor(r->rcol, ctmp);
562 if (!hitfront)
563 flipsurface(r);
564 }
565 copycolor(ctmp, nd.tdiff);
566 addcolor(ctmp, nd.tunsamp);
567 if (bright(ctmp) > FTINY) { /* ambient from other side */
568 FVECT bnorm;
569 if (hitfront)
570 flipsurface(r);
571 bnorm[0] = -nd.pnorm[0];
572 bnorm[1] = -nd.pnorm[1];
573 bnorm[2] = -nd.pnorm[2];
574 if (nd.thick != 0) { /* proxy with offset? */
575 VCOPY(vtmp, r->rop);
576 VSUM(r->rop, vtmp, r->ron, nd.thick);
577 multambient(ctmp, r, bnorm);
578 VCOPY(r->rop, vtmp);
579 } else
580 multambient(ctmp, r, bnorm);
581 addcolor(r->rcol, ctmp);
582 if (hitfront)
583 flipsurface(r);
584 }
585 /* add direct component */
586 if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
587 (nd.sd->tb == NULL)) {
588 direct(r, dir_brdf, &nd); /* reflection only */
589 } else if (nd.thick == 0) {
590 direct(r, dir_bsdf, &nd); /* thin surface scattering */
591 } else {
592 direct(r, dir_brdf, &nd); /* reflection first */
593 VCOPY(vtmp, r->rop); /* offset for transmitted */
594 VSUM(r->rop, vtmp, r->ron, -nd.thick);
595 direct(r, dir_btdf, &nd); /* separate transmission */
596 VCOPY(r->rop, vtmp);
597 }
598 /* clean up */
599 SDfreeCache(nd.sd);
600 return(1);
601 }