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.8 by greg, Tue Aug 24 12:59:25 1993 UTC vs.
Revision 2.21 by schorsch, Tue Mar 30 16:13:01 2004 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  
18   /*
# Line 78 | Line 77 | typedef struct {
77   }  BRDFDAT;             /* BRDF material data */
78  
79  
80 < dirbrdf(cval, np, ldir, omega)          /* compute source contribution */
81 < COLOR  cval;                    /* returned coefficient */
82 < register BRDFDAT  *np;          /* material data */
83 < FVECT  ldir;                    /* light source direction */
84 < double  omega;                  /* light source size */
80 > static srcdirf_t dirbrdf;
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 +        register BRDFDAT *np = nnp;
93          double  ldot;
94          double  dtmp;
95          COLOR  ctmp;
96          FVECT  ldx;
97 <        double  lddx[3], pt[MAXDIM];
92 <        double  vldx[4];
97 >        static double  vldx[5], pt[MAXDIM];
98          register char   **sa;
99          register int    i;
100 + #define lddx (vldx+1)
101  
102          setcolor(cval, 0.0, 0.0, 0.0);
103          
# Line 132 | Line 138 | double  omega;                 /* light source size */
138          multv3(ldx, ldir, funcxf.xfm);
139          for (i = 0; i < 3; i++)
140                  lddx[i] = ldx[i]/funcxf.sca;
141 +        lddx[3] = omega;
142                                          /* compute BRTDF */
143          if (np->mp->otype == MAT_BRTDF) {
144                  if (sa[6][0] == '0')            /* special case */
145                          colval(ctmp,RED) = 0.0;
146                  else
147 <                        colval(ctmp,RED) = funvalue(sa[6], 3, lddx);
147 >                        colval(ctmp,RED) = funvalue(sa[6], 4, lddx);
148                  if (!strcmp(sa[7],sa[6]))
149                          colval(ctmp,GRN) = colval(ctmp,RED);
150                  else
151 <                        colval(ctmp,GRN) = funvalue(sa[7], 3, lddx);
151 >                        colval(ctmp,GRN) = funvalue(sa[7], 4, lddx);
152                  if (!strcmp(sa[8],sa[6]))
153                          colval(ctmp,BLU) = colval(ctmp,RED);
154                  else if (!strcmp(sa[8],sa[7]))
155                          colval(ctmp,BLU) = colval(ctmp,GRN);
156                  else
157 <                        colval(ctmp,BLU) = funvalue(sa[8], 3, lddx);
157 >                        colval(ctmp,BLU) = funvalue(sa[8], 4, lddx);
158                  dtmp = bright(ctmp);
159          } else if (np->dp == NULL) {
160 <                dtmp = funvalue(sa[0], 3, lddx);
160 >                dtmp = funvalue(sa[0], 4, lddx);
161                  setcolor(ctmp, dtmp, dtmp, dtmp);
162          } else {
163                  for (i = 0; i < np->dp->nd; i++)
164 <                        pt[i] = funvalue(sa[3+i], 3, lddx);
164 >                        pt[i] = funvalue(sa[3+i], 4, lddx);
165                  vldx[0] = datavalue(np->dp, pt);
166 <                vldx[1] = lddx[0]; vldx[2] = lddx[1]; vldx[3] = lddx[2];
160 <                dtmp = funvalue(sa[0], 4, vldx);
166 >                dtmp = funvalue(sa[0], 5, vldx);
167                  setcolor(ctmp, dtmp, dtmp, dtmp);
168          }
169 <        if (errno) {
169 >        if (errno == EDOM || errno == ERANGE) {
170                  objerror(np->mp, WARNING, "compute error");
171                  return;
172          }
# Line 170 | Line 176 | double  omega;                 /* light source size */
176                  /*
177                   *  Compute reflected non-diffuse component.
178                   */
179 <                if (np->mp->otype == MAT_MFUNC | np->mp->otype == MAT_MDATA)
179 >                if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA))
180                          multcolor(ctmp, np->mcolor);
181                  dtmp = ldot * omega * np->rspec;
182                  scalecolor(ctmp, dtmp);
# Line 179 | Line 185 | double  omega;                 /* light source size */
185                  /*
186                   *  Compute transmitted non-diffuse component.
187                   */
188 <                if (np->mp->otype == MAT_TFUNC | np->mp->otype == MAT_TDATA)
188 >                if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA))
189                          multcolor(ctmp, np->mcolor);
190                  dtmp = -ldot * omega * np->tspec;
191                  scalecolor(ctmp, dtmp);
192                  addcolor(cval, ctmp);
193          }
194 + #undef lddx
195   }
196  
197  
198 < m_brdf(m, r)                    /* color a ray which hit a BRDTF material */
199 < register OBJREC  *m;
200 < register RAY  *r;
198 > extern int
199 > m_brdf(                 /* color a ray that hit a BRDTfunc material */
200 >        register OBJREC  *m,
201 >        register RAY  *r
202 > )
203   {
204 +        int  hitfront = 1;
205          BRDFDAT  nd;
206          RAY  sr;
207          double  transtest, transdist;
208          int  hasrefl, hastrans;
209          COLOR  ctmp;
210 <        double  dtmp;
210 >        FVECT  vtmp;
211          register MFUNC  *mf;
212          register int  i;
213                                                  /* check arguments */
214 <        if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9)
214 >        if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
215                  objerror(m, USER, "bad # arguments");
216          nd.mp = m;
217          nd.pr = r;
# Line 221 | Line 231 | register RAY  *r;
231          setcolor(nd.tdiff, m->oargs.farg[6],
232                          m->oargs.farg[7],
233                          m->oargs.farg[8]);
234 <                                        /* get modifiers */
234 >                                                /* get modifiers */
235          raytexture(r, m->omod);
236          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
237          if (r->rod < 0.0) {                     /* orient perturbed values */
# Line 230 | Line 240 | register RAY  *r;
240                          nd.pnorm[i] = -nd.pnorm[i];
241                          r->pert[i] = -r->pert[i];
242                  }
243 +                hitfront = 0;
244          }
245          copycolor(nd.mcolor, r->pcol);          /* get pattern color */
246          multcolor(nd.rdiff, nd.mcolor);         /* modify diffuse values */
# Line 247 | Line 258 | register RAY  *r;
258          setcolor(ctmp, evalue(mf->ep[3]),
259                          evalue(mf->ep[4]),
260                          evalue(mf->ep[5]));
261 <        if (errno)
261 >        if (errno == EDOM || errno == ERANGE)
262                  objerror(m, WARNING, "compute error");
263          else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
264                  if (!(r->crtype & SHADOW) &&
# Line 269 | Line 280 | register RAY  *r;
280                  transdist = r->rot + sr.rt;
281          }
282          if (r->crtype & SHADOW)                 /* the rest is shadow */
283 <                return;
283 >                return(1);
284                                                  /* compute reflected ray */
285          setbrdfunc(&nd);
286          errno = 0;
287          setcolor(ctmp, evalue(mf->ep[0]),
288                          evalue(mf->ep[1]),
289                          evalue(mf->ep[2]));
290 <        if (errno)
290 >        if (errno == EDOM || errno == ERANGE)
291                  objerror(m, WARNING, "compute error");
292          else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
293                  for (i = 0; i < 3; i++)
# Line 287 | Line 298 | register RAY  *r;
298          }
299                                                  /* compute ambient */
300          if (hasrefl) {
301 <                if (nd.pdot < 0.0)
301 >                if (!hitfront)
302                          flipsurface(r);
303 <                ambient(ctmp, r);
303 >                ambient(ctmp, r, nd.pnorm);
304                  multcolor(ctmp, nd.rdiff);
305                  addcolor(r->rcol, ctmp);        /* add to returned color */
306 <                if (nd.pdot < 0.0)
306 >                if (!hitfront)
307                          flipsurface(r);
308          }
309          if (hastrans) {                         /* from other side */
310 <                if (nd.pdot > 0.0)
310 >                if (hitfront)
311                          flipsurface(r);
312 <                ambient(ctmp, r);
312 >                vtmp[0] = -nd.pnorm[0];
313 >                vtmp[1] = -nd.pnorm[1];
314 >                vtmp[2] = -nd.pnorm[2];
315 >                ambient(ctmp, r, vtmp);
316                  multcolor(ctmp, nd.tdiff);
317                  addcolor(r->rcol, ctmp);
318 <                if (nd.pdot > 0.0)
318 >                if (hitfront)
319                          flipsurface(r);
320          }
321          if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
# Line 309 | Line 323 | register RAY  *r;
323                                                  /* check distance */
324          if (transtest > bright(r->rcol))
325                  r->rt = transdist;
326 +
327 +        return(1);
328   }
329  
330  
331  
332 < m_brdf2(m, r)                   /* color a ray which hit a BRDF material */
333 < register OBJREC  *m;
334 < register RAY  *r;
332 > extern int
333 > m_brdf2(                        /* color a ray that hit a BRDF material */
334 >        register OBJREC  *m,
335 >        register RAY  *r
336 > )
337   {
338          BRDFDAT  nd;
339          COLOR  ctmp;
340 +        FVECT  vtmp;
341          double  dtmp;
342                                                  /* always a shadow */
343          if (r->crtype & SHADOW)
344 <                return;
344 >                return(1);
345                                                  /* check arguments */
346 <        if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs <
347 <                        (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4))
346 >        if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
347 >                        ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
348                  objerror(m, USER, "bad # arguments");
349 +                                                /* check for back side */
350 +        if (r->rod < 0.0) {
351 +                if (!backvis && m->otype != MAT_TFUNC
352 +                                && m->otype != MAT_TDATA) {
353 +                        raytrans(r);
354 +                        return(1);
355 +                }
356 +                raytexture(r, m->omod);
357 +                flipsurface(r);                 /* reorient if backvis */
358 +        } else
359 +                raytexture(r, m->omod);
360 +
361          nd.mp = m;
362          nd.pr = r;
363                                                  /* get material color */
# Line 336 | Line 367 | register RAY  *r;
367                                                  /* get specular component */
368          nd.rspec = m->oargs.farg[3];
369                                                  /* compute transmittance */
370 <        if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) {
370 >        if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
371                  nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
372                  nd.tspec = nd.trans * m->oargs.farg[5];
373                  dtmp = nd.trans - nd.tspec;
# Line 348 | Line 379 | register RAY  *r;
379                                                  /* compute reflectance */
380          dtmp = 1.0 - nd.trans - nd.rspec;
381          setcolor(nd.rdiff, dtmp, dtmp, dtmp);
351                                                /* fix orientation */
352        if (r->rod < 0.0)
353                flipsurface(r);
354                                                /* get modifiers */
355        raytexture(r, m->omod);
382          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
383          multcolor(nd.mcolor, r->pcol);          /* modify material color */
384          multcolor(nd.rdiff, nd.mcolor);
# Line 367 | Line 393 | register RAY  *r;
393          }
394                                                  /* compute ambient */
395          if (nd.trans < 1.0-FTINY) {
396 <                ambient(ctmp, r);
396 >                ambient(ctmp, r, nd.pnorm);
397                  scalecolor(ctmp, 1.0-nd.trans);
398                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
399                  addcolor(r->rcol, ctmp);        /* add to returned color */
400          }
401          if (nd.trans > FTINY) {         /* from other side */
402                  flipsurface(r);
403 <                ambient(ctmp, r);
403 >                vtmp[0] = -nd.pnorm[0];
404 >                vtmp[1] = -nd.pnorm[1];
405 >                vtmp[2] = -nd.pnorm[2];
406 >                ambient(ctmp, r, vtmp);
407                  scalecolor(ctmp, nd.trans);
408                  multcolor(ctmp, nd.mcolor);
409                  addcolor(r->rcol, ctmp);
# Line 382 | Line 411 | register RAY  *r;
411          }
412                                                  /* add direct component */
413          direct(r, dirbrdf, &nd);
414 +
415 +        return(1);
416   }
417  
418  
419 < setbrdfunc(np)                  /* set up brdf function and variables */
420 < register BRDFDAT  *np;
419 > static int
420 > setbrdfunc(                     /* set up brdf function and variables */
421 >        register BRDFDAT  *np
422 > )
423   {
424          FVECT  vec;
425  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines