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.13 by greg, Mon Nov 6 12:03:22 1995 UTC vs.
Revision 2.26 by greg, Sun Sep 26 15:51:15 2010 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  
19 extern int  backvis;                    /* back faces visible? */
20
18   /*
19   *      Arguments to this material include the color and specularity.
20   *  String arguments include the reflection function and files.
# Line 80 | 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;
# Line 125 | Line 129 | double  omega;                 /* light source size */
129                  addcolor(cval, ctmp);
130          }
131          if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY)
132 <                return;         /* no specular component */
132 >                return;         /* diffuse only */
133                                          /* set up function */
134          setbrdfunc(np);
135          sa = np->mp->oargs.sarg;
# Line 141 | Line 145 | double  omega;                 /* light source size */
145                          colval(ctmp,RED) = 0.0;
146                  else
147                          colval(ctmp,RED) = funvalue(sa[6], 4, lddx);
148 <                if (!strcmp(sa[7],sa[6]))
148 >                if (sa[7][0] == '0')
149 >                        colval(ctmp,GRN) = 0.0;
150 >                else if (!strcmp(sa[7],sa[6]))
151                          colval(ctmp,GRN) = colval(ctmp,RED);
152                  else
153                          colval(ctmp,GRN) = funvalue(sa[7], 4, lddx);
# Line 162 | Line 168 | double  omega;                 /* light source size */
168                  dtmp = funvalue(sa[0], 5, vldx);
169                  setcolor(ctmp, dtmp, dtmp, dtmp);
170          }
171 <        if (errno) {
171 >        if ((errno == EDOM) | (errno == ERANGE)) {
172                  objerror(np->mp, WARNING, "compute error");
173                  return;
174          }
# Line 172 | Line 178 | double  omega;                 /* light source size */
178                  /*
179                   *  Compute reflected non-diffuse component.
180                   */
181 <                if (np->mp->otype == MAT_MFUNC | np->mp->otype == MAT_MDATA)
181 >                if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA))
182                          multcolor(ctmp, np->mcolor);
183                  dtmp = ldot * omega * np->rspec;
184                  scalecolor(ctmp, dtmp);
# Line 181 | Line 187 | double  omega;                 /* light source size */
187                  /*
188                   *  Compute transmitted non-diffuse component.
189                   */
190 <                if (np->mp->otype == MAT_TFUNC | np->mp->otype == MAT_TDATA)
190 >                if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA))
191                          multcolor(ctmp, np->mcolor);
192                  dtmp = -ldot * omega * np->tspec;
193                  scalecolor(ctmp, dtmp);
# Line 191 | Line 197 | double  omega;                 /* light source size */
197   }
198  
199  
200 < m_brdf(m, r)                    /* color a ray which hit a BRDTfunc material */
201 < register OBJREC  *m;
202 < register RAY  *r;
200 > extern int
201 > m_brdf(                 /* color a ray that hit a BRDTfunc material */
202 >        register OBJREC  *m,
203 >        register RAY  *r
204 > )
205   {
206 +        int  hitfront = 1;
207          BRDFDAT  nd;
208          RAY  sr;
209 +        double  mirtest=0, mirdist=0;
210          double  transtest, transdist;
211          int  hasrefl, hastrans;
212 +        int  hastexture;
213          COLOR  ctmp;
214 +        FVECT  vtmp;
215 +        double  d;
216          register MFUNC  *mf;
217          register int  i;
218                                                  /* check arguments */
219 <        if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9)
219 >        if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
220                  objerror(m, USER, "bad # arguments");
221          nd.mp = m;
222          nd.pr = r;
# Line 223 | Line 236 | register RAY  *r;
236          setcolor(nd.tdiff, m->oargs.farg[6],
237                          m->oargs.farg[7],
238                          m->oargs.farg[8]);
239 <                                        /* get modifiers */
239 >                                                /* get modifiers */
240          raytexture(r, m->omod);
241 <        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
241 >        hastexture = DOT(r->pert,r->pert) > FTINY*FTINY;
242 >        if (hastexture) {                       /* perturb normal */
243 >                nd.pdot = raynormal(nd.pnorm, r);
244 >        } else {
245 >                VCOPY(nd.pnorm, r->ron);
246 >                nd.pdot = r->rod;
247 >        }
248          if (r->rod < 0.0) {                     /* orient perturbed values */
249                  nd.pdot = -nd.pdot;
250                  for (i = 0; i < 3; i++) {
251                          nd.pnorm[i] = -nd.pnorm[i];
252                          r->pert[i] = -r->pert[i];
253                  }
254 +                hitfront = 0;
255          }
256          copycolor(nd.mcolor, r->pcol);          /* get pattern color */
257          multcolor(nd.rdiff, nd.mcolor);         /* modify diffuse values */
# Line 243 | Line 263 | register RAY  *r;
263          mf = getfunc(m, 9, 0x3f, 0);
264                                                  /* compute transmitted ray */
265          setbrdfunc(&nd);
246        transtest = 0;
247        transdist = r->rot;
266          errno = 0;
267          setcolor(ctmp, evalue(mf->ep[3]),
268                          evalue(mf->ep[4]),
269                          evalue(mf->ep[5]));
270 <        if (errno)
270 >        if ((errno == EDOM) | (errno == ERANGE))
271                  objerror(m, WARNING, "compute error");
272 <        else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
273 <                if (!(r->crtype & SHADOW) &&
256 <                                DOT(r->pert,r->pert) > FTINY*FTINY) {
272 >        else if (rayorigin(&sr, TRANS, r, ctmp) == 0) {
273 >                if (!(r->crtype & SHADOW) && hastexture) {
274                          for (i = 0; i < 3; i++) /* perturb direction */
275                                  sr.rdir[i] = r->rdir[i] - .75*r->pert[i];
276                          if (normalize(sr.rdir) == 0.0) {
# Line 262 | Line 279 | register RAY  *r;
279                          }
280                  } else {
281                          VCOPY(sr.rdir, r->rdir);
265                        transtest = 2;
282                  }
283                  rayvalue(&sr);
284 <                multcolor(sr.rcol, ctmp);
284 >                multcolor(sr.rcol, sr.rcoef);
285                  addcolor(r->rcol, sr.rcol);
286 <                transtest *= bright(sr.rcol);
287 <                transdist = r->rot + sr.rt;
286 >                if (!hastexture) {
287 >                        transtest = 2.0*bright(sr.rcol);
288 >                        transdist = r->rot + sr.rt;
289 >                }
290          }
291          if (r->crtype & SHADOW)                 /* the rest is shadow */
292                  return(1);
# Line 278 | Line 296 | register RAY  *r;
296          setcolor(ctmp, evalue(mf->ep[0]),
297                          evalue(mf->ep[1]),
298                          evalue(mf->ep[2]));
299 <        if (errno)
299 >        if ((errno == EDOM) | (errno == ERANGE))
300                  objerror(m, WARNING, "compute error");
301 <        else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
301 >        else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) {
302                  for (i = 0; i < 3; i++)
303                          sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
304 +                checknorm(sr.rdir);
305                  rayvalue(&sr);
306 <                multcolor(sr.rcol, ctmp);
306 >                multcolor(sr.rcol, sr.rcoef);
307                  addcolor(r->rcol, sr.rcol);
308 +                if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) {
309 +                        mirtest = 2.0*bright(sr.rcol);
310 +                        mirdist = r->rot + sr.rt;
311 +                }
312          }
313                                                  /* compute ambient */
314          if (hasrefl) {
315 <                if (nd.pdot < 0.0)
315 >                if (!hitfront)
316                          flipsurface(r);
317 <                ambient(ctmp, r, nd.pnorm);
318 <                multcolor(ctmp, nd.rdiff);
317 >                copycolor(ctmp, nd.rdiff);
318 >                multambient(ctmp, r, nd.pnorm);
319                  addcolor(r->rcol, ctmp);        /* add to returned color */
320 <                if (nd.pdot < 0.0)
320 >                if (!hitfront)
321                          flipsurface(r);
322          }
323          if (hastrans) {                         /* from other side */
324 <                if (nd.pdot > 0.0)
324 >                if (hitfront)
325                          flipsurface(r);
326 <                ambient(ctmp, r, nd.pnorm);
327 <                multcolor(ctmp, nd.tdiff);
326 >                vtmp[0] = -nd.pnorm[0];
327 >                vtmp[1] = -nd.pnorm[1];
328 >                vtmp[2] = -nd.pnorm[2];
329 >                copycolor(ctmp, nd.tdiff);
330 >                multambient(ctmp, r, vtmp);
331                  addcolor(r->rcol, ctmp);
332 <                if (nd.pdot > 0.0)
332 >                if (hitfront)
333                          flipsurface(r);
334          }
335          if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
336                  direct(r, dirbrdf, &nd);        /* add direct component */
337 <                                                /* check distance */
338 <        if (transtest > bright(r->rcol))
337 >
338 >        d = bright(r->rcol);                    /* set effective distance */
339 >        if (transtest > d)
340                  r->rt = transdist;
341 +        else if (mirtest > d)
342 +                r->rt = mirdist;
343  
344          return(1);
345   }
346  
347  
348  
349 < m_brdf2(m, r)                   /* color a ray which hit a BRDF material */
350 < register OBJREC  *m;
351 < register RAY  *r;
349 > extern int
350 > m_brdf2(                        /* color a ray that hit a BRDF material */
351 >        register OBJREC  *m,
352 >        register RAY  *r
353 > )
354   {
355          BRDFDAT  nd;
356          COLOR  ctmp;
357 +        FVECT  vtmp;
358          double  dtmp;
359                                                  /* always a shadow */
360          if (r->crtype & SHADOW)
361                  return(1);
362                                                  /* check arguments */
363 <        if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs <
364 <                        (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4))
363 >        if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
364 >                        ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
365                  objerror(m, USER, "bad # arguments");
366 +                                                /* check for back side */
367 +        if (r->rod < 0.0) {
368 +                if (!backvis && m->otype != MAT_TFUNC
369 +                                && m->otype != MAT_TDATA) {
370 +                        raytrans(r);
371 +                        return(1);
372 +                }
373 +                raytexture(r, m->omod);
374 +                flipsurface(r);                 /* reorient if backvis */
375 +        } else
376 +                raytexture(r, m->omod);
377 +
378          nd.mp = m;
379          nd.pr = r;
380                                                  /* get material color */
# Line 340 | Line 384 | register RAY  *r;
384                                                  /* get specular component */
385          nd.rspec = m->oargs.farg[3];
386                                                  /* compute transmittance */
387 <        if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) {
387 >        if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
388                  nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
389                  nd.tspec = nd.trans * m->oargs.farg[5];
390                  dtmp = nd.trans - nd.tspec;
# Line 352 | Line 396 | register RAY  *r;
396                                                  /* compute reflectance */
397          dtmp = 1.0 - nd.trans - nd.rspec;
398          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);
399          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
400          multcolor(nd.mcolor, r->pcol);          /* modify material color */
401          multcolor(nd.rdiff, nd.mcolor);
# Line 377 | Line 410 | register RAY  *r;
410          }
411                                                  /* compute ambient */
412          if (nd.trans < 1.0-FTINY) {
413 <                ambient(ctmp, r, nd.pnorm);
413 >                copycolor(ctmp, nd.mcolor);     /* modified by material color */
414                  scalecolor(ctmp, 1.0-nd.trans);
415 <                multcolor(ctmp, nd.mcolor);     /* modified by material color */
415 >                multambient(ctmp, r, nd.pnorm);
416                  addcolor(r->rcol, ctmp);        /* add to returned color */
417          }
418          if (nd.trans > FTINY) {         /* from other side */
419                  flipsurface(r);
420 <                ambient(ctmp, r, nd.pnorm);
420 >                vtmp[0] = -nd.pnorm[0];
421 >                vtmp[1] = -nd.pnorm[1];
422 >                vtmp[2] = -nd.pnorm[2];
423 >                copycolor(ctmp, nd.mcolor);
424                  scalecolor(ctmp, nd.trans);
425 <                multcolor(ctmp, nd.mcolor);
425 >                multambient(ctmp, r, vtmp);
426                  addcolor(r->rcol, ctmp);
427                  flipsurface(r);
428          }
# Line 397 | Line 433 | register RAY  *r;
433   }
434  
435  
436 < setbrdfunc(np)                  /* set up brdf function and variables */
437 < register BRDFDAT  *np;
436 > static int
437 > setbrdfunc(                     /* set up brdf function and variables */
438 >        register BRDFDAT  *np
439 > )
440   {
441          FVECT  vec;
442  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines