ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.39
Committed: Fri Jun 2 18:10:11 2017 UTC (6 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.38: +22 -19 lines
Log Message:
Fixed over-counting error introduced in revision 2.31

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: m_bsdf.c,v 2.38 2017/05/19 15:13:41 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 specular & ambient
41 * samples that are too close to this "through" direction. The same
42 * restriction applies for the proxy case (thickness != 0).
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 = 2.0;
110 SDSpectralDF *dfp;
111 FVECT pdir;
112 double tomega, srchrad;
113 COLOR vpeak, vsum;
114 int nsum, i;
115 SDError ec;
116
117 setcolor(ndp->cthru, .0, .0, .0); /* starting assumption */
118
119 if (!(ndp->pr->crtype & (SPECULAR|AMBIENT|SHADOW)))
120 return; /* simply don't need to know */
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 nsum = 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 ++nsum;
149 if (bright(vcol) > bright(vpeak)) {
150 copycolor(vpeak, vcol);
151 VCOPY(pdir, tdir);
152 }
153 }
154 ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
155 if (ec)
156 goto baderror;
157 if (tomega > 1.5*dfp->minProjSA)
158 return; /* not really a peak? */
159 if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .007)
160 return; /* < 0.7% transmission */
161 for (i = 3; i--; ) /* remove peak from average */
162 colval(vsum,i) -= colval(vpeak,i);
163 --nsum;
164 if (peak_over*bright(vsum) >= nsum*bright(vpeak))
165 return; /* not peaky enough */
166 copycolor(ndp->cthru, vpeak); /* else use it */
167 scalecolor(ndp->cthru, tomega);
168 multcolor(ndp->cthru, ndp->pr->pcol); /* modify by pattern */
169 return;
170 baderror:
171 objerror(ndp->mp, USER, transSDError(ec));
172 #undef NDIR2CHECK
173 }
174
175 /* Jitter ray sample according to projected solid angle and specjitter */
176 static void
177 bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
178 {
179 VCOPY(vres, ndp->vray);
180 if (specjitter < 1.)
181 sr_psa *= specjitter;
182 if (sr_psa <= FTINY)
183 return;
184 vres[0] += sr_psa*(.5 - frandom());
185 vres[1] += sr_psa*(.5 - frandom());
186 normalize(vres);
187 }
188
189 /* Get BSDF specular for direct component, returning true if OK to proceed */
190 static int
191 direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
192 {
193 int nsamp, ok = 0;
194 FVECT vsrc, vsmp, vjit;
195 double tomega, tomega2;
196 double sf, tsr, sd[2];
197 COLOR csmp, cdiff;
198 double diffY;
199 SDValue sv;
200 SDError ec;
201 int i;
202 /* in case we fail */
203 setcolor(cval, .0, .0, .0);
204 /* transform source direction */
205 if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
206 return(0);
207 /* will discount diffuse portion */
208 switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
209 case 3:
210 if (ndp->sd->rf == NULL)
211 return(0); /* all diffuse */
212 sv = ndp->sd->rLambFront;
213 break;
214 case 0:
215 if (ndp->sd->rb == NULL)
216 return(0); /* all diffuse */
217 sv = ndp->sd->rLambBack;
218 break;
219 default:
220 if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
221 return(0); /* all diffuse */
222 sv = ndp->sd->tLamb;
223 break;
224 }
225 if (sv.cieY > FTINY) {
226 diffY = sv.cieY *= 1./PI;
227 cvt_sdcolor(cdiff, &sv);
228 } else {
229 diffY = .0;
230 setcolor(cdiff, .0, .0, .0);
231 }
232 /* need projected solid angles */
233 omega *= fabs(vsrc[2]);
234 ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
235 if (ec)
236 goto baderror;
237 /* check indirect over-counting */
238 if (ndp->pr->crtype & (SPECULAR|AMBIENT)
239 && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)
240 && 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 addcolor(cval, csmp); /* else average it in */
281 ++ok;
282 }
283 if (!ok) /* no valid specular samples? */
284 return(0);
285
286 sf = 1./(double)ok; /* compute average BSDF */
287 scalecolor(cval, sf);
288 /* subtract diffuse contribution */
289 for (i = 3*(diffY > FTINY); i--; )
290 if ((colval(cval,i) -= colval(cdiff,i)) < .0)
291 colval(cval,i) = .0;
292 return(1);
293 baderror:
294 objerror(ndp->mp, USER, transSDError(ec));
295 return(0); /* gratis return */
296 }
297
298 /* Compute source contribution for BSDF (reflected & transmitted) */
299 static void
300 dir_bsdf(
301 COLOR cval, /* returned coefficient */
302 void *nnp, /* material data */
303 FVECT ldir, /* light source direction */
304 double omega /* light source size */
305 )
306 {
307 BSDFDAT *np = (BSDFDAT *)nnp;
308 double ldot;
309 double dtmp;
310 COLOR ctmp;
311
312 setcolor(cval, .0, .0, .0);
313
314 ldot = DOT(np->pnorm, ldir);
315 if ((-FTINY <= ldot) & (ldot <= FTINY))
316 return;
317
318 if (ldot > 0 && bright(np->rdiff) > FTINY) {
319 /*
320 * Compute diffuse reflected component
321 */
322 copycolor(ctmp, np->rdiff);
323 dtmp = ldot * omega * (1./PI);
324 scalecolor(ctmp, dtmp);
325 addcolor(cval, ctmp);
326 }
327 if (ldot < 0 && bright(np->tdiff) > FTINY) {
328 /*
329 * Compute diffuse transmission
330 */
331 copycolor(ctmp, np->tdiff);
332 dtmp = -ldot * omega * (1.0/PI);
333 scalecolor(ctmp, dtmp);
334 addcolor(cval, ctmp);
335 }
336 if (ambRayInPmap(np->pr))
337 return; /* specular already in photon map */
338 /*
339 * Compute specular scattering coefficient using BSDF
340 */
341 if (!direct_specular_OK(ctmp, ldir, omega, np))
342 return;
343 if (ldot < 0) { /* pattern for specular transmission */
344 multcolor(ctmp, np->pr->pcol);
345 dtmp = -ldot * omega;
346 } else
347 dtmp = ldot * omega;
348 scalecolor(ctmp, dtmp);
349 addcolor(cval, ctmp);
350 }
351
352 /* Compute source contribution for BSDF (reflected only) */
353 static void
354 dir_brdf(
355 COLOR cval, /* returned coefficient */
356 void *nnp, /* material data */
357 FVECT ldir, /* light source direction */
358 double omega /* light source size */
359 )
360 {
361 BSDFDAT *np = (BSDFDAT *)nnp;
362 double ldot;
363 double dtmp;
364 COLOR ctmp, ctmp1, ctmp2;
365
366 setcolor(cval, .0, .0, .0);
367
368 ldot = DOT(np->pnorm, ldir);
369
370 if (ldot <= FTINY)
371 return;
372
373 if (bright(np->rdiff) > FTINY) {
374 /*
375 * Compute diffuse reflected component
376 */
377 copycolor(ctmp, np->rdiff);
378 dtmp = ldot * omega * (1./PI);
379 scalecolor(ctmp, dtmp);
380 addcolor(cval, ctmp);
381 }
382 if (ambRayInPmap(np->pr))
383 return; /* specular already in photon map */
384 /*
385 * Compute specular reflection coefficient using BSDF
386 */
387 if (!direct_specular_OK(ctmp, ldir, omega, np))
388 return;
389 dtmp = ldot * omega;
390 scalecolor(ctmp, dtmp);
391 addcolor(cval, ctmp);
392 }
393
394 /* Compute source contribution for BSDF (transmitted only) */
395 static void
396 dir_btdf(
397 COLOR cval, /* returned coefficient */
398 void *nnp, /* material data */
399 FVECT ldir, /* light source direction */
400 double omega /* light source size */
401 )
402 {
403 BSDFDAT *np = (BSDFDAT *)nnp;
404 double ldot;
405 double dtmp;
406 COLOR ctmp;
407
408 setcolor(cval, .0, .0, .0);
409
410 ldot = DOT(np->pnorm, ldir);
411
412 if (ldot >= -FTINY)
413 return;
414
415 if (bright(np->tdiff) > FTINY) {
416 /*
417 * Compute diffuse transmission
418 */
419 copycolor(ctmp, np->tdiff);
420 dtmp = -ldot * omega * (1.0/PI);
421 scalecolor(ctmp, dtmp);
422 addcolor(cval, ctmp);
423 }
424 if (ambRayInPmap(np->pr))
425 return; /* specular already in photon map */
426 /*
427 * Compute specular scattering coefficient using BSDF
428 */
429 if (!direct_specular_OK(ctmp, ldir, omega, np))
430 return;
431 /* full pattern on transmission */
432 multcolor(ctmp, np->pr->pcol);
433 dtmp = -ldot * omega;
434 scalecolor(ctmp, dtmp);
435 addcolor(cval, ctmp);
436 }
437
438 /* Sample separate BSDF component */
439 static int
440 sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
441 {
442 int nstarget = 1;
443 int nsent;
444 SDError ec;
445 SDValue bsv;
446 double xrand;
447 FVECT vsmp;
448 RAY sr;
449 /* multiple samples? */
450 if (specjitter > 1.5) {
451 nstarget = specjitter*ndp->pr->rweight + .5;
452 nstarget += !nstarget;
453 }
454 /* run through our samples */
455 for (nsent = 0; nsent < nstarget; nsent++) {
456 if (nstarget == 1) { /* stratify random variable */
457 xrand = urand(ilhash(dimlist,ndims)+samplendx);
458 if (specjitter < 1.)
459 xrand = .5 + specjitter*(xrand-.5);
460 } else {
461 xrand = (nsent + frandom())/(double)nstarget;
462 }
463 SDerrorDetail[0] = '\0'; /* sample direction & coef. */
464 bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
465 ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
466 if (ec)
467 objerror(ndp->mp, USER, transSDError(ec));
468 if (bsv.cieY <= FTINY) /* zero component? */
469 break;
470 /* map vector to world */
471 if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
472 break;
473 /* spawn a specular ray */
474 if (nstarget > 1)
475 bsv.cieY /= (double)nstarget;
476 cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */
477 if (usepat) /* apply pattern? */
478 multcolor(sr.rcoef, ndp->pr->pcol);
479 if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
480 if (maxdepth > 0)
481 break;
482 continue; /* Russian roulette victim */
483 }
484 /* need to offset origin? */
485 if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
486 VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
487 rayvalue(&sr); /* send & evaluate sample */
488 multcolor(sr.rcol, sr.rcoef);
489 addcolor(ndp->pr->rcol, sr.rcol);
490 }
491 return(nsent);
492 }
493
494 /* Sample non-diffuse components of BSDF */
495 static int
496 sample_sdf(BSDFDAT *ndp, int sflags)
497 {
498 int n, ntotal = 0;
499 SDSpectralDF *dfp;
500 COLORV *unsc;
501
502 if (sflags == SDsampSpT) {
503 unsc = ndp->tunsamp;
504 if (ndp->pr->rod > 0)
505 dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
506 else
507 dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
508 } else /* sflags == SDsampSpR */ {
509 unsc = ndp->runsamp;
510 if (ndp->pr->rod > 0)
511 dfp = ndp->sd->rf;
512 else
513 dfp = ndp->sd->rb;
514 }
515 setcolor(unsc, 0., 0., 0.);
516 if (dfp == NULL) /* no specular component? */
517 return(0);
518 /* below sampling threshold? */
519 if (dfp->maxHemi <= specthresh+FTINY) {
520 if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */
521 FVECT vjit;
522 double d;
523 bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
524 d = SDdirectHemi(vjit, sflags, ndp->sd);
525 if (sflags == SDsampSpT) {
526 copycolor(unsc, ndp->pr->pcol);
527 scalecolor(unsc, d);
528 } else /* no pattern on reflection */
529 setcolor(unsc, d, d, d);
530 }
531 return(0);
532 }
533 /* else need to sample */
534 dimlist[ndims++] = (int)(size_t)ndp->mp;
535 ndims++;
536 for (n = dfp->ncomp; n--; ) { /* loop over components */
537 dimlist[ndims-1] = n + 9438;
538 ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
539 }
540 ndims -= 2;
541 return(ntotal);
542 }
543
544 /* Color a ray that hit a BSDF material */
545 int
546 m_bsdf(OBJREC *m, RAY *r)
547 {
548 int hitfront;
549 COLOR ctmp;
550 SDError ec;
551 FVECT upvec, vtmp;
552 MFUNC *mf;
553 BSDFDAT nd;
554 /* check arguments */
555 if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
556 (m->oargs.nfargs % 3))
557 objerror(m, USER, "bad # arguments");
558 /* record surface struck */
559 hitfront = (r->rod > 0);
560 /* load cal file */
561 mf = getfunc(m, 5, 0x1d, 1);
562 setfunc(m, r);
563 /* get thickness */
564 nd.thick = evalue(mf->ep[0]);
565 if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
566 nd.thick = .0;
567 /* check backface visibility */
568 if (!hitfront & !backvis) {
569 raytrans(r);
570 return(1);
571 }
572 /* check other rays to pass */
573 if (nd.thick != 0 && (r->crtype & SHADOW ||
574 !(r->crtype & (SPECULAR|AMBIENT)) ||
575 (nd.thick > 0) ^ hitfront)) {
576 raytrans(r); /* hide our proxy */
577 return(1);
578 }
579 nd.mp = m;
580 nd.pr = r;
581 /* get BSDF data */
582 nd.sd = loadBSDF(m->oargs.sarg[1]);
583 /* early shadow check */
584 if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
585 return(1);
586 /* diffuse reflectance */
587 if (hitfront) {
588 cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
589 if (m->oargs.nfargs >= 3) {
590 setcolor(ctmp, m->oargs.farg[0],
591 m->oargs.farg[1],
592 m->oargs.farg[2]);
593 addcolor(nd.rdiff, ctmp);
594 }
595 } else {
596 cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
597 if (m->oargs.nfargs >= 6) {
598 setcolor(ctmp, m->oargs.farg[3],
599 m->oargs.farg[4],
600 m->oargs.farg[5]);
601 addcolor(nd.rdiff, ctmp);
602 }
603 }
604 /* diffuse transmittance */
605 cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
606 if (m->oargs.nfargs >= 9) {
607 setcolor(ctmp, m->oargs.farg[6],
608 m->oargs.farg[7],
609 m->oargs.farg[8]);
610 addcolor(nd.tdiff, ctmp);
611 }
612 /* get modifiers */
613 raytexture(r, m->omod);
614 /* modify diffuse values */
615 multcolor(nd.rdiff, r->pcol);
616 multcolor(nd.tdiff, r->pcol);
617 /* get up vector */
618 upvec[0] = evalue(mf->ep[1]);
619 upvec[1] = evalue(mf->ep[2]);
620 upvec[2] = evalue(mf->ep[3]);
621 /* return to world coords */
622 if (mf->fxp != &unitxf) {
623 multv3(upvec, upvec, mf->fxp->xfm);
624 nd.thick *= mf->fxp->sca;
625 }
626 if (r->rox != NULL) {
627 multv3(upvec, upvec, r->rox->f.xfm);
628 nd.thick *= r->rox->f.sca;
629 }
630 raynormal(nd.pnorm, r);
631 /* compute local BSDF xform */
632 ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
633 if (!ec) {
634 nd.vray[0] = -r->rdir[0];
635 nd.vray[1] = -r->rdir[1];
636 nd.vray[2] = -r->rdir[2];
637 ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
638 }
639 if (ec) {
640 objerror(m, WARNING, "Illegal orientation vector");
641 return(1);
642 }
643 compute_through(&nd); /* compute through component */
644 if (r->crtype & SHADOW) {
645 RAY tr; /* attempt to pass shadow ray */
646 if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
647 return(1); /* blocked */
648 VCOPY(tr.rdir, r->rdir);
649 rayvalue(&tr); /* transmit with scaling */
650 multcolor(tr.rcol, tr.rcoef);
651 copycolor(r->rcol, tr.rcol);
652 return(1); /* we're done */
653 }
654 ec = SDinvXform(nd.fromloc, nd.toloc);
655 if (!ec) /* determine BSDF resolution */
656 ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
657 SDqueryMin+SDqueryMax, nd.sd);
658 if (ec)
659 objerror(m, USER, transSDError(ec));
660
661 nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
662 nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
663 if (!hitfront) { /* perturb normal towards hit */
664 nd.pnorm[0] = -nd.pnorm[0];
665 nd.pnorm[1] = -nd.pnorm[1];
666 nd.pnorm[2] = -nd.pnorm[2];
667 }
668 /* sample reflection */
669 sample_sdf(&nd, SDsampSpR);
670 /* sample transmission */
671 sample_sdf(&nd, SDsampSpT);
672 /* compute indirect diffuse */
673 copycolor(ctmp, nd.rdiff);
674 addcolor(ctmp, nd.runsamp);
675 if (bright(ctmp) > FTINY) { /* ambient from reflection */
676 if (!hitfront)
677 flipsurface(r);
678 multambient(ctmp, r, nd.pnorm);
679 addcolor(r->rcol, ctmp);
680 if (!hitfront)
681 flipsurface(r);
682 }
683 copycolor(ctmp, nd.tdiff);
684 addcolor(ctmp, nd.tunsamp);
685 if (bright(ctmp) > FTINY) { /* ambient from other side */
686 FVECT bnorm;
687 if (hitfront)
688 flipsurface(r);
689 bnorm[0] = -nd.pnorm[0];
690 bnorm[1] = -nd.pnorm[1];
691 bnorm[2] = -nd.pnorm[2];
692 if (nd.thick != 0) { /* proxy with offset? */
693 VCOPY(vtmp, r->rop);
694 VSUM(r->rop, vtmp, r->ron, nd.thick);
695 multambient(ctmp, r, bnorm);
696 VCOPY(r->rop, vtmp);
697 } else
698 multambient(ctmp, r, bnorm);
699 addcolor(r->rcol, ctmp);
700 if (hitfront)
701 flipsurface(r);
702 }
703 /* add direct component */
704 if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
705 (nd.sd->tb == NULL)) {
706 direct(r, dir_brdf, &nd); /* reflection only */
707 } else if (nd.thick == 0) {
708 direct(r, dir_bsdf, &nd); /* thin surface scattering */
709 } else {
710 direct(r, dir_brdf, &nd); /* reflection first */
711 VCOPY(vtmp, r->rop); /* offset for transmitted */
712 VSUM(r->rop, vtmp, r->ron, -nd.thick);
713 direct(r, dir_btdf, &nd); /* separate transmission */
714 VCOPY(r->rop, vtmp);
715 }
716 /* clean up */
717 SDfreeCache(nd.sd);
718 return(1);
719 }