ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.2
Committed: Fri Feb 18 02:41:55 2011 UTC (13 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +6 -31 lines
Log Message:
Minor fixes and adjustments

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: m_bsdf.c,v 2.1 2011/02/18 00:40:25 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 thickness variable causes the strange but useful behavior
21 * of translating transmitted rays this distance past the surface
22 * intersection in the normal direction to bypass intervening geometry.
23 * This only affects scattered, non-source directed samples. Thus,
24 * thickness is relevant only if there is a transmitted component.
25 * A positive 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 our BSDF invisible. This allows the
28 * underlying geometry to become visible. A matching surface should be
29 * placed on the other side, less than the thickness away, if the backside
30 * reflectance is non-zero.
31 * The "up" vector for the BSDF is given by three variables, defined
32 * (along with the thickness) by the named function file, or '.' if none.
33 * Together with the surface normal, this defines the local coordinate
34 * system for the BSDF.
35 * We do not reorient the surface, so if the BSDF has no back-side
36 * reflectance and none is given in the real arguments, the surface will
37 * appear as black when viewed from behind (unless backvis is false).
38 * The diffuse compnent arguments are added to components in the BSDF file,
39 * not multiplied. However, patterns affect this material as a multiplier
40 * on everything except non-diffuse reflection.
41 *
42 * Arguments for MAT_BSDF are:
43 * 6+ thick BSDFfile ux uy uz funcfile transform
44 * 0
45 * 0|3|9 rdf gdf bdf
46 * rdb gdb bdb
47 * rdt gdt bdt
48 */
49
50 typedef struct {
51 OBJREC *mp; /* material pointer */
52 RAY *pr; /* intersected ray */
53 FVECT pnorm; /* perturbed surface normal */
54 FVECT vinc; /* local incident vector */
55 RREAL toloc[3][3]; /* world to local BSDF coords */
56 RREAL fromloc[3][3]; /* local BSDF coords to world */
57 double thick; /* surface thickness */
58 SDData *sd; /* loaded BSDF data */
59 COLOR runsamp; /* BSDF hemispherical reflection */
60 COLOR rdiff; /* added diffuse reflection */
61 COLOR tunsamp; /* BSDF hemispherical transmission */
62 COLOR tdiff; /* added diffuse transmission */
63 } BSDFDAT; /* BSDF material data */
64
65 #define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
66
67 /* Compute source contribution for BSDF */
68 static void
69 dirbsdf(
70 COLOR cval, /* returned coefficient */
71 void *nnp, /* material data */
72 FVECT ldir, /* light source direction */
73 double omega /* light source size */
74 )
75 {
76 BSDFDAT *np = nnp;
77 SDError ec;
78 SDValue sv;
79 FVECT vout;
80 double ldot;
81 double dtmp;
82 COLOR ctmp;
83
84 setcolor(cval, .0, .0, .0);
85
86 ldot = DOT(np->pnorm, ldir);
87 if ((-FTINY <= ldot) & (ldot <= FTINY))
88 return;
89
90 if (ldot > .0 && bright(np->rdiff) > FTINY) {
91 /*
92 * Compute added diffuse reflected component.
93 */
94 copycolor(ctmp, np->rdiff);
95 dtmp = ldot * omega * (1./PI);
96 scalecolor(ctmp, dtmp);
97 addcolor(cval, ctmp);
98 }
99 if (ldot < .0 && bright(np->tdiff) > FTINY) {
100 /*
101 * Compute added diffuse transmission.
102 */
103 copycolor(ctmp, np->tdiff);
104 dtmp = -ldot * omega * (1.0/PI);
105 scalecolor(ctmp, dtmp);
106 addcolor(cval, ctmp);
107 }
108 /*
109 * Compute scattering coefficient using BSDF.
110 */
111 if (SDmapDir(vout, np->toloc, ldir) != SDEnone)
112 return;
113 ec = SDevalBSDF(&sv, vout, np->vinc, np->sd);
114 if (ec)
115 objerror(np->mp, USER, transSDError(ec));
116
117 if (sv.cieY <= FTINY) /* not worth using? */
118 return;
119 cvt_sdcolor(ctmp, &sv);
120 if (ldot > .0) { /* pattern only diffuse reflection */
121 COLOR ctmp1, ctmp2;
122 dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
123 : np->sd->rLambBack.cieY;
124 dtmp /= PI * sv.cieY; /* diffuse fraction */
125 copycolor(ctmp2, np->pr->pcol);
126 scalecolor(ctmp2, dtmp);
127 setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
128 addcolor(ctmp1, ctmp2);
129 multcolor(ctmp, ctmp1); /* apply desaturated pattern */
130 dtmp = ldot * omega;
131 } else { /* full pattern on transmission */
132 multcolor(ctmp, np->pr->pcol);
133 dtmp = -ldot * omega;
134 }
135 scalecolor(ctmp, dtmp);
136 addcolor(cval, ctmp);
137 }
138
139 /* Sample separate BSDF component */
140 static int
141 sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
142 {
143 int nstarget = 1;
144 int nsent = 0;
145 SDError ec;
146 SDValue bsv;
147 double sthick;
148 FVECT vout;
149 RAY sr;
150 int ntrials;
151 /* multiple samples? */
152 if (specjitter > 1.5) {
153 nstarget = specjitter*ndp->pr->rweight + .5;
154 if (nstarget < 1)
155 nstarget = 1;
156 }
157 /* run through our trials */
158 for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
159 SDerrorDetail[0] = '\0';
160 /* sample direction & coef. */
161 ec = SDsampComponent(&bsv, vout, ndp->vinc,
162 ntrials ? frandom()
163 : urand(ilhash(dimlist,ndims)+samplendx),
164 dcp);
165 if (ec)
166 objerror(ndp->mp, USER, transSDError(ec));
167 /* zero component? */
168 if (bsv.cieY <= FTINY)
169 break;
170 /* map vector to world */
171 if (SDmapDir(sr.rdir, ndp->fromloc, vout) != SDEnone)
172 break;
173 /* unintentional penetration? */
174 if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vout[2] > .0)
175 continue;
176 /* spawn a specular ray */
177 if (nstarget > 1)
178 bsv.cieY /= (double)nstarget;
179 cvt_sdcolor(sr.rcoef, &bsv); /* use color */
180 if (usepat) /* pattern on transmission */
181 multcolor(sr.rcoef, ndp->pr->pcol);
182 if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
183 if (maxdepth > 0)
184 break;
185 ++nsent; /* Russian roulette victim */
186 continue;
187 }
188 /* need to move origin? */
189 sthick = (ndp->pr->rod > .0) ? -ndp->thick : ndp->thick;
190 if (sthick < .0 ^ vout[2] > .0)
191 VSUM(sr.rorg, sr.rorg, ndp->pr->ron, sthick);
192
193 rayvalue(&sr); /* send & evaluate sample */
194 multcolor(sr.rcol, sr.rcoef);
195 addcolor(ndp->pr->rcol, sr.rcol);
196 ++nsent;
197 }
198 return(nsent);
199 }
200
201 /* Sample non-diffuse components of BSDF */
202 static int
203 sample_sdf(BSDFDAT *ndp, int sflags)
204 {
205 int n, ntotal = 0;
206 SDSpectralDF *dfp;
207 COLORV *unsc;
208
209 if (sflags == SDsampSpT) {
210 unsc = ndp->tunsamp;
211 dfp = ndp->sd->tf;
212 cvt_sdcolor(unsc, &ndp->sd->tLamb);
213 } else /* sflags == SDsampSpR */ {
214 unsc = ndp->runsamp;
215 if (ndp->pr->rod > .0) {
216 dfp = ndp->sd->rf;
217 cvt_sdcolor(unsc, &ndp->sd->rLambFront);
218 } else {
219 dfp = ndp->sd->rb;
220 cvt_sdcolor(unsc, &ndp->sd->rLambBack);
221 }
222 }
223 multcolor(unsc, ndp->pr->pcol);
224 if (dfp == NULL) /* no specular component? */
225 return(0);
226 /* below sampling threshold? */
227 if (dfp->maxHemi <= specthresh+FTINY) {
228 if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF! */
229 double d = SDdirectHemi(ndp->vinc, sflags, ndp->sd);
230 COLOR ctmp;
231 if (sflags == SDsampSpT) {
232 copycolor(ctmp, ndp->pr->pcol);
233 scalecolor(ctmp, d);
234 } else /* no pattern on reflection */
235 setcolor(ctmp, d, d, d);
236 addcolor(unsc, ctmp);
237 }
238 return(0);
239 }
240 /* else need to sample */
241 dimlist[ndims++] = (int)(size_t)ndp->mp;
242 ndims++;
243 for (n = dfp->ncomp; n--; ) { /* loop over components */
244 dimlist[ndims-1] = n + 9438;
245 ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
246 }
247 ndims -= 2;
248 return(ntotal);
249 }
250
251 /* Color a ray that hit a BSDF material */
252 int
253 m_bsdf(OBJREC *m, RAY *r)
254 {
255 COLOR ctmp;
256 SDError ec;
257 FVECT upvec, outVec;
258 MFUNC *mf;
259 BSDFDAT nd;
260 /* check arguments */
261 if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
262 (m->oargs.nfargs % 3))
263 objerror(m, USER, "bad # arguments");
264
265 /* get BSDF data */
266 nd.sd = loadBSDF(m->oargs.sarg[1]);
267 /* load cal file */
268 mf = getfunc(m, 5, 0x1d, 1);
269 /* get thickness */
270 nd.thick = evalue(mf->ep[0]);
271 if (nd.thick < .0)
272 nd.thick = .0;
273 /* check shadow */
274 if (r->crtype & SHADOW) {
275 SDfreeCache(nd.sd);
276 if (nd.thick > FTINY && nd.sd->tf != NULL &&
277 nd.sd->tf->maxHemi > FTINY)
278 raytrans(r); /* pass-through */
279 return(1); /* else shadow */
280 }
281 /* check unscattered ray */
282 if (!(r->crtype & (SPECULAR|AMBIENT)) && nd.thick > FTINY &&
283 nd.sd->tf != NULL && nd.sd->tf->maxHemi > FTINY) {
284 SDfreeCache(nd.sd);
285 raytrans(r); /* pass-through */
286 return(1);
287 }
288 /* diffuse reflectance */
289 if (r->rod > .0) {
290 if (m->oargs.nfargs < 3)
291 setcolor(nd.rdiff, .0, .0, .0);
292 else
293 setcolor(nd.rdiff, m->oargs.farg[0],
294 m->oargs.farg[1],
295 m->oargs.farg[2]);
296 } else {
297 if (m->oargs.nfargs < 6) { /* check invisible backside */
298 if (!backvis && (nd.sd->rb == NULL ||
299 nd.sd->rb->maxHemi <= FTINY) &&
300 (nd.sd->tf == NULL ||
301 nd.sd->tf->maxHemi <= FTINY)) {
302 SDfreeCache(nd.sd);
303 raytrans(r);
304 return(1);
305 }
306 setcolor(nd.rdiff, .0, .0, .0);
307 } else
308 setcolor(nd.rdiff, m->oargs.farg[3],
309 m->oargs.farg[4],
310 m->oargs.farg[5]);
311 }
312 /* diffuse transmittance */
313 if (m->oargs.nfargs < 9)
314 setcolor(nd.tdiff, .0, .0, .0);
315 else
316 setcolor(nd.tdiff, m->oargs.farg[6],
317 m->oargs.farg[7],
318 m->oargs.farg[8]);
319 nd.mp = m;
320 nd.pr = r;
321 /* get modifiers */
322 raytexture(r, m->omod);
323 if (bright(r->pcol) <= FTINY) { /* black pattern?! */
324 SDfreeCache(nd.sd);
325 return(1);
326 }
327 /* modify diffuse values */
328 multcolor(nd.rdiff, r->pcol);
329 multcolor(nd.tdiff, r->pcol);
330 /* get up vector */
331 upvec[0] = evalue(mf->ep[1]);
332 upvec[1] = evalue(mf->ep[2]);
333 upvec[2] = evalue(mf->ep[3]);
334 /* return to world coords */
335 if (mf->f != &unitxf) {
336 multv3(upvec, upvec, mf->f->xfm);
337 nd.thick *= mf->f->sca;
338 }
339 raynormal(nd.pnorm, r);
340 /* compute local BSDF xform */
341 ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
342 if (!ec) {
343 nd.vinc[0] = -r->rdir[0];
344 nd.vinc[1] = -r->rdir[1];
345 nd.vinc[2] = -r->rdir[2];
346 ec = SDmapDir(nd.vinc, nd.toloc, nd.vinc);
347 }
348 if (!ec)
349 ec = SDinvXform(nd.fromloc, nd.toloc);
350 if (ec) {
351 objerror(m, WARNING, transSDError(ec));
352 SDfreeCache(nd.sd);
353 return(1);
354 }
355 if (r->rod < .0) { /* perturb normal towards hit */
356 nd.pnorm[0] = -nd.pnorm[0];
357 nd.pnorm[1] = -nd.pnorm[1];
358 nd.pnorm[2] = -nd.pnorm[2];
359 }
360 /* sample reflection */
361 sample_sdf(&nd, SDsampSpR);
362 /* sample transmission */
363 sample_sdf(&nd, SDsampSpT);
364 /* compute indirect diffuse */
365 copycolor(ctmp, nd.rdiff);
366 addcolor(ctmp, nd.runsamp);
367 if (bright(ctmp) > FTINY) { /* ambient from this side */
368 if (r->rod < .0)
369 flipsurface(r);
370 multambient(ctmp, r, nd.pnorm);
371 addcolor(r->rcol, ctmp);
372 if (r->rod < .0)
373 flipsurface(r);
374 }
375 copycolor(ctmp, nd.tdiff);
376 addcolor(ctmp, nd.tunsamp);
377 if (bright(ctmp) > FTINY) { /* ambient from other side */
378 FVECT bnorm;
379 if (r->rod > .0)
380 flipsurface(r);
381 bnorm[0] = -nd.pnorm[0];
382 bnorm[1] = -nd.pnorm[1];
383 bnorm[2] = -nd.pnorm[2];
384 multambient(ctmp, r, bnorm);
385 addcolor(r->rcol, ctmp);
386 if (r->rod > .0)
387 flipsurface(r);
388 }
389 /* add direct component */
390 direct(r, dirbsdf, &nd);
391 /* clean up */
392 SDfreeCache(nd.sd);
393 return(1);
394 }