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 2.4 by greg, Wed Oct 14 12:30:54 1992 UTC vs.
Revision 2.16 by greg, Tue Feb 25 02:47:22 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines