ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_brdf.c
Revision: 2.21
Committed: Tue Mar 30 16:13:01 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.20: +28 -21 lines
Log Message:
Continued ANSIfication. There are only bits and pieces left now.

File Contents

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