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.18 by greg, Wed Mar 5 16:16:53 2003 UTC vs.
Revision 2.39 by greg, Fri Apr 19 19:01:32 2019 UTC

# Line 8 | Line 8 | static const char      RCSid[] = "$Id$";
8   #include "copyright.h"
9  
10   #include  "ray.h"
11 <
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.
# Line 38 | Line 39 | static const char      RCSid[] = "$Id$";
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 + static int setbrdfunc(BRDFDAT *np);
82 +
83 +
84   static void
85 < dirbrdf(cval, np, ldir, omega)          /* compute source contribution */
86 < COLOR  cval;                    /* returned coefficient */
87 < register BRDFDAT  *np;          /* material data */
88 < FVECT  ldir;                    /* light source direction */
89 < double  omega;                  /* light source size */
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          static double  vldx[5], pt[MAXDIM];
98 <        register char   **sa;
99 <        register int    i;
98 >        char    **sa;
99 >        int     i;
100   #define lddx (vldx+1)
101  
102          setcolor(cval, 0.0, 0.0, 0.0);
# Line 122 | 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 135 | Line 142 | double  omega;                 /* light source size */
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], 4, lddx);
149 <                if (!strcmp(sa[7],sa[6]))
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], 4, lddx);
155 <                if (!strcmp(sa[8],sa[6]))
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);
# Line 160 | Line 171 | double  omega;                 /* light source size */
171                  dtmp = funvalue(sa[0], 5, vldx);
172                  setcolor(ctmp, dtmp, dtmp, dtmp);
173          }
174 <        if (errno == EDOM || errno == ERANGE) {
174 >        if ((errno == EDOM) | (errno == ERANGE)) {
175                  objerror(np->mp, WARNING, "compute error");
176                  return;
177          }
# Line 170 | 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 179 | 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);
# Line 190 | Line 201 | double  omega;                 /* light source size */
201  
202  
203   int
204 < m_brdf(m, r)                    /* color a ray that hit a BRDTfunc material */
205 < register OBJREC  *m;
206 < register RAY  *r;
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;
200        double  transtest, transdist;
212          int  hasrefl, hastrans;
213 +        int  hastexture;
214          COLOR  ctmp;
215          FVECT  vtmp;
216 <        register MFUNC  *mf;
217 <        register int  i;
216 >        double  d;
217 >        MFUNC  *mf;
218 >        int  i;
219                                                  /* check arguments */
220 <        if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9)
220 >        if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
221                  objerror(m, USER, "bad # arguments");
222          nd.mp = m;
223          nd.pr = r;
# Line 226 | Line 239 | register RAY  *r;
239                          m->oargs.farg[8]);
240                                                  /* get modifiers */
241          raytexture(r, m->omod);
242 <        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
242 >        hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY);
243 >        if (hastexture) {                       /* perturb normal */
244 >                nd.pdot = raynormal(nd.pnorm, r);
245 >        } else {
246 >                VCOPY(nd.pnorm, r->ron);
247 >                nd.pdot = r->rod;
248 >        }
249          if (r->rod < 0.0) {                     /* orient perturbed values */
250                  nd.pdot = -nd.pdot;
251                  for (i = 0; i < 3; i++) {
# Line 238 | Line 257 | register RAY  *r;
257          copycolor(nd.mcolor, r->pcol);          /* get pattern color */
258          multcolor(nd.rdiff, nd.mcolor);         /* modify diffuse values */
259          multcolor(nd.tdiff, nd.mcolor);
260 <        hasrefl = bright(nd.rdiff) > FTINY;
261 <        hastrans = bright(nd.tdiff) > FTINY;
260 >        hasrefl = (bright(nd.rdiff) > FTINY);
261 >        hastrans = (bright(nd.tdiff) > FTINY);
262                                                  /* load cal file */
263          nd.dp = NULL;
264          mf = getfunc(m, 9, 0x3f, 0);
265                                                  /* compute transmitted ray */
266          setbrdfunc(&nd);
248        transtest = 0;
249        transdist = r->rot;
267          errno = 0;
268          setcolor(ctmp, evalue(mf->ep[3]),
269                          evalue(mf->ep[4]),
270                          evalue(mf->ep[5]));
271 <        if (errno == EDOM || errno == ERANGE)
271 >        if ((errno == EDOM) | (errno == ERANGE))
272                  objerror(m, WARNING, "compute error");
273 <        else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
274 <                if (!(r->crtype & SHADOW) &&
275 <                                DOT(r->pert,r->pert) > FTINY*FTINY) {
276 <                        for (i = 0; i < 3; i++) /* perturb direction */
260 <                                sr.rdir[i] = r->rdir[i] - .75*r->pert[i];
273 >        else if (rayorigin(&sr, TRANS, r, ctmp) == 0) {
274 >                if (hastexture && !(r->crtype & (SHADOW|AMBIENT))) {
275 >                                                /* perturb direction */
276 >                        VSUB(sr.rdir, r->rdir, r->pert);
277                          if (normalize(sr.rdir) == 0.0) {
278                                  objerror(m, WARNING, "illegal perturbation");
279                                  VCOPY(sr.rdir, r->rdir);
280                          }
281                  } else {
282                          VCOPY(sr.rdir, r->rdir);
267                        transtest = 2;
283                  }
284                  rayvalue(&sr);
285 <                multcolor(sr.rcol, ctmp);
285 >                multcolor(sr.rcol, sr.rcoef);
286                  addcolor(r->rcol, sr.rcol);
287 <                transtest *= bright(sr.rcol);
288 <                transdist = r->rot + sr.rt;
287 >                if ((!hastexture || r->crtype & (SHADOW|AMBIENT)) &&
288 >                                nd.tspec > bright(nd.tdiff) + bright(nd.rdiff))
289 >                        r->rxt = r->rot + raydistance(&sr);
290          }
291          if (r->crtype & SHADOW)                 /* the rest is shadow */
292                  return(1);
293 +
294                                                  /* compute reflected ray */
295          setbrdfunc(&nd);
296          errno = 0;
297          setcolor(ctmp, evalue(mf->ep[0]),
298                          evalue(mf->ep[1]),
299                          evalue(mf->ep[2]));
300 <        if (errno == EDOM || errno == ERANGE)
300 >        if ((errno == EDOM) | (errno == ERANGE))
301                  objerror(m, WARNING, "compute error");
302 <        else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
303 <                for (i = 0; i < 3; i++)
304 <                        sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
302 >        else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) {
303 >                VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot);
304 >                checknorm(sr.rdir);
305                  rayvalue(&sr);
306 <                multcolor(sr.rcol, ctmp);
306 >                multcolor(sr.rcol, sr.rcoef);
307 >                copycolor(r->mcol, sr.rcol);
308                  addcolor(r->rcol, sr.rcol);
309 +                r->rmt = r->rot;
310 +                if (r->ro != NULL && isflat(r->ro->otype) &&
311 +                                !hastexture | (r->crtype & AMBIENT))
312 +                        r->rmt += raydistance(&sr);
313          }
314                                                  /* compute ambient */
315          if (hasrefl) {
316                  if (!hitfront)
317                          flipsurface(r);
318 <                ambient(ctmp, r, nd.pnorm);
319 <                multcolor(ctmp, nd.rdiff);
318 >                copycolor(ctmp, nd.rdiff);
319 >                multambient(ctmp, r, nd.pnorm);
320                  addcolor(r->rcol, ctmp);        /* add to returned color */
321                  if (!hitfront)
322                          flipsurface(r);
# Line 305 | Line 327 | register RAY  *r;
327                  vtmp[0] = -nd.pnorm[0];
328                  vtmp[1] = -nd.pnorm[1];
329                  vtmp[2] = -nd.pnorm[2];
330 <                ambient(ctmp, r, vtmp);
331 <                multcolor(ctmp, nd.tdiff);
330 >                copycolor(ctmp, nd.tdiff);
331 >                multambient(ctmp, r, vtmp);
332                  addcolor(r->rcol, ctmp);
333                  if (hitfront)
334                          flipsurface(r);
335          }
336          if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
337                  direct(r, dirbrdf, &nd);        /* add direct component */
316                                                /* check distance */
317        if (transtest > bright(r->rcol))
318                r->rt = transdist;
338  
339          return(1);
340   }
# Line 323 | Line 342 | register RAY  *r;
342  
343  
344   int
345 < m_brdf2(m, r)                   /* color a ray that hit a BRDF material */
346 < register OBJREC  *m;
347 < register RAY  *r;
345 > m_brdf2(                        /* color a ray that hit a BRDF material */
346 >        OBJREC  *m,
347 >        RAY  *r
348 > )
349   {
350          BRDFDAT  nd;
351          COLOR  ctmp;
# Line 335 | Line 355 | register RAY  *r;
355          if (r->crtype & SHADOW)
356                  return(1);
357                                                  /* check arguments */
358 <        if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs <
359 <                        (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4))
358 >        if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
359 >                        ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
360                  objerror(m, USER, "bad # arguments");
361                                                  /* check for back side */
362          if (r->rod < 0.0) {
363 <                if (!backvis && m->otype != MAT_TFUNC
344 <                                && m->otype != MAT_TDATA) {
363 >                if (!backvis) {
364                          raytrans(r);
365                          return(1);
366                  }
# Line 359 | Line 378 | register RAY  *r;
378                                                  /* get specular component */
379          nd.rspec = m->oargs.farg[3];
380                                                  /* compute transmittance */
381 <        if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) {
381 >        if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
382                  nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
383                  nd.tspec = nd.trans * m->oargs.farg[5];
384                  dtmp = nd.trans - nd.tspec;
# Line 385 | Line 404 | register RAY  *r;
404          }
405                                                  /* compute ambient */
406          if (nd.trans < 1.0-FTINY) {
407 <                ambient(ctmp, r, nd.pnorm);
407 >                copycolor(ctmp, nd.mcolor);     /* modified by material color */
408                  scalecolor(ctmp, 1.0-nd.trans);
409 <                multcolor(ctmp, nd.mcolor);     /* modified by material color */
409 >                multambient(ctmp, r, nd.pnorm);
410                  addcolor(r->rcol, ctmp);        /* add to returned color */
411          }
412 <        if (nd.trans > FTINY) {         /* from other side */
412 >        if (nd.trans > FTINY) {                 /* from other side */
413                  flipsurface(r);
414                  vtmp[0] = -nd.pnorm[0];
415                  vtmp[1] = -nd.pnorm[1];
416                  vtmp[2] = -nd.pnorm[2];
417 <                ambient(ctmp, r, vtmp);
417 >                copycolor(ctmp, nd.mcolor);
418                  scalecolor(ctmp, nd.trans);
419 <                multcolor(ctmp, nd.mcolor);
419 >                multambient(ctmp, r, vtmp);
420                  addcolor(r->rcol, ctmp);
421                  flipsurface(r);
422          }
# Line 408 | Line 427 | register RAY  *r;
427   }
428  
429  
430 < int
431 < setbrdfunc(np)                  /* set up brdf function and variables */
432 < register BRDFDAT  *np;
430 > static int
431 > setbrdfunc(                     /* set up brdf function and variables */
432 >        BRDFDAT  *np
433 > )
434   {
435          FVECT  vec;
436  
# Line 418 | Line 438 | register BRDFDAT  *np;
438                  return(0);      /* it's OK, setfunc says we're done */
439                                  /* else (re)assign special variables */
440          multv3(vec, np->pnorm, funcxf.xfm);
441 <        varset("NxP", '=', vec[0]/funcxf.sca);
442 <        varset("NyP", '=', vec[1]/funcxf.sca);
443 <        varset("NzP", '=', vec[2]/funcxf.sca);
444 <        varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
441 >        varset("NxP`", '=', vec[0]/funcxf.sca);
442 >        varset("NyP`", '=', vec[1]/funcxf.sca);
443 >        varset("NzP`", '=', vec[2]/funcxf.sca);
444 >        varset("RdotP`", '=', np->pdot <= -1.0 ? -1.0 :
445                          np->pdot >= 1.0 ? 1.0 : np->pdot);
446          varset("CrP", '=', colval(np->mcolor,RED));
447          varset("CgP", '=', colval(np->mcolor,GRN));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines