ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_brdf.c
Revision: 2.20
Committed: Thu Aug 28 03:22:16 2003 UTC (20 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.19: +3 -1 lines
Log Message:
Created proper prototypes for function pointers and included missing headers

File Contents

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