ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_brdf.c
Revision: 2.19
Committed: Sun Jul 27 22:12:03 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.18: +7 -7 lines
Log Message:
Added grouping parens to reduce ambiguity warnings.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.19 static const char RCSid[] = "$Id: m_brdf.c,v 2.18 2003/03/05 16:16:53 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    
12     #include "data.h"
13    
14     #include "otypes.h"
15    
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     * 4+ red grn blu rspec trans tspec A7 ..
42     *
43     * Arguments for MAT_TDATA are:
44     * 4+ func datafile funcfile v0 .. transform
45     * 0
46     * 4+ red grn blu rspec trans tspec A7 ..
47     *
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.15 static void
81 greg 1.1 dirbrdf(cval, np, ldir, omega) /* compute source contribution */
82     COLOR cval; /* returned coefficient */
83     register BRDFDAT *np; /* material data */
84     FVECT ldir; /* light source direction */
85     double omega; /* light source size */
86     {
87     double ldot;
88     double dtmp;
89     COLOR ctmp;
90 greg 1.4 FVECT ldx;
91 greg 2.12 static double vldx[5], pt[MAXDIM];
92 greg 1.5 register char **sa;
93 greg 1.1 register int i;
94 greg 2.12 #define lddx (vldx+1)
95 greg 1.1
96     setcolor(cval, 0.0, 0.0, 0.0);
97    
98     ldot = DOT(np->pnorm, ldir);
99    
100 greg 1.5 if (ldot <= FTINY && ldot >= -FTINY)
101     return; /* too close to grazing */
102 greg 2.6
103 greg 1.5 if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
104 greg 1.1 return; /* wrong side */
105    
106 greg 2.6 if (ldot > 0.0) {
107 greg 1.1 /*
108     * Compute and add diffuse reflected component to returned
109     * color. The diffuse reflected component will always be
110     * modified by the color of the material.
111     */
112 greg 2.6 copycolor(ctmp, np->rdiff);
113     dtmp = ldot * omega / PI;
114 greg 1.1 scalecolor(ctmp, dtmp);
115     addcolor(cval, ctmp);
116 greg 2.6 } else {
117 greg 1.1 /*
118 greg 1.5 * Diffuse transmitted component.
119 greg 1.1 */
120 greg 2.6 copycolor(ctmp, np->tdiff);
121     dtmp = -ldot * omega / PI;
122 greg 1.5 scalecolor(ctmp, dtmp);
123     addcolor(cval, ctmp);
124 greg 1.1 }
125 greg 1.5 if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY)
126     return; /* no specular component */
127     /* set up function */
128 greg 1.10 setbrdfunc(np);
129 greg 1.5 sa = np->mp->oargs.sarg;
130     errno = 0;
131     /* transform light vector */
132     multv3(ldx, ldir, funcxf.xfm);
133     for (i = 0; i < 3; i++)
134 greg 2.3 lddx[i] = ldx[i]/funcxf.sca;
135 greg 2.12 lddx[3] = omega;
136 greg 1.5 /* compute BRTDF */
137     if (np->mp->otype == MAT_BRTDF) {
138 greg 2.6 if (sa[6][0] == '0') /* special case */
139     colval(ctmp,RED) = 0.0;
140     else
141 greg 2.12 colval(ctmp,RED) = funvalue(sa[6], 4, lddx);
142 greg 1.7 if (!strcmp(sa[7],sa[6]))
143 greg 1.5 colval(ctmp,GRN) = colval(ctmp,RED);
144     else
145 greg 2.12 colval(ctmp,GRN) = funvalue(sa[7], 4, lddx);
146 greg 1.7 if (!strcmp(sa[8],sa[6]))
147 greg 1.5 colval(ctmp,BLU) = colval(ctmp,RED);
148 greg 1.7 else if (!strcmp(sa[8],sa[7]))
149 greg 1.5 colval(ctmp,BLU) = colval(ctmp,GRN);
150     else
151 greg 2.12 colval(ctmp,BLU) = funvalue(sa[8], 4, lddx);
152 greg 1.5 dtmp = bright(ctmp);
153     } else if (np->dp == NULL) {
154 greg 2.12 dtmp = funvalue(sa[0], 4, lddx);
155 greg 1.5 setcolor(ctmp, dtmp, dtmp, dtmp);
156     } else {
157     for (i = 0; i < np->dp->nd; i++)
158 greg 2.12 pt[i] = funvalue(sa[3+i], 4, lddx);
159 greg 2.7 vldx[0] = datavalue(np->dp, pt);
160 greg 2.12 dtmp = funvalue(sa[0], 5, vldx);
161 greg 1.5 setcolor(ctmp, dtmp, dtmp, dtmp);
162     }
163 greg 2.18 if (errno == EDOM || errno == ERANGE) {
164 greg 2.2 objerror(np->mp, WARNING, "compute error");
165     return;
166     }
167 greg 1.5 if (dtmp <= FTINY)
168     return;
169     if (ldot > 0.0) {
170     /*
171     * Compute reflected non-diffuse component.
172     */
173 schorsch 2.19 if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA))
174 greg 1.6 multcolor(ctmp, np->mcolor);
175     dtmp = ldot * omega * np->rspec;
176 greg 1.5 scalecolor(ctmp, dtmp);
177     addcolor(cval, ctmp);
178     } else {
179     /*
180     * Compute transmitted non-diffuse component.
181     */
182 schorsch 2.19 if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA))
183 greg 1.6 multcolor(ctmp, np->mcolor);
184 greg 1.5 dtmp = -ldot * omega * np->tspec;
185     scalecolor(ctmp, dtmp);
186     addcolor(cval, ctmp);
187     }
188 greg 2.12 #undef lddx
189 greg 1.1 }
190    
191    
192 greg 2.15 int
193     m_brdf(m, r) /* color a ray that hit a BRDTfunc material */
194 greg 1.1 register OBJREC *m;
195     register RAY *r;
196     {
197 greg 2.15 int hitfront = 1;
198 greg 1.1 BRDFDAT nd;
199 greg 2.6 RAY sr;
200 greg 1.7 double transtest, transdist;
201 greg 2.6 int hasrefl, hastrans;
202 greg 1.1 COLOR ctmp;
203 greg 2.14 FVECT vtmp;
204 greg 2.6 register MFUNC *mf;
205 greg 1.1 register int i;
206 greg 1.5 /* check arguments */
207 schorsch 2.19 if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
208 greg 2.6 objerror(m, USER, "bad # arguments");
209     nd.mp = m;
210     nd.pr = r;
211     /* dummy values */
212     nd.rspec = nd.tspec = 1.0;
213     nd.trans = 0.5;
214     /* diffuse reflectance */
215     if (r->rod > 0.0)
216     setcolor(nd.rdiff, m->oargs.farg[0],
217     m->oargs.farg[1],
218     m->oargs.farg[2]);
219     else
220     setcolor(nd.rdiff, m->oargs.farg[3],
221     m->oargs.farg[4],
222     m->oargs.farg[5]);
223     /* diffuse transmittance */
224     setcolor(nd.tdiff, m->oargs.farg[6],
225     m->oargs.farg[7],
226     m->oargs.farg[8]);
227 greg 2.17 /* get modifiers */
228 greg 2.6 raytexture(r, m->omod);
229     nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
230     if (r->rod < 0.0) { /* orient perturbed values */
231     nd.pdot = -nd.pdot;
232     for (i = 0; i < 3; i++) {
233     nd.pnorm[i] = -nd.pnorm[i];
234     r->pert[i] = -r->pert[i];
235     }
236 greg 2.15 hitfront = 0;
237 greg 1.5 }
238 greg 2.6 copycolor(nd.mcolor, r->pcol); /* get pattern color */
239     multcolor(nd.rdiff, nd.mcolor); /* modify diffuse values */
240     multcolor(nd.tdiff, nd.mcolor);
241     hasrefl = bright(nd.rdiff) > FTINY;
242     hastrans = bright(nd.tdiff) > FTINY;
243     /* load cal file */
244     nd.dp = NULL;
245     mf = getfunc(m, 9, 0x3f, 0);
246     /* compute transmitted ray */
247     setbrdfunc(&nd);
248     transtest = 0;
249 greg 2.8 transdist = r->rot;
250 greg 2.6 errno = 0;
251     setcolor(ctmp, evalue(mf->ep[3]),
252     evalue(mf->ep[4]),
253     evalue(mf->ep[5]));
254 greg 2.18 if (errno == EDOM || errno == ERANGE)
255 greg 2.6 objerror(m, WARNING, "compute error");
256     else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
257     if (!(r->crtype & SHADOW) &&
258     DOT(r->pert,r->pert) > FTINY*FTINY) {
259     for (i = 0; i < 3; i++) /* perturb direction */
260     sr.rdir[i] = r->rdir[i] - .75*r->pert[i];
261     if (normalize(sr.rdir) == 0.0) {
262     objerror(m, WARNING, "illegal perturbation");
263     VCOPY(sr.rdir, r->rdir);
264     }
265     } else {
266     VCOPY(sr.rdir, r->rdir);
267     transtest = 2;
268     }
269     rayvalue(&sr);
270     multcolor(sr.rcol, ctmp);
271     addcolor(r->rcol, sr.rcol);
272     transtest *= bright(sr.rcol);
273     transdist = r->rot + sr.rt;
274     }
275     if (r->crtype & SHADOW) /* the rest is shadow */
276 greg 2.10 return(1);
277 greg 2.6 /* compute reflected ray */
278     setbrdfunc(&nd);
279     errno = 0;
280     setcolor(ctmp, evalue(mf->ep[0]),
281     evalue(mf->ep[1]),
282     evalue(mf->ep[2]));
283 greg 2.18 if (errno == EDOM || errno == ERANGE)
284 greg 2.6 objerror(m, WARNING, "compute error");
285     else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
286     for (i = 0; i < 3; i++)
287     sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
288     rayvalue(&sr);
289     multcolor(sr.rcol, ctmp);
290     addcolor(r->rcol, sr.rcol);
291     }
292     /* compute ambient */
293     if (hasrefl) {
294 greg 2.15 if (!hitfront)
295 greg 2.6 flipsurface(r);
296 greg 2.15 ambient(ctmp, r, nd.pnorm);
297 greg 2.6 multcolor(ctmp, nd.rdiff);
298     addcolor(r->rcol, ctmp); /* add to returned color */
299 greg 2.15 if (!hitfront)
300 greg 2.6 flipsurface(r);
301     }
302     if (hastrans) { /* from other side */
303 greg 2.15 if (hitfront)
304 greg 2.6 flipsurface(r);
305 greg 2.15 vtmp[0] = -nd.pnorm[0];
306     vtmp[1] = -nd.pnorm[1];
307     vtmp[2] = -nd.pnorm[2];
308 greg 2.14 ambient(ctmp, r, vtmp);
309 greg 2.6 multcolor(ctmp, nd.tdiff);
310     addcolor(r->rcol, ctmp);
311 greg 2.15 if (hitfront)
312 greg 2.6 flipsurface(r);
313     }
314     if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
315     direct(r, dirbrdf, &nd); /* add direct component */
316     /* check distance */
317     if (transtest > bright(r->rcol))
318     r->rt = transdist;
319 greg 2.10
320     return(1);
321 greg 2.6 }
322    
323    
324    
325 greg 2.15 int
326     m_brdf2(m, r) /* color a ray that hit a BRDF material */
327 greg 2.6 register OBJREC *m;
328     register RAY *r;
329     {
330     BRDFDAT nd;
331     COLOR ctmp;
332 greg 2.14 FVECT vtmp;
333 greg 2.6 double dtmp;
334     /* always a shadow */
335     if (r->crtype & SHADOW)
336 greg 2.10 return(1);
337 greg 2.6 /* check arguments */
338 schorsch 2.19 if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
339     ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
340 greg 1.1 objerror(m, USER, "bad # arguments");
341 greg 2.17 /* check for back side */
342     if (r->rod < 0.0) {
343     if (!backvis && m->otype != MAT_TFUNC
344     && m->otype != MAT_TDATA) {
345     raytrans(r);
346     return(1);
347     }
348     raytexture(r, m->omod);
349     flipsurface(r); /* reorient if backvis */
350     } else
351     raytexture(r, m->omod);
352    
353 greg 1.1 nd.mp = m;
354     nd.pr = r;
355 greg 2.6 /* get material color */
356     setcolor(nd.mcolor, m->oargs.farg[0],
357     m->oargs.farg[1],
358     m->oargs.farg[2]);
359 greg 1.5 /* get specular component */
360     nd.rspec = m->oargs.farg[3];
361 greg 2.6 /* compute transmittance */
362 schorsch 2.19 if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
363 greg 1.5 nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
364     nd.tspec = nd.trans * m->oargs.farg[5];
365 greg 2.6 dtmp = nd.trans - nd.tspec;
366     setcolor(nd.tdiff, dtmp, dtmp, dtmp);
367     } else {
368     nd.tspec = nd.trans = 0.0;
369     setcolor(nd.tdiff, 0.0, 0.0, 0.0);
370     }
371     /* compute reflectance */
372     dtmp = 1.0 - nd.trans - nd.rspec;
373     setcolor(nd.rdiff, dtmp, dtmp, dtmp);
374 greg 1.5 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
375     multcolor(nd.mcolor, r->pcol); /* modify material color */
376 greg 2.6 multcolor(nd.rdiff, nd.mcolor);
377     multcolor(nd.tdiff, nd.mcolor);
378 greg 1.1 /* load auxiliary files */
379 greg 2.2 if (hasdata(m->otype)) {
380 greg 1.1 nd.dp = getdata(m->oargs.sarg[1]);
381 greg 2.6 getfunc(m, 2, 0, 0);
382 greg 1.1 } else {
383     nd.dp = NULL;
384 greg 2.6 getfunc(m, 1, 0, 0);
385 greg 1.1 }
386     /* compute ambient */
387 greg 2.6 if (nd.trans < 1.0-FTINY) {
388 greg 2.13 ambient(ctmp, r, nd.pnorm);
389 greg 2.6 scalecolor(ctmp, 1.0-nd.trans);
390 greg 1.1 multcolor(ctmp, nd.mcolor); /* modified by material color */
391     addcolor(r->rcol, ctmp); /* add to returned color */
392 greg 1.5 }
393 greg 2.6 if (nd.trans > FTINY) { /* from other side */
394 greg 1.5 flipsurface(r);
395 greg 2.14 vtmp[0] = -nd.pnorm[0];
396     vtmp[1] = -nd.pnorm[1];
397     vtmp[2] = -nd.pnorm[2];
398     ambient(ctmp, r, vtmp);
399 greg 2.6 scalecolor(ctmp, nd.trans);
400 greg 1.5 multcolor(ctmp, nd.mcolor);
401     addcolor(r->rcol, ctmp);
402     flipsurface(r);
403 greg 1.1 }
404     /* add direct component */
405     direct(r, dirbrdf, &nd);
406 greg 2.10
407     return(1);
408 greg 1.10 }
409    
410    
411 greg 2.15 int
412 greg 1.10 setbrdfunc(np) /* set up brdf function and variables */
413     register BRDFDAT *np;
414     {
415     FVECT vec;
416    
417     if (setfunc(np->mp, np->pr) == 0)
418     return(0); /* it's OK, setfunc says we're done */
419     /* else (re)assign special variables */
420     multv3(vec, np->pnorm, funcxf.xfm);
421     varset("NxP", '=', vec[0]/funcxf.sca);
422     varset("NyP", '=', vec[1]/funcxf.sca);
423     varset("NzP", '=', vec[2]/funcxf.sca);
424 greg 1.11 varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
425     np->pdot >= 1.0 ? 1.0 : np->pdot);
426 greg 1.10 varset("CrP", '=', colval(np->mcolor,RED));
427     varset("CgP", '=', colval(np->mcolor,GRN));
428     varset("CbP", '=', colval(np->mcolor,BLU));
429     return(1);
430 greg 1.1 }