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 1.1 by greg, Wed Dec 12 22:02:58 1990 UTC vs.
Revision 2.8 by greg, Tue Aug 24 12:59:25 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "otypes.h"
16  
17 + #include  "func.h"
18 +
19   /*
20   *      Arguments to this material include the color and specularity.
21   *  String arguments include the reflection function and files.
22   *  The BRDF is currently used just for the specular component to light
23   *  sources.  Reflectance values or data coordinates are functions
24 < *  of the direction to the light source.
24 > *  of the direction to the light source.  (Data modification functions
25 > *  are passed the source direction as args 2-4.)
26   *      We orient the surface towards the incoming ray, so a single
27   *  surface can be used to represent an infinitely thin object.
28   *
29   *  Arguments for MAT_PFUNC and MAT_MFUNC are:
30 < *      2+      func    funcfile        transform ..
30 > *      2+      func    funcfile        transform
31   *      0
32 < *      4+      red     grn     blu     specularity     args ..
32 > *      4+      red     grn     blu     specularity     A5 ..
33   *
34   *  Arguments for MAT_PDATA and MAT_MDATA are:
35 < *      4+      func    datafile        funcfile        v0 ..   transform ..
35 > *      4+      func    datafile        funcfile        v0 ..   transform
36   *      0
37 < *      4+      red     grn     blu     specularity     args ..
37 > *      4+      red     grn     blu     specularity     A5 ..
38 > *
39 > *  Arguments for MAT_TFUNC are:
40 > *      2+      func    funcfile        transform
41 > *      0
42 > *      4+      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 ..
48 > *
49 > *  Arguments for the more general MAT_BRTDF are:
50 > *      10+     rrefl   grefl   brefl
51 > *              rtrns   gtrns   btrns
52 > *              rbrtd   gbrtd   bbrtd
53 > *              funcfile        transform
54 > *      0
55 > *      9+      rdf     gdf     bdf
56 > *              rdb     gdb     bdb
57 > *              rdt     gdt     bdt     A10 ..
58 > *
59 > *      In addition to the normal variables available to functions,
60 > *  we define the following:
61 > *              NxP, NyP, NzP -         perturbed surface normal
62 > *              RdotP -                 perturbed ray dot product
63 > *              CrP, CgP, CbP -         perturbed material color (or pattern)
64   */
65  
37 #define  BSPEC(m)               (6.0)           /* specular parameter b */
38
66   typedef struct {
67          OBJREC  *mp;            /* material pointer */
68          RAY  *pr;               /* intersected ray */
69 <        DATARRAY  *dp;          /* data array for PDATA or MDATA */
70 <        COLOR  mcolor;          /* color of this material */
71 <        COLOR  scolor;          /* color of specular component */
72 <        double  rspec;          /* specular reflection */
73 <        double  rdiff;          /* diffuse reflection */
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 */
73 >        double  rspec;          /* specular reflectance (1 for BRDTF) */
74 >        double  trans;          /* transmissivity (.5 for BRDTF) */
75 >        double  tspec;          /* specular transmittance (1 for BRDTF) */
76          FVECT  pnorm;           /* perturbed surface normal */
77          double  pdot;           /* perturbed dot product */
78   }  BRDFDAT;             /* BRDF material data */
# Line 58 | Line 87 | double  omega;                 /* light source size */
87          double  ldot;
88          double  dtmp;
89          COLOR  ctmp;
90 <        double  pt[MAXDIM];
90 >        FVECT  ldx;
91 >        double  lddx[3], pt[MAXDIM];
92 >        double  vldx[4];
93 >        register char   **sa;
94          register int    i;
95  
96          setcolor(cval, 0.0, 0.0, 0.0);
97          
98          ldot = DOT(np->pnorm, ldir);
99  
100 <        if (ldot < 0.0)
100 >        if (ldot <= FTINY && ldot >= -FTINY)
101 >                return;         /* too close to grazing */
102 >
103 >        if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
104                  return;         /* wrong side */
105  
106 <        if (np->rdiff > FTINY) {
106 >        if (ldot > 0.0) {
107                  /*
108                   *  Compute and add diffuse reflected component to returned
109                   *  color.  The diffuse reflected component will always be
110                   *  modified by the color of the material.
111                   */
112 <                copycolor(ctmp, np->mcolor);
113 <                dtmp = ldot * omega * np->rdiff / PI;
112 >                copycolor(ctmp, np->rdiff);
113 >                dtmp = ldot * omega / PI;
114                  scalecolor(ctmp, dtmp);
115                  addcolor(cval, ctmp);
116 +        } else {
117 +                /*
118 +                 *  Diffuse transmitted component.
119 +                 */
120 +                copycolor(ctmp, np->tdiff);
121 +                dtmp = -ldot * omega / PI;
122 +                scalecolor(ctmp, dtmp);
123 +                addcolor(cval, ctmp);
124          }
125 <        if (np->rspec > FTINY) {
125 >        if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY)
126 >                return;         /* no specular component */
127 >                                        /* set up function */
128 >        setbrdfunc(np);
129 >        sa = np->mp->oargs.sarg;
130 >        errno = 0;
131 >                                        /* transform light vector */
132 >        multv3(ldx, ldir, funcxf.xfm);
133 >        for (i = 0; i < 3; i++)
134 >                lddx[i] = ldx[i]/funcxf.sca;
135 >                                        /* compute BRTDF */
136 >        if (np->mp->otype == MAT_BRTDF) {
137 >                if (sa[6][0] == '0')            /* special case */
138 >                        colval(ctmp,RED) = 0.0;
139 >                else
140 >                        colval(ctmp,RED) = funvalue(sa[6], 3, lddx);
141 >                if (!strcmp(sa[7],sa[6]))
142 >                        colval(ctmp,GRN) = colval(ctmp,RED);
143 >                else
144 >                        colval(ctmp,GRN) = funvalue(sa[7], 3, lddx);
145 >                if (!strcmp(sa[8],sa[6]))
146 >                        colval(ctmp,BLU) = colval(ctmp,RED);
147 >                else if (!strcmp(sa[8],sa[7]))
148 >                        colval(ctmp,BLU) = colval(ctmp,GRN);
149 >                else
150 >                        colval(ctmp,BLU) = funvalue(sa[8], 3, lddx);
151 >                dtmp = bright(ctmp);
152 >        } else if (np->dp == NULL) {
153 >                dtmp = funvalue(sa[0], 3, lddx);
154 >                setcolor(ctmp, dtmp, dtmp, dtmp);
155 >        } else {
156 >                for (i = 0; i < np->dp->nd; i++)
157 >                        pt[i] = funvalue(sa[3+i], 3, lddx);
158 >                vldx[0] = datavalue(np->dp, pt);
159 >                vldx[1] = lddx[0]; vldx[2] = lddx[1]; vldx[3] = lddx[2];
160 >                dtmp = funvalue(sa[0], 4, vldx);
161 >                setcolor(ctmp, dtmp, dtmp, dtmp);
162 >        }
163 >        if (errno) {
164 >                objerror(np->mp, WARNING, "compute error");
165 >                return;
166 >        }
167 >        if (dtmp <= FTINY)
168 >                return;
169 >        if (ldot > 0.0) {
170                  /*
171 <                 *  Compute specular component.
171 >                 *  Compute reflected non-diffuse component.
172                   */
173 <                setfunc(np->mp, np->pr);
174 <                errno = 0;
175 <                if (np->dp == NULL)
176 <                        dtmp = funvalue(np->mp->oargs.sarg[0], 3, ldir);
177 <                else {
178 <                        for (i = 0; i < np->dp->nd; i++)
179 <                                pt[i] = funvalue(np->mp->oargs.sarg[3+i],
180 <                                                3, ldir);
181 <                        dtmp = datavalue(np->dp, pt);
182 <                        dtmp = funvalue(np->mp->oargs.sarg[0], 1, &dtmp);
183 <                }
184 <                if (errno)
185 <                        goto computerr;
186 <                if (dtmp > FTINY) {
100 <                        copycolor(ctmp, np->scolor);
101 <                        dtmp *= ldot * omega;
102 <                        scalecolor(ctmp, dtmp);
103 <                        addcolor(cval, ctmp);
104 <                }
173 >                if (np->mp->otype == MAT_MFUNC | np->mp->otype == MAT_MDATA)
174 >                        multcolor(ctmp, np->mcolor);
175 >                dtmp = ldot * omega * np->rspec;
176 >                scalecolor(ctmp, dtmp);
177 >                addcolor(cval, ctmp);
178 >        } else {
179 >                /*
180 >                 *  Compute transmitted non-diffuse component.
181 >                 */
182 >                if (np->mp->otype == MAT_TFUNC | np->mp->otype == MAT_TDATA)
183 >                        multcolor(ctmp, np->mcolor);
184 >                dtmp = -ldot * omega * np->tspec;
185 >                scalecolor(ctmp, dtmp);
186 >                addcolor(cval, ctmp);
187          }
106        return;
107 computerr:
108        objerror(np->mp, WARNING, "compute error");
109        return;
188   }
189  
190  
191 < m_brdf(m, r)                    /* color a ray which hit a BRDF material */
191 > m_brdf(m, r)                    /* color a ray which hit a BRDTF material */
192   register OBJREC  *m;
193   register RAY  *r;
194   {
195          BRDFDAT  nd;
196 <        double  dtmp;
196 >        RAY  sr;
197 >        double  transtest, transdist;
198 >        int  hasrefl, hastrans;
199          COLOR  ctmp;
200 +        double  dtmp;
201 +        register MFUNC  *mf;
202          register int  i;
203 <
204 <        if (m->oargs.nsargs < 2 || m->oargs.nfargs < 4)
203 >                                                /* check arguments */
204 >        if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9)
205                  objerror(m, USER, "bad # arguments");
206 <                                                /* easy shadow test */
206 >        nd.mp = m;
207 >        nd.pr = r;
208 >                                                /* dummy values */
209 >        nd.rspec = nd.tspec = 1.0;
210 >        nd.trans = 0.5;
211 >                                                /* diffuse reflectance */
212 >        if (r->rod > 0.0)
213 >                setcolor(nd.rdiff, m->oargs.farg[0],
214 >                                m->oargs.farg[1],
215 >                                m->oargs.farg[2]);
216 >        else
217 >                setcolor(nd.rdiff, m->oargs.farg[3],
218 >                                m->oargs.farg[4],
219 >                                m->oargs.farg[5]);
220 >                                                /* diffuse transmittance */
221 >        setcolor(nd.tdiff, m->oargs.farg[6],
222 >                        m->oargs.farg[7],
223 >                        m->oargs.farg[8]);
224 >                                        /* get modifiers */
225 >        raytexture(r, m->omod);
226 >        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
227 >        if (r->rod < 0.0) {                     /* orient perturbed values */
228 >                nd.pdot = -nd.pdot;
229 >                for (i = 0; i < 3; i++) {
230 >                        nd.pnorm[i] = -nd.pnorm[i];
231 >                        r->pert[i] = -r->pert[i];
232 >                }
233 >        }
234 >        copycolor(nd.mcolor, r->pcol);          /* get pattern color */
235 >        multcolor(nd.rdiff, nd.mcolor);         /* modify diffuse values */
236 >        multcolor(nd.tdiff, nd.mcolor);
237 >        hasrefl = bright(nd.rdiff) > FTINY;
238 >        hastrans = bright(nd.tdiff) > FTINY;
239 >                                                /* load cal file */
240 >        nd.dp = NULL;
241 >        mf = getfunc(m, 9, 0x3f, 0);
242 >                                                /* compute transmitted ray */
243 >        setbrdfunc(&nd);
244 >        transtest = 0;
245 >        transdist = r->rot;
246 >        errno = 0;
247 >        setcolor(ctmp, evalue(mf->ep[3]),
248 >                        evalue(mf->ep[4]),
249 >                        evalue(mf->ep[5]));
250 >        if (errno)
251 >                objerror(m, WARNING, "compute error");
252 >        else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
253 >                if (!(r->crtype & SHADOW) &&
254 >                                DOT(r->pert,r->pert) > FTINY*FTINY) {
255 >                        for (i = 0; i < 3; i++) /* perturb direction */
256 >                                sr.rdir[i] = r->rdir[i] - .75*r->pert[i];
257 >                        if (normalize(sr.rdir) == 0.0) {
258 >                                objerror(m, WARNING, "illegal perturbation");
259 >                                VCOPY(sr.rdir, r->rdir);
260 >                        }
261 >                } else {
262 >                        VCOPY(sr.rdir, r->rdir);
263 >                        transtest = 2;
264 >                }
265 >                rayvalue(&sr);
266 >                multcolor(sr.rcol, ctmp);
267 >                addcolor(r->rcol, sr.rcol);
268 >                transtest *= bright(sr.rcol);
269 >                transdist = r->rot + sr.rt;
270 >        }
271 >        if (r->crtype & SHADOW)                 /* the rest is shadow */
272 >                return;
273 >                                                /* compute reflected ray */
274 >        setbrdfunc(&nd);
275 >        errno = 0;
276 >        setcolor(ctmp, evalue(mf->ep[0]),
277 >                        evalue(mf->ep[1]),
278 >                        evalue(mf->ep[2]));
279 >        if (errno)
280 >                objerror(m, WARNING, "compute error");
281 >        else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
282 >                for (i = 0; i < 3; i++)
283 >                        sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
284 >                rayvalue(&sr);
285 >                multcolor(sr.rcol, ctmp);
286 >                addcolor(r->rcol, sr.rcol);
287 >        }
288 >                                                /* compute ambient */
289 >        if (hasrefl) {
290 >                if (nd.pdot < 0.0)
291 >                        flipsurface(r);
292 >                ambient(ctmp, r);
293 >                multcolor(ctmp, nd.rdiff);
294 >                addcolor(r->rcol, ctmp);        /* add to returned color */
295 >                if (nd.pdot < 0.0)
296 >                        flipsurface(r);
297 >        }
298 >        if (hastrans) {                         /* from other side */
299 >                if (nd.pdot > 0.0)
300 >                        flipsurface(r);
301 >                ambient(ctmp, r);
302 >                multcolor(ctmp, nd.tdiff);
303 >                addcolor(r->rcol, ctmp);
304 >                if (nd.pdot > 0.0)
305 >                        flipsurface(r);
306 >        }
307 >        if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
308 >                direct(r, dirbrdf, &nd);        /* add direct component */
309 >                                                /* check distance */
310 >        if (transtest > bright(r->rcol))
311 >                r->rt = transdist;
312 > }
313 >
314 >
315 >
316 > m_brdf2(m, r)                   /* color a ray which hit a BRDF material */
317 > register OBJREC  *m;
318 > register RAY  *r;
319 > {
320 >        BRDFDAT  nd;
321 >        COLOR  ctmp;
322 >        double  dtmp;
323 >                                                /* always a shadow */
324          if (r->crtype & SHADOW)
325                  return;
326 +                                                /* check arguments */
327 +        if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs <
328 +                        (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4))
329 +                objerror(m, USER, "bad # arguments");
330          nd.mp = m;
331          nd.pr = r;
129                                                /* load auxiliary files */
130        if (m->otype == MAT_PDATA || m->otype == MAT_MDATA) {
131                nd.dp = getdata(m->oargs.sarg[1]);
132                for (i = 3; i < m->oargs.nsargs; i++)
133                        if (m->oargs.sarg[i][0] == '-')
134                                break;
135                if (i-3 != nd.dp->nd)
136                        objerror(m, USER, "dimension error");
137                if (!fundefined(m->oargs.sarg[3]))
138                        loadfunc(m->oargs.sarg[2]);
139        } else {
140                nd.dp = NULL;
141                if (!fundefined(m->oargs.sarg[0]))
142                        loadfunc(m->oargs.sarg[1]);
143        }
332                                                  /* get material color */
333          setcolor(nd.mcolor, m->oargs.farg[0],
334 <                           m->oargs.farg[1],
335 <                           m->oargs.farg[2]);
336 <                                                /* get roughness */
334 >                        m->oargs.farg[1],
335 >                        m->oargs.farg[2]);
336 >                                                /* get specular component */
337 >        nd.rspec = m->oargs.farg[3];
338 >                                                /* compute transmittance */
339 >        if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) {
340 >                nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
341 >                nd.tspec = nd.trans * m->oargs.farg[5];
342 >                dtmp = nd.trans - nd.tspec;
343 >                setcolor(nd.tdiff, dtmp, dtmp, dtmp);
344 >        } else {
345 >                nd.tspec = nd.trans = 0.0;
346 >                setcolor(nd.tdiff, 0.0, 0.0, 0.0);
347 >        }
348 >                                                /* compute reflectance */
349 >        dtmp = 1.0 - nd.trans - nd.rspec;
350 >        setcolor(nd.rdiff, dtmp, dtmp, dtmp);
351 >                                                /* fix orientation */
352          if (r->rod < 0.0)
353                  flipsurface(r);
354                                                  /* get modifiers */
355          raytexture(r, m->omod);
356          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
357          multcolor(nd.mcolor, r->pcol);          /* modify material color */
358 <        r->rt = r->rot;                         /* default ray length */
359 <                                                /* get specular component */
360 <        nd.rspec = m->oargs.farg[3];
361 <
362 <        if (nd.rspec > FTINY) {                 /* has specular component */
363 <                                                /* compute specular color */
364 <                if (m->otype == MAT_MFUNC || m->otype == MAT_MDATA)
365 <                        copycolor(nd.scolor, nd.mcolor);
366 <                else
164 <                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
165 <                scalecolor(nd.scolor, nd.rspec);
166 <                                                /* improved model */
167 <                dtmp = exp(-BSPEC(m)*nd.pdot);
168 <                for (i = 0; i < 3; i++)
169 <                        colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
170 <                nd.rspec += (1.0-nd.rspec)*dtmp;
358 >        multcolor(nd.rdiff, nd.mcolor);
359 >        multcolor(nd.tdiff, nd.mcolor);
360 >                                                /* load auxiliary files */
361 >        if (hasdata(m->otype)) {
362 >                nd.dp = getdata(m->oargs.sarg[1]);
363 >                getfunc(m, 2, 0, 0);
364 >        } else {
365 >                nd.dp = NULL;
366 >                getfunc(m, 1, 0, 0);
367          }
172                                                /* diffuse reflection */
173        nd.rdiff = 1.0 - nd.rspec;
368                                                  /* compute ambient */
369 <        if (nd.rdiff > FTINY) {
369 >        if (nd.trans < 1.0-FTINY) {
370                  ambient(ctmp, r);
371 +                scalecolor(ctmp, 1.0-nd.trans);
372                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
373                  addcolor(r->rcol, ctmp);        /* add to returned color */
374          }
375 +        if (nd.trans > FTINY) {         /* from other side */
376 +                flipsurface(r);
377 +                ambient(ctmp, r);
378 +                scalecolor(ctmp, nd.trans);
379 +                multcolor(ctmp, nd.mcolor);
380 +                addcolor(r->rcol, ctmp);
381 +                flipsurface(r);
382 +        }
383                                                  /* add direct component */
384          direct(r, dirbrdf, &nd);
385 + }
386 +
387 +
388 + setbrdfunc(np)                  /* set up brdf function and variables */
389 + register BRDFDAT  *np;
390 + {
391 +        FVECT  vec;
392 +
393 +        if (setfunc(np->mp, np->pr) == 0)
394 +                return(0);      /* it's OK, setfunc says we're done */
395 +                                /* else (re)assign special variables */
396 +        multv3(vec, np->pnorm, funcxf.xfm);
397 +        varset("NxP", '=', vec[0]/funcxf.sca);
398 +        varset("NyP", '=', vec[1]/funcxf.sca);
399 +        varset("NzP", '=', vec[2]/funcxf.sca);
400 +        varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
401 +                        np->pdot >= 1.0 ? 1.0 : np->pdot);
402 +        varset("CrP", '=', colval(np->mcolor,RED));
403 +        varset("CgP", '=', colval(np->mcolor,GRN));
404 +        varset("CbP", '=', colval(np->mcolor,BLU));
405 +        return(1);
406   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines