ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_brdf.c
Revision: 2.32
Committed: Thu Aug 6 16:06:06 2015 UTC (8 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.31: +6 -4 lines
Log Message:
Improved error-checking for 0 arguments in place of BRTDfunc direcitonal-diffuse

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.32 static const char RCSid[] = "$Id: m_brdf.c,v 2.31 2014/01/25 18:27:39 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Shading for materials with arbitrary BRDF's
6     */
7    
8 greg 2.16 #include "copyright.h"
9 greg 2.15
10 greg 1.1 #include "ray.h"
11 greg 2.20 #include "ambient.h"
12 greg 1.1 #include "data.h"
13 schorsch 2.21 #include "source.h"
14 greg 1.1 #include "otypes.h"
15 schorsch 2.21 #include "rtotypes.h"
16 greg 2.2 #include "func.h"
17    
18 greg 1.1 /*
19     * Arguments to this material include the color and specularity.
20     * String arguments include the reflection function and files.
21     * The BRDF is currently used just for the specular component to light
22     * sources. Reflectance values or data coordinates are functions
23 greg 2.7 * of the direction to the light source. (Data modification functions
24     * are passed the source direction as args 2-4.)
25 greg 1.1 * We orient the surface towards the incoming ray, so a single
26     * surface can be used to represent an infinitely thin object.
27     *
28     * Arguments for MAT_PFUNC and MAT_MFUNC are:
29 greg 1.4 * 2+ func funcfile transform
30 greg 1.1 * 0
31 greg 1.4 * 4+ red grn blu specularity A5 ..
32 greg 1.1 *
33     * Arguments for MAT_PDATA and MAT_MDATA are:
34 greg 1.4 * 4+ func datafile funcfile v0 .. transform
35 greg 1.1 * 0
36 greg 1.4 * 4+ red grn blu specularity A5 ..
37 greg 1.5 *
38     * Arguments for MAT_TFUNC are:
39     * 2+ func funcfile transform
40     * 0
41 greg 2.29 * 6+ red grn blu rspec trans tspec A7 ..
42 greg 1.5 *
43     * Arguments for MAT_TDATA are:
44     * 4+ func datafile funcfile v0 .. transform
45     * 0
46 greg 2.29 * 6+ red grn blu rspec trans tspec A7 ..
47 greg 1.5 *
48     * Arguments for the more general MAT_BRTDF are:
49     * 10+ rrefl grefl brefl
50     * rtrns gtrns btrns
51     * rbrtd gbrtd bbrtd
52     * funcfile transform
53     * 0
54 greg 2.6 * 9+ rdf gdf bdf
55     * rdb gdb bdb
56     * rdt gdt bdt A10 ..
57 greg 1.5 *
58     * In addition to the normal variables available to functions,
59     * we define the following:
60     * NxP, NyP, NzP - perturbed surface normal
61     * RdotP - perturbed ray dot product
62 greg 2.6 * CrP, CgP, CbP - perturbed material color (or pattern)
63 greg 1.1 */
64    
65     typedef struct {
66     OBJREC *mp; /* material pointer */
67     RAY *pr; /* intersected ray */
68 greg 1.5 DATARRAY *dp; /* data array for PDATA, MDATA or TDATA */
69 greg 2.6 COLOR mcolor; /* material (or pattern) color */
70     COLOR rdiff; /* diffuse reflection */
71     COLOR tdiff; /* diffuse transmission */
72     double rspec; /* specular reflectance (1 for BRDTF) */
73     double trans; /* transmissivity (.5 for BRDTF) */
74     double tspec; /* specular transmittance (1 for BRDTF) */
75 greg 1.1 FVECT pnorm; /* perturbed surface normal */
76     double pdot; /* perturbed dot product */
77     } BRDFDAT; /* BRDF material data */
78    
79    
80 greg 2.28 static int setbrdfunc(BRDFDAT *np);
81 schorsch 2.21
82    
83 greg 2.15 static void
84 schorsch 2.21 dirbrdf( /* compute source contribution */
85     COLOR cval, /* returned coefficient */
86     void *nnp, /* material data */
87     FVECT ldir, /* light source direction */
88     double omega /* light source size */
89     )
90 greg 1.1 {
91 greg 2.28 BRDFDAT *np = nnp;
92 greg 1.1 double ldot;
93     double dtmp;
94     COLOR ctmp;
95 greg 1.4 FVECT ldx;
96 greg 2.12 static double vldx[5], pt[MAXDIM];
97 greg 2.28 char **sa;
98     int i;
99 greg 2.12 #define lddx (vldx+1)
100 greg 1.1
101     setcolor(cval, 0.0, 0.0, 0.0);
102    
103     ldot = DOT(np->pnorm, ldir);
104    
105 greg 1.5 if (ldot <= FTINY && ldot >= -FTINY)
106     return; /* too close to grazing */
107 greg 2.6
108 greg 1.5 if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
109 greg 1.1 return; /* wrong side */
110    
111 greg 2.6 if (ldot > 0.0) {
112 greg 1.1 /*
113     * Compute and add diffuse reflected component to returned
114     * color. The diffuse reflected component will always be
115     * modified by the color of the material.
116     */
117 greg 2.6 copycolor(ctmp, np->rdiff);
118     dtmp = ldot * omega / PI;
119 greg 1.1 scalecolor(ctmp, dtmp);
120     addcolor(cval, ctmp);
121 greg 2.6 } else {
122 greg 1.1 /*
123 greg 1.5 * Diffuse transmitted component.
124 greg 1.1 */
125 greg 2.6 copycolor(ctmp, np->tdiff);
126     dtmp = -ldot * omega / PI;
127 greg 1.5 scalecolor(ctmp, dtmp);
128     addcolor(cval, ctmp);
129 greg 1.1 }
130 greg 1.5 if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY)
131 greg 2.24 return; /* diffuse only */
132 greg 1.5 /* set up function */
133 greg 1.10 setbrdfunc(np);
134 greg 1.5 sa = np->mp->oargs.sarg;
135     errno = 0;
136     /* transform light vector */
137     multv3(ldx, ldir, funcxf.xfm);
138     for (i = 0; i < 3; i++)
139 greg 2.3 lddx[i] = ldx[i]/funcxf.sca;
140 greg 2.12 lddx[3] = omega;
141 greg 1.5 /* compute BRTDF */
142     if (np->mp->otype == MAT_BRTDF) {
143 greg 2.32 if (sa[6][0] == '0' && !sa[6][1]) /* special case */
144 greg 2.6 colval(ctmp,RED) = 0.0;
145     else
146 greg 2.12 colval(ctmp,RED) = funvalue(sa[6], 4, lddx);
147 greg 2.32 if (sa[7][0] == '0' && !sa[7][1])
148 greg 2.24 colval(ctmp,GRN) = 0.0;
149     else if (!strcmp(sa[7],sa[6]))
150 greg 1.5 colval(ctmp,GRN) = colval(ctmp,RED);
151     else
152 greg 2.12 colval(ctmp,GRN) = funvalue(sa[7], 4, lddx);
153 greg 2.32 if (sa[8][0] == '0' && !sa[8][1])
154     colval(ctmp,BLU) = 0.0;
155     else if (!strcmp(sa[8],sa[6]))
156 greg 1.5 colval(ctmp,BLU) = colval(ctmp,RED);
157 greg 1.7 else if (!strcmp(sa[8],sa[7]))
158 greg 1.5 colval(ctmp,BLU) = colval(ctmp,GRN);
159     else
160 greg 2.12 colval(ctmp,BLU) = funvalue(sa[8], 4, lddx);
161 greg 1.5 dtmp = bright(ctmp);
162     } else if (np->dp == NULL) {
163 greg 2.12 dtmp = funvalue(sa[0], 4, lddx);
164 greg 1.5 setcolor(ctmp, dtmp, dtmp, dtmp);
165     } else {
166     for (i = 0; i < np->dp->nd; i++)
167 greg 2.12 pt[i] = funvalue(sa[3+i], 4, lddx);
168 greg 2.7 vldx[0] = datavalue(np->dp, pt);
169 greg 2.12 dtmp = funvalue(sa[0], 5, vldx);
170 greg 1.5 setcolor(ctmp, dtmp, dtmp, dtmp);
171     }
172 greg 2.25 if ((errno == EDOM) | (errno == ERANGE)) {
173 greg 2.2 objerror(np->mp, WARNING, "compute error");
174     return;
175     }
176 greg 1.5 if (dtmp <= FTINY)
177     return;
178     if (ldot > 0.0) {
179     /*
180     * Compute reflected non-diffuse component.
181     */
182 schorsch 2.19 if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA))
183 greg 1.6 multcolor(ctmp, np->mcolor);
184     dtmp = ldot * omega * np->rspec;
185 greg 1.5 scalecolor(ctmp, dtmp);
186     addcolor(cval, ctmp);
187     } else {
188     /*
189     * Compute transmitted non-diffuse component.
190     */
191 schorsch 2.19 if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA))
192 greg 1.6 multcolor(ctmp, np->mcolor);
193 greg 1.5 dtmp = -ldot * omega * np->tspec;
194     scalecolor(ctmp, dtmp);
195     addcolor(cval, ctmp);
196     }
197 greg 2.12 #undef lddx
198 greg 1.1 }
199    
200    
201 greg 2.28 int
202 schorsch 2.21 m_brdf( /* color a ray that hit a BRDTfunc material */
203 greg 2.28 OBJREC *m,
204     RAY *r
205 schorsch 2.21 )
206 greg 1.1 {
207 greg 2.15 int hitfront = 1;
208 greg 1.1 BRDFDAT nd;
209 greg 2.6 RAY sr;
210 greg 2.22 double mirtest=0, mirdist=0;
211 greg 2.30 double transtest=0, transdist=0;
212 greg 2.6 int hasrefl, hastrans;
213 greg 2.22 int hastexture;
214 greg 1.1 COLOR ctmp;
215 greg 2.14 FVECT vtmp;
216 greg 2.22 double d;
217 greg 2.28 MFUNC *mf;
218     int i;
219 greg 1.5 /* check arguments */
220 schorsch 2.19 if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
221 greg 2.6 objerror(m, USER, "bad # arguments");
222     nd.mp = m;
223     nd.pr = r;
224     /* dummy values */
225     nd.rspec = nd.tspec = 1.0;
226     nd.trans = 0.5;
227     /* diffuse reflectance */
228     if (r->rod > 0.0)
229     setcolor(nd.rdiff, m->oargs.farg[0],
230     m->oargs.farg[1],
231     m->oargs.farg[2]);
232     else
233     setcolor(nd.rdiff, m->oargs.farg[3],
234     m->oargs.farg[4],
235     m->oargs.farg[5]);
236     /* diffuse transmittance */
237     setcolor(nd.tdiff, m->oargs.farg[6],
238     m->oargs.farg[7],
239     m->oargs.farg[8]);
240 greg 2.17 /* get modifiers */
241 greg 2.6 raytexture(r, m->omod);
242 greg 2.22 hastexture = DOT(r->pert,r->pert) > FTINY*FTINY;
243     if (hastexture) { /* perturb normal */
244     nd.pdot = raynormal(nd.pnorm, r);
245     } else {
246     VCOPY(nd.pnorm, r->ron);
247     nd.pdot = r->rod;
248     }
249 greg 2.6 if (r->rod < 0.0) { /* orient perturbed values */
250     nd.pdot = -nd.pdot;
251     for (i = 0; i < 3; i++) {
252     nd.pnorm[i] = -nd.pnorm[i];
253     r->pert[i] = -r->pert[i];
254     }
255 greg 2.15 hitfront = 0;
256 greg 1.5 }
257 greg 2.6 copycolor(nd.mcolor, r->pcol); /* get pattern color */
258     multcolor(nd.rdiff, nd.mcolor); /* modify diffuse values */
259     multcolor(nd.tdiff, nd.mcolor);
260     hasrefl = bright(nd.rdiff) > FTINY;
261     hastrans = bright(nd.tdiff) > FTINY;
262     /* load cal file */
263     nd.dp = NULL;
264     mf = getfunc(m, 9, 0x3f, 0);
265     /* compute transmitted ray */
266     setbrdfunc(&nd);
267     errno = 0;
268     setcolor(ctmp, evalue(mf->ep[3]),
269     evalue(mf->ep[4]),
270     evalue(mf->ep[5]));
271 greg 2.25 if ((errno == EDOM) | (errno == ERANGE))
272 greg 2.6 objerror(m, WARNING, "compute error");
273 greg 2.23 else if (rayorigin(&sr, TRANS, r, ctmp) == 0) {
274 greg 2.22 if (!(r->crtype & SHADOW) && hastexture) {
275 greg 2.27 /* perturb direction */
276     VSUM(sr.rdir, r->rdir, r->pert, -.75);
277 greg 2.6 if (normalize(sr.rdir) == 0.0) {
278     objerror(m, WARNING, "illegal perturbation");
279     VCOPY(sr.rdir, r->rdir);
280     }
281     } else {
282     VCOPY(sr.rdir, r->rdir);
283     }
284     rayvalue(&sr);
285 greg 2.23 multcolor(sr.rcol, sr.rcoef);
286 greg 2.6 addcolor(r->rcol, sr.rcol);
287 greg 2.22 if (!hastexture) {
288     transtest = 2.0*bright(sr.rcol);
289     transdist = r->rot + sr.rt;
290     }
291 greg 2.6 }
292     if (r->crtype & SHADOW) /* the rest is shadow */
293 greg 2.10 return(1);
294 greg 2.6 /* compute reflected ray */
295     setbrdfunc(&nd);
296     errno = 0;
297     setcolor(ctmp, evalue(mf->ep[0]),
298     evalue(mf->ep[1]),
299     evalue(mf->ep[2]));
300 greg 2.25 if ((errno == EDOM) | (errno == ERANGE))
301 greg 2.6 objerror(m, WARNING, "compute error");
302 greg 2.23 else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) {
303 greg 2.27 VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot);
304 greg 2.26 checknorm(sr.rdir);
305 greg 2.6 rayvalue(&sr);
306 greg 2.23 multcolor(sr.rcol, sr.rcoef);
307 greg 2.6 addcolor(r->rcol, sr.rcol);
308 greg 2.22 if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) {
309     mirtest = 2.0*bright(sr.rcol);
310     mirdist = r->rot + sr.rt;
311     }
312 greg 2.6 }
313     /* compute ambient */
314     if (hasrefl) {
315 greg 2.15 if (!hitfront)
316 greg 2.6 flipsurface(r);
317 greg 2.23 copycolor(ctmp, nd.rdiff);
318     multambient(ctmp, r, nd.pnorm);
319 greg 2.6 addcolor(r->rcol, ctmp); /* add to returned color */
320 greg 2.15 if (!hitfront)
321 greg 2.6 flipsurface(r);
322     }
323     if (hastrans) { /* from other side */
324 greg 2.15 if (hitfront)
325 greg 2.6 flipsurface(r);
326 greg 2.15 vtmp[0] = -nd.pnorm[0];
327     vtmp[1] = -nd.pnorm[1];
328     vtmp[2] = -nd.pnorm[2];
329 greg 2.23 copycolor(ctmp, nd.tdiff);
330     multambient(ctmp, r, vtmp);
331 greg 2.6 addcolor(r->rcol, ctmp);
332 greg 2.15 if (hitfront)
333 greg 2.6 flipsurface(r);
334     }
335     if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
336     direct(r, dirbrdf, &nd); /* add direct component */
337 greg 2.22
338     d = bright(r->rcol); /* set effective distance */
339     if (transtest > d)
340 greg 2.6 r->rt = transdist;
341 greg 2.22 else if (mirtest > d)
342     r->rt = mirdist;
343 greg 2.10
344     return(1);
345 greg 2.6 }
346    
347    
348    
349 greg 2.28 int
350 schorsch 2.21 m_brdf2( /* color a ray that hit a BRDF material */
351 greg 2.28 OBJREC *m,
352     RAY *r
353 schorsch 2.21 )
354 greg 2.6 {
355     BRDFDAT nd;
356     COLOR ctmp;
357 greg 2.14 FVECT vtmp;
358 greg 2.6 double dtmp;
359     /* always a shadow */
360     if (r->crtype & SHADOW)
361 greg 2.10 return(1);
362 greg 2.6 /* check arguments */
363 schorsch 2.19 if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
364     ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
365 greg 1.1 objerror(m, USER, "bad # arguments");
366 greg 2.17 /* check for back side */
367     if (r->rod < 0.0) {
368 greg 2.31 if (!backvis) {
369 greg 2.17 raytrans(r);
370     return(1);
371     }
372     raytexture(r, m->omod);
373     flipsurface(r); /* reorient if backvis */
374     } else
375     raytexture(r, m->omod);
376    
377 greg 1.1 nd.mp = m;
378     nd.pr = r;
379 greg 2.6 /* get material color */
380     setcolor(nd.mcolor, m->oargs.farg[0],
381     m->oargs.farg[1],
382     m->oargs.farg[2]);
383 greg 1.5 /* get specular component */
384     nd.rspec = m->oargs.farg[3];
385 greg 2.6 /* compute transmittance */
386 schorsch 2.19 if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
387 greg 1.5 nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
388     nd.tspec = nd.trans * m->oargs.farg[5];
389 greg 2.6 dtmp = nd.trans - nd.tspec;
390     setcolor(nd.tdiff, dtmp, dtmp, dtmp);
391     } else {
392     nd.tspec = nd.trans = 0.0;
393     setcolor(nd.tdiff, 0.0, 0.0, 0.0);
394     }
395     /* compute reflectance */
396     dtmp = 1.0 - nd.trans - nd.rspec;
397     setcolor(nd.rdiff, dtmp, dtmp, dtmp);
398 greg 1.5 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
399     multcolor(nd.mcolor, r->pcol); /* modify material color */
400 greg 2.6 multcolor(nd.rdiff, nd.mcolor);
401     multcolor(nd.tdiff, nd.mcolor);
402 greg 1.1 /* load auxiliary files */
403 greg 2.2 if (hasdata(m->otype)) {
404 greg 1.1 nd.dp = getdata(m->oargs.sarg[1]);
405 greg 2.6 getfunc(m, 2, 0, 0);
406 greg 1.1 } else {
407     nd.dp = NULL;
408 greg 2.6 getfunc(m, 1, 0, 0);
409 greg 1.1 }
410     /* compute ambient */
411 greg 2.6 if (nd.trans < 1.0-FTINY) {
412 greg 2.23 copycolor(ctmp, nd.mcolor); /* modified by material color */
413 greg 2.6 scalecolor(ctmp, 1.0-nd.trans);
414 greg 2.23 multambient(ctmp, r, nd.pnorm);
415 greg 1.1 addcolor(r->rcol, ctmp); /* add to returned color */
416 greg 1.5 }
417 greg 2.6 if (nd.trans > FTINY) { /* from other side */
418 greg 1.5 flipsurface(r);
419 greg 2.14 vtmp[0] = -nd.pnorm[0];
420     vtmp[1] = -nd.pnorm[1];
421     vtmp[2] = -nd.pnorm[2];
422 greg 2.23 copycolor(ctmp, nd.mcolor);
423 greg 2.6 scalecolor(ctmp, nd.trans);
424 greg 2.23 multambient(ctmp, r, vtmp);
425 greg 1.5 addcolor(r->rcol, ctmp);
426     flipsurface(r);
427 greg 1.1 }
428     /* add direct component */
429     direct(r, dirbrdf, &nd);
430 greg 2.10
431     return(1);
432 greg 1.10 }
433    
434    
435 schorsch 2.21 static int
436     setbrdfunc( /* set up brdf function and variables */
437 greg 2.28 BRDFDAT *np
438 schorsch 2.21 )
439 greg 1.10 {
440     FVECT vec;
441    
442     if (setfunc(np->mp, np->pr) == 0)
443     return(0); /* it's OK, setfunc says we're done */
444     /* else (re)assign special variables */
445     multv3(vec, np->pnorm, funcxf.xfm);
446     varset("NxP", '=', vec[0]/funcxf.sca);
447     varset("NyP", '=', vec[1]/funcxf.sca);
448     varset("NzP", '=', vec[2]/funcxf.sca);
449 greg 1.11 varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
450     np->pdot >= 1.0 ? 1.0 : np->pdot);
451 greg 1.10 varset("CrP", '=', colval(np->mcolor,RED));
452     varset("CgP", '=', colval(np->mcolor,GRN));
453     varset("CbP", '=', colval(np->mcolor,BLU));
454     return(1);
455 greg 1.1 }