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.2 by greg, Wed Dec 12 22:35:07 1990 UTC vs.
Revision 2.20 by greg, Thu Aug 28 03:22:16 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1990 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  Shading for materials with arbitrary BRDF's
6   */
7  
8 + #include "copyright.h"
9 +
10   #include  "ray.h"
11  
12 + #include  "ambient.h"
13 +
14   #include  "data.h"
15  
16   #include  "otypes.h"
17  
18 + #include  "func.h"
19 +
20   /*
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 < *  of the direction to the light source.
25 > *  of the direction to the light source.  (Data modification functions
26 > *  are passed the source direction as args 2-4.)
27   *      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 < *      2+      func    funcfile        transform ..
31 > *      2+      func    funcfile        transform
32   *      0
33 < *      4+      red     grn     blu     specularity     args ..
33 > *      4+      red     grn     blu     specularity     A5 ..
34   *
35   *  Arguments for MAT_PDATA and MAT_MDATA are:
36 < *      4+      func    datafile        funcfile        v0 ..   transform ..
36 > *      4+      func    datafile        funcfile        v0 ..   transform
37   *      0
38 < *      4+      red     grn     blu     specularity     args ..
38 > *      4+      red     grn     blu     specularity     A5 ..
39 > *
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 > *      9+      rdf     gdf     bdf
57 > *              rdb     gdb     bdb
58 > *              rdt     gdt     bdt     A10 ..
59 > *
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 > *              CrP, CgP, CbP -         perturbed material color (or pattern)
65   */
66  
37 extern double   funvalue(), varvalue();
38
39 #define  BSPEC(m)               (6.0)           /* specular parameter b */
40
67   typedef struct {
68          OBJREC  *mp;            /* material pointer */
69          RAY  *pr;               /* intersected ray */
70 <        DATARRAY  *dp;          /* data array for PDATA or MDATA */
71 <        COLOR  mcolor;          /* color of this material */
72 <        COLOR  scolor;          /* color of specular component */
73 <        double  rspec;          /* specular reflection */
74 <        double  rdiff;          /* diffuse reflection */
70 >        DATARRAY  *dp;          /* data array for PDATA, MDATA or TDATA */
71 >        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          FVECT  pnorm;           /* perturbed surface normal */
78          double  pdot;           /* perturbed dot product */
79   }  BRDFDAT;             /* BRDF material data */
80  
81  
82 + static void
83   dirbrdf(cval, np, ldir, omega)          /* compute source contribution */
84   COLOR  cval;                    /* returned coefficient */
85   register BRDFDAT  *np;          /* material data */
# Line 60 | Line 89 | double  omega;                 /* light source size */
89          double  ldot;
90          double  dtmp;
91          COLOR  ctmp;
92 <        double  pt[MAXDIM];
92 >        FVECT  ldx;
93 >        static double  vldx[5], pt[MAXDIM];
94 >        register char   **sa;
95          register int    i;
96 + #define lddx (vldx+1)
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 >        lddx[3] = omega;
138 >                                        /* compute BRTDF */
139 >        if (np->mp->otype == MAT_BRTDF) {
140 >                if (sa[6][0] == '0')            /* special case */
141 >                        colval(ctmp,RED) = 0.0;
142 >                else
143 >                        colval(ctmp,RED) = funvalue(sa[6], 4, lddx);
144 >                if (!strcmp(sa[7],sa[6]))
145 >                        colval(ctmp,GRN) = colval(ctmp,RED);
146 >                else
147 >                        colval(ctmp,GRN) = funvalue(sa[7], 4, lddx);
148 >                if (!strcmp(sa[8],sa[6]))
149 >                        colval(ctmp,BLU) = colval(ctmp,RED);
150 >                else if (!strcmp(sa[8],sa[7]))
151 >                        colval(ctmp,BLU) = colval(ctmp,GRN);
152 >                else
153 >                        colval(ctmp,BLU) = funvalue(sa[8], 4, lddx);
154 >                dtmp = bright(ctmp);
155 >        } else if (np->dp == NULL) {
156 >                dtmp = funvalue(sa[0], 4, lddx);
157 >                setcolor(ctmp, dtmp, dtmp, dtmp);
158 >        } else {
159 >                for (i = 0; i < np->dp->nd; i++)
160 >                        pt[i] = funvalue(sa[3+i], 4, lddx);
161 >                vldx[0] = datavalue(np->dp, pt);
162 >                dtmp = funvalue(sa[0], 5, vldx);
163 >                setcolor(ctmp, dtmp, dtmp, dtmp);
164 >        }
165 >        if (errno == EDOM || errno == ERANGE) {
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) {
102 <                        copycolor(ctmp, np->scolor);
103 <                        dtmp *= ldot * omega;
104 <                        scalecolor(ctmp, dtmp);
105 <                        addcolor(cval, ctmp);
106 <                }
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          }
190 <        return;
109 < computerr:
110 <        objerror(np->mp, WARNING, "compute error");
111 <        return;
190 > #undef lddx
191   }
192  
193  
194 < m_brdf(m, r)                    /* color a ray which hit a BRDF material */
194 > int
195 > m_brdf(m, r)                    /* color a ray that hit a BRDTfunc material */
196   register OBJREC  *m;
197   register RAY  *r;
198   {
199 +        int  hitfront = 1;
200          BRDFDAT  nd;
201 <        double  dtmp;
201 >        RAY  sr;
202 >        double  transtest, transdist;
203 >        int  hasrefl, hastrans;
204          COLOR  ctmp;
205 +        FVECT  vtmp;
206 +        register MFUNC  *mf;
207          register int  i;
208 <
209 <        if (m->oargs.nsargs < 2 || m->oargs.nfargs < 4)
208 >                                                /* check arguments */
209 >        if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
210                  objerror(m, USER, "bad # arguments");
126                                                /* easy shadow test */
127        if (r->crtype & SHADOW)
128                return;
211          nd.mp = m;
212          nd.pr = r;
213 <                                                /* load auxiliary files */
214 <        if (m->otype == MAT_PDATA || m->otype == MAT_MDATA) {
215 <                nd.dp = getdata(m->oargs.sarg[1]);
216 <                for (i = 3; i < m->oargs.nsargs; i++)
217 <                        if (m->oargs.sarg[i][0] == '-')
218 <                                break;
219 <                if (i-3 != nd.dp->nd)
220 <                        objerror(m, USER, "dimension error");
221 <                if (!fundefined(m->oargs.sarg[3]))
222 <                        loadfunc(m->oargs.sarg[2]);
223 <        } else {
224 <                nd.dp = NULL;
225 <                if (!fundefined(m->oargs.sarg[0]))
226 <                        loadfunc(m->oargs.sarg[1]);
227 <        }
228 <                                                /* get material color */
147 <        setcolor(nd.mcolor, m->oargs.farg[0],
148 <                           m->oargs.farg[1],
149 <                           m->oargs.farg[2]);
150 <                                                /* get roughness */
151 <        if (r->rod < 0.0)
152 <                flipsurface(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                                                  /* get modifiers */
230          raytexture(r, m->omod);
231          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
232 <        multcolor(nd.mcolor, r->pcol);          /* modify material color */
233 <        r->rt = r->rot;                         /* default ray length */
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 >                hitfront = 0;
239 >        }
240 >        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 >        transdist = r->rot;
252 >        errno = 0;
253 >        setcolor(ctmp, evalue(mf->ep[3]),
254 >                        evalue(mf->ep[4]),
255 >                        evalue(mf->ep[5]));
256 >        if (errno == EDOM || errno == ERANGE)
257 >                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 >                return(1);
279 >                                                /* 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 >        if (errno == EDOM || errno == ERANGE)
286 >                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 >                if (!hitfront)
297 >                        flipsurface(r);
298 >                ambient(ctmp, r, nd.pnorm);
299 >                multcolor(ctmp, nd.rdiff);
300 >                addcolor(r->rcol, ctmp);        /* add to returned color */
301 >                if (!hitfront)
302 >                        flipsurface(r);
303 >        }
304 >        if (hastrans) {                         /* from other side */
305 >                if (hitfront)
306 >                        flipsurface(r);
307 >                vtmp[0] = -nd.pnorm[0];
308 >                vtmp[1] = -nd.pnorm[1];
309 >                vtmp[2] = -nd.pnorm[2];
310 >                ambient(ctmp, r, vtmp);
311 >                multcolor(ctmp, nd.tdiff);
312 >                addcolor(r->rcol, ctmp);
313 >                if (hitfront)
314 >                        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 >
322 >        return(1);
323 > }
324 >
325 >
326 >
327 > int
328 > m_brdf2(m, r)                   /* color a ray that hit a BRDF material */
329 > register OBJREC  *m;
330 > register RAY  *r;
331 > {
332 >        BRDFDAT  nd;
333 >        COLOR  ctmp;
334 >        FVECT  vtmp;
335 >        double  dtmp;
336 >                                                /* always a shadow */
337 >        if (r->crtype & SHADOW)
338 >                return(1);
339 >                                                /* check arguments */
340 >        if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
341 >                        ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
342 >                objerror(m, USER, "bad # arguments");
343 >                                                /* 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 >        nd.mp = m;
356 >        nd.pr = r;
357 >                                                /* get material color */
358 >        setcolor(nd.mcolor, m->oargs.farg[0],
359 >                        m->oargs.farg[1],
360 >                        m->oargs.farg[2]);
361                                                  /* get specular component */
362          nd.rspec = m->oargs.farg[3];
363 <
364 <        if (nd.rspec > FTINY) {                 /* has specular component */
365 <                                                /* compute specular color */
366 <                if (m->otype == MAT_MFUNC || m->otype == MAT_MDATA)
367 <                        copycolor(nd.scolor, nd.mcolor);
368 <                else
369 <                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
370 <                scalecolor(nd.scolor, nd.rspec);
371 <                                                /* improved model */
169 <                dtmp = exp(-BSPEC(m)*nd.pdot);
170 <                for (i = 0; i < 3; i++)
171 <                        colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
172 <                nd.rspec += (1.0-nd.rspec)*dtmp;
363 >                                                /* compute transmittance */
364 >        if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
365 >                nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
366 >                nd.tspec = nd.trans * m->oargs.farg[5];
367 >                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 <                                                /* diffuse reflection */
374 <        nd.rdiff = 1.0 - nd.rspec;
373 >                                                /* compute reflectance */
374 >        dtmp = 1.0 - nd.trans - nd.rspec;
375 >        setcolor(nd.rdiff, dtmp, dtmp, dtmp);
376 >        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
377 >        multcolor(nd.mcolor, r->pcol);          /* modify material color */
378 >        multcolor(nd.rdiff, nd.mcolor);
379 >        multcolor(nd.tdiff, nd.mcolor);
380 >                                                /* load auxiliary files */
381 >        if (hasdata(m->otype)) {
382 >                nd.dp = getdata(m->oargs.sarg[1]);
383 >                getfunc(m, 2, 0, 0);
384 >        } else {
385 >                nd.dp = NULL;
386 >                getfunc(m, 1, 0, 0);
387 >        }
388                                                  /* compute ambient */
389 <        if (nd.rdiff > FTINY) {
390 <                ambient(ctmp, r);
389 >        if (nd.trans < 1.0-FTINY) {
390 >                ambient(ctmp, r, nd.pnorm);
391 >                scalecolor(ctmp, 1.0-nd.trans);
392                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
393                  addcolor(r->rcol, ctmp);        /* add to returned color */
394          }
395 +        if (nd.trans > FTINY) {         /* from other side */
396 +                flipsurface(r);
397 +                vtmp[0] = -nd.pnorm[0];
398 +                vtmp[1] = -nd.pnorm[1];
399 +                vtmp[2] = -nd.pnorm[2];
400 +                ambient(ctmp, r, vtmp);
401 +                scalecolor(ctmp, nd.trans);
402 +                multcolor(ctmp, nd.mcolor);
403 +                addcolor(r->rcol, ctmp);
404 +                flipsurface(r);
405 +        }
406                                                  /* add direct component */
407          direct(r, dirbrdf, &nd);
408 +
409 +        return(1);
410 + }
411 +
412 +
413 + int
414 + 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 +        varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
427 +                        np->pdot >= 1.0 ? 1.0 : np->pdot);
428 +        varset("CrP", '=', colval(np->mcolor,RED));
429 +        varset("CgP", '=', colval(np->mcolor,GRN));
430 +        varset("CbP", '=', colval(np->mcolor,BLU));
431 +        return(1);
432   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines