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.29 by greg, Wed Mar 20 23:07:51 2013 UTC vs.
Revision 2.42 by greg, Thu Dec 5 19:23:43 2024 UTC

# Line 14 | Line 14 | static const char      RCSid[] = "$Id$";
14   #include  "otypes.h"
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 66 | Line 67 | typedef struct {
67          OBJREC  *mp;            /* material pointer */
68          RAY  *pr;               /* intersected ray */
69          DATARRAY  *dp;          /* data array for PDATA, MDATA or TDATA */
70 <        COLOR  mcolor;          /* material (or pattern) color */
71 <        COLOR  rdiff;           /* diffuse reflection */
72 <        COLOR  tdiff;           /* diffuse transmission */
70 >        SCOLOR  mcolor;         /* material (or pattern) color */
71 >        SCOLOR  rdiff;          /* diffuse reflection */
72 >        SCOLOR  tdiff;          /* diffuse transmission */
73          double  rspec;          /* specular reflectance (1 for BRDTF) */
74          double  trans;          /* transmissivity (.5 for BRDTF) */
75          double  tspec;          /* specular transmittance (1 for BRDTF) */
# Line 82 | Line 83 | static int setbrdfunc(BRDFDAT *np);
83  
84   static void
85   dirbrdf(                /* compute source contribution */
86 <        COLOR  cval,                    /* returned coefficient */
87 <        void  *nnp,             /* material data */
86 >        SCOLOR  scval,                  /* returned coefficient */
87 >        void  *nnp,                     /* material data */
88          FVECT  ldir,                    /* light source direction */
89          double  omega                   /* light source size */
90   )
# Line 91 | Line 92 | dirbrdf(               /* compute source contribution */
92          BRDFDAT *np = nnp;
93          double  ldot;
94          double  dtmp;
95 +        SCOLOR  sctmp;
96          COLOR  ctmp;
97          FVECT  ldx;
98 <        static double  vldx[5], pt[MAXDIM];
98 >        static double  vldx[5], pt[MAXDDIM];
99          char    **sa;
100          int     i;
101   #define lddx (vldx+1)
102  
103 <        setcolor(cval, 0.0, 0.0, 0.0);
103 >        scolorblack(scval);
104          
105          ldot = DOT(np->pnorm, ldir);
106  
# Line 114 | Line 116 | dirbrdf(               /* compute source contribution */
116                   *  color.  The diffuse reflected component will always be
117                   *  modified by the color of the material.
118                   */
119 <                copycolor(ctmp, np->rdiff);
119 >                copyscolor(sctmp, np->rdiff);
120                  dtmp = ldot * omega / PI;
121 <                scalecolor(ctmp, dtmp);
122 <                addcolor(cval, ctmp);
121 >                scalescolor(sctmp, dtmp);
122 >                saddscolor(scval, sctmp);
123          } else {
124                  /*
125                   *  Diffuse transmitted component.
126                   */
127 <                copycolor(ctmp, np->tdiff);
127 >                copyscolor(sctmp, np->tdiff);
128                  dtmp = -ldot * omega / PI;
129 <                scalecolor(ctmp, dtmp);
130 <                addcolor(cval, ctmp);
129 >                scalescolor(sctmp, dtmp);
130 >                saddscolor(scval, sctmp);
131          }
132 <        if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY)
132 >        if ((ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) ||
133 >                        ambRayInPmap(np->pr))
134                  return;         /* diffuse only */
135                                          /* set up function */
136          setbrdfunc(np);
# Line 140 | Line 143 | dirbrdf(               /* compute source contribution */
143          lddx[3] = omega;
144                                          /* compute BRTDF */
145          if (np->mp->otype == MAT_BRTDF) {
146 <                if (sa[6][0] == '0')            /* special case */
146 >                if (sa[6][0] == '0' && !sa[6][1])       /* special case */
147                          colval(ctmp,RED) = 0.0;
148                  else
149                          colval(ctmp,RED) = funvalue(sa[6], 4, lddx);
150 <                if (sa[7][0] == '0')
150 >                if (sa[7][0] == '0' && !sa[7][1])
151                          colval(ctmp,GRN) = 0.0;
152                  else if (!strcmp(sa[7],sa[6]))
153                          colval(ctmp,GRN) = colval(ctmp,RED);
154                  else
155                          colval(ctmp,GRN) = funvalue(sa[7], 4, lddx);
156 <                if (!strcmp(sa[8],sa[6]))
156 >                if (sa[8][0] == '0' && !sa[8][1])
157 >                        colval(ctmp,BLU) = 0.0;
158 >                else if (!strcmp(sa[8],sa[6]))
159                          colval(ctmp,BLU) = colval(ctmp,RED);
160                  else if (!strcmp(sa[8],sa[7]))
161                          colval(ctmp,BLU) = colval(ctmp,GRN);
# Line 173 | Line 178 | dirbrdf(               /* compute source contribution */
178          }
179          if (dtmp <= FTINY)
180                  return;
181 +        setscolor(sctmp, colval(ctmp,RED), colval(ctmp,GRN), colval(ctmp,BLU));
182          if (ldot > 0.0) {
183                  /*
184                   *  Compute reflected non-diffuse component.
185                   */
186                  if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA))
187 <                        multcolor(ctmp, np->mcolor);
187 >                        smultscolor(sctmp, np->mcolor);
188                  dtmp = ldot * omega * np->rspec;
189 <                scalecolor(ctmp, dtmp);
190 <                addcolor(cval, ctmp);
189 >                scalescolor(sctmp, dtmp);
190 >                saddscolor(scval, sctmp);
191          } else {
192                  /*
193                   *  Compute transmitted non-diffuse component.
194                   */
195                  if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA))
196 <                        multcolor(ctmp, np->mcolor);
196 >                        smultscolor(sctmp, np->mcolor);
197                  dtmp = -ldot * omega * np->tspec;
198 <                scalecolor(ctmp, dtmp);
199 <                addcolor(cval, ctmp);
198 >                scalescolor(sctmp, dtmp);
199 >                saddscolor(scval, sctmp);
200          }
201   #undef lddx
202   }
# Line 202 | Line 208 | m_brdf(                        /* color a ray that hit a BRDTfunc material
208          RAY  *r
209   )
210   {
205        int  hitfront = 1;
211          BRDFDAT  nd;
212          RAY  sr;
208        double  mirtest=0, mirdist=0;
209        double  transtest, transdist;
213          int  hasrefl, hastrans;
214          int  hastexture;
215 <        COLOR  ctmp;
215 >        SCOLOR  sctmp;
216          FVECT  vtmp;
217          double  d;
218          MFUNC  *mf;
# Line 224 | Line 227 | m_brdf(                        /* color a ray that hit a BRDTfunc material
227          nd.trans = 0.5;
228                                                  /* diffuse reflectance */
229          if (r->rod > 0.0)
230 <                setcolor(nd.rdiff, m->oargs.farg[0],
230 >                setscolor(nd.rdiff, m->oargs.farg[0],
231                                  m->oargs.farg[1],
232                                  m->oargs.farg[2]);
233          else
234 <                setcolor(nd.rdiff, m->oargs.farg[3],
234 >                setscolor(nd.rdiff, m->oargs.farg[3],
235                                  m->oargs.farg[4],
236                                  m->oargs.farg[5]);
237                                                  /* diffuse transmittance */
238 <        setcolor(nd.tdiff, m->oargs.farg[6],
238 >        setscolor(nd.tdiff, m->oargs.farg[6],
239                          m->oargs.farg[7],
240                          m->oargs.farg[8]);
241                                                  /* get modifiers */
242          raytexture(r, m->omod);
243 <        hastexture = DOT(r->pert,r->pert) > FTINY*FTINY;
243 >        hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY);
244          if (hastexture) {                       /* perturb normal */
245                  nd.pdot = raynormal(nd.pnorm, r);
246          } else {
# Line 250 | Line 253 | m_brdf(                        /* color a ray that hit a BRDTfunc material
253                          nd.pnorm[i] = -nd.pnorm[i];
254                          r->pert[i] = -r->pert[i];
255                  }
253                hitfront = 0;
256          }
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;
257 >        copyscolor(nd.mcolor, r->pcol);         /* get pattern color */
258 >        smultscolor(nd.rdiff, nd.mcolor);       /* modify diffuse values */
259 >        smultscolor(nd.tdiff, nd.mcolor);
260 >        hasrefl = (sintens(nd.rdiff) > FTINY);
261 >        hastrans = (sintens(nd.tdiff) > FTINY);
262                                                  /* load cal file */
263          nd.dp = NULL;
264 <        mf = getfunc(m, 9, 0x3f, 0);
264 >        mf = getfunc(m, 9, 0x3F, 0);
265                                                  /* compute transmitted ray */
266          setbrdfunc(&nd);
267          errno = 0;
268 <        setcolor(ctmp, evalue(mf->ep[3]),
268 >        setscolor(sctmp, evalue(mf->ep[3]),
269                          evalue(mf->ep[4]),
270                          evalue(mf->ep[5]));
271          if ((errno == EDOM) | (errno == ERANGE))
272                  objerror(m, WARNING, "compute error");
273 <        else if (rayorigin(&sr, TRANS, r, ctmp) == 0) {
274 <                if (!(r->crtype & SHADOW) && hastexture) {
273 >        else if (rayorigin(&sr, TRANS, r, sctmp) == 0) {
274 >                if (hastexture && !(r->crtype & (SHADOW|AMBIENT))) {
275                                                  /* perturb direction */
276 <                        VSUM(sr.rdir, r->rdir, r->pert, -.75);
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);
# Line 280 | Line 282 | m_brdf(                        /* color a ray that hit a BRDTfunc material
282                          VCOPY(sr.rdir, r->rdir);
283                  }
284                  rayvalue(&sr);
285 <                multcolor(sr.rcol, sr.rcoef);
286 <                addcolor(r->rcol, sr.rcol);
287 <                if (!hastexture) {
288 <                        transtest = 2.0*bright(sr.rcol);
289 <                        transdist = r->rot + sr.rt;
288 <                }
285 >                smultscolor(sr.rcol, sr.rcoef);
286 >                saddscolor(r->rcol, sr.rcol);
287 >                if ((!hastexture || r->crtype & (SHADOW|AMBIENT)) &&
288 >                                nd.tspec > pbright(nd.tdiff) + pbright(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]),
297 >        setscolor(sctmp, evalue(mf->ep[0]),
298                          evalue(mf->ep[1]),
299                          evalue(mf->ep[2]));
300          if ((errno == EDOM) | (errno == ERANGE))
301                  objerror(m, WARNING, "compute error");
302 <        else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) {
302 >        else if (rayorigin(&sr, REFLECTED, r, sctmp) == 0) {
303                  VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot);
304                  checknorm(sr.rdir);
305                  rayvalue(&sr);
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 <                }
306 >                smultscolor(sr.rcol, sr.rcoef);
307 >                copyscolor(r->mcol, sr.rcol);
308 >                saddscolor(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 <                copycolor(ctmp, nd.rdiff);
316 <                multambient(ctmp, r, nd.pnorm);
317 <                addcolor(r->rcol, ctmp);        /* add to returned color */
318 <                if (!hitfront)
319 <                        flipsurface(r);
316 >                copyscolor(sctmp, nd.rdiff);
317 >                multambient(sctmp, r, nd.pnorm);
318 >                saddscolor(r->rcol, sctmp);     /* add to returned color */
319          }
320          if (hastrans) {                         /* from other side */
322                if (hitfront)
323                        flipsurface(r);
321                  vtmp[0] = -nd.pnorm[0];
322                  vtmp[1] = -nd.pnorm[1];
323                  vtmp[2] = -nd.pnorm[2];
324 <                copycolor(ctmp, nd.tdiff);
325 <                multambient(ctmp, r, vtmp);
326 <                addcolor(r->rcol, ctmp);
330 <                if (hitfront)
331 <                        flipsurface(r);
324 >                copyscolor(sctmp, nd.tdiff);
325 >                multambient(sctmp, r, vtmp);
326 >                saddscolor(r->rcol, sctmp);
327          }
328          if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
329                  direct(r, dirbrdf, &nd);        /* add direct component */
330  
336        d = bright(r->rcol);                    /* set effective distance */
337        if (transtest > d)
338                r->rt = transdist;
339        else if (mirtest > d)
340                r->rt = mirdist;
341
331          return(1);
332   }
333  
# Line 351 | Line 340 | m_brdf2(                       /* color a ray that hit a BRDF material */
340   )
341   {
342          BRDFDAT  nd;
343 <        COLOR  ctmp;
343 >        SCOLOR  sctmp;
344          FVECT  vtmp;
345          double  dtmp;
346                                                  /* always a shadow */
# Line 363 | Line 352 | m_brdf2(                       /* color a ray that hit a BRDF material */
352                  objerror(m, USER, "bad # arguments");
353                                                  /* check for back side */
354          if (r->rod < 0.0) {
355 <                if (!backvis && m->otype != MAT_TFUNC
367 <                                && m->otype != MAT_TDATA) {
355 >                if (!backvis) {
356                          raytrans(r);
357                          return(1);
358                  }
# Line 376 | Line 364 | m_brdf2(                       /* color a ray that hit a BRDF material */
364          nd.mp = m;
365          nd.pr = r;
366                                                  /* get material color */
367 <        setcolor(nd.mcolor, m->oargs.farg[0],
367 >        setscolor(nd.mcolor, m->oargs.farg[0],
368                          m->oargs.farg[1],
369                          m->oargs.farg[2]);
370                                                  /* get specular component */
# Line 386 | Line 374 | m_brdf2(                       /* color a ray that hit a BRDF material */
374                  nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
375                  nd.tspec = nd.trans * m->oargs.farg[5];
376                  dtmp = nd.trans - nd.tspec;
377 <                setcolor(nd.tdiff, dtmp, dtmp, dtmp);
377 >                setscolor(nd.tdiff, dtmp, dtmp, dtmp);
378          } else {
379                  nd.tspec = nd.trans = 0.0;
380 <                setcolor(nd.tdiff, 0.0, 0.0, 0.0);
380 >                scolorblack(nd.tdiff);
381          }
382                                                  /* compute reflectance */
383          dtmp = 1.0 - nd.trans - nd.rspec;
384 <        setcolor(nd.rdiff, dtmp, dtmp, dtmp);
384 >        setscolor(nd.rdiff, dtmp, dtmp, dtmp);
385          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
386 <        multcolor(nd.mcolor, r->pcol);          /* modify material color */
387 <        multcolor(nd.rdiff, nd.mcolor);
388 <        multcolor(nd.tdiff, nd.mcolor);
386 >        smultscolor(nd.mcolor, r->pcol);        /* modify material color */
387 >        smultscolor(nd.rdiff, nd.mcolor);
388 >        smultscolor(nd.tdiff, nd.mcolor);
389                                                  /* load auxiliary files */
390          if (hasdata(m->otype)) {
391                  nd.dp = getdata(m->oargs.sarg[1]);
# Line 408 | Line 396 | m_brdf2(                       /* color a ray that hit a BRDF material */
396          }
397                                                  /* compute ambient */
398          if (nd.trans < 1.0-FTINY) {
399 <                copycolor(ctmp, nd.mcolor);     /* modified by material color */
400 <                scalecolor(ctmp, 1.0-nd.trans);
401 <                multambient(ctmp, r, nd.pnorm);
402 <                addcolor(r->rcol, ctmp);        /* add to returned color */
399 >                copyscolor(sctmp, nd.mcolor);   /* modified by material color */
400 >                scalescolor(sctmp, 1.0-nd.trans);
401 >                multambient(sctmp, r, nd.pnorm);
402 >                saddscolor(r->rcol, sctmp);     /* add to returned color */
403          }
404 <        if (nd.trans > FTINY) {         /* from other side */
417 <                flipsurface(r);
404 >        if (nd.trans > FTINY) {                 /* from other side */
405                  vtmp[0] = -nd.pnorm[0];
406                  vtmp[1] = -nd.pnorm[1];
407                  vtmp[2] = -nd.pnorm[2];
408 <                copycolor(ctmp, nd.mcolor);
409 <                scalecolor(ctmp, nd.trans);
410 <                multambient(ctmp, r, vtmp);
411 <                addcolor(r->rcol, ctmp);
425 <                flipsurface(r);
408 >                copyscolor(sctmp, nd.mcolor);
409 >                scalescolor(sctmp, nd.trans);
410 >                multambient(sctmp, r, vtmp);
411 >                saddscolor(r->rcol, sctmp);
412          }
413                                                  /* add direct component */
414          direct(r, dirbrdf, &nd);
# Line 437 | Line 423 | setbrdfunc(                    /* set up brdf function and variables */
423   )
424   {
425          FVECT  vec;
426 +        COLOR  ctmp;
427  
428          if (setfunc(np->mp, np->pr) == 0)
429                  return(0);      /* it's OK, setfunc says we're done */
430                                  /* else (re)assign special variables */
431          multv3(vec, np->pnorm, funcxf.xfm);
432 <        varset("NxP", '=', vec[0]/funcxf.sca);
433 <        varset("NyP", '=', vec[1]/funcxf.sca);
434 <        varset("NzP", '=', vec[2]/funcxf.sca);
435 <        varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
432 >        varset("NxP`", '=', vec[0]/funcxf.sca);
433 >        varset("NyP`", '=', vec[1]/funcxf.sca);
434 >        varset("NzP`", '=', vec[2]/funcxf.sca);
435 >        varset("RdotP`", '=', np->pdot <= -1.0 ? -1.0 :
436                          np->pdot >= 1.0 ? 1.0 : np->pdot);
437 <        varset("CrP", '=', colval(np->mcolor,RED));
438 <        varset("CgP", '=', colval(np->mcolor,GRN));
439 <        varset("CbP", '=', colval(np->mcolor,BLU));
437 >        scolor_color(ctmp, np->mcolor);         /* should use scolor_rgb()? */
438 >        varset("CrP", '=', colval(ctmp,RED));
439 >        varset("CgP", '=', colval(ctmp,GRN));
440 >        varset("CbP", '=', colval(ctmp,BLU));
441          return(1);
442   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines