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.15 by greg, Thu Aug 8 11:30:01 1991 UTC vs.
Revision 2.13 by greg, Mon Nov 6 12:03:22 1995 UTC

# 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   *
# Line 49 | Line 54 | static char SCCSid[] = "$SunId$ LBL";
54   *              rbrtd   gbrtd   bbrtd
55   *              funcfile        transform
56   *      0
57 < *      6+      red     grn     blu     rspec   trans   tspec   A7 ..
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
65 > *              CrP, CgP, CbP -         perturbed material color (or pattern)
66   */
67  
61 extern double   funvalue(), varvalue();
62 extern XF  funcxf;
63
68   typedef struct {
69          OBJREC  *mp;            /* material pointer */
70          RAY  *pr;               /* intersected ray */
71          DATARRAY  *dp;          /* data array for PDATA, MDATA or TDATA */
72 <        COLOR  mcolor;          /* color of this material */
73 <        double  rspec;          /* specular reflection */
74 <        double  rdiff;          /* diffuse reflection */
75 <        double  trans;          /* transmissivity */
76 <        double  tspec;          /* specular transmission */
77 <        double  tdiff;          /* diffuse transmission */
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 86 | Line 90 | double  omega;                 /* light source size */
90          double  dtmp;
91          COLOR  ctmp;
92          FVECT  ldx;
93 <        double  pt[MAXDIM];
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          
# Line 96 | Line 101 | double  omega;                 /* light source size */
101  
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 (ldot > 0.0 && 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 <        }
113 <        if (ldot < 0.0 && np->tdiff > FTINY) {
118 >        } else {
119                  /*
120                   *  Diffuse transmitted component.
121                   */
122 <                copycolor(ctmp, np->mcolor);
123 <                dtmp = -ldot * omega * np->tdiff / PI;
122 >                copycolor(ctmp, np->tdiff);
123 >                dtmp = -ldot * omega / PI;
124                  scalecolor(ctmp, dtmp);
125                  addcolor(cval, ctmp);
126          }
# Line 128 | Line 133 | double  omega;                 /* light source size */
133                                          /* transform light vector */
134          multv3(ldx, ldir, funcxf.xfm);
135          for (i = 0; i < 3; i++)
136 <                ldx[i] /= funcxf.sca;
136 >                lddx[i] = ldx[i]/funcxf.sca;
137 >        lddx[3] = omega;
138                                          /* compute BRTDF */
139          if (np->mp->otype == MAT_BRTDF) {
140 <                colval(ctmp,RED) = funvalue(sa[6], 3, ldx);
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], 3, ldx);
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], 3, ldx);
153 >                        colval(ctmp,BLU) = funvalue(sa[8], 4, lddx);
154                  dtmp = bright(ctmp);
155          } else if (np->dp == NULL) {
156 <                dtmp = funvalue(sa[0], 3, ldx);
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], 3, ldx);
161 <                dtmp = datavalue(np->dp, pt);
162 <                dtmp = funvalue(sa[0], 1, &dtmp);
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)
166 <                goto computerr;
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 reflected non-diffuse component.
174                   */
175 <                if (np->mp->otype == MAT_MFUNC || np->mp->otype == MAT_MDATA)
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);
# Line 170 | Line 181 | double  omega;                 /* light source size */
181                  /*
182                   *  Compute transmitted non-diffuse component.
183                   */
184 <                if (np->mp->otype == MAT_TFUNC || np->mp->otype == MAT_TDATA)
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;
180 < computerr:
181 <        objerror(np->mp, WARNING, "compute error");
182 <        return;
190 > #undef lddx
191   }
192  
193  
194 < m_brdf(m, r)                    /* color a ray which hit a BRDF material */
194 > m_brdf(m, r)                    /* color a ray which hit a BRDTfunc material */
195   register OBJREC  *m;
196   register RAY  *r;
197   {
190        int  minsa, minfa;
198          BRDFDAT  nd;
199 +        RAY  sr;
200          double  transtest, transdist;
201 +        int  hasrefl, hastrans;
202          COLOR  ctmp;
203 <        double  dtmp, tspect, rspecr;
203 >        register MFUNC  *mf;
204          register int  i;
205                                                  /* check arguments */
206 <        switch (m->otype) {
207 <        case MAT_PFUNC: case MAT_MFUNC:
208 <                minsa = 2; minfa = 4; break;
209 <        case MAT_PDATA: case MAT_MDATA:
210 <                minsa = 4; minfa = 4; break;
211 <        case MAT_TFUNC:
212 <                minsa = 2; minfa = 6; break;
213 <        case MAT_TDATA:
214 <                minsa = 4; minfa = 6; break;
215 <        case MAT_BRTDF:
216 <                minsa = 10; minfa = 6; break;
206 >        if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9)
207 >                objerror(m, USER, "bad # arguments");
208 >        nd.mp = m;
209 >        nd.pr = r;
210 >                                                /* dummy values */
211 >        nd.rspec = nd.tspec = 1.0;
212 >        nd.trans = 0.5;
213 >                                                /* diffuse reflectance */
214 >        if (r->rod > 0.0)
215 >                setcolor(nd.rdiff, m->oargs.farg[0],
216 >                                m->oargs.farg[1],
217 >                                m->oargs.farg[2]);
218 >        else
219 >                setcolor(nd.rdiff, m->oargs.farg[3],
220 >                                m->oargs.farg[4],
221 >                                m->oargs.farg[5]);
222 >                                                /* diffuse transmittance */
223 >        setcolor(nd.tdiff, m->oargs.farg[6],
224 >                        m->oargs.farg[7],
225 >                        m->oargs.farg[8]);
226 >                                        /* get modifiers */
227 >        raytexture(r, m->omod);
228 >        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
229 >        if (r->rod < 0.0) {                     /* orient perturbed values */
230 >                nd.pdot = -nd.pdot;
231 >                for (i = 0; i < 3; i++) {
232 >                        nd.pnorm[i] = -nd.pnorm[i];
233 >                        r->pert[i] = -r->pert[i];
234 >                }
235          }
236 <        if (m->oargs.nsargs < minsa || m->oargs.nfargs < minfa)
236 >        copycolor(nd.mcolor, r->pcol);          /* get pattern color */
237 >        multcolor(nd.rdiff, nd.mcolor);         /* modify diffuse values */
238 >        multcolor(nd.tdiff, nd.mcolor);
239 >        hasrefl = bright(nd.rdiff) > FTINY;
240 >        hastrans = bright(nd.tdiff) > FTINY;
241 >                                                /* load cal file */
242 >        nd.dp = NULL;
243 >        mf = getfunc(m, 9, 0x3f, 0);
244 >                                                /* compute transmitted ray */
245 >        setbrdfunc(&nd);
246 >        transtest = 0;
247 >        transdist = r->rot;
248 >        errno = 0;
249 >        setcolor(ctmp, evalue(mf->ep[3]),
250 >                        evalue(mf->ep[4]),
251 >                        evalue(mf->ep[5]));
252 >        if (errno)
253 >                objerror(m, WARNING, "compute error");
254 >        else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
255 >                if (!(r->crtype & SHADOW) &&
256 >                                DOT(r->pert,r->pert) > FTINY*FTINY) {
257 >                        for (i = 0; i < 3; i++) /* perturb direction */
258 >                                sr.rdir[i] = r->rdir[i] - .75*r->pert[i];
259 >                        if (normalize(sr.rdir) == 0.0) {
260 >                                objerror(m, WARNING, "illegal perturbation");
261 >                                VCOPY(sr.rdir, r->rdir);
262 >                        }
263 >                } else {
264 >                        VCOPY(sr.rdir, r->rdir);
265 >                        transtest = 2;
266 >                }
267 >                rayvalue(&sr);
268 >                multcolor(sr.rcol, ctmp);
269 >                addcolor(r->rcol, sr.rcol);
270 >                transtest *= bright(sr.rcol);
271 >                transdist = r->rot + sr.rt;
272 >        }
273 >        if (r->crtype & SHADOW)                 /* the rest is shadow */
274 >                return(1);
275 >                                                /* compute reflected ray */
276 >        setbrdfunc(&nd);
277 >        errno = 0;
278 >        setcolor(ctmp, evalue(mf->ep[0]),
279 >                        evalue(mf->ep[1]),
280 >                        evalue(mf->ep[2]));
281 >        if (errno)
282 >                objerror(m, WARNING, "compute error");
283 >        else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
284 >                for (i = 0; i < 3; i++)
285 >                        sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
286 >                rayvalue(&sr);
287 >                multcolor(sr.rcol, ctmp);
288 >                addcolor(r->rcol, sr.rcol);
289 >        }
290 >                                                /* compute ambient */
291 >        if (hasrefl) {
292 >                if (nd.pdot < 0.0)
293 >                        flipsurface(r);
294 >                ambient(ctmp, r, nd.pnorm);
295 >                multcolor(ctmp, nd.rdiff);
296 >                addcolor(r->rcol, ctmp);        /* add to returned color */
297 >                if (nd.pdot < 0.0)
298 >                        flipsurface(r);
299 >        }
300 >        if (hastrans) {                         /* from other side */
301 >                if (nd.pdot > 0.0)
302 >                        flipsurface(r);
303 >                ambient(ctmp, r, nd.pnorm);
304 >                multcolor(ctmp, nd.tdiff);
305 >                addcolor(r->rcol, ctmp);
306 >                if (nd.pdot > 0.0)
307 >                        flipsurface(r);
308 >        }
309 >        if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
310 >                direct(r, dirbrdf, &nd);        /* add direct component */
311 >                                                /* check distance */
312 >        if (transtest > bright(r->rcol))
313 >                r->rt = transdist;
314 >
315 >        return(1);
316 > }
317 >
318 >
319 >
320 > m_brdf2(m, r)                   /* color a ray which hit a BRDF material */
321 > register OBJREC  *m;
322 > register RAY  *r;
323 > {
324 >        BRDFDAT  nd;
325 >        COLOR  ctmp;
326 >        double  dtmp;
327 >                                                /* always a shadow */
328 >        if (r->crtype & SHADOW)
329 >                return(1);
330 >                                                /* check arguments */
331 >        if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs <
332 >                        (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4))
333                  objerror(m, USER, "bad # arguments");
334          nd.mp = m;
335          nd.pr = r;
336 +                                                /* get material color */
337 +        setcolor(nd.mcolor, m->oargs.farg[0],
338 +                        m->oargs.farg[1],
339 +                        m->oargs.farg[2]);
340                                                  /* get specular component */
341          nd.rspec = m->oargs.farg[3];
342 <                                                /* compute transmission */
343 <        if (m->otype == MAT_TFUNC || m->otype == MAT_TDATA
217 <                        || m->otype == MAT_BRTDF) {
342 >                                                /* compute transmittance */
343 >        if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) {
344                  nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
345                  nd.tspec = nd.trans * m->oargs.farg[5];
346 <                nd.tdiff = nd.trans - nd.tspec;
347 <        } else
348 <                nd.tdiff = nd.tspec = nd.trans = 0.0;
349 <                                                /* early shadow check */
350 <        if (r->crtype & SHADOW && (m->otype != MAT_BRTDF || nd.tspec <= FTINY))
351 <                return;
352 <                                                /* diffuse reflection */
353 <        nd.rdiff = 1.0 - nd.trans - nd.rspec;
354 <                                                /* get material color */
355 <        setcolor(nd.mcolor, m->oargs.farg[0],
356 <                           m->oargs.farg[1],
357 <                           m->oargs.farg[2]);
358 <                                                /* fix orientation */
359 <        if (r->rod < 0.0)
360 <                flipsurface(r);
346 >                dtmp = nd.trans - nd.tspec;
347 >                setcolor(nd.tdiff, dtmp, dtmp, dtmp);
348 >        } else {
349 >                nd.tspec = nd.trans = 0.0;
350 >                setcolor(nd.tdiff, 0.0, 0.0, 0.0);
351 >        }
352 >                                                /* compute reflectance */
353 >        dtmp = 1.0 - nd.trans - nd.rspec;
354 >        setcolor(nd.rdiff, dtmp, dtmp, dtmp);
355 >                                                /* check for back side */
356 >        if (r->rod < 0.0) {
357 >                if (!backvis && m->otype != MAT_TFUNC
358 >                                && m->otype != MAT_TDATA) {
359 >                        raytrans(r);
360 >                        return(1);
361 >                }
362 >                flipsurface(r);                 /* reorient if backvis */
363 >        }
364                                                  /* get modifiers */
365          raytexture(r, m->omod);
366          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
367          multcolor(nd.mcolor, r->pcol);          /* modify material color */
368 <        transtest = 0;
368 >        multcolor(nd.rdiff, nd.mcolor);
369 >        multcolor(nd.tdiff, nd.mcolor);
370                                                  /* load auxiliary files */
371 <        if (m->otype == MAT_PDATA || m->otype == MAT_MDATA
242 <                        || m->otype == MAT_TDATA) {
371 >        if (hasdata(m->otype)) {
372                  nd.dp = getdata(m->oargs.sarg[1]);
373 <                for (i = 3; i < m->oargs.nsargs; i++)
245 <                        if (m->oargs.sarg[i][0] == '-')
246 <                                break;
247 <                if (i-3 != nd.dp->nd)
248 <                        objerror(m, USER, "dimension error");
249 <                funcfile(m->oargs.sarg[2]);
250 <        } else if (m->otype == MAT_BRTDF) {
251 <                nd.dp = NULL;
252 <                funcfile(m->oargs.sarg[9]);
373 >                getfunc(m, 2, 0, 0);
374          } else {
375                  nd.dp = NULL;
376 <                funcfile(m->oargs.sarg[1]);
376 >                getfunc(m, 1, 0, 0);
377          }
257                                                /* set special variables */
258        setbrdfunc(&nd);
259                                                /* compute transmitted ray */
260        tspect = 0.;
261        if (m->otype == MAT_BRTDF && nd.tspec > FTINY) {
262                RAY  sr;
263                errno = 0;
264                setcolor(ctmp, varvalue(m->oargs.sarg[3]),
265                                varvalue(m->oargs.sarg[4]),
266                                varvalue(m->oargs.sarg[5]));
267                scalecolor(ctmp, nd.trans);
268                if (errno)
269                        objerror(m, WARNING, "compute error");
270                else if ((tspect = bright(ctmp)) > FTINY &&
271                                rayorigin(&sr, r, TRANS, tspect) == 0) {
272                        if (DOT(r->pert,r->pert) > FTINY*FTINY) {
273                                for (i = 0; i < 3; i++) /* perturb direction */
274                                        sr.rdir[i] = r->rdir[i] -
275                                                        .75*r->pert[i];
276                                normalize(sr.rdir);
277                        } else {
278                                VCOPY(sr.rdir, r->rdir);
279                                transtest = 2;
280                        }
281                        rayvalue(&sr);
282                        multcolor(sr.rcol, ctmp);
283                        addcolor(r->rcol, sr.rcol);
284                        transtest *= bright(sr.rcol);
285                        transdist = r->rot + sr.rt;
286                }
287        }
288        if (r->crtype & SHADOW)                 /* the rest is shadow */
289                return;
290                                                /* compute reflected ray */
291        rspecr = 0.;
292        if (m->otype == MAT_BRTDF && nd.rspec > FTINY) {
293                RAY  sr;
294                errno = 0;
295                setcolor(ctmp, varvalue(m->oargs.sarg[0]),
296                                varvalue(m->oargs.sarg[1]),
297                                varvalue(m->oargs.sarg[2]));
298                if (errno)
299                        objerror(m, WARNING, "compute error");
300                else if ((rspecr = bright(ctmp)) > FTINY &&
301                                rayorigin(&sr, r, REFLECTED, rspecr) == 0) {
302                        for (i = 0; i < 3; i++)
303                                sr.rdir[i] = r->rdir[i] +
304                                                2.0*nd.pdot*nd.pnorm[i];
305                        rayvalue(&sr);
306                        multcolor(sr.rcol, ctmp);
307                        addcolor(r->rcol, sr.rcol);
308                }
309        }
378                                                  /* compute ambient */
379 <        if ((dtmp = 1.0-nd.trans-rspecr) > FTINY) {
380 <                ambient(ctmp, r);
381 <                scalecolor(ctmp, dtmp);
379 >        if (nd.trans < 1.0-FTINY) {
380 >                ambient(ctmp, r, nd.pnorm);
381 >                scalecolor(ctmp, 1.0-nd.trans);
382                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
383                  addcolor(r->rcol, ctmp);        /* add to returned color */
384          }
385 <        if ((dtmp = nd.trans-tspect) > FTINY) { /* from other side */
385 >        if (nd.trans > FTINY) {         /* from other side */
386                  flipsurface(r);
387 <                ambient(ctmp, r);
388 <                scalecolor(ctmp, dtmp);
387 >                ambient(ctmp, r, nd.pnorm);
388 >                scalecolor(ctmp, nd.trans);
389                  multcolor(ctmp, nd.mcolor);
390                  addcolor(r->rcol, ctmp);
391                  flipsurface(r);
392          }
393                                                  /* add direct component */
394          direct(r, dirbrdf, &nd);
395 <                                                /* check distance */
396 <        if (transtest > bright(r->rcol))
329 <                r->rt = transdist;
395 >
396 >        return(1);
397   }
398  
399  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines