ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_brdf.c
Revision: 2.41
Committed: Fri Apr 5 01:10:26 2024 UTC (4 weeks, 6 days ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.40: +1 -11 lines
Log Message:
fix: Improved tracking of reflected vs. transmitted rays for antimatter

File Contents

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