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.5 by greg, Thu May 20 10:08:24 1993 UTC vs.
Revision 2.6 by greg, Thu May 27 15:28:01 1993 UTC

# Line 51 | Line 51 | static char SCCSid[] = "$SunId$ LBL";
51   *              rbrtd   gbrtd   bbrtd
52   *              funcfile        transform
53   *      0
54 < *      6+      red     grn     blu     rspec   trans   tspec   A7 ..
54 > *      9+      rdf     gdf     bdf
55 > *              rdb     gdb     bdb
56 > *              rdt     gdt     bdt     A10 ..
57   *
58   *      In addition to the normal variables available to functions,
59   *  we define the following:
60   *              NxP, NyP, NzP -         perturbed surface normal
61   *              RdotP -                 perturbed ray dot product
62 < *              CrP, CgP, CbP -         perturbed material color
62 > *              CrP, CgP, CbP -         perturbed material color (or pattern)
63   */
64  
65   typedef struct {
66          OBJREC  *mp;            /* material pointer */
67          RAY  *pr;               /* intersected ray */
68          DATARRAY  *dp;          /* data array for PDATA, MDATA or TDATA */
69 <        COLOR  mcolor;          /* color of this material */
70 <        double  rspec;          /* specular reflection */
71 <        double  rdiff;          /* diffuse reflection */
72 <        double  trans;          /* transmissivity */
73 <        double  tspec;          /* specular transmission */
74 <        double  tdiff;          /* diffuse transmission */
69 >        COLOR  mcolor;          /* material (or pattern) color */
70 >        COLOR  rdiff;           /* diffuse reflection */
71 >        COLOR  tdiff;           /* diffuse transmission */
72 >        double  rspec;          /* specular reflectance (1 for BRDTF) */
73 >        double  trans;          /* transmissivity (.5 for BRDTF) */
74 >        double  tspec;          /* specular transmittance (1 for BRDTF) */
75          FVECT  pnorm;           /* perturbed surface normal */
76          double  pdot;           /* perturbed dot product */
77   }  BRDFDAT;             /* BRDF material data */
# Line 95 | Line 97 | double  omega;                 /* light source size */
97  
98          if (ldot <= FTINY && ldot >= -FTINY)
99                  return;         /* too close to grazing */
100 +
101          if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
102                  return;         /* wrong side */
103  
104 <        if (ldot > 0.0 && np->rdiff > FTINY) {
104 >        if (ldot > 0.0) {
105                  /*
106                   *  Compute and add diffuse reflected component to returned
107                   *  color.  The diffuse reflected component will always be
108                   *  modified by the color of the material.
109                   */
110 <                copycolor(ctmp, np->mcolor);
111 <                dtmp = ldot * omega * np->rdiff / PI;
110 >                copycolor(ctmp, np->rdiff);
111 >                dtmp = ldot * omega / PI;
112                  scalecolor(ctmp, dtmp);
113                  addcolor(cval, ctmp);
114 <        }
112 <        if (ldot < 0.0 && np->tdiff > FTINY) {
114 >        } else {
115                  /*
116                   *  Diffuse transmitted component.
117                   */
118 <                copycolor(ctmp, np->mcolor);
119 <                dtmp = -ldot * omega * np->tdiff / PI;
118 >                copycolor(ctmp, np->tdiff);
119 >                dtmp = -ldot * omega / PI;
120                  scalecolor(ctmp, dtmp);
121                  addcolor(cval, ctmp);
122          }
# Line 130 | Line 132 | double  omega;                 /* light source size */
132                  lddx[i] = ldx[i]/funcxf.sca;
133                                          /* compute BRTDF */
134          if (np->mp->otype == MAT_BRTDF) {
135 <                colval(ctmp,RED) = funvalue(sa[6], 3, lddx);
135 >                if (sa[6][0] == '0')            /* special case */
136 >                        colval(ctmp,RED) = 0.0;
137 >                else
138 >                        colval(ctmp,RED) = funvalue(sa[6], 3, lddx);
139                  if (!strcmp(sa[7],sa[6]))
140                          colval(ctmp,GRN) = colval(ctmp,RED);
141                  else
# Line 162 | Line 167 | double  omega;                 /* light source size */
167                  /*
168                   *  Compute reflected non-diffuse component.
169                   */
170 <                if (np->mp->otype == MAT_MFUNC || np->mp->otype == MAT_MDATA)
170 >                if (np->mp->otype == MAT_MFUNC | np->mp->otype == MAT_MDATA)
171                          multcolor(ctmp, np->mcolor);
172                  dtmp = ldot * omega * np->rspec;
173                  scalecolor(ctmp, dtmp);
# Line 171 | Line 176 | double  omega;                 /* light source size */
176                  /*
177                   *  Compute transmitted non-diffuse component.
178                   */
179 <                if (np->mp->otype == MAT_TFUNC || np->mp->otype == MAT_TDATA)
179 >                if (np->mp->otype == MAT_TFUNC | np->mp->otype == MAT_TDATA)
180                          multcolor(ctmp, np->mcolor);
181                  dtmp = -ldot * omega * np->tspec;
182                  scalecolor(ctmp, dtmp);
# Line 180 | Line 185 | double  omega;                 /* light source size */
185   }
186  
187  
188 < m_brdf(m, r)                    /* color a ray which hit a BRDF material */
188 > m_brdf(m, r)                    /* color a ray which hit a BRDTF material */
189   register OBJREC  *m;
190   register RAY  *r;
191   {
187        int  minsa, minfa;
192          BRDFDAT  nd;
193 +        RAY  sr;
194          double  transtest, transdist;
195 +        int  hasrefl, hastrans;
196          COLOR  ctmp;
197 <        double  dtmp, tspect, rspecr;
198 <        MFUNC  *mf;
197 >        double  dtmp;
198 >        register MFUNC  *mf;
199          register int  i;
200                                                  /* check arguments */
201 <        switch (m->otype) {
202 <        case MAT_PFUNC: case MAT_MFUNC:
203 <                minsa = 2; minfa = 4; break;
204 <        case MAT_PDATA: case MAT_MDATA:
205 <                minsa = 4; minfa = 4; break;
206 <        case MAT_TFUNC:
207 <                minsa = 2; minfa = 6; break;
208 <        case MAT_TDATA:
209 <                minsa = 4; minfa = 6; break;
210 <        case MAT_BRTDF:
211 <                minsa = 10; minfa = 6; break;
201 >        if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9)
202 >                objerror(m, USER, "bad # arguments");
203 >        nd.mp = m;
204 >        nd.pr = r;
205 >                                                /* dummy values */
206 >        nd.rspec = nd.tspec = 1.0;
207 >        nd.trans = 0.5;
208 >                                                /* diffuse reflectance */
209 >        if (r->rod > 0.0)
210 >                setcolor(nd.rdiff, m->oargs.farg[0],
211 >                                m->oargs.farg[1],
212 >                                m->oargs.farg[2]);
213 >        else
214 >                setcolor(nd.rdiff, m->oargs.farg[3],
215 >                                m->oargs.farg[4],
216 >                                m->oargs.farg[5]);
217 >                                                /* diffuse transmittance */
218 >        setcolor(nd.tdiff, m->oargs.farg[6],
219 >                        m->oargs.farg[7],
220 >                        m->oargs.farg[8]);
221 >                                        /* get modifiers */
222 >        raytexture(r, m->omod);
223 >        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
224 >        if (r->rod < 0.0) {                     /* orient perturbed values */
225 >                nd.pdot = -nd.pdot;
226 >                for (i = 0; i < 3; i++) {
227 >                        nd.pnorm[i] = -nd.pnorm[i];
228 >                        r->pert[i] = -r->pert[i];
229 >                }
230          }
231 <        if (m->oargs.nsargs < minsa || m->oargs.nfargs < minfa)
231 >        copycolor(nd.mcolor, r->pcol);          /* get pattern color */
232 >        multcolor(nd.rdiff, nd.mcolor);         /* modify diffuse values */
233 >        multcolor(nd.tdiff, nd.mcolor);
234 >        hasrefl = bright(nd.rdiff) > FTINY;
235 >        hastrans = bright(nd.tdiff) > FTINY;
236 >                                                /* load cal file */
237 >        nd.dp = NULL;
238 >        mf = getfunc(m, 9, 0x3f, 0);
239 >                                                /* compute transmitted ray */
240 >        setbrdfunc(&nd);
241 >        transtest = 0;
242 >        errno = 0;
243 >        setcolor(ctmp, evalue(mf->ep[3]),
244 >                        evalue(mf->ep[4]),
245 >                        evalue(mf->ep[5]));
246 >        if (errno)
247 >                objerror(m, WARNING, "compute error");
248 >        else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
249 >                if (!(r->crtype & SHADOW) &&
250 >                                DOT(r->pert,r->pert) > FTINY*FTINY) {
251 >                        for (i = 0; i < 3; i++) /* perturb direction */
252 >                                sr.rdir[i] = r->rdir[i] - .75*r->pert[i];
253 >                        if (normalize(sr.rdir) == 0.0) {
254 >                                objerror(m, WARNING, "illegal perturbation");
255 >                                VCOPY(sr.rdir, r->rdir);
256 >                        }
257 >                } else {
258 >                        VCOPY(sr.rdir, r->rdir);
259 >                        transtest = 2;
260 >                }
261 >                rayvalue(&sr);
262 >                multcolor(sr.rcol, ctmp);
263 >                addcolor(r->rcol, sr.rcol);
264 >                transtest *= bright(sr.rcol);
265 >                transdist = r->rot + sr.rt;
266 >        }
267 >        if (r->crtype & SHADOW)                 /* the rest is shadow */
268 >                return;
269 >                                                /* compute reflected ray */
270 >        setbrdfunc(&nd);
271 >        errno = 0;
272 >        setcolor(ctmp, evalue(mf->ep[0]),
273 >                        evalue(mf->ep[1]),
274 >                        evalue(mf->ep[2]));
275 >        if (errno)
276 >                objerror(m, WARNING, "compute error");
277 >        else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
278 >                for (i = 0; i < 3; i++)
279 >                        sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
280 >                rayvalue(&sr);
281 >                multcolor(sr.rcol, ctmp);
282 >                addcolor(r->rcol, sr.rcol);
283 >        }
284 >                                                /* compute ambient */
285 >        if (hasrefl) {
286 >                if (nd.pdot < 0.0)
287 >                        flipsurface(r);
288 >                ambient(ctmp, r);
289 >                multcolor(ctmp, nd.rdiff);
290 >                addcolor(r->rcol, ctmp);        /* add to returned color */
291 >                if (nd.pdot < 0.0)
292 >                        flipsurface(r);
293 >        }
294 >        if (hastrans) {                         /* from other side */
295 >                if (nd.pdot > 0.0)
296 >                        flipsurface(r);
297 >                ambient(ctmp, r);
298 >                multcolor(ctmp, nd.tdiff);
299 >                addcolor(r->rcol, ctmp);
300 >                if (nd.pdot > 0.0)
301 >                        flipsurface(r);
302 >        }
303 >        if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
304 >                direct(r, dirbrdf, &nd);        /* add direct component */
305 >                                                /* check distance */
306 >        if (transtest > bright(r->rcol))
307 >                r->rt = transdist;
308 > }
309 >
310 >
311 >
312 > m_brdf2(m, r)                   /* color a ray which hit a BRDF material */
313 > register OBJREC  *m;
314 > register RAY  *r;
315 > {
316 >        BRDFDAT  nd;
317 >        COLOR  ctmp;
318 >        double  dtmp;
319 >                                                /* always a shadow */
320 >        if (r->crtype & SHADOW)
321 >                return;
322 >                                                /* check arguments */
323 >        if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs <
324 >                        (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4))
325                  objerror(m, USER, "bad # arguments");
326          nd.mp = m;
327          nd.pr = r;
328 +                                                /* get material color */
329 +        setcolor(nd.mcolor, m->oargs.farg[0],
330 +                        m->oargs.farg[1],
331 +                        m->oargs.farg[2]);
332                                                  /* get specular component */
333          nd.rspec = m->oargs.farg[3];
334 <                                                /* compute transmission */
335 <        if (m->otype == MAT_TFUNC || m->otype == MAT_TDATA
215 <                        || m->otype == MAT_BRTDF) {
334 >                                                /* compute transmittance */
335 >        if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) {
336                  nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
337                  nd.tspec = nd.trans * m->oargs.farg[5];
338 <                nd.tdiff = nd.trans - nd.tspec;
339 <        } else
340 <                nd.tdiff = nd.tspec = nd.trans = 0.0;
341 <                                                /* early shadow check */
342 <        if (r->crtype & SHADOW && (m->otype != MAT_BRTDF || nd.tspec <= FTINY))
343 <                return;
344 <                                                /* diffuse reflection */
345 <        nd.rdiff = 1.0 - nd.trans - nd.rspec;
346 <                                                /* get material color */
227 <        setcolor(nd.mcolor, m->oargs.farg[0],
228 <                           m->oargs.farg[1],
229 <                           m->oargs.farg[2]);
338 >                dtmp = nd.trans - nd.tspec;
339 >                setcolor(nd.tdiff, dtmp, dtmp, dtmp);
340 >        } else {
341 >                nd.tspec = nd.trans = 0.0;
342 >                setcolor(nd.tdiff, 0.0, 0.0, 0.0);
343 >        }
344 >                                                /* compute reflectance */
345 >        dtmp = 1.0 - nd.trans - nd.rspec;
346 >        setcolor(nd.rdiff, dtmp, dtmp, dtmp);
347                                                  /* fix orientation */
348 <        if (m->otype != MAT_BRTDF && r->rod < 0.0)
348 >        if (r->rod < 0.0)
349                  flipsurface(r);
350                                                  /* get modifiers */
351          raytexture(r, m->omod);
352          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
353          multcolor(nd.mcolor, r->pcol);          /* modify material color */
354 <        transtest = 0;
354 >        multcolor(nd.rdiff, nd.mcolor);
355 >        multcolor(nd.tdiff, nd.mcolor);
356                                                  /* load auxiliary files */
357          if (hasdata(m->otype)) {
358                  nd.dp = getdata(m->oargs.sarg[1]);
359 <                i = (1 << nd.dp->nd) - 1;
242 <                mf = getfunc(m, 2, i<<3, 0);
243 <        } else if (m->otype == MAT_BRTDF) {
244 <                nd.dp = NULL;
245 <                mf = getfunc(m, 9, 0x3f, 0);
359 >                getfunc(m, 2, 0, 0);
360          } else {
361                  nd.dp = NULL;
362 <                mf = getfunc(m, 1, 0, 0);
362 >                getfunc(m, 1, 0, 0);
363          }
250                                                /* set special variables */
251        setbrdfunc(&nd);
252                                                /* compute transmitted ray */
253        tspect = 0.;
254        if (m->otype == MAT_BRTDF && nd.tspec > FTINY) {
255                RAY  sr;
256                errno = 0;
257                setcolor(ctmp, evalue(mf->ep[3]),
258                                evalue(mf->ep[4]),
259                                evalue(mf->ep[5]));
260                scalecolor(ctmp, nd.trans);
261                if (errno)
262                        objerror(m, WARNING, "compute error");
263                else if ((tspect = bright(ctmp)) > FTINY &&
264                                rayorigin(&sr, r, TRANS, tspect) == 0) {
265                        if (!(r->crtype & SHADOW) &&
266                                        DOT(r->pert,r->pert) > FTINY*FTINY) {
267                                for (i = 0; i < 3; i++) /* perturb direction */
268                                        sr.rdir[i] = r->rdir[i] -
269                                                        .75*r->pert[i];
270                                if (normalize(sr.rdir) == 0.0) {
271                                        objerror(m, WARNING, "illegal perturbation");
272                                        VCOPY(sr.rdir, r->rdir);
273                                }
274                        } else {
275                                VCOPY(sr.rdir, r->rdir);
276                                transtest = 2;
277                        }
278                        rayvalue(&sr);
279                        multcolor(sr.rcol, ctmp);
280                        addcolor(r->rcol, sr.rcol);
281                        transtest *= bright(sr.rcol);
282                        transdist = r->rot + sr.rt;
283                }
284        }
285        if (r->crtype & SHADOW)                 /* the rest is shadow */
286                return;
287                                                /* compute reflected ray */
288        rspecr = 0.;
289        if (m->otype == MAT_BRTDF && nd.rspec > FTINY) {
290                RAY  sr;
291                errno = 0;
292                setcolor(ctmp, evalue(mf->ep[0]),
293                                evalue(mf->ep[1]),
294                                evalue(mf->ep[2]));
295                if (errno)
296                        objerror(m, WARNING, "compute error");
297                else if ((rspecr = bright(ctmp)) > FTINY &&
298                                rayorigin(&sr, r, REFLECTED, rspecr) == 0) {
299                        for (i = 0; i < 3; i++)
300                                sr.rdir[i] = r->rdir[i] +
301                                                2.0*nd.pdot*nd.pnorm[i];
302                        rayvalue(&sr);
303                        multcolor(sr.rcol, ctmp);
304                        addcolor(r->rcol, sr.rcol);
305                }
306        }
364                                                  /* compute ambient */
365 <        if ((dtmp = 1.0-nd.trans-rspecr) > FTINY) {
365 >        if (nd.trans < 1.0-FTINY) {
366                  ambient(ctmp, r);
367 <                scalecolor(ctmp, dtmp);
367 >                scalecolor(ctmp, 1.0-nd.trans);
368                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
369                  addcolor(r->rcol, ctmp);        /* add to returned color */
370          }
371 <        if ((dtmp = nd.trans-tspect) > FTINY) { /* from other side */
371 >        if (nd.trans > FTINY) {         /* from other side */
372                  flipsurface(r);
373                  ambient(ctmp, r);
374 <                scalecolor(ctmp, dtmp);
374 >                scalecolor(ctmp, nd.trans);
375                  multcolor(ctmp, nd.mcolor);
376                  addcolor(r->rcol, ctmp);
377                  flipsurface(r);
378          }
379                                                  /* add direct component */
380          direct(r, dirbrdf, &nd);
324                                                /* check distance */
325        if (transtest > bright(r->rcol))
326                r->rt = transdist;
381   }
382  
383  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines