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.20 by greg, Thu Aug 28 03:22:16 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  "ambient.h"
13 +
14   #include  "data.h"
15  
16   #include  "otypes.h"
# Line 21 | Line 22 | static char SCCSid[] = "$SunId$ LBL";
22   *  String arguments include the reflection function and files.
23   *  The BRDF is currently used just for the specular component to light
24   *  sources.  Reflectance values or data coordinates are functions
25 < *  of the direction to the light source.
25 > *  of the direction to the light source.  (Data modification functions
26 > *  are passed the source direction as args 2-4.)
27   *      We orient the surface towards the incoming ray, so a single
28   *  surface can be used to represent an infinitely thin object.
29   *
# Line 77 | Line 79 | typedef struct {
79   }  BRDFDAT;             /* BRDF material data */
80  
81  
82 + static void
83   dirbrdf(cval, np, ldir, omega)          /* compute source contribution */
84   COLOR  cval;                    /* returned coefficient */
85   register BRDFDAT  *np;          /* material data */
# Line 87 | Line 90 | double  omega;                 /* light source size */
90          double  dtmp;
91          COLOR  ctmp;
92          FVECT  ldx;
93 <        double  lddx[3], 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 130 | Line 134 | double  omega;                 /* light source size */
134          multv3(ldx, ldir, funcxf.xfm);
135          for (i = 0; i < 3; i++)
136                  lddx[i] = ldx[i]/funcxf.sca;
137 +        lddx[3] = omega;
138                                          /* compute BRTDF */
139          if (np->mp->otype == MAT_BRTDF) {
140                  if (sa[6][0] == '0')            /* special case */
141                          colval(ctmp,RED) = 0.0;
142                  else
143 <                        colval(ctmp,RED) = funvalue(sa[6], 3, lddx);
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, lddx);
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, lddx);
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, lddx);
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, lddx);
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) {
165 >        if (errno == EDOM || errno == ERANGE) {
166                  objerror(np->mp, WARNING, "compute error");
167                  return;
168          }
# Line 167 | Line 172 | double  omega;                 /* light source size */
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 176 | 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 + #undef lddx
191   }
192  
193  
194 < m_brdf(m, r)                    /* color a ray which hit a BRDTF material */
194 > int
195 > m_brdf(m, r)                    /* color a ray that hit a BRDTfunc material */
196   register OBJREC  *m;
197   register RAY  *r;
198   {
199 +        int  hitfront = 1;
200          BRDFDAT  nd;
201          RAY  sr;
202          double  transtest, transdist;
203          int  hasrefl, hastrans;
204          COLOR  ctmp;
205 <        double  dtmp;
205 >        FVECT  vtmp;
206          register MFUNC  *mf;
207          register int  i;
208                                                  /* check arguments */
209 <        if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9)
209 >        if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
210                  objerror(m, USER, "bad # arguments");
211          nd.mp = m;
212          nd.pr = r;
# Line 218 | Line 226 | register RAY  *r;
226          setcolor(nd.tdiff, m->oargs.farg[6],
227                          m->oargs.farg[7],
228                          m->oargs.farg[8]);
229 <                                        /* get modifiers */
229 >                                                /* get modifiers */
230          raytexture(r, m->omod);
231          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
232          if (r->rod < 0.0) {                     /* orient perturbed values */
# Line 227 | Line 235 | register RAY  *r;
235                          nd.pnorm[i] = -nd.pnorm[i];
236                          r->pert[i] = -r->pert[i];
237                  }
238 +                hitfront = 0;
239          }
240          copycolor(nd.mcolor, r->pcol);          /* get pattern color */
241          multcolor(nd.rdiff, nd.mcolor);         /* modify diffuse values */
# Line 239 | Line 248 | register RAY  *r;
248                                                  /* compute transmitted ray */
249          setbrdfunc(&nd);
250          transtest = 0;
251 +        transdist = r->rot;
252          errno = 0;
253          setcolor(ctmp, evalue(mf->ep[3]),
254                          evalue(mf->ep[4]),
255                          evalue(mf->ep[5]));
256 <        if (errno)
256 >        if (errno == EDOM || errno == ERANGE)
257                  objerror(m, WARNING, "compute error");
258          else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
259                  if (!(r->crtype & SHADOW) &&
# Line 265 | Line 275 | register RAY  *r;
275                  transdist = r->rot + sr.rt;
276          }
277          if (r->crtype & SHADOW)                 /* the rest is shadow */
278 <                return;
278 >                return(1);
279                                                  /* compute reflected ray */
280          setbrdfunc(&nd);
281          errno = 0;
282          setcolor(ctmp, evalue(mf->ep[0]),
283                          evalue(mf->ep[1]),
284                          evalue(mf->ep[2]));
285 <        if (errno)
285 >        if (errno == EDOM || errno == ERANGE)
286                  objerror(m, WARNING, "compute error");
287          else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
288                  for (i = 0; i < 3; i++)
# Line 283 | Line 293 | register RAY  *r;
293          }
294                                                  /* compute ambient */
295          if (hasrefl) {
296 <                if (nd.pdot < 0.0)
296 >                if (!hitfront)
297                          flipsurface(r);
298 <                ambient(ctmp, r);
298 >                ambient(ctmp, r, nd.pnorm);
299                  multcolor(ctmp, nd.rdiff);
300                  addcolor(r->rcol, ctmp);        /* add to returned color */
301 <                if (nd.pdot < 0.0)
301 >                if (!hitfront)
302                          flipsurface(r);
303          }
304          if (hastrans) {                         /* from other side */
305 <                if (nd.pdot > 0.0)
305 >                if (hitfront)
306                          flipsurface(r);
307 <                ambient(ctmp, r);
307 >                vtmp[0] = -nd.pnorm[0];
308 >                vtmp[1] = -nd.pnorm[1];
309 >                vtmp[2] = -nd.pnorm[2];
310 >                ambient(ctmp, r, vtmp);
311                  multcolor(ctmp, nd.tdiff);
312                  addcolor(r->rcol, ctmp);
313 <                if (nd.pdot > 0.0)
313 >                if (hitfront)
314                          flipsurface(r);
315          }
316          if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
# Line 305 | Line 318 | register RAY  *r;
318                                                  /* check distance */
319          if (transtest > bright(r->rcol))
320                  r->rt = transdist;
321 +
322 +        return(1);
323   }
324  
325  
326  
327 < m_brdf2(m, r)                   /* color a ray which hit a BRDF material */
327 > int
328 > m_brdf2(m, r)                   /* color a ray that hit a BRDF material */
329   register OBJREC  *m;
330   register RAY  *r;
331   {
332          BRDFDAT  nd;
333          COLOR  ctmp;
334 +        FVECT  vtmp;
335          double  dtmp;
336                                                  /* always a shadow */
337          if (r->crtype & SHADOW)
338 <                return;
338 >                return(1);
339                                                  /* check arguments */
340 <        if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs <
341 <                        (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4))
340 >        if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
341 >                        ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
342                  objerror(m, USER, "bad # arguments");
343 +                                                /* check for back side */
344 +        if (r->rod < 0.0) {
345 +                if (!backvis && m->otype != MAT_TFUNC
346 +                                && m->otype != MAT_TDATA) {
347 +                        raytrans(r);
348 +                        return(1);
349 +                }
350 +                raytexture(r, m->omod);
351 +                flipsurface(r);                 /* reorient if backvis */
352 +        } else
353 +                raytexture(r, m->omod);
354 +
355          nd.mp = m;
356          nd.pr = r;
357                                                  /* get material color */
# Line 332 | Line 361 | register RAY  *r;
361                                                  /* get specular component */
362          nd.rspec = m->oargs.farg[3];
363                                                  /* compute transmittance */
364 <        if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) {
364 >        if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
365                  nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
366                  nd.tspec = nd.trans * m->oargs.farg[5];
367                  dtmp = nd.trans - nd.tspec;
# Line 344 | Line 373 | register RAY  *r;
373                                                  /* compute reflectance */
374          dtmp = 1.0 - nd.trans - nd.rspec;
375          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);
376          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
377          multcolor(nd.mcolor, r->pcol);          /* modify material color */
378          multcolor(nd.rdiff, nd.mcolor);
# Line 363 | Line 387 | register RAY  *r;
387          }
388                                                  /* compute ambient */
389          if (nd.trans < 1.0-FTINY) {
390 <                ambient(ctmp, r);
390 >                ambient(ctmp, r, nd.pnorm);
391                  scalecolor(ctmp, 1.0-nd.trans);
392                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
393                  addcolor(r->rcol, ctmp);        /* add to returned color */
394          }
395          if (nd.trans > FTINY) {         /* from other side */
396                  flipsurface(r);
397 <                ambient(ctmp, r);
397 >                vtmp[0] = -nd.pnorm[0];
398 >                vtmp[1] = -nd.pnorm[1];
399 >                vtmp[2] = -nd.pnorm[2];
400 >                ambient(ctmp, r, vtmp);
401                  scalecolor(ctmp, nd.trans);
402                  multcolor(ctmp, nd.mcolor);
403                  addcolor(r->rcol, ctmp);
# Line 378 | Line 405 | register RAY  *r;
405          }
406                                                  /* add direct component */
407          direct(r, dirbrdf, &nd);
408 +
409 +        return(1);
410   }
411  
412  
413 + int
414   setbrdfunc(np)                  /* set up brdf function and variables */
415   register BRDFDAT  *np;
416   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines