ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.3
Committed: Sat Feb 19 01:48:59 2011 UTC (13 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +15 -18 lines
Log Message:
Minor changes and fixes -- first working version of BSDF material

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.3 static const char RCSid[] = "$Id: m_bsdf.c,v 2.2 2011/02/18 02:41:55 greg Exp $";
3 greg 2.1 #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 greg 2.3 BSDFDAT *np = (BSDFDAT *)nnp;
77 greg 2.1 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 greg 2.2 objerror(np->mp, USER, transSDError(ec));
116 greg 2.1
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 greg 2.3 multcolor(ctmp, ctmp1); /* apply derated pattern */
130 greg 2.1 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 greg 2.2 objerror(ndp->mp, USER, transSDError(ec));
167 greg 2.1 /* 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 greg 2.3 if (ndp->thick > FTINY) { /* 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 greg 2.1 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 greg 2.3 if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */
229 greg 2.1 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 greg 2.2 /* get BSDF data */
266     nd.sd = loadBSDF(m->oargs.sarg[1]);
267 greg 2.1 /* 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 greg 2.3 if ((nd.thick > FTINY) & (nd.sd->tf != NULL))
276     raytrans(r); /* pass-through */
277 greg 2.1 SDfreeCache(nd.sd);
278     return(1); /* else shadow */
279     }
280     /* check unscattered ray */
281 greg 2.3 if (!(r->crtype & (SPECULAR|AMBIENT)) &&
282     (nd.thick > FTINY) & (nd.sd->tf != NULL)) {
283 greg 2.1 SDfreeCache(nd.sd);
284     raytrans(r); /* pass-through */
285     return(1);
286     }
287     /* diffuse reflectance */
288     if (r->rod > .0) {
289     if (m->oargs.nfargs < 3)
290     setcolor(nd.rdiff, .0, .0, .0);
291     else
292     setcolor(nd.rdiff, m->oargs.farg[0],
293     m->oargs.farg[1],
294     m->oargs.farg[2]);
295     } else {
296     if (m->oargs.nfargs < 6) { /* check invisible backside */
297 greg 2.3 if (!backvis && (nd.sd->rb == NULL) &
298     (nd.sd->tf == NULL)) {
299 greg 2.1 SDfreeCache(nd.sd);
300     raytrans(r);
301     return(1);
302     }
303     setcolor(nd.rdiff, .0, .0, .0);
304     } else
305     setcolor(nd.rdiff, m->oargs.farg[3],
306     m->oargs.farg[4],
307     m->oargs.farg[5]);
308     }
309     /* diffuse transmittance */
310     if (m->oargs.nfargs < 9)
311     setcolor(nd.tdiff, .0, .0, .0);
312     else
313     setcolor(nd.tdiff, m->oargs.farg[6],
314     m->oargs.farg[7],
315     m->oargs.farg[8]);
316     nd.mp = m;
317     nd.pr = r;
318     /* get modifiers */
319     raytexture(r, m->omod);
320     if (bright(r->pcol) <= FTINY) { /* black pattern?! */
321     SDfreeCache(nd.sd);
322     return(1);
323     }
324     /* modify diffuse values */
325     multcolor(nd.rdiff, r->pcol);
326     multcolor(nd.tdiff, r->pcol);
327     /* get up vector */
328     upvec[0] = evalue(mf->ep[1]);
329     upvec[1] = evalue(mf->ep[2]);
330     upvec[2] = evalue(mf->ep[3]);
331     /* return to world coords */
332     if (mf->f != &unitxf) {
333     multv3(upvec, upvec, mf->f->xfm);
334     nd.thick *= mf->f->sca;
335     }
336     raynormal(nd.pnorm, r);
337     /* compute local BSDF xform */
338     ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
339     if (!ec) {
340     nd.vinc[0] = -r->rdir[0];
341     nd.vinc[1] = -r->rdir[1];
342     nd.vinc[2] = -r->rdir[2];
343     ec = SDmapDir(nd.vinc, nd.toloc, nd.vinc);
344     }
345     if (!ec)
346     ec = SDinvXform(nd.fromloc, nd.toloc);
347     if (ec) {
348 greg 2.2 objerror(m, WARNING, transSDError(ec));
349 greg 2.1 SDfreeCache(nd.sd);
350     return(1);
351     }
352     if (r->rod < .0) { /* perturb normal towards hit */
353     nd.pnorm[0] = -nd.pnorm[0];
354     nd.pnorm[1] = -nd.pnorm[1];
355     nd.pnorm[2] = -nd.pnorm[2];
356     }
357     /* sample reflection */
358     sample_sdf(&nd, SDsampSpR);
359     /* sample transmission */
360     sample_sdf(&nd, SDsampSpT);
361     /* compute indirect diffuse */
362     copycolor(ctmp, nd.rdiff);
363     addcolor(ctmp, nd.runsamp);
364     if (bright(ctmp) > FTINY) { /* ambient from this side */
365     if (r->rod < .0)
366     flipsurface(r);
367     multambient(ctmp, r, nd.pnorm);
368     addcolor(r->rcol, ctmp);
369     if (r->rod < .0)
370     flipsurface(r);
371     }
372     copycolor(ctmp, nd.tdiff);
373     addcolor(ctmp, nd.tunsamp);
374     if (bright(ctmp) > FTINY) { /* ambient from other side */
375     FVECT bnorm;
376     if (r->rod > .0)
377     flipsurface(r);
378     bnorm[0] = -nd.pnorm[0];
379     bnorm[1] = -nd.pnorm[1];
380     bnorm[2] = -nd.pnorm[2];
381     multambient(ctmp, r, bnorm);
382     addcolor(r->rcol, ctmp);
383     if (r->rod > .0)
384     flipsurface(r);
385     }
386     /* add direct component */
387     direct(r, dirbsdf, &nd);
388     /* clean up */
389     SDfreeCache(nd.sd);
390     return(1);
391     }