ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.46
Committed: Mon Feb 12 18:46:29 2018 UTC (7 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.45: +8 -8 lines
Log Message:
Fixed issue with under-counting specular

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: m_bsdf.c,v 2.45 2018/01/05 02:47:46 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, making the BSDF
27 * surface invisible and showing the proxied geometry instead. Thickness
28 * has the further effect of turning off reflection on the reverse side so
29 * rays 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 * When thickness is set to zero, shadow rays will be blocked unless
39 * a BTDF has a strong "through" component in the source direction.
40 * A separate test prevents over-counting by dropping samples that are
41 * too close to this "through" direction. BSDFs with such a through direction
42 * will also have a view component, meaning they are somewhat see-through.
43 * The "up" vector for the BSDF is given by three variables, defined
44 * (along with the thickness) by the named function file, or '.' if none.
45 * Together with the surface normal, this defines the local coordinate
46 * system for the BSDF.
47 * We do not reorient the surface, so if the BSDF has no back-side
48 * reflectance and none is given in the real arguments, a BSDF surface
49 * with zero thickness will appear black when viewed from behind
50 * unless backface visibility is on, when it becomes invisible.
51 * The diffuse arguments are added to components in the BSDF file,
52 * not multiplied. However, patterns affect this material as a multiplier
53 * on everything except non-diffuse reflection.
54 *
55 * Arguments for MAT_BSDF are:
56 * 6+ thick BSDFfile ux uy uz funcfile transform
57 * 0
58 * 0|3|6|9 rdf gdf bdf
59 * rdb gdb bdb
60 * rdt gdt bdt
61 */
62
63 /*
64 * Note that our reverse ray-tracing process means that the positions
65 * of incoming and outgoing vectors may be reversed in our calls
66 * to the BSDF library. This is usually fine, since the bidirectional nature
67 * of the BSDF (that's what the 'B' stands for) means it all works out.
68 */
69
70 typedef struct {
71 OBJREC *mp; /* material pointer */
72 RAY *pr; /* intersected ray */
73 FVECT pnorm; /* perturbed surface normal */
74 FVECT vray; /* local outgoing (return) vector */
75 double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */
76 RREAL toloc[3][3]; /* world to local BSDF coords */
77 RREAL fromloc[3][3]; /* local BSDF coords to world */
78 double thick; /* surface thickness */
79 COLOR cthru; /* "through" component multiplier */
80 SDData *sd; /* loaded BSDF data */
81 COLOR rdiff; /* diffuse reflection */
82 COLOR runsamp; /* BSDF hemispherical reflection */
83 COLOR tdiff; /* diffuse transmission */
84 COLOR tunsamp; /* BSDF hemispherical transmission */
85 } BSDFDAT; /* BSDF material data */
86
87 #define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
88
89 /* Compute "through" component color */
90 static void
91 compute_through(BSDFDAT *ndp)
92 {
93 #define NDIR2CHECK 13
94 static const float dir2check[NDIR2CHECK][2] = {
95 {0, 0},
96 {-0.8, 0},
97 {0, 0.8},
98 {0, -0.8},
99 {0.8, 0},
100 {-0.8, 0.8},
101 {-0.8, -0.8},
102 {0.8, 0.8},
103 {0.8, -0.8},
104 {-1.6, 0},
105 {0, 1.6},
106 {0, -1.6},
107 {1.6, 0},
108 };
109 const double peak_over = 1.5;
110 SDSpectralDF *dfp;
111 FVECT pdir;
112 double tomega, srchrad;
113 COLOR vpeak, vsum;
114 int i;
115 SDError ec;
116
117 setcolor(ndp->cthru, 0, 0, 0); /* starting assumption */
118
119 if (ndp->pr->crtype & (SPECULAR|AMBIENT) && !(ndp->pr->crtype & SHADOW))
120 return; /* no need for through comp. */
121
122 if (ndp->pr->rod > 0)
123 dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
124 else
125 dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
126
127 if (dfp == NULL)
128 return; /* no specular transmission */
129 if (bright(ndp->pr->pcol) <= FTINY)
130 return; /* pattern is black, here */
131 srchrad = sqrt(dfp->minProjSA); /* else search for peak */
132 setcolor(vpeak, 0, 0, 0);
133 setcolor(vsum, 0, 0, 0);
134 pdir[2] = 0.0;
135 for (i = 0; i < NDIR2CHECK; i++) {
136 FVECT tdir;
137 SDValue sv;
138 COLOR vcol;
139 tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
140 tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
141 tdir[2] = -ndp->vray[2];
142 normalize(tdir);
143 ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
144 if (ec)
145 goto baderror;
146 cvt_sdcolor(vcol, &sv);
147 addcolor(vsum, vcol);
148 if (sv.cieY > bright(vpeak)) {
149 copycolor(vpeak, vcol);
150 VCOPY(pdir, tdir);
151 }
152 }
153 if (pdir[2] == 0.0)
154 return; /* zero neighborhood */
155 ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
156 if (ec)
157 goto baderror;
158 if (tomega > 1.5*dfp->minProjSA)
159 return; /* not really a peak? */
160 tomega /= fabs(pdir[2]); /* remove cosine factor */
161 if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
162 return; /* < 0.1% transmission */
163 for (i = 3; i--; ) /* remove peak from average */
164 colval(vsum,i) -= colval(vpeak,i);
165 if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
166 return; /* not peaky enough */
167 copycolor(ndp->cthru, vpeak); /* else use it */
168 scalecolor(ndp->cthru, tomega);
169 multcolor(ndp->cthru, ndp->pr->pcol); /* modify by pattern */
170 return;
171 baderror:
172 objerror(ndp->mp, USER, transSDError(ec));
173 #undef NDIR2CHECK
174 }
175
176 /* Jitter ray sample according to projected solid angle and specjitter */
177 static void
178 bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
179 {
180 VCOPY(vres, ndp->vray);
181 if (specjitter < 1.)
182 sr_psa *= specjitter;
183 if (sr_psa <= FTINY)
184 return;
185 vres[0] += sr_psa*(.5 - frandom());
186 vres[1] += sr_psa*(.5 - frandom());
187 normalize(vres);
188 }
189
190 /* Get BSDF specular for direct component, returning true if OK to proceed */
191 static int
192 direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
193 {
194 int nsamp;
195 double wtot = 0;
196 FVECT vsrc, vsmp, vjit;
197 double tomega, tomega2;
198 double sf, tsr, sd[2];
199 COLOR csmp, cdiff;
200 double diffY;
201 SDValue sv;
202 SDError ec;
203 int i;
204 /* in case we fail */
205 setcolor(cval, 0, 0, 0);
206 /* transform source direction */
207 if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
208 return(0);
209 /* will discount diffuse portion */
210 switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
211 case 3:
212 if (ndp->sd->rf == NULL)
213 return(0); /* all diffuse */
214 sv = ndp->sd->rLambFront;
215 break;
216 case 0:
217 if (ndp->sd->rb == NULL)
218 return(0); /* all diffuse */
219 sv = ndp->sd->rLambBack;
220 break;
221 default:
222 if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
223 return(0); /* all diffuse */
224 sv = ndp->sd->tLamb;
225 break;
226 }
227 if (sv.cieY > FTINY) {
228 diffY = sv.cieY *= 1./PI;
229 cvt_sdcolor(cdiff, &sv);
230 } else {
231 diffY = 0;
232 setcolor(cdiff, 0, 0, 0);
233 }
234 /* need projected solid angles */
235 omega *= fabs(vsrc[2]);
236 ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
237 if (ec)
238 goto baderror;
239 /* check indirect over-counting */
240 if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
241 double dx = vsrc[0] + ndp->vray[0];
242 double dy = vsrc[1] + ndp->vray[1];
243 if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
244 2.*sqrt(omega*tomega)))
245 return(0);
246 }
247 /* assign number of samples */
248 sf = specjitter * ndp->pr->rweight;
249 if (tomega <= 0)
250 nsamp = 1;
251 else if (25.*tomega <= omega)
252 nsamp = 100.*sf + .5;
253 else
254 nsamp = 4.*sf*omega/tomega + .5;
255 nsamp += !nsamp;
256 sf = sqrt(omega); /* sample our source area */
257 tsr = sqrt(tomega);
258 for (i = nsamp; i--; ) {
259 VCOPY(vsmp, vsrc); /* jitter query directions */
260 if (nsamp > 1) {
261 multisamp(sd, 2, (i + frandom())/(double)nsamp);
262 vsmp[0] += (sd[0] - .5)*sf;
263 vsmp[1] += (sd[1] - .5)*sf;
264 normalize(vsmp);
265 }
266 bsdf_jitter(vjit, ndp, tsr);
267 /* compute BSDF */
268 ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
269 if (ec)
270 goto baderror;
271 if (sv.cieY - diffY <= FTINY)
272 continue; /* no specular part */
273 /* check for variable resolution */
274 ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
275 if (ec)
276 goto baderror;
277 if (tomega2 < .12*tomega)
278 continue; /* not safe to include */
279 cvt_sdcolor(csmp, &sv);
280
281 if (sf < 2.5*tsr) { /* weight by Y for small sources */
282 scalecolor(csmp, sv.cieY);
283 wtot += sv.cieY;
284 } else
285 wtot += 1.;
286 addcolor(cval, csmp);
287 }
288 if (wtot <= FTINY) /* no valid specular samples? */
289 return(0);
290
291 sf = 1./wtot; /* weighted average BSDF */
292 scalecolor(cval, sf);
293 /* subtract diffuse contribution */
294 for (i = 3*(diffY > FTINY); i--; )
295 if ((colval(cval,i) -= colval(cdiff,i)) < 0)
296 colval(cval,i) = 0;
297 return(1);
298 baderror:
299 objerror(ndp->mp, USER, transSDError(ec));
300 return(0); /* gratis return */
301 }
302
303 /* Compute source contribution for BSDF (reflected & transmitted) */
304 static void
305 dir_bsdf(
306 COLOR cval, /* returned coefficient */
307 void *nnp, /* material data */
308 FVECT ldir, /* light source direction */
309 double omega /* light source size */
310 )
311 {
312 BSDFDAT *np = (BSDFDAT *)nnp;
313 double ldot;
314 double dtmp;
315 COLOR ctmp;
316
317 setcolor(cval, 0, 0, 0);
318
319 ldot = DOT(np->pnorm, ldir);
320 if ((-FTINY <= ldot) & (ldot <= FTINY))
321 return;
322
323 if (ldot > 0 && bright(np->rdiff) > FTINY) {
324 /*
325 * Compute diffuse reflected component
326 */
327 copycolor(ctmp, np->rdiff);
328 dtmp = ldot * omega * (1./PI);
329 scalecolor(ctmp, dtmp);
330 addcolor(cval, ctmp);
331 }
332 if (ldot < 0 && bright(np->tdiff) > FTINY) {
333 /*
334 * Compute diffuse transmission
335 */
336 copycolor(ctmp, np->tdiff);
337 dtmp = -ldot * omega * (1.0/PI);
338 scalecolor(ctmp, dtmp);
339 addcolor(cval, ctmp);
340 }
341 if (ambRayInPmap(np->pr))
342 return; /* specular already in photon map */
343 /*
344 * Compute specular scattering coefficient using BSDF
345 */
346 if (!direct_specular_OK(ctmp, ldir, omega, np))
347 return;
348 if (ldot < 0) { /* pattern for specular transmission */
349 multcolor(ctmp, np->pr->pcol);
350 dtmp = -ldot * omega;
351 } else
352 dtmp = ldot * omega;
353 scalecolor(ctmp, dtmp);
354 addcolor(cval, ctmp);
355 }
356
357 /* Compute source contribution for BSDF (reflected only) */
358 static void
359 dir_brdf(
360 COLOR cval, /* returned coefficient */
361 void *nnp, /* material data */
362 FVECT ldir, /* light source direction */
363 double omega /* light source size */
364 )
365 {
366 BSDFDAT *np = (BSDFDAT *)nnp;
367 double ldot;
368 double dtmp;
369 COLOR ctmp, ctmp1, ctmp2;
370
371 setcolor(cval, 0, 0, 0);
372
373 ldot = DOT(np->pnorm, ldir);
374
375 if (ldot <= FTINY)
376 return;
377
378 if (bright(np->rdiff) > FTINY) {
379 /*
380 * Compute diffuse reflected component
381 */
382 copycolor(ctmp, np->rdiff);
383 dtmp = ldot * omega * (1./PI);
384 scalecolor(ctmp, dtmp);
385 addcolor(cval, ctmp);
386 }
387 if (ambRayInPmap(np->pr))
388 return; /* specular already in photon map */
389 /*
390 * Compute specular reflection coefficient using BSDF
391 */
392 if (!direct_specular_OK(ctmp, ldir, omega, np))
393 return;
394 dtmp = ldot * omega;
395 scalecolor(ctmp, dtmp);
396 addcolor(cval, ctmp);
397 }
398
399 /* Compute source contribution for BSDF (transmitted only) */
400 static void
401 dir_btdf(
402 COLOR cval, /* returned coefficient */
403 void *nnp, /* material data */
404 FVECT ldir, /* light source direction */
405 double omega /* light source size */
406 )
407 {
408 BSDFDAT *np = (BSDFDAT *)nnp;
409 double ldot;
410 double dtmp;
411 COLOR ctmp;
412
413 setcolor(cval, 0, 0, 0);
414
415 ldot = DOT(np->pnorm, ldir);
416
417 if (ldot >= -FTINY)
418 return;
419
420 if (bright(np->tdiff) > FTINY) {
421 /*
422 * Compute diffuse transmission
423 */
424 copycolor(ctmp, np->tdiff);
425 dtmp = -ldot * omega * (1.0/PI);
426 scalecolor(ctmp, dtmp);
427 addcolor(cval, ctmp);
428 }
429 if (ambRayInPmap(np->pr))
430 return; /* specular already in photon map */
431 /*
432 * Compute specular scattering coefficient using BSDF
433 */
434 if (!direct_specular_OK(ctmp, ldir, omega, np))
435 return;
436 /* full pattern on transmission */
437 multcolor(ctmp, np->pr->pcol);
438 dtmp = -ldot * omega;
439 scalecolor(ctmp, dtmp);
440 addcolor(cval, ctmp);
441 }
442
443 /* Sample separate BSDF component */
444 static int
445 sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
446 {
447 const int hasthru = (xmit && bright(ndp->cthru) > FTINY);
448 int nstarget = 1;
449 int nsent = 0;
450 int n;
451 SDError ec;
452 SDValue bsv;
453 double xrand;
454 FVECT vsmp, vinc;
455 RAY sr;
456 /* multiple samples? */
457 if (specjitter > 1.5) {
458 nstarget = specjitter*ndp->pr->rweight + .5;
459 nstarget += !nstarget;
460 }
461 /* run through our samples */
462 for (n = 0; n < nstarget; n++) {
463 if (nstarget == 1) { /* stratify random variable */
464 xrand = urand(ilhash(dimlist,ndims)+samplendx);
465 if (specjitter < 1.)
466 xrand = .5 + specjitter*(xrand-.5);
467 } else {
468 xrand = (n + frandom())/(double)nstarget;
469 }
470 SDerrorDetail[0] = '\0'; /* sample direction & coef. */
471 bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
472 VCOPY(vinc, vsmp); /* to compare after */
473 ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
474 if (ec)
475 objerror(ndp->mp, USER, transSDError(ec));
476 if (bsv.cieY <= FTINY) /* zero component? */
477 break;
478 if (hasthru) { /* check for view ray */
479 double dx = vinc[0] + vsmp[0];
480 double dy = vinc[1] + vsmp[1];
481 if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
482 continue; /* exclude view sample */
483 }
484 /* map non-view sample->world */
485 if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
486 break;
487 /* spawn a specular ray */
488 if (nstarget > 1)
489 bsv.cieY /= (double)nstarget;
490 cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */
491 if (xmit) /* apply pattern on transmit */
492 multcolor(sr.rcoef, ndp->pr->pcol);
493 if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
494 if (maxdepth > 0)
495 break;
496 continue; /* Russian roulette victim */
497 }
498 if (xmit && ndp->thick != 0) /* need to offset origin? */
499 VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
500 rayvalue(&sr); /* send & evaluate sample */
501 multcolor(sr.rcol, sr.rcoef);
502 addcolor(ndp->pr->rcol, sr.rcol);
503 ++nsent;
504 }
505 return(nsent);
506 }
507
508 /* Sample non-diffuse components of BSDF */
509 static int
510 sample_sdf(BSDFDAT *ndp, int sflags)
511 {
512 int hasthru = (sflags == SDsampSpT &&
513 bright(ndp->cthru) > FTINY);
514 int n, ntotal = 0;
515 double b = 0;
516 SDSpectralDF *dfp;
517 COLORV *unsc;
518
519 if (sflags == SDsampSpT) {
520 unsc = ndp->tunsamp;
521 if (ndp->pr->rod > 0)
522 dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
523 else
524 dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
525 } else /* sflags == SDsampSpR */ {
526 unsc = ndp->runsamp;
527 if (ndp->pr->rod > 0)
528 dfp = ndp->sd->rf;
529 else
530 dfp = ndp->sd->rb;
531 }
532 setcolor(unsc, 0, 0, 0);
533 if (dfp == NULL) /* no specular component? */
534 return(0);
535
536 if (hasthru) { /* separate view sample? */
537 RAY tr;
538 if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
539 VCOPY(tr.rdir, ndp->pr->rdir);
540 rayvalue(&tr);
541 multcolor(tr.rcol, tr.rcoef);
542 addcolor(ndp->pr->rcol, tr.rcol);
543 ++ntotal;
544 b = bright(ndp->cthru);
545 } else
546 hasthru = 0;
547 }
548 if (dfp->maxHemi - b <= FTINY) { /* have specular to sample? */
549 b = 0;
550 } else {
551 FVECT vjit;
552 bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
553 b = SDdirectHemi(vjit, sflags, ndp->sd) - b;
554 if (b < 0) b = 0;
555 }
556 if (b <= specthresh+FTINY) { /* below sampling threshold? */
557 if (b > FTINY) { /* XXX no color from BSDF */
558 if (sflags == SDsampSpT) {
559 copycolor(unsc, ndp->pr->pcol);
560 scalecolor(unsc, b);
561 } else /* no pattern on reflection */
562 setcolor(unsc, b, b, b);
563 }
564 return(ntotal);
565 }
566 dimlist[ndims] = (int)(size_t)ndp->mp; /* else sample specular */
567 ndims += 2;
568 for (n = dfp->ncomp; n--; ) { /* loop over components */
569 dimlist[ndims-1] = n + 9438;
570 ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
571 }
572 ndims -= 2;
573 return(ntotal);
574 }
575
576 /* Color a ray that hit a BSDF material */
577 int
578 m_bsdf(OBJREC *m, RAY *r)
579 {
580 int hitfront;
581 COLOR ctmp;
582 SDError ec;
583 FVECT upvec, vtmp;
584 MFUNC *mf;
585 BSDFDAT nd;
586 /* check arguments */
587 if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
588 (m->oargs.nfargs % 3))
589 objerror(m, USER, "bad # arguments");
590 /* record surface struck */
591 hitfront = (r->rod > 0);
592 /* load cal file */
593 mf = getfunc(m, 5, 0x1d, 1);
594 setfunc(m, r);
595 /* get thickness */
596 nd.thick = evalue(mf->ep[0]);
597 if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
598 nd.thick = 0;
599 /* check backface visibility */
600 if (!hitfront & !backvis) {
601 raytrans(r);
602 return(1);
603 }
604 /* check other rays to pass */
605 if (nd.thick != 0 && (r->crtype & SHADOW ||
606 !(r->crtype & (SPECULAR|AMBIENT)) ||
607 (nd.thick > 0) ^ hitfront)) {
608 raytrans(r); /* hide our proxy */
609 return(1);
610 }
611 nd.mp = m;
612 nd.pr = r;
613 /* get BSDF data */
614 nd.sd = loadBSDF(m->oargs.sarg[1]);
615 /* early shadow check */
616 if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
617 return(1);
618 /* diffuse reflectance */
619 if (hitfront) {
620 cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
621 if (m->oargs.nfargs >= 3) {
622 setcolor(ctmp, m->oargs.farg[0],
623 m->oargs.farg[1],
624 m->oargs.farg[2]);
625 addcolor(nd.rdiff, ctmp);
626 }
627 } else {
628 cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
629 if (m->oargs.nfargs >= 6) {
630 setcolor(ctmp, m->oargs.farg[3],
631 m->oargs.farg[4],
632 m->oargs.farg[5]);
633 addcolor(nd.rdiff, ctmp);
634 }
635 }
636 /* diffuse transmittance */
637 cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
638 if (m->oargs.nfargs >= 9) {
639 setcolor(ctmp, m->oargs.farg[6],
640 m->oargs.farg[7],
641 m->oargs.farg[8]);
642 addcolor(nd.tdiff, ctmp);
643 }
644 /* get modifiers */
645 raytexture(r, m->omod);
646 /* modify diffuse values */
647 multcolor(nd.rdiff, r->pcol);
648 multcolor(nd.tdiff, r->pcol);
649 /* get up vector */
650 upvec[0] = evalue(mf->ep[1]);
651 upvec[1] = evalue(mf->ep[2]);
652 upvec[2] = evalue(mf->ep[3]);
653 /* return to world coords */
654 if (mf->fxp != &unitxf) {
655 multv3(upvec, upvec, mf->fxp->xfm);
656 nd.thick *= mf->fxp->sca;
657 }
658 if (r->rox != NULL) {
659 multv3(upvec, upvec, r->rox->f.xfm);
660 nd.thick *= r->rox->f.sca;
661 }
662 raynormal(nd.pnorm, r);
663 /* compute local BSDF xform */
664 ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
665 if (!ec) {
666 nd.vray[0] = -r->rdir[0];
667 nd.vray[1] = -r->rdir[1];
668 nd.vray[2] = -r->rdir[2];
669 ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
670 }
671 if (ec) {
672 objerror(m, WARNING, "Illegal orientation vector");
673 return(1);
674 }
675 compute_through(&nd); /* compute through component */
676 if (r->crtype & SHADOW) {
677 RAY tr; /* attempt to pass shadow ray */
678 if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
679 return(1); /* no through component */
680 VCOPY(tr.rdir, r->rdir);
681 rayvalue(&tr); /* transmit with scaling */
682 multcolor(tr.rcol, tr.rcoef);
683 copycolor(r->rcol, tr.rcol);
684 return(1); /* we're done */
685 }
686 ec = SDinvXform(nd.fromloc, nd.toloc);
687 if (!ec) /* determine BSDF resolution */
688 ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
689 SDqueryMin+SDqueryMax, nd.sd);
690 if (ec)
691 objerror(m, USER, transSDError(ec));
692
693 nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
694 nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
695 if (!hitfront) { /* perturb normal towards hit */
696 nd.pnorm[0] = -nd.pnorm[0];
697 nd.pnorm[1] = -nd.pnorm[1];
698 nd.pnorm[2] = -nd.pnorm[2];
699 }
700 /* sample reflection */
701 sample_sdf(&nd, SDsampSpR);
702 /* sample transmission */
703 sample_sdf(&nd, SDsampSpT);
704 /* compute indirect diffuse */
705 copycolor(ctmp, nd.rdiff);
706 addcolor(ctmp, nd.runsamp);
707 if (bright(ctmp) > FTINY) { /* ambient from reflection */
708 if (!hitfront)
709 flipsurface(r);
710 multambient(ctmp, r, nd.pnorm);
711 addcolor(r->rcol, ctmp);
712 if (!hitfront)
713 flipsurface(r);
714 }
715 copycolor(ctmp, nd.tdiff);
716 addcolor(ctmp, nd.tunsamp);
717 if (bright(ctmp) > FTINY) { /* ambient from other side */
718 FVECT bnorm;
719 if (hitfront)
720 flipsurface(r);
721 bnorm[0] = -nd.pnorm[0];
722 bnorm[1] = -nd.pnorm[1];
723 bnorm[2] = -nd.pnorm[2];
724 if (nd.thick != 0) { /* proxy with offset? */
725 VCOPY(vtmp, r->rop);
726 VSUM(r->rop, vtmp, r->ron, nd.thick);
727 multambient(ctmp, r, bnorm);
728 VCOPY(r->rop, vtmp);
729 } else
730 multambient(ctmp, r, bnorm);
731 addcolor(r->rcol, ctmp);
732 if (hitfront)
733 flipsurface(r);
734 }
735 /* add direct component */
736 if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
737 (nd.sd->tb == NULL)) {
738 direct(r, dir_brdf, &nd); /* reflection only */
739 } else if (nd.thick == 0) {
740 direct(r, dir_bsdf, &nd); /* thin surface scattering */
741 } else {
742 direct(r, dir_brdf, &nd); /* reflection first */
743 VCOPY(vtmp, r->rop); /* offset for transmitted */
744 VSUM(r->rop, vtmp, r->ron, -nd.thick);
745 direct(r, dir_btdf, &nd); /* separate transmission */
746 VCOPY(r->rop, vtmp);
747 }
748 /* clean up */
749 SDfreeCache(nd.sd);
750 return(1);
751 }