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.9 by greg, Thu Jun 13 13:58:16 1991 UTC vs.
Revision 2.35 by greg, Wed Jan 10 17:45:11 2018 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  "ray.h"
8 > #include "copyright.h"
9  
10 + #include  "ray.h"
11 + #include  "ambient.h"
12   #include  "data.h"
13 <
13 > #include  "source.h"
14   #include  "otypes.h"
15 + #include  "rtotypes.h"
16 + #include  "func.h"
17 + #include  "pmapmat.h"
18  
19   /*
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 < *  of the direction to the light source.
24 > *  of the direction to the light source.  (Data modification functions
25 > *  are passed the source direction as args 2-4.)
26   *      We orient the surface towards the incoming ray, so a single
27   *  surface can be used to represent an infinitely thin object.
28   *
# Line 36 | Line 39 | static char SCCSid[] = "$SunId$ LBL";
39   *  Arguments for MAT_TFUNC are:
40   *      2+      func    funcfile        transform
41   *      0
42 < *      4+      red     grn     blu     rspec   trans   tspec   A7 ..
42 > *      6+      red     grn     blu     rspec   trans   tspec   A7 ..
43   *
44   *  Arguments for MAT_TDATA are:
45   *      4+      func    datafile        funcfile        v0 ..   transform
46   *      0
47 < *      4+      red     grn     blu     rspec   trans   tspec   A7 ..
47 > *      6+      red     grn     blu     rspec   trans   tspec   A7 ..
48   *
49   *  Arguments for the more general MAT_BRTDF are:
50   *      10+     rrefl   grefl   brefl
# Line 49 | Line 52 | static char SCCSid[] = "$SunId$ LBL";
52   *              rbrtd   gbrtd   bbrtd
53   *              funcfile        transform
54   *      0
55 < *      6+      red     grn     blu     rspec   trans   tspec   A7 ..
55 > *      9+      rdf     gdf     bdf
56 > *              rdb     gdb     bdb
57 > *              rdt     gdt     bdt     A10 ..
58   *
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 < *              CrP, CgP, CbP -         perturbed material color
63 > *              CrP, CgP, CbP -         perturbed material color (or pattern)
64   */
65  
61 extern double   funvalue(), varvalue();
62 extern XF  funcxf;
63
66   typedef struct {
67          OBJREC  *mp;            /* material pointer */
68          RAY  *pr;               /* intersected ray */
69          DATARRAY  *dp;          /* data array for PDATA, MDATA or TDATA */
70 <        COLOR  mcolor;          /* color of this material */
71 <        double  rspec;          /* specular reflection */
72 <        double  rdiff;          /* diffuse reflection */
73 <        double  trans;          /* transmissivity */
74 <        double  tspec;          /* specular transmission */
75 <        double  tdiff;          /* diffuse transmission */
70 >        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          FVECT  pnorm;           /* perturbed surface normal */
77          double  pdot;           /* perturbed dot product */
78   }  BRDFDAT;             /* BRDF material data */
79  
80  
81 < dirbrdf(cval, np, ldir, omega)          /* compute source contribution */
82 < COLOR  cval;                    /* returned coefficient */
83 < register BRDFDAT  *np;          /* material data */
84 < FVECT  ldir;                    /* light source direction */
85 < double  omega;                  /* light source size */
81 > static int setbrdfunc(BRDFDAT *np);
82 >
83 >
84 > static void
85 > 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   {
92 +        BRDFDAT *np = nnp;
93          double  ldot;
94          double  dtmp;
95          COLOR  ctmp;
96          FVECT  ldx;
97 <        double  pt[MAXDIM];
98 <        register char   **sa;
99 <        register int    i;
97 >        static double  vldx[5], pt[MAXDIM];
98 >        char    **sa;
99 >        int     i;
100 > #define lddx (vldx+1)
101  
102          setcolor(cval, 0.0, 0.0, 0.0);
103          
# Line 96 | Line 105 | double  omega;                 /* light source size */
105  
106          if (ldot <= FTINY && ldot >= -FTINY)
107                  return;         /* too close to grazing */
108 +
109          if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
110                  return;         /* wrong side */
111  
112 <        if (ldot > 0.0 && np->rdiff > FTINY) {
112 >        if (ldot > 0.0) {
113                  /*
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 <                copycolor(ctmp, np->mcolor);
119 <                dtmp = ldot * omega * np->rdiff / PI;
118 >                copycolor(ctmp, np->rdiff);
119 >                dtmp = ldot * omega / PI;
120                  scalecolor(ctmp, dtmp);
121                  addcolor(cval, ctmp);
122 <        }
113 <        if (ldot < 0.0 && np->tdiff > FTINY) {
122 >        } else {
123                  /*
124                   *  Diffuse transmitted component.
125                   */
126 <                copycolor(ctmp, np->mcolor);
127 <                dtmp = -ldot * omega * np->tdiff / PI;
126 >                copycolor(ctmp, np->tdiff);
127 >                dtmp = -ldot * omega / PI;
128                  scalecolor(ctmp, dtmp);
129                  addcolor(cval, ctmp);
130          }
131 <        if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY)
132 <                return;         /* no specular component */
131 >        if ((ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) ||
132 >                        ambRayInPmap(np->pr))
133 >                return;         /* diffuse only */
134                                          /* set up function */
135 <        setfunc(np->mp, np->pr);
135 >        setbrdfunc(np);
136          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 <                ldx[i] /= funcxf.sca;
141 >                lddx[i] = ldx[i]/funcxf.sca;
142 >        lddx[3] = omega;
143                                          /* compute BRTDF */
144          if (np->mp->otype == MAT_BRTDF) {
145 <                colval(ctmp,RED) = funvalue(sa[6], 3, ldx);
146 <                if (!strcmp(sa[7],sa[6]))
145 >                if (sa[6][0] == '0' && !sa[6][1])       /* special case */
146 >                        colval(ctmp,RED) = 0.0;
147 >                else
148 >                        colval(ctmp,RED) = funvalue(sa[6], 4, lddx);
149 >                if (sa[7][0] == '0' && !sa[7][1])
150 >                        colval(ctmp,GRN) = 0.0;
151 >                else if (!strcmp(sa[7],sa[6]))
152                          colval(ctmp,GRN) = colval(ctmp,RED);
153                  else
154 <                        colval(ctmp,GRN) = funvalue(sa[7], 3, ldx);
155 <                if (!strcmp(sa[8],sa[6]))
154 >                        colval(ctmp,GRN) = funvalue(sa[7], 4, lddx);
155 >                if (sa[8][0] == '0' && !sa[8][1])
156 >                        colval(ctmp,BLU) = 0.0;
157 >                else if (!strcmp(sa[8],sa[6]))
158                          colval(ctmp,BLU) = colval(ctmp,RED);
159                  else if (!strcmp(sa[8],sa[7]))
160                          colval(ctmp,BLU) = colval(ctmp,GRN);
161                  else
162 <                        colval(ctmp,BLU) = funvalue(sa[8], 3, ldx);
162 >                        colval(ctmp,BLU) = funvalue(sa[8], 4, lddx);
163                  dtmp = bright(ctmp);
164          } else if (np->dp == NULL) {
165 <                dtmp = funvalue(sa[0], 3, ldx);
165 >                dtmp = funvalue(sa[0], 4, lddx);
166                  setcolor(ctmp, dtmp, dtmp, dtmp);
167          } else {
168                  for (i = 0; i < np->dp->nd; i++)
169 <                        pt[i] = funvalue(sa[3+i], 3, ldx);
170 <                dtmp = datavalue(np->dp, pt);
171 <                dtmp = funvalue(sa[0], 1, &dtmp);
169 >                        pt[i] = funvalue(sa[3+i], 4, lddx);
170 >                vldx[0] = datavalue(np->dp, pt);
171 >                dtmp = funvalue(sa[0], 5, vldx);
172                  setcolor(ctmp, dtmp, dtmp, dtmp);
173          }
174 <        if (errno)
175 <                goto computerr;
174 >        if ((errno == EDOM) | (errno == ERANGE)) {
175 >                objerror(np->mp, WARNING, "compute error");
176 >                return;
177 >        }
178          if (dtmp <= FTINY)
179                  return;
180          if (ldot > 0.0) {
181                  /*
182                   *  Compute reflected non-diffuse component.
183                   */
184 <                if (np->mp->otype == MAT_MFUNC || np->mp->otype == MAT_MDATA)
184 >                if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA))
185                          multcolor(ctmp, np->mcolor);
186                  dtmp = ldot * omega * np->rspec;
187                  scalecolor(ctmp, dtmp);
# Line 170 | Line 190 | double  omega;                 /* light source size */
190                  /*
191                   *  Compute transmitted non-diffuse component.
192                   */
193 <                if (np->mp->otype == MAT_TFUNC || np->mp->otype == MAT_TDATA)
193 >                if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA))
194                          multcolor(ctmp, np->mcolor);
195                  dtmp = -ldot * omega * np->tspec;
196                  scalecolor(ctmp, dtmp);
197                  addcolor(cval, ctmp);
198          }
199 <        return;
180 < computerr:
181 <        objerror(np->mp, WARNING, "compute error");
182 <        return;
199 > #undef lddx
200   }
201  
202  
203 < m_brdf(m, r)                    /* color a ray which hit a BRDF material */
204 < register OBJREC  *m;
205 < register RAY  *r;
203 > int
204 > m_brdf(                 /* color a ray that hit a BRDTfunc material */
205 >        OBJREC  *m,
206 >        RAY  *r
207 > )
208   {
209 <        int  minsa, minfa;
209 >        int  hitfront = 1;
210          BRDFDAT  nd;
211 <        double  transtest, transdist;
211 >        RAY  sr;
212 >        double  mirtest=0, mirdist;
213 >        double  transtest=0, transdist;
214 >        int  hasrefl, hastrans;
215 >        int  hastexture;
216          COLOR  ctmp;
217 <        double  dtmp;
218 <        FVECT  vec;
219 <        register int  i;
217 >        FVECT  vtmp;
218 >        double  d;
219 >        MFUNC  *mf;
220 >        int  i;
221                                                  /* check arguments */
222 <        switch (m->otype) {
199 <        case MAT_PFUNC: case MAT_MFUNC:
200 <                minsa = 2; minfa = 4; break;
201 <        case MAT_PDATA: case MAT_MDATA:
202 <                minsa = 4; minfa = 4; break;
203 <        case MAT_TFUNC:
204 <                minsa = 2; minfa = 6; break;
205 <        case MAT_TDATA:
206 <                minsa = 4; minfa = 6; break;
207 <        case MAT_BRTDF:
208 <                minsa = 10; minfa = 6; break;
209 <        }
210 <        if (m->oargs.nsargs < minsa || m->oargs.nfargs < minfa)
222 >        if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
223                  objerror(m, USER, "bad # arguments");
224          nd.mp = m;
225          nd.pr = r;
226 <                                                /* get specular component */
227 <        nd.rspec = m->oargs.farg[3];
228 <                                                /* compute transmission */
229 <        if (m->otype == MAT_TFUNC || m->otype == MAT_TDATA
230 <                        || m->otype == MAT_BRTDF) {
231 <                nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
232 <                nd.tspec = nd.trans * m->oargs.farg[5];
233 <                nd.tdiff = nd.trans - nd.tspec;
234 <        } else
235 <                nd.tdiff = nd.tspec = nd.trans = 0.0;
236 <                                                /* early shadow check */
237 <        if (r->crtype & SHADOW && (m->otype != MAT_BRTDF || nd.tspec <= FTINY))
238 <                return;
239 <                                                /* diffuse reflection */
240 <        nd.rdiff = 1.0 - nd.trans - nd.rspec;
241 <                                                /* get material color */
230 <        setcolor(nd.mcolor, m->oargs.farg[0],
231 <                           m->oargs.farg[1],
232 <                           m->oargs.farg[2]);
233 <                                                /* fix orientation */
234 <        if (r->rod < 0.0)
235 <                flipsurface(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 >                setcolor(nd.rdiff, m->oargs.farg[0],
232 >                                m->oargs.farg[1],
233 >                                m->oargs.farg[2]);
234 >        else
235 >                setcolor(nd.rdiff, m->oargs.farg[3],
236 >                                m->oargs.farg[4],
237 >                                m->oargs.farg[5]);
238 >                                                /* diffuse transmittance */
239 >        setcolor(nd.tdiff, m->oargs.farg[6],
240 >                        m->oargs.farg[7],
241 >                        m->oargs.farg[8]);
242                                                  /* get modifiers */
243          raytexture(r, m->omod);
244 <        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
245 <        multcolor(nd.mcolor, r->pcol);          /* modify material color */
246 <        transtest = 0;
241 <                                                /* load auxiliary files */
242 <        if (m->otype == MAT_PDATA || m->otype == MAT_MDATA
243 <                        || m->otype == MAT_TDATA) {
244 <                nd.dp = getdata(m->oargs.sarg[1]);
245 <                for (i = 3; i < m->oargs.nsargs; i++)
246 <                        if (m->oargs.sarg[i][0] == '-')
247 <                                break;
248 <                if (i-3 != nd.dp->nd)
249 <                        objerror(m, USER, "dimension error");
250 <                if (!fundefined(m->oargs.sarg[3]))
251 <                        loadfunc(m->oargs.sarg[2]);
252 <        } else if (m->otype == MAT_BRTDF) {
253 <                nd.dp = NULL;
254 <                if (!fundefined(m->oargs.sarg[7]))
255 <                        loadfunc(m->oargs.sarg[9]);
244 >        hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY);
245 >        if (hastexture) {                       /* perturb normal */
246 >                nd.pdot = raynormal(nd.pnorm, r);
247          } else {
248 <                nd.dp = NULL;
249 <                if (!fundefined(m->oargs.sarg[0]))
259 <                        loadfunc(m->oargs.sarg[1]);
248 >                VCOPY(nd.pnorm, r->ron);
249 >                nd.pdot = r->rod;
250          }
251 <                                                /* set special variables */
252 <        setfunc(m, r);
253 <        multv3(vec, nd.pnorm, funcxf.xfm);
254 <        varset("NxP", '=', vec[0]/funcxf.sca);
255 <        varset("NyP", '=', vec[1]/funcxf.sca);
256 <        varset("NzP", '=', vec[2]/funcxf.sca);
257 <        varset("RdotP", '=', nd.pdot);
258 <        varset("CrP", '=', colval(nd.mcolor,RED));
259 <        varset("CgP", '=', colval(nd.mcolor,GRN));
260 <        varset("CbP", '=', colval(nd.mcolor,BLU));
251 >        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 >                hitfront = 0;
258 >        }
259 >        copycolor(nd.mcolor, r->pcol);          /* get pattern color */
260 >        multcolor(nd.rdiff, nd.mcolor);         /* modify diffuse values */
261 >        multcolor(nd.tdiff, nd.mcolor);
262 >        hasrefl = (bright(nd.rdiff) > FTINY);
263 >        hastrans = (bright(nd.tdiff) > FTINY);
264 >        mirdist = transdist = r->rot;
265 >                                                /* load cal file */
266 >        nd.dp = NULL;
267 >        mf = getfunc(m, 9, 0x3f, 0);
268                                                  /* compute transmitted ray */
269 <        if (m->otype == MAT_BRTDF && nd.tspec > FTINY) {
270 <                RAY  sr;
271 <                errno = 0;
272 <                setcolor(ctmp, varvalue(m->oargs.sarg[0]),
273 <                                varvalue(m->oargs.sarg[1]),
274 <                                varvalue(m->oargs.sarg[2]));
275 <                scalecolor(ctmp, nd.tspec);
276 <                if (errno)
277 <                        objerror(m, WARNING, "compute error");
278 <                else if ((dtmp = bright(ctmp)) > FTINY &&
279 <                                rayorigin(&sr, r, TRANS, dtmp) == 0) {
280 <                        if (DOT(r->pert,r->pert) > FTINY*FTINY) {
281 <                                for (i = 0; i < 3; i++) /* perturb direction */
285 <                                        sr.rdir[i] = r->rdir[i] -
286 <                                                        .75*r->pert[i];
287 <                                normalize(sr.rdir);
288 <                        } else {
269 >        setbrdfunc(&nd);
270 >        errno = 0;
271 >        setcolor(ctmp, evalue(mf->ep[3]),
272 >                        evalue(mf->ep[4]),
273 >                        evalue(mf->ep[5]));
274 >        if ((errno == EDOM) | (errno == ERANGE))
275 >                objerror(m, WARNING, "compute error");
276 >        else if (rayorigin(&sr, TRANS, r, ctmp) == 0) {
277 >                if (hastexture && !(r->crtype & (SHADOW|AMBIENT))) {
278 >                                                /* perturb direction */
279 >                        VSUB(sr.rdir, r->rdir, r->pert);
280 >                        if (normalize(sr.rdir) == 0.0) {
281 >                                objerror(m, WARNING, "illegal perturbation");
282                                  VCOPY(sr.rdir, r->rdir);
290                                transtest = 2;
283                          }
284 <                        rayvalue(&sr);
285 <                        multcolor(sr.rcol, ctmp);
286 <                        addcolor(r->rcol, sr.rcol);
287 <                        transtest *= bright(sr.rcol);
284 >                } else {
285 >                        VCOPY(sr.rdir, r->rdir);
286 >                }
287 >                rayvalue(&sr);
288 >                multcolor(sr.rcol, sr.rcoef);
289 >                addcolor(r->rcol, sr.rcol);
290 >                if (!hastexture || r->crtype & (SHADOW|AMBIENT)) {
291 >                        transtest = 2.0*bright(sr.rcol);
292                          transdist = r->rot + sr.rt;
293                  }
294          }
295 <        if (r->crtype & SHADOW)                 /* the rest is shadow */
296 <                return;
295 >        if (r->crtype & SHADOW) {               /* the rest is shadow */
296 >                r->rt = transdist;
297 >                return(1);
298 >        }
299                                                  /* compute reflected ray */
300 <        if (m->otype == MAT_BRTDF && nd.rspec > FTINY) {
301 <                RAY  sr;
302 <                errno = 0;
303 <                setcolor(ctmp, varvalue(m->oargs.sarg[3]),
304 <                                varvalue(m->oargs.sarg[4]),
305 <                                varvalue(m->oargs.sarg[5]));
306 <                scalecolor(ctmp, nd.rspec);
307 <                if (errno)
308 <                        objerror(m, WARNING, "compute error");
309 <                else if ((dtmp = bright(ctmp)) > FTINY &&
310 <                                rayorigin(&sr, r, REFLECTED, dtmp) == 0) {
311 <                        for (i = 0; i < 3; i++)
312 <                                sr.rdir[i] = r->rdir[i] +
313 <                                                2.0*nd.pdot*nd.pnorm[i];
314 <                        rayvalue(&sr);
315 <                        multcolor(sr.rcol, ctmp);
318 <                        addcolor(r->rcol, sr.rcol);
300 >        setbrdfunc(&nd);
301 >        errno = 0;
302 >        setcolor(ctmp, evalue(mf->ep[0]),
303 >                        evalue(mf->ep[1]),
304 >                        evalue(mf->ep[2]));
305 >        if ((errno == EDOM) | (errno == ERANGE))
306 >                objerror(m, WARNING, "compute error");
307 >        else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) {
308 >                VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot);
309 >                checknorm(sr.rdir);
310 >                rayvalue(&sr);
311 >                multcolor(sr.rcol, sr.rcoef);
312 >                addcolor(r->rcol, sr.rcol);
313 >                if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) {
314 >                        mirtest = 2.0*bright(sr.rcol);
315 >                        mirdist = r->rot + sr.rt;
316                  }
317          }
318                                                  /* compute ambient */
319 <        if (nd.rdiff > FTINY) {
320 <                ambient(ctmp, r);
321 <                if (m->otype == MAT_BRTDF)
322 <                        scalecolor(ctmp, nd.rdiff);
323 <                else
327 <                        scalecolor(ctmp, 1.0-nd.trans);
328 <                multcolor(ctmp, nd.mcolor);     /* modified by material color */
319 >        if (hasrefl) {
320 >                if (!hitfront)
321 >                        flipsurface(r);
322 >                copycolor(ctmp, nd.rdiff);
323 >                multambient(ctmp, r, nd.pnorm);
324                  addcolor(r->rcol, ctmp);        /* add to returned color */
325 +                if (!hitfront)
326 +                        flipsurface(r);
327          }
328 <        if (nd.tdiff > FTINY) {                 /* from other side */
328 >        if (hastrans) {                         /* from other side */
329 >                if (hitfront)
330 >                        flipsurface(r);
331 >                vtmp[0] = -nd.pnorm[0];
332 >                vtmp[1] = -nd.pnorm[1];
333 >                vtmp[2] = -nd.pnorm[2];
334 >                copycolor(ctmp, nd.tdiff);
335 >                multambient(ctmp, r, vtmp);
336 >                addcolor(r->rcol, ctmp);
337 >                if (hitfront)
338 >                        flipsurface(r);
339 >        }
340 >        if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
341 >                direct(r, dirbrdf, &nd);        /* add direct component */
342 >
343 >        d = bright(r->rcol);                    /* set effective distance */
344 >        if (transtest > d)
345 >                r->rt = transdist;
346 >        else if (mirtest > d)
347 >                r->rt = mirdist;
348 >
349 >        return(1);
350 > }
351 >
352 >
353 >
354 > int
355 > m_brdf2(                        /* color a ray that hit a BRDF material */
356 >        OBJREC  *m,
357 >        RAY  *r
358 > )
359 > {
360 >        BRDFDAT  nd;
361 >        COLOR  ctmp;
362 >        FVECT  vtmp;
363 >        double  dtmp;
364 >                                                /* always a shadow */
365 >        if (r->crtype & SHADOW)
366 >                return(1);
367 >                                                /* check arguments */
368 >        if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
369 >                        ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
370 >                objerror(m, USER, "bad # arguments");
371 >                                                /* check for back side */
372 >        if (r->rod < 0.0) {
373 >                if (!backvis) {
374 >                        raytrans(r);
375 >                        return(1);
376 >                }
377 >                raytexture(r, m->omod);
378 >                flipsurface(r);                 /* reorient if backvis */
379 >        } else
380 >                raytexture(r, m->omod);
381 >
382 >        nd.mp = m;
383 >        nd.pr = r;
384 >                                                /* get material color */
385 >        setcolor(nd.mcolor, m->oargs.farg[0],
386 >                        m->oargs.farg[1],
387 >                        m->oargs.farg[2]);
388 >                                                /* get specular component */
389 >        nd.rspec = m->oargs.farg[3];
390 >                                                /* compute transmittance */
391 >        if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
392 >                nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
393 >                nd.tspec = nd.trans * m->oargs.farg[5];
394 >                dtmp = nd.trans - nd.tspec;
395 >                setcolor(nd.tdiff, dtmp, dtmp, dtmp);
396 >        } else {
397 >                nd.tspec = nd.trans = 0.0;
398 >                setcolor(nd.tdiff, 0.0, 0.0, 0.0);
399 >        }
400 >                                                /* compute reflectance */
401 >        dtmp = 1.0 - nd.trans - nd.rspec;
402 >        setcolor(nd.rdiff, dtmp, dtmp, dtmp);
403 >        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
404 >        multcolor(nd.mcolor, r->pcol);          /* modify material color */
405 >        multcolor(nd.rdiff, nd.mcolor);
406 >        multcolor(nd.tdiff, nd.mcolor);
407 >                                                /* load auxiliary files */
408 >        if (hasdata(m->otype)) {
409 >                nd.dp = getdata(m->oargs.sarg[1]);
410 >                getfunc(m, 2, 0, 0);
411 >        } else {
412 >                nd.dp = NULL;
413 >                getfunc(m, 1, 0, 0);
414 >        }
415 >                                                /* compute ambient */
416 >        if (nd.trans < 1.0-FTINY) {
417 >                copycolor(ctmp, nd.mcolor);     /* modified by material color */
418 >                scalecolor(ctmp, 1.0-nd.trans);
419 >                multambient(ctmp, r, nd.pnorm);
420 >                addcolor(r->rcol, ctmp);        /* add to returned color */
421 >        }
422 >        if (nd.trans > FTINY) {         /* from other side */
423                  flipsurface(r);
424 <                ambient(ctmp, r);
425 <                if (m->otype == MAT_BRTDF)
426 <                        scalecolor(ctmp, nd.tdiff);
427 <                else
428 <                        scalecolor(ctmp, nd.trans);
429 <                multcolor(ctmp, nd.mcolor);
424 >                vtmp[0] = -nd.pnorm[0];
425 >                vtmp[1] = -nd.pnorm[1];
426 >                vtmp[2] = -nd.pnorm[2];
427 >                copycolor(ctmp, nd.mcolor);
428 >                scalecolor(ctmp, nd.trans);
429 >                multambient(ctmp, r, vtmp);
430                  addcolor(r->rcol, ctmp);
431                  flipsurface(r);
432          }
433                                                  /* add direct component */
434          direct(r, dirbrdf, &nd);
435 <                                                /* check distance */
436 <        if (transtest > bright(r->rcol))
437 <                r->rt = transdist;
435 >
436 >        return(1);
437 > }
438 >
439 >
440 > static int
441 > setbrdfunc(                     /* set up brdf function and variables */
442 >        BRDFDAT  *np
443 > )
444 > {
445 >        FVECT  vec;
446 >
447 >        if (setfunc(np->mp, np->pr) == 0)
448 >                return(0);      /* it's OK, setfunc says we're done */
449 >                                /* else (re)assign special variables */
450 >        multv3(vec, np->pnorm, funcxf.xfm);
451 >        varset("NxP", '=', vec[0]/funcxf.sca);
452 >        varset("NyP", '=', vec[1]/funcxf.sca);
453 >        varset("NzP", '=', vec[2]/funcxf.sca);
454 >        varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
455 >                        np->pdot >= 1.0 ? 1.0 : np->pdot);
456 >        varset("CrP", '=', colval(np->mcolor,RED));
457 >        varset("CgP", '=', colval(np->mcolor,GRN));
458 >        varset("CbP", '=', colval(np->mcolor,BLU));
459 >        return(1);
460   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines