ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.14
Committed: Sun Aug 21 22:38:12 2011 UTC (12 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.13: +14 -13 lines
Log Message:
Minor improvements to direct specular sampling

File Contents

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