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.26 by greg, Sun Sep 26 15:51:15 2010 UTC vs.
Revision 2.40 by greg, Wed Nov 15 18:02:52 2023 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 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 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 77 | Line 78 | typedef struct {
78   }  BRDFDAT;             /* BRDF material data */
79  
80  
81 < static srcdirf_t dirbrdf;
81 < static int setbrdfunc(BRDFDAT  *np);
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 */
86 >        SCOLOR  scval,                  /* 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;
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];
99 <        register char   **sa;
100 <        register int    i;
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 115 | 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 141 | 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 174 | 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   }
203  
204  
205 < extern int
205 > int
206   m_brdf(                 /* color a ray that hit a BRDTfunc material */
207 <        register OBJREC  *m,
208 <        register RAY  *r
207 >        OBJREC  *m,
208 >        RAY  *r
209   )
210   {
211          int  hitfront = 1;
212          BRDFDAT  nd;
213          RAY  sr;
209        double  mirtest=0, mirdist=0;
210        double  transtest, transdist;
214          int  hasrefl, hastrans;
215          int  hastexture;
216 <        COLOR  ctmp;
216 >        SCOLOR  sctmp;
217          FVECT  vtmp;
218          double  d;
219 <        register MFUNC  *mf;
220 <        register int  i;
219 >        MFUNC  *mf;
220 >        int  i;
221                                                  /* check arguments */
222          if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
223                  objerror(m, USER, "bad # arguments");
# Line 225 | Line 228 | m_brdf(                        /* color a ray that hit a BRDTfunc material
228          nd.trans = 0.5;
229                                                  /* diffuse reflectance */
230          if (r->rod > 0.0)
231 <                setcolor(nd.rdiff, m->oargs.farg[0],
231 >                setscolor(nd.rdiff, m->oargs.farg[0],
232                                  m->oargs.farg[1],
233                                  m->oargs.farg[2]);
234          else
235 <                setcolor(nd.rdiff, m->oargs.farg[3],
235 >                setscolor(nd.rdiff, m->oargs.farg[3],
236                                  m->oargs.farg[4],
237                                  m->oargs.farg[5]);
238                                                  /* diffuse transmittance */
239 <        setcolor(nd.tdiff, m->oargs.farg[6],
239 >        setscolor(nd.tdiff, m->oargs.farg[6],
240                          m->oargs.farg[7],
241                          m->oargs.farg[8]);
242                                                  /* get modifiers */
243          raytexture(r, m->omod);
244 <        hastexture = DOT(r->pert,r->pert) > FTINY*FTINY;
244 >        hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY);
245          if (hastexture) {                       /* perturb normal */
246                  nd.pdot = raynormal(nd.pnorm, r);
247          } else {
# Line 253 | Line 256 | m_brdf(                        /* color a ray that hit a BRDTfunc material
256                  }
257                  hitfront = 0;
258          }
259 <        copycolor(nd.mcolor, r->pcol);          /* get pattern color */
260 <        multcolor(nd.rdiff, nd.mcolor);         /* modify diffuse values */
261 <        multcolor(nd.tdiff, nd.mcolor);
262 <        hasrefl = bright(nd.rdiff) > FTINY;
263 <        hastrans = bright(nd.tdiff) > FTINY;
259 >        copyscolor(nd.mcolor, r->pcol);         /* get pattern color */
260 >        smultscolor(nd.rdiff, nd.mcolor);       /* modify diffuse values */
261 >        smultscolor(nd.tdiff, nd.mcolor);
262 >        hasrefl = (sintens(nd.rdiff) > FTINY);
263 >        hastrans = (sintens(nd.tdiff) > FTINY);
264                                                  /* load cal file */
265          nd.dp = NULL;
266          mf = getfunc(m, 9, 0x3f, 0);
267                                                  /* compute transmitted ray */
268          setbrdfunc(&nd);
269          errno = 0;
270 <        setcolor(ctmp, evalue(mf->ep[3]),
270 >        setscolor(sctmp, evalue(mf->ep[3]),
271                          evalue(mf->ep[4]),
272                          evalue(mf->ep[5]));
273          if ((errno == EDOM) | (errno == ERANGE))
274                  objerror(m, WARNING, "compute error");
275 <        else if (rayorigin(&sr, TRANS, r, ctmp) == 0) {
276 <                if (!(r->crtype & SHADOW) && hastexture) {
277 <                        for (i = 0; i < 3; i++) /* perturb direction */
278 <                                sr.rdir[i] = r->rdir[i] - .75*r->pert[i];
275 >        else if (rayorigin(&sr, TRANS, r, sctmp) == 0) {
276 >                if (hastexture && !(r->crtype & (SHADOW|AMBIENT))) {
277 >                                                /* perturb direction */
278 >                        VSUB(sr.rdir, r->rdir, r->pert);
279                          if (normalize(sr.rdir) == 0.0) {
280                                  objerror(m, WARNING, "illegal perturbation");
281                                  VCOPY(sr.rdir, r->rdir);
# Line 281 | Line 284 | m_brdf(                        /* color a ray that hit a BRDTfunc material
284                          VCOPY(sr.rdir, r->rdir);
285                  }
286                  rayvalue(&sr);
287 <                multcolor(sr.rcol, sr.rcoef);
288 <                addcolor(r->rcol, sr.rcol);
289 <                if (!hastexture) {
290 <                        transtest = 2.0*bright(sr.rcol);
291 <                        transdist = r->rot + sr.rt;
289 <                }
287 >                smultscolor(sr.rcol, sr.rcoef);
288 >                saddscolor(r->rcol, sr.rcol);
289 >                if ((!hastexture || r->crtype & (SHADOW|AMBIENT)) &&
290 >                                nd.tspec > pbright(nd.tdiff) + pbright(nd.rdiff))
291 >                        r->rxt = r->rot + raydistance(&sr);
292          }
293          if (r->crtype & SHADOW)                 /* the rest is shadow */
294                  return(1);
295 +
296                                                  /* compute reflected ray */
297          setbrdfunc(&nd);
298          errno = 0;
299 <        setcolor(ctmp, evalue(mf->ep[0]),
299 >        setscolor(sctmp, evalue(mf->ep[0]),
300                          evalue(mf->ep[1]),
301                          evalue(mf->ep[2]));
302          if ((errno == EDOM) | (errno == ERANGE))
303                  objerror(m, WARNING, "compute error");
304 <        else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) {
305 <                for (i = 0; i < 3; i++)
303 <                        sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
304 >        else if (rayorigin(&sr, REFLECTED, r, sctmp) == 0) {
305 >                VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot);
306                  checknorm(sr.rdir);
307                  rayvalue(&sr);
308 <                multcolor(sr.rcol, sr.rcoef);
309 <                addcolor(r->rcol, sr.rcol);
310 <                if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) {
311 <                        mirtest = 2.0*bright(sr.rcol);
312 <                        mirdist = r->rot + sr.rt;
313 <                }
308 >                smultscolor(sr.rcol, sr.rcoef);
309 >                copyscolor(r->mcol, sr.rcol);
310 >                saddscolor(r->rcol, sr.rcol);
311 >                r->rmt = r->rot;
312 >                if (r->ro != NULL && isflat(r->ro->otype) &&
313 >                                !hastexture | (r->crtype & AMBIENT))
314 >                        r->rmt += raydistance(&sr);
315          }
316                                                  /* compute ambient */
317          if (hasrefl) {
318                  if (!hitfront)
319                          flipsurface(r);
320 <                copycolor(ctmp, nd.rdiff);
321 <                multambient(ctmp, r, nd.pnorm);
322 <                addcolor(r->rcol, ctmp);        /* add to returned color */
320 >                copyscolor(sctmp, nd.rdiff);
321 >                multambient(sctmp, r, nd.pnorm);
322 >                saddscolor(r->rcol, sctmp);     /* add to returned color */
323                  if (!hitfront)
324                          flipsurface(r);
325          }
# Line 326 | Line 329 | m_brdf(                        /* color a ray that hit a BRDTfunc material
329                  vtmp[0] = -nd.pnorm[0];
330                  vtmp[1] = -nd.pnorm[1];
331                  vtmp[2] = -nd.pnorm[2];
332 <                copycolor(ctmp, nd.tdiff);
333 <                multambient(ctmp, r, vtmp);
334 <                addcolor(r->rcol, ctmp);
332 >                copyscolor(sctmp, nd.tdiff);
333 >                multambient(sctmp, r, vtmp);
334 >                saddscolor(r->rcol, sctmp);
335                  if (hitfront)
336                          flipsurface(r);
337          }
338          if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
339                  direct(r, dirbrdf, &nd);        /* add direct component */
340  
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
341          return(1);
342   }
343  
344  
345  
346 < extern int
346 > int
347   m_brdf2(                        /* color a ray that hit a BRDF material */
348 <        register OBJREC  *m,
349 <        register RAY  *r
348 >        OBJREC  *m,
349 >        RAY  *r
350   )
351   {
352          BRDFDAT  nd;
353 <        COLOR  ctmp;
353 >        SCOLOR  sctmp;
354          FVECT  vtmp;
355          double  dtmp;
356                                                  /* always a shadow */
# Line 365 | Line 362 | m_brdf2(                       /* color a ray that hit a BRDF material */
362                  objerror(m, USER, "bad # arguments");
363                                                  /* check for back side */
364          if (r->rod < 0.0) {
365 <                if (!backvis && m->otype != MAT_TFUNC
369 <                                && m->otype != MAT_TDATA) {
365 >                if (!backvis) {
366                          raytrans(r);
367                          return(1);
368                  }
# Line 378 | Line 374 | m_brdf2(                       /* color a ray that hit a BRDF material */
374          nd.mp = m;
375          nd.pr = r;
376                                                  /* get material color */
377 <        setcolor(nd.mcolor, m->oargs.farg[0],
377 >        setscolor(nd.mcolor, m->oargs.farg[0],
378                          m->oargs.farg[1],
379                          m->oargs.farg[2]);
380                                                  /* get specular component */
# Line 388 | Line 384 | m_brdf2(                       /* color a ray that hit a BRDF material */
384                  nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
385                  nd.tspec = nd.trans * m->oargs.farg[5];
386                  dtmp = nd.trans - nd.tspec;
387 <                setcolor(nd.tdiff, dtmp, dtmp, dtmp);
387 >                setscolor(nd.tdiff, dtmp, dtmp, dtmp);
388          } else {
389                  nd.tspec = nd.trans = 0.0;
390 <                setcolor(nd.tdiff, 0.0, 0.0, 0.0);
390 >                scolorblack(nd.tdiff);
391          }
392                                                  /* compute reflectance */
393          dtmp = 1.0 - nd.trans - nd.rspec;
394 <        setcolor(nd.rdiff, dtmp, dtmp, dtmp);
394 >        setscolor(nd.rdiff, dtmp, dtmp, dtmp);
395          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
396 <        multcolor(nd.mcolor, r->pcol);          /* modify material color */
397 <        multcolor(nd.rdiff, nd.mcolor);
398 <        multcolor(nd.tdiff, nd.mcolor);
396 >        smultscolor(nd.mcolor, r->pcol);        /* modify material color */
397 >        smultscolor(nd.rdiff, nd.mcolor);
398 >        smultscolor(nd.tdiff, nd.mcolor);
399                                                  /* load auxiliary files */
400          if (hasdata(m->otype)) {
401                  nd.dp = getdata(m->oargs.sarg[1]);
# Line 410 | Line 406 | m_brdf2(                       /* color a ray that hit a BRDF material */
406          }
407                                                  /* compute ambient */
408          if (nd.trans < 1.0-FTINY) {
409 <                copycolor(ctmp, nd.mcolor);     /* modified by material color */
410 <                scalecolor(ctmp, 1.0-nd.trans);
411 <                multambient(ctmp, r, nd.pnorm);
412 <                addcolor(r->rcol, ctmp);        /* add to returned color */
409 >                copyscolor(sctmp, nd.mcolor);   /* modified by material color */
410 >                scalescolor(sctmp, 1.0-nd.trans);
411 >                multambient(sctmp, r, nd.pnorm);
412 >                saddscolor(r->rcol, sctmp);     /* add to returned color */
413          }
414 <        if (nd.trans > FTINY) {         /* from other side */
414 >        if (nd.trans > FTINY) {                 /* from other side */
415                  flipsurface(r);
416                  vtmp[0] = -nd.pnorm[0];
417                  vtmp[1] = -nd.pnorm[1];
418                  vtmp[2] = -nd.pnorm[2];
419 <                copycolor(ctmp, nd.mcolor);
420 <                scalecolor(ctmp, nd.trans);
421 <                multambient(ctmp, r, vtmp);
422 <                addcolor(r->rcol, ctmp);
419 >                copyscolor(sctmp, nd.mcolor);
420 >                scalescolor(sctmp, nd.trans);
421 >                multambient(sctmp, r, vtmp);
422 >                saddscolor(r->rcol, sctmp);
423                  flipsurface(r);
424          }
425                                                  /* add direct component */
# Line 435 | Line 431 | m_brdf2(                       /* color a ray that hit a BRDF material */
431  
432   static int
433   setbrdfunc(                     /* set up brdf function and variables */
434 <        register BRDFDAT  *np
434 >        BRDFDAT  *np
435   )
436   {
437          FVECT  vec;
438 +        COLOR  ctmp;
439  
440          if (setfunc(np->mp, np->pr) == 0)
441                  return(0);      /* it's OK, setfunc says we're done */
442                                  /* else (re)assign special variables */
443          multv3(vec, np->pnorm, funcxf.xfm);
444 <        varset("NxP", '=', vec[0]/funcxf.sca);
445 <        varset("NyP", '=', vec[1]/funcxf.sca);
446 <        varset("NzP", '=', vec[2]/funcxf.sca);
447 <        varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
444 >        varset("NxP`", '=', vec[0]/funcxf.sca);
445 >        varset("NyP`", '=', vec[1]/funcxf.sca);
446 >        varset("NzP`", '=', vec[2]/funcxf.sca);
447 >        varset("RdotP`", '=', np->pdot <= -1.0 ? -1.0 :
448                          np->pdot >= 1.0 ? 1.0 : np->pdot);
449 <        varset("CrP", '=', colval(np->mcolor,RED));
450 <        varset("CgP", '=', colval(np->mcolor,GRN));
451 <        varset("CbP", '=', colval(np->mcolor,BLU));
449 >        scolor_color(ctmp, np->mcolor);         /* should use scolor_rgb()? */
450 >        varset("CrP", '=', colval(ctmp,RED));
451 >        varset("CgP", '=', colval(ctmp,GRN));
452 >        varset("CbP", '=', colval(ctmp,BLU));
453          return(1);
454   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines