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.3 by greg, Fri Dec 14 21:57:59 1990 UTC vs.
Revision 1.16 by greg, Mon Aug 12 08:20:53 1991 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 24 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24   *  surface can be used to represent an infinitely thin object.
25   *
26   *  Arguments for MAT_PFUNC and MAT_MFUNC are:
27 < *      2+      func    funcfile        transform ..
27 > *      2+      func    funcfile        transform
28   *      0
29 < *      4+      red     grn     blu     specularity     args ..
29 > *      4+      red     grn     blu     specularity     A5 ..
30   *
31   *  Arguments for MAT_PDATA and MAT_MDATA are:
32 < *      4+      func    datafile        funcfile        v0 ..   transform ..
32 > *      4+      func    datafile        funcfile        v0 ..   transform
33   *      0
34 < *      4+      red     grn     blu     specularity     args ..
34 > *      4+      red     grn     blu     specularity     A5 ..
35 > *
36 > *  Arguments for MAT_TFUNC are:
37 > *      2+      func    funcfile        transform
38 > *      0
39 > *      4+      red     grn     blu     rspec   trans   tspec   A7 ..
40 > *
41 > *  Arguments for MAT_TDATA are:
42 > *      4+      func    datafile        funcfile        v0 ..   transform
43 > *      0
44 > *      4+      red     grn     blu     rspec   trans   tspec   A7 ..
45 > *
46 > *  Arguments for the more general MAT_BRTDF are:
47 > *      10+     rrefl   grefl   brefl
48 > *              rtrns   gtrns   btrns
49 > *              rbrtd   gbrtd   bbrtd
50 > *              funcfile        transform
51 > *      0
52 > *      6+      red     grn     blu     rspec   trans   tspec   A7 ..
53 > *
54 > *      In addition to the normal variables available to functions,
55 > *  we define the following:
56 > *              NxP, NyP, NzP -         perturbed surface normal
57 > *              RdotP -                 perturbed ray dot product
58 > *              CrP, CgP, CbP -         perturbed material color
59   */
60  
61   extern double   funvalue(), varvalue();
62 + extern XF  funcxf;
63  
64   typedef struct {
65          OBJREC  *mp;            /* material pointer */
66          RAY  *pr;               /* intersected ray */
67 <        DATARRAY  *dp;          /* data array for PDATA or MDATA */
67 >        DATARRAY  *dp;          /* data array for PDATA, MDATA or TDATA */
68          COLOR  mcolor;          /* color of this material */
44        COLOR  scolor;          /* color of specular component */
69          double  rspec;          /* specular reflection */
70          double  rdiff;          /* diffuse reflection */
71 +        double  trans;          /* transmissivity */
72 +        double  tspec;          /* specular transmission */
73 +        double  tdiff;          /* diffuse transmission */
74          FVECT  pnorm;           /* perturbed surface normal */
75          double  pdot;           /* perturbed dot product */
76   }  BRDFDAT;             /* BRDF material data */
# Line 58 | Line 85 | double  omega;                 /* light source size */
85          double  ldot;
86          double  dtmp;
87          COLOR  ctmp;
88 +        FVECT  ldx;
89          double  pt[MAXDIM];
90 +        register char   **sa;
91          register int    i;
92  
93          setcolor(cval, 0.0, 0.0, 0.0);
94          
95          ldot = DOT(np->pnorm, ldir);
96  
97 <        if (ldot < 0.0)
97 >        if (ldot <= FTINY && ldot >= -FTINY)
98 >                return;         /* too close to grazing */
99 >        if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
100                  return;         /* wrong side */
101  
102 <        if (np->rdiff > FTINY) {
102 >        if (ldot > 0.0 && np->rdiff > FTINY) {
103                  /*
104                   *  Compute and add diffuse reflected component to returned
105                   *  color.  The diffuse reflected component will always be
# Line 79 | Line 110 | double  omega;                 /* light source size */
110                  scalecolor(ctmp, dtmp);
111                  addcolor(cval, ctmp);
112          }
113 <        if (np->rspec > FTINY) {
113 >        if (ldot < 0.0 && np->tdiff > FTINY) {
114                  /*
115 <                 *  Compute specular component.
115 >                 *  Diffuse transmitted component.
116                   */
117 <                setfunc(np->mp, np->pr);
118 <                errno = 0;
119 <                if (np->dp == NULL)
120 <                        dtmp = funvalue(np->mp->oargs.sarg[0], 3, ldir);
90 <                else {
91 <                        for (i = 0; i < np->dp->nd; i++)
92 <                                pt[i] = funvalue(np->mp->oargs.sarg[3+i],
93 <                                                3, ldir);
94 <                        dtmp = datavalue(np->dp, pt);
95 <                        dtmp = funvalue(np->mp->oargs.sarg[0], 1, &dtmp);
96 <                }
97 <                if (errno)
98 <                        goto computerr;
99 <                if (dtmp > FTINY) {
100 <                        copycolor(ctmp, np->scolor);
101 <                        dtmp *= ldot * omega;
102 <                        scalecolor(ctmp, dtmp);
103 <                        addcolor(cval, ctmp);
104 <                }
117 >                copycolor(ctmp, np->mcolor);
118 >                dtmp = -ldot * omega * np->tdiff / PI;
119 >                scalecolor(ctmp, dtmp);
120 >                addcolor(cval, ctmp);
121          }
122 +        if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY)
123 +                return;         /* no specular component */
124 +                                        /* set up function */
125 +        setbrdfunc(np);
126 +        sa = np->mp->oargs.sarg;
127 +        errno = 0;
128 +                                        /* transform light vector */
129 +        multv3(ldx, ldir, funcxf.xfm);
130 +        for (i = 0; i < 3; i++)
131 +                ldx[i] /= funcxf.sca;
132 +                                        /* compute BRTDF */
133 +        if (np->mp->otype == MAT_BRTDF) {
134 +                colval(ctmp,RED) = funvalue(sa[6], 3, ldx);
135 +                if (!strcmp(sa[7],sa[6]))
136 +                        colval(ctmp,GRN) = colval(ctmp,RED);
137 +                else
138 +                        colval(ctmp,GRN) = funvalue(sa[7], 3, ldx);
139 +                if (!strcmp(sa[8],sa[6]))
140 +                        colval(ctmp,BLU) = colval(ctmp,RED);
141 +                else if (!strcmp(sa[8],sa[7]))
142 +                        colval(ctmp,BLU) = colval(ctmp,GRN);
143 +                else
144 +                        colval(ctmp,BLU) = funvalue(sa[8], 3, ldx);
145 +                dtmp = bright(ctmp);
146 +        } else if (np->dp == NULL) {
147 +                dtmp = funvalue(sa[0], 3, ldx);
148 +                setcolor(ctmp, dtmp, dtmp, dtmp);
149 +        } else {
150 +                for (i = 0; i < np->dp->nd; i++)
151 +                        pt[i] = funvalue(sa[3+i], 3, ldx);
152 +                dtmp = datavalue(np->dp, pt);
153 +                dtmp = funvalue(sa[0], 1, &dtmp);
154 +                setcolor(ctmp, dtmp, dtmp, dtmp);
155 +        }
156 +        if (errno)
157 +                goto computerr;
158 +        if (dtmp <= FTINY)
159 +                return;
160 +        if (ldot > 0.0) {
161 +                /*
162 +                 *  Compute reflected non-diffuse component.
163 +                 */
164 +                if (np->mp->otype == MAT_MFUNC || np->mp->otype == MAT_MDATA)
165 +                        multcolor(ctmp, np->mcolor);
166 +                dtmp = ldot * omega * np->rspec;
167 +                scalecolor(ctmp, dtmp);
168 +                addcolor(cval, ctmp);
169 +        } else {
170 +                /*
171 +                 *  Compute transmitted non-diffuse component.
172 +                 */
173 +                if (np->mp->otype == MAT_TFUNC || np->mp->otype == MAT_TDATA)
174 +                        multcolor(ctmp, np->mcolor);
175 +                dtmp = -ldot * omega * np->tspec;
176 +                scalecolor(ctmp, dtmp);
177 +                addcolor(cval, ctmp);
178 +        }
179          return;
180   computerr:
181          objerror(np->mp, WARNING, "compute error");
# Line 114 | Line 187 | m_brdf(m, r)                   /* color a ray which hit a BRDF materia
187   register OBJREC  *m;
188   register RAY  *r;
189   {
190 +        int  minsa, minfa;
191          BRDFDAT  nd;
192 <        double  dtmp;
192 >        double  transtest, transdist;
193          COLOR  ctmp;
194 +        double  dtmp, tspect, rspecr;
195          register int  i;
196 <
197 <        if (m->oargs.nsargs < 2 || m->oargs.nfargs < 4)
196 >                                                /* check arguments */
197 >        switch (m->otype) {
198 >        case MAT_PFUNC: case MAT_MFUNC:
199 >                minsa = 2; minfa = 4; break;
200 >        case MAT_PDATA: case MAT_MDATA:
201 >                minsa = 4; minfa = 4; break;
202 >        case MAT_TFUNC:
203 >                minsa = 2; minfa = 6; break;
204 >        case MAT_TDATA:
205 >                minsa = 4; minfa = 6; break;
206 >        case MAT_BRTDF:
207 >                minsa = 10; minfa = 6; break;
208 >        }
209 >        if (m->oargs.nsargs < minsa || m->oargs.nfargs < minfa)
210                  objerror(m, USER, "bad # arguments");
124                                                /* easy shadow test */
125        if (r->crtype & SHADOW)
126                return;
211          nd.mp = m;
212          nd.pr = r;
213 <                                                /* load auxiliary files */
214 <        if (m->otype == MAT_PDATA || m->otype == MAT_MDATA) {
215 <                nd.dp = getdata(m->oargs.sarg[1]);
216 <                for (i = 3; i < m->oargs.nsargs; i++)
217 <                        if (m->oargs.sarg[i][0] == '-')
218 <                                break;
219 <                if (i-3 != nd.dp->nd)
220 <                        objerror(m, USER, "dimension error");
221 <                if (!fundefined(m->oargs.sarg[3]))
222 <                        loadfunc(m->oargs.sarg[2]);
223 <        } else {
224 <                nd.dp = NULL;
225 <                if (!fundefined(m->oargs.sarg[0]))
226 <                        loadfunc(m->oargs.sarg[1]);
227 <        }
213 >                                                /* get specular component */
214 >        nd.rspec = m->oargs.farg[3];
215 >                                                /* compute transmission */
216 >        if (m->otype == MAT_TFUNC || m->otype == MAT_TDATA
217 >                        || m->otype == MAT_BRTDF) {
218 >                nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
219 >                nd.tspec = nd.trans * m->oargs.farg[5];
220 >                nd.tdiff = nd.trans - nd.tspec;
221 >        } else
222 >                nd.tdiff = nd.tspec = nd.trans = 0.0;
223 >                                                /* early shadow check */
224 >        if (r->crtype & SHADOW && (m->otype != MAT_BRTDF || nd.tspec <= FTINY))
225 >                return;
226 >                                                /* diffuse reflection */
227 >        nd.rdiff = 1.0 - nd.trans - nd.rspec;
228                                                  /* get material color */
229          setcolor(nd.mcolor, m->oargs.farg[0],
230                             m->oargs.farg[1],
231                             m->oargs.farg[2]);
232 <                                                /* get roughness */
232 >                                                /* fix orientation */
233          if (r->rod < 0.0)
234                  flipsurface(r);
235                                                  /* get modifiers */
236          raytexture(r, m->omod);
237          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
238          multcolor(nd.mcolor, r->pcol);          /* modify material color */
239 <        r->rt = r->rot;                         /* default ray length */
240 <                                                /* get specular component */
241 <        nd.rspec = m->oargs.farg[3];
242 <
243 <        if (nd.rspec > FTINY) {                 /* has specular component */
244 <                                                /* compute specular color */
245 <                if (m->otype == MAT_MFUNC || m->otype == MAT_MDATA)
246 <                        copycolor(nd.scolor, nd.mcolor);
247 <                else
248 <                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
249 <                scalecolor(nd.scolor, nd.rspec);
239 >        transtest = 0;
240 >                                                /* load auxiliary files */
241 >        if (m->otype == MAT_PDATA || m->otype == MAT_MDATA
242 >                        || m->otype == MAT_TDATA) {
243 >                nd.dp = getdata(m->oargs.sarg[1]);
244 >                for (i = 3; i < m->oargs.nsargs; i++)
245 >                        if (m->oargs.sarg[i][0] == '-')
246 >                                break;
247 >                if (i-3 != nd.dp->nd)
248 >                        objerror(m, USER, "dimension error");
249 >                funcfile(m->oargs.sarg[2]);
250 >        } else if (m->otype == MAT_BRTDF) {
251 >                nd.dp = NULL;
252 >                funcfile(m->oargs.sarg[9]);
253 >        } else {
254 >                nd.dp = NULL;
255 >                funcfile(m->oargs.sarg[1]);
256          }
257 <                                                /* diffuse reflection */
258 <        nd.rdiff = 1.0 - nd.rspec;
257 >                                                /* set special variables */
258 >        setbrdfunc(&nd);
259 >                                                /* compute transmitted ray */
260 >        tspect = 0.;
261 >        if (m->otype == MAT_BRTDF && nd.tspec > FTINY) {
262 >                RAY  sr;
263 >                errno = 0;
264 >                setcolor(ctmp, varvalue(m->oargs.sarg[3]),
265 >                                varvalue(m->oargs.sarg[4]),
266 >                                varvalue(m->oargs.sarg[5]));
267 >                scalecolor(ctmp, nd.trans);
268 >                if (errno)
269 >                        objerror(m, WARNING, "compute error");
270 >                else if ((tspect = bright(ctmp)) > FTINY &&
271 >                                rayorigin(&sr, r, TRANS, tspect) == 0) {
272 >                        if (!(r->crtype & SHADOW) &&
273 >                                        DOT(r->pert,r->pert) > FTINY*FTINY) {
274 >                                for (i = 0; i < 3; i++) /* perturb direction */
275 >                                        sr.rdir[i] = r->rdir[i] -
276 >                                                        .75*r->pert[i];
277 >                                normalize(sr.rdir);
278 >                        } else {
279 >                                VCOPY(sr.rdir, r->rdir);
280 >                                transtest = 2;
281 >                        }
282 >                        rayvalue(&sr);
283 >                        multcolor(sr.rcol, ctmp);
284 >                        addcolor(r->rcol, sr.rcol);
285 >                        transtest *= bright(sr.rcol);
286 >                        transdist = r->rot + sr.rt;
287 >                }
288 >        }
289 >        if (r->crtype & SHADOW)                 /* the rest is shadow */
290 >                return;
291 >                                                /* compute reflected ray */
292 >        rspecr = 0.;
293 >        if (m->otype == MAT_BRTDF && nd.rspec > FTINY) {
294 >                RAY  sr;
295 >                errno = 0;
296 >                setcolor(ctmp, varvalue(m->oargs.sarg[0]),
297 >                                varvalue(m->oargs.sarg[1]),
298 >                                varvalue(m->oargs.sarg[2]));
299 >                if (errno)
300 >                        objerror(m, WARNING, "compute error");
301 >                else if ((rspecr = bright(ctmp)) > FTINY &&
302 >                                rayorigin(&sr, r, REFLECTED, rspecr) == 0) {
303 >                        for (i = 0; i < 3; i++)
304 >                                sr.rdir[i] = r->rdir[i] +
305 >                                                2.0*nd.pdot*nd.pnorm[i];
306 >                        rayvalue(&sr);
307 >                        multcolor(sr.rcol, ctmp);
308 >                        addcolor(r->rcol, sr.rcol);
309 >                }
310 >        }
311                                                  /* compute ambient */
312 <        if (nd.rdiff > FTINY) {
312 >        if ((dtmp = 1.0-nd.trans-rspecr) > FTINY) {
313                  ambient(ctmp, r);
314 +                scalecolor(ctmp, dtmp);
315                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
316                  addcolor(r->rcol, ctmp);        /* add to returned color */
317          }
318 +        if ((dtmp = nd.trans-tspect) > FTINY) { /* from other side */
319 +                flipsurface(r);
320 +                ambient(ctmp, r);
321 +                scalecolor(ctmp, dtmp);
322 +                multcolor(ctmp, nd.mcolor);
323 +                addcolor(r->rcol, ctmp);
324 +                flipsurface(r);
325 +        }
326                                                  /* add direct component */
327          direct(r, dirbrdf, &nd);
328 +                                                /* check distance */
329 +        if (transtest > bright(r->rcol))
330 +                r->rt = transdist;
331 + }
332 +
333 +
334 + setbrdfunc(np)                  /* set up brdf function and variables */
335 + register BRDFDAT  *np;
336 + {
337 +        FVECT  vec;
338 +
339 +        if (setfunc(np->mp, np->pr) == 0)
340 +                return(0);      /* it's OK, setfunc says we're done */
341 +                                /* else (re)assign special variables */
342 +        multv3(vec, np->pnorm, funcxf.xfm);
343 +        varset("NxP", '=', vec[0]/funcxf.sca);
344 +        varset("NyP", '=', vec[1]/funcxf.sca);
345 +        varset("NzP", '=', vec[2]/funcxf.sca);
346 +        varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
347 +                        np->pdot >= 1.0 ? 1.0 : np->pdot);
348 +        varset("CrP", '=', colval(np->mcolor,RED));
349 +        varset("CgP", '=', colval(np->mcolor,GRN));
350 +        varset("CbP", '=', colval(np->mcolor,BLU));
351 +        return(1);
352   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines