ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_brdf.c
Revision: 2.36
Committed: Tue Nov 13 19:58:33 2018 UTC (5 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.35: +9 -21 lines
Log Message:
Added -orRxX options to rtrace for VR rendering

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.36 static const char RCSid[] = "$Id: m_brdf.c,v 2.35 2018/01/10 17:45:11 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 greg 2.33 #include "pmapmat.h"
18 greg 2.2
19 greg 1.1 /*
20     * Arguments to this material include the color and specularity.
21     * String arguments include the reflection function and files.
22     * The BRDF is currently used just for the specular component to light
23     * sources. Reflectance values or data coordinates are functions
24 greg 2.7 * of the direction to the light source. (Data modification functions
25     * are passed the source direction as args 2-4.)
26 greg 1.1 * We orient the surface towards the incoming ray, so a single
27     * surface can be used to represent an infinitely thin object.
28     *
29     * Arguments for MAT_PFUNC and MAT_MFUNC are:
30 greg 1.4 * 2+ func funcfile transform
31 greg 1.1 * 0
32 greg 1.4 * 4+ red grn blu specularity A5 ..
33 greg 1.1 *
34     * Arguments for MAT_PDATA and MAT_MDATA are:
35 greg 1.4 * 4+ func datafile funcfile v0 .. transform
36 greg 1.1 * 0
37 greg 1.4 * 4+ red grn blu specularity A5 ..
38 greg 1.5 *
39     * Arguments for MAT_TFUNC are:
40     * 2+ func funcfile transform
41     * 0
42 greg 2.29 * 6+ red grn blu rspec trans tspec A7 ..
43 greg 1.5 *
44     * Arguments for MAT_TDATA are:
45     * 4+ func datafile funcfile v0 .. transform
46     * 0
47 greg 2.29 * 6+ red grn blu rspec trans tspec A7 ..
48 greg 1.5 *
49     * Arguments for the more general MAT_BRTDF are:
50     * 10+ rrefl grefl brefl
51     * rtrns gtrns btrns
52     * rbrtd gbrtd bbrtd
53     * funcfile transform
54     * 0
55 greg 2.6 * 9+ rdf gdf bdf
56     * rdb gdb bdb
57     * rdt gdt bdt A10 ..
58 greg 1.5 *
59     * In addition to the normal variables available to functions,
60     * we define the following:
61     * NxP, NyP, NzP - perturbed surface normal
62     * RdotP - perturbed ray dot product
63 greg 2.6 * CrP, CgP, CbP - perturbed material color (or pattern)
64 greg 1.1 */
65    
66     typedef struct {
67     OBJREC *mp; /* material pointer */
68     RAY *pr; /* intersected ray */
69 greg 1.5 DATARRAY *dp; /* data array for PDATA, MDATA or TDATA */
70 greg 2.6 COLOR mcolor; /* material (or pattern) color */
71     COLOR rdiff; /* diffuse reflection */
72     COLOR tdiff; /* diffuse transmission */
73     double rspec; /* specular reflectance (1 for BRDTF) */
74     double trans; /* transmissivity (.5 for BRDTF) */
75     double tspec; /* specular transmittance (1 for BRDTF) */
76 greg 1.1 FVECT pnorm; /* perturbed surface normal */
77     double pdot; /* perturbed dot product */
78     } BRDFDAT; /* BRDF material data */
79    
80    
81 greg 2.28 static int setbrdfunc(BRDFDAT *np);
82 schorsch 2.21
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 greg 2.28 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 2.28 char **sa;
99     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 2.33 if ((ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) ||
132     ambRayInPmap(np->pr))
133 greg 2.24 return; /* diffuse only */
134 greg 1.5 /* set up function */
135 greg 1.10 setbrdfunc(np);
136 greg 1.5 sa = np->mp->oargs.sarg;
137     errno = 0;
138     /* transform light vector */
139     multv3(ldx, ldir, funcxf.xfm);
140     for (i = 0; i < 3; i++)
141 greg 2.3 lddx[i] = ldx[i]/funcxf.sca;
142 greg 2.12 lddx[3] = omega;
143 greg 1.5 /* compute BRTDF */
144     if (np->mp->otype == MAT_BRTDF) {
145 greg 2.32 if (sa[6][0] == '0' && !sa[6][1]) /* special case */
146 greg 2.6 colval(ctmp,RED) = 0.0;
147     else
148 greg 2.12 colval(ctmp,RED) = funvalue(sa[6], 4, lddx);
149 greg 2.32 if (sa[7][0] == '0' && !sa[7][1])
150 greg 2.24 colval(ctmp,GRN) = 0.0;
151     else if (!strcmp(sa[7],sa[6]))
152 greg 1.5 colval(ctmp,GRN) = colval(ctmp,RED);
153     else
154 greg 2.12 colval(ctmp,GRN) = funvalue(sa[7], 4, lddx);
155 greg 2.32 if (sa[8][0] == '0' && !sa[8][1])
156     colval(ctmp,BLU) = 0.0;
157     else if (!strcmp(sa[8],sa[6]))
158 greg 1.5 colval(ctmp,BLU) = colval(ctmp,RED);
159 greg 1.7 else if (!strcmp(sa[8],sa[7]))
160 greg 1.5 colval(ctmp,BLU) = colval(ctmp,GRN);
161     else
162 greg 2.12 colval(ctmp,BLU) = funvalue(sa[8], 4, lddx);
163 greg 1.5 dtmp = bright(ctmp);
164     } else if (np->dp == NULL) {
165 greg 2.12 dtmp = funvalue(sa[0], 4, lddx);
166 greg 1.5 setcolor(ctmp, dtmp, dtmp, dtmp);
167     } else {
168     for (i = 0; i < np->dp->nd; i++)
169 greg 2.12 pt[i] = funvalue(sa[3+i], 4, lddx);
170 greg 2.7 vldx[0] = datavalue(np->dp, pt);
171 greg 2.12 dtmp = funvalue(sa[0], 5, vldx);
172 greg 1.5 setcolor(ctmp, dtmp, dtmp, dtmp);
173     }
174 greg 2.25 if ((errno == EDOM) | (errno == ERANGE)) {
175 greg 2.2 objerror(np->mp, WARNING, "compute error");
176     return;
177     }
178 greg 1.5 if (dtmp <= FTINY)
179     return;
180     if (ldot > 0.0) {
181     /*
182     * Compute reflected non-diffuse component.
183     */
184 schorsch 2.19 if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA))
185 greg 1.6 multcolor(ctmp, np->mcolor);
186     dtmp = ldot * omega * np->rspec;
187 greg 1.5 scalecolor(ctmp, dtmp);
188     addcolor(cval, ctmp);
189     } else {
190     /*
191     * Compute transmitted non-diffuse component.
192     */
193 schorsch 2.19 if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA))
194 greg 1.6 multcolor(ctmp, np->mcolor);
195 greg 1.5 dtmp = -ldot * omega * np->tspec;
196     scalecolor(ctmp, dtmp);
197     addcolor(cval, ctmp);
198     }
199 greg 2.12 #undef lddx
200 greg 1.1 }
201    
202    
203 greg 2.28 int
204 schorsch 2.21 m_brdf( /* color a ray that hit a BRDTfunc material */
205 greg 2.28 OBJREC *m,
206     RAY *r
207 schorsch 2.21 )
208 greg 1.1 {
209 greg 2.15 int hitfront = 1;
210 greg 1.1 BRDFDAT nd;
211 greg 2.6 RAY sr;
212     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.34 hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY);
243 greg 2.22 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 greg 2.34 hasrefl = (bright(nd.rdiff) > FTINY);
261     hastrans = (bright(nd.tdiff) > FTINY);
262 greg 2.6 /* 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.34 if (hastexture && !(r->crtype & (SHADOW|AMBIENT))) {
275 greg 2.27 /* perturb direction */
276 greg 2.34 VSUB(sr.rdir, r->rdir, r->pert);
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.36 if (!hastexture || r->crtype & (SHADOW|AMBIENT))
288     r->rxt = r->rot + raydistance(&sr);
289 greg 2.6 }
290 greg 2.36 if (r->crtype & SHADOW) /* the rest is shadow */
291 greg 2.10 return(1);
292 greg 2.36
293 greg 2.6 /* compute reflected ray */
294     setbrdfunc(&nd);
295     errno = 0;
296     setcolor(ctmp, evalue(mf->ep[0]),
297     evalue(mf->ep[1]),
298     evalue(mf->ep[2]));
299 greg 2.25 if ((errno == EDOM) | (errno == ERANGE))
300 greg 2.6 objerror(m, WARNING, "compute error");
301 greg 2.23 else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) {
302 greg 2.27 VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot);
303 greg 2.26 checknorm(sr.rdir);
304 greg 2.6 rayvalue(&sr);
305 greg 2.23 multcolor(sr.rcol, sr.rcoef);
306 greg 2.36 copycolor(r->mcol, sr.rcol);
307 greg 2.6 addcolor(r->rcol, sr.rcol);
308 greg 2.36 if (r->ro != NULL && isflat(r->ro->otype) &&
309     !hastexture | (r->crtype & AMBIENT))
310     r->rmt = r->rot + raydistance(&sr);
311 greg 2.6 }
312     /* compute ambient */
313     if (hasrefl) {
314 greg 2.15 if (!hitfront)
315 greg 2.6 flipsurface(r);
316 greg 2.23 copycolor(ctmp, nd.rdiff);
317     multambient(ctmp, r, nd.pnorm);
318 greg 2.6 addcolor(r->rcol, ctmp); /* add to returned color */
319 greg 2.15 if (!hitfront)
320 greg 2.6 flipsurface(r);
321     }
322     if (hastrans) { /* from other side */
323 greg 2.15 if (hitfront)
324 greg 2.6 flipsurface(r);
325 greg 2.15 vtmp[0] = -nd.pnorm[0];
326     vtmp[1] = -nd.pnorm[1];
327     vtmp[2] = -nd.pnorm[2];
328 greg 2.23 copycolor(ctmp, nd.tdiff);
329     multambient(ctmp, r, vtmp);
330 greg 2.6 addcolor(r->rcol, ctmp);
331 greg 2.15 if (hitfront)
332 greg 2.6 flipsurface(r);
333     }
334     if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
335     direct(r, dirbrdf, &nd); /* add direct component */
336 greg 2.22
337 greg 2.10 return(1);
338 greg 2.6 }
339    
340    
341    
342 greg 2.28 int
343 schorsch 2.21 m_brdf2( /* color a ray that hit a BRDF material */
344 greg 2.28 OBJREC *m,
345     RAY *r
346 schorsch 2.21 )
347 greg 2.6 {
348     BRDFDAT nd;
349     COLOR ctmp;
350 greg 2.14 FVECT vtmp;
351 greg 2.6 double dtmp;
352     /* always a shadow */
353     if (r->crtype & SHADOW)
354 greg 2.10 return(1);
355 greg 2.6 /* check arguments */
356 schorsch 2.19 if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
357     ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
358 greg 1.1 objerror(m, USER, "bad # arguments");
359 greg 2.17 /* check for back side */
360     if (r->rod < 0.0) {
361 greg 2.31 if (!backvis) {
362 greg 2.17 raytrans(r);
363     return(1);
364     }
365     raytexture(r, m->omod);
366     flipsurface(r); /* reorient if backvis */
367     } else
368     raytexture(r, m->omod);
369    
370 greg 1.1 nd.mp = m;
371     nd.pr = r;
372 greg 2.6 /* get material color */
373     setcolor(nd.mcolor, m->oargs.farg[0],
374     m->oargs.farg[1],
375     m->oargs.farg[2]);
376 greg 1.5 /* get specular component */
377     nd.rspec = m->oargs.farg[3];
378 greg 2.6 /* compute transmittance */
379 schorsch 2.19 if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
380 greg 1.5 nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
381     nd.tspec = nd.trans * m->oargs.farg[5];
382 greg 2.6 dtmp = nd.trans - nd.tspec;
383     setcolor(nd.tdiff, dtmp, dtmp, dtmp);
384     } else {
385     nd.tspec = nd.trans = 0.0;
386     setcolor(nd.tdiff, 0.0, 0.0, 0.0);
387     }
388     /* compute reflectance */
389     dtmp = 1.0 - nd.trans - nd.rspec;
390     setcolor(nd.rdiff, dtmp, dtmp, dtmp);
391 greg 1.5 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
392     multcolor(nd.mcolor, r->pcol); /* modify material color */
393 greg 2.6 multcolor(nd.rdiff, nd.mcolor);
394     multcolor(nd.tdiff, nd.mcolor);
395 greg 1.1 /* load auxiliary files */
396 greg 2.2 if (hasdata(m->otype)) {
397 greg 1.1 nd.dp = getdata(m->oargs.sarg[1]);
398 greg 2.6 getfunc(m, 2, 0, 0);
399 greg 1.1 } else {
400     nd.dp = NULL;
401 greg 2.6 getfunc(m, 1, 0, 0);
402 greg 1.1 }
403     /* compute ambient */
404 greg 2.6 if (nd.trans < 1.0-FTINY) {
405 greg 2.23 copycolor(ctmp, nd.mcolor); /* modified by material color */
406 greg 2.6 scalecolor(ctmp, 1.0-nd.trans);
407 greg 2.23 multambient(ctmp, r, nd.pnorm);
408 greg 1.1 addcolor(r->rcol, ctmp); /* add to returned color */
409 greg 1.5 }
410 greg 2.6 if (nd.trans > FTINY) { /* from other side */
411 greg 1.5 flipsurface(r);
412 greg 2.14 vtmp[0] = -nd.pnorm[0];
413     vtmp[1] = -nd.pnorm[1];
414     vtmp[2] = -nd.pnorm[2];
415 greg 2.23 copycolor(ctmp, nd.mcolor);
416 greg 2.6 scalecolor(ctmp, nd.trans);
417 greg 2.23 multambient(ctmp, r, vtmp);
418 greg 1.5 addcolor(r->rcol, ctmp);
419     flipsurface(r);
420 greg 1.1 }
421     /* add direct component */
422     direct(r, dirbrdf, &nd);
423 greg 2.10
424     return(1);
425 greg 1.10 }
426    
427    
428 schorsch 2.21 static int
429     setbrdfunc( /* set up brdf function and variables */
430 greg 2.28 BRDFDAT *np
431 schorsch 2.21 )
432 greg 1.10 {
433     FVECT vec;
434    
435     if (setfunc(np->mp, np->pr) == 0)
436     return(0); /* it's OK, setfunc says we're done */
437     /* else (re)assign special variables */
438     multv3(vec, np->pnorm, funcxf.xfm);
439     varset("NxP", '=', vec[0]/funcxf.sca);
440     varset("NyP", '=', vec[1]/funcxf.sca);
441     varset("NzP", '=', vec[2]/funcxf.sca);
442 greg 1.11 varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
443     np->pdot >= 1.0 ? 1.0 : np->pdot);
444 greg 1.10 varset("CrP", '=', colval(np->mcolor,RED));
445     varset("CgP", '=', colval(np->mcolor,GRN));
446     varset("CbP", '=', colval(np->mcolor,BLU));
447     return(1);
448 greg 1.1 }