ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_brdf.c
(Generate patch)

Comparing ray/src/rt/m_brdf.c (file contents):
Revision 1.3 by greg, Fri Dec 14 21:57:59 1990 UTC vs.
Revision 2.11 by greg, Wed Dec 21 09:51:46 1994 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines