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.6 by greg, Thu May 27 15:28:01 1993 UTC vs.
Revision 2.33 by greg, Wed Sep 2 18:59:01 2015 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 <
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 38 | 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 77 | Line 78 | typedef struct {
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  lddx[3], 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 120 | Line 128 | double  omega;                 /* light source size */
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          setbrdfunc(np);
136          sa = np->mp->oargs.sarg;
# Line 130 | Line 139 | double  omega;                 /* light source size */
139          multv3(ldx, ldir, funcxf.xfm);
140          for (i = 0; i < 3; i++)
141                  lddx[i] = ldx[i]/funcxf.sca;
142 +        lddx[3] = omega;
143                                          /* compute BRTDF */
144          if (np->mp->otype == MAT_BRTDF) {
145 <                if (sa[6][0] == '0')            /* special case */
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], 3, lddx);
149 <                if (!strcmp(sa[7],sa[6]))
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, lddx);
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, lddx);
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, lddx);
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, lddx);
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) {
174 >        if ((errno == EDOM) | (errno == ERANGE)) {
175                  objerror(np->mp, WARNING, "compute error");
176                  return;
177          }
# Line 167 | Line 181 | double  omega;                 /* light source size */
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 176 | 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 + #undef lddx
200   }
201  
202  
203 < m_brdf(m, r)                    /* color a ray which hit a BRDTF 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  hitfront = 1;
210          BRDFDAT  nd;
211          RAY  sr;
212 <        double  transtest, transdist;
212 >        double  mirtest=0, mirdist=0;
213 >        double  transtest=0, transdist=0;
214          int  hasrefl, hastrans;
215 +        int  hastexture;
216          COLOR  ctmp;
217 <        double  dtmp;
218 <        register MFUNC  *mf;
219 <        register int  i;
217 >        FVECT  vtmp;
218 >        double  d;
219 >        MFUNC  *mf;
220 >        int  i;
221                                                  /* check arguments */
222 <        if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9)
222 >        if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
223                  objerror(m, USER, "bad # arguments");
224          nd.mp = m;
225          nd.pr = r;
# Line 218 | Line 239 | register RAY  *r;
239          setcolor(nd.tdiff, m->oargs.farg[6],
240                          m->oargs.farg[7],
241                          m->oargs.farg[8]);
242 <                                        /* get modifiers */
242 >                                                /* get modifiers */
243          raytexture(r, m->omod);
244 <        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
244 >        hastexture = DOT(r->pert,r->pert) > FTINY*FTINY;
245 >        if (hastexture) {                       /* perturb normal */
246 >                nd.pdot = raynormal(nd.pnorm, r);
247 >        } else {
248 >                VCOPY(nd.pnorm, r->ron);
249 >                nd.pdot = r->rod;
250 >        }
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 */
# Line 238 | Line 266 | register RAY  *r;
266          mf = getfunc(m, 9, 0x3f, 0);
267                                                  /* compute transmitted ray */
268          setbrdfunc(&nd);
241        transtest = 0;
269          errno = 0;
270          setcolor(ctmp, evalue(mf->ep[3]),
271                          evalue(mf->ep[4]),
272                          evalue(mf->ep[5]));
273 <        if (errno)
273 >        if ((errno == EDOM) | (errno == ERANGE))
274                  objerror(m, WARNING, "compute error");
275 <        else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
276 <                if (!(r->crtype & SHADOW) &&
277 <                                DOT(r->pert,r->pert) > FTINY*FTINY) {
278 <                        for (i = 0; i < 3; i++) /* perturb direction */
252 <                                sr.rdir[i] = r->rdir[i] - .75*r->pert[i];
275 >        else if (rayorigin(&sr, TRANS, r, ctmp) == 0) {
276 >                if (!(r->crtype & SHADOW) && hastexture) {
277 >                                                /* perturb direction */
278 >                        VSUM(sr.rdir, r->rdir, r->pert, -.75);
279                          if (normalize(sr.rdir) == 0.0) {
280                                  objerror(m, WARNING, "illegal perturbation");
281                                  VCOPY(sr.rdir, r->rdir);
282                          }
283                  } else {
284                          VCOPY(sr.rdir, r->rdir);
259                        transtest = 2;
285                  }
286                  rayvalue(&sr);
287 <                multcolor(sr.rcol, ctmp);
287 >                multcolor(sr.rcol, sr.rcoef);
288                  addcolor(r->rcol, sr.rcol);
289 <                transtest *= bright(sr.rcol);
290 <                transdist = r->rot + sr.rt;
289 >                if (!hastexture) {
290 >                        transtest = 2.0*bright(sr.rcol);
291 >                        transdist = r->rot + sr.rt;
292 >                }
293          }
294          if (r->crtype & SHADOW)                 /* the rest is shadow */
295 <                return;
295 >                return(1);
296                                                  /* compute reflected ray */
297          setbrdfunc(&nd);
298          errno = 0;
299          setcolor(ctmp, evalue(mf->ep[0]),
300                          evalue(mf->ep[1]),
301                          evalue(mf->ep[2]));
302 <        if (errno)
302 >        if ((errno == EDOM) | (errno == ERANGE))
303                  objerror(m, WARNING, "compute error");
304 <        else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
305 <                for (i = 0; i < 3; i++)
306 <                        sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
304 >        else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) {
305 >                VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot);
306 >                checknorm(sr.rdir);
307                  rayvalue(&sr);
308 <                multcolor(sr.rcol, ctmp);
308 >                multcolor(sr.rcol, sr.rcoef);
309                  addcolor(r->rcol, sr.rcol);
310 +                if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) {
311 +                        mirtest = 2.0*bright(sr.rcol);
312 +                        mirdist = r->rot + sr.rt;
313 +                }
314          }
315                                                  /* compute ambient */
316          if (hasrefl) {
317 <                if (nd.pdot < 0.0)
317 >                if (!hitfront)
318                          flipsurface(r);
319 <                ambient(ctmp, r);
320 <                multcolor(ctmp, nd.rdiff);
319 >                copycolor(ctmp, nd.rdiff);
320 >                multambient(ctmp, r, nd.pnorm);
321                  addcolor(r->rcol, ctmp);        /* add to returned color */
322 <                if (nd.pdot < 0.0)
322 >                if (!hitfront)
323                          flipsurface(r);
324          }
325          if (hastrans) {                         /* from other side */
326 <                if (nd.pdot > 0.0)
326 >                if (hitfront)
327                          flipsurface(r);
328 <                ambient(ctmp, r);
329 <                multcolor(ctmp, nd.tdiff);
328 >                vtmp[0] = -nd.pnorm[0];
329 >                vtmp[1] = -nd.pnorm[1];
330 >                vtmp[2] = -nd.pnorm[2];
331 >                copycolor(ctmp, nd.tdiff);
332 >                multambient(ctmp, r, vtmp);
333                  addcolor(r->rcol, ctmp);
334 <                if (nd.pdot > 0.0)
334 >                if (hitfront)
335                          flipsurface(r);
336          }
337          if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
338                  direct(r, dirbrdf, &nd);        /* add direct component */
339 <                                                /* check distance */
340 <        if (transtest > bright(r->rcol))
339 >
340 >        d = bright(r->rcol);                    /* set effective distance */
341 >        if (transtest > d)
342                  r->rt = transdist;
343 +        else if (mirtest > d)
344 +                r->rt = mirdist;
345 +
346 +        return(1);
347   }
348  
349  
350  
351 < m_brdf2(m, r)                   /* color a ray which hit a BRDF material */
352 < register OBJREC  *m;
353 < register RAY  *r;
351 > int
352 > m_brdf2(                        /* color a ray that hit a BRDF material */
353 >        OBJREC  *m,
354 >        RAY  *r
355 > )
356   {
357          BRDFDAT  nd;
358          COLOR  ctmp;
359 +        FVECT  vtmp;
360          double  dtmp;
361                                                  /* always a shadow */
362          if (r->crtype & SHADOW)
363 <                return;
363 >                return(1);
364                                                  /* check arguments */
365 <        if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs <
366 <                        (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4))
365 >        if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
366 >                        ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
367                  objerror(m, USER, "bad # arguments");
368 +                                                /* check for back side */
369 +        if (r->rod < 0.0) {
370 +                if (!backvis) {
371 +                        raytrans(r);
372 +                        return(1);
373 +                }
374 +                raytexture(r, m->omod);
375 +                flipsurface(r);                 /* reorient if backvis */
376 +        } else
377 +                raytexture(r, m->omod);
378 +
379          nd.mp = m;
380          nd.pr = r;
381                                                  /* get material color */
# Line 332 | Line 385 | register RAY  *r;
385                                                  /* get specular component */
386          nd.rspec = m->oargs.farg[3];
387                                                  /* compute transmittance */
388 <        if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) {
388 >        if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
389                  nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
390                  nd.tspec = nd.trans * m->oargs.farg[5];
391                  dtmp = nd.trans - nd.tspec;
# Line 344 | Line 397 | register RAY  *r;
397                                                  /* compute reflectance */
398          dtmp = 1.0 - nd.trans - nd.rspec;
399          setcolor(nd.rdiff, dtmp, dtmp, dtmp);
347                                                /* fix orientation */
348        if (r->rod < 0.0)
349                flipsurface(r);
350                                                /* get modifiers */
351        raytexture(r, m->omod);
400          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
401          multcolor(nd.mcolor, r->pcol);          /* modify material color */
402          multcolor(nd.rdiff, nd.mcolor);
# Line 363 | Line 411 | register RAY  *r;
411          }
412                                                  /* compute ambient */
413          if (nd.trans < 1.0-FTINY) {
414 <                ambient(ctmp, r);
414 >                copycolor(ctmp, nd.mcolor);     /* modified by material color */
415                  scalecolor(ctmp, 1.0-nd.trans);
416 <                multcolor(ctmp, nd.mcolor);     /* modified by material color */
416 >                multambient(ctmp, r, nd.pnorm);
417                  addcolor(r->rcol, ctmp);        /* add to returned color */
418          }
419          if (nd.trans > FTINY) {         /* from other side */
420                  flipsurface(r);
421 <                ambient(ctmp, r);
421 >                vtmp[0] = -nd.pnorm[0];
422 >                vtmp[1] = -nd.pnorm[1];
423 >                vtmp[2] = -nd.pnorm[2];
424 >                copycolor(ctmp, nd.mcolor);
425                  scalecolor(ctmp, nd.trans);
426 <                multcolor(ctmp, nd.mcolor);
426 >                multambient(ctmp, r, vtmp);
427                  addcolor(r->rcol, ctmp);
428                  flipsurface(r);
429          }
430                                                  /* add direct component */
431          direct(r, dirbrdf, &nd);
432 +
433 +        return(1);
434   }
435  
436  
437 < setbrdfunc(np)                  /* set up brdf function and variables */
438 < register BRDFDAT  *np;
437 > static int
438 > setbrdfunc(                     /* set up brdf function and variables */
439 >        BRDFDAT  *np
440 > )
441   {
442          FVECT  vec;
443  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines