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.15 by greg, Sat Feb 22 02:07:28 2003 UTC vs.
Revision 2.35 by greg, Wed Jan 10 17:45:11 2018 UTC

# Line 5 | Line 5 | static const char      RCSid[] = "$Id$";
5   *  Shading for materials with arbitrary BRDF's
6   */
7  
8 < /* ====================================================================
9 < * The Radiance Software License, Version 1.0
10 < *
11 < * Copyright (c) 1990 - 2002 The Regents of the University of California,
12 < * through Lawrence Berkeley National Laboratory.   All rights reserved.
13 < *
14 < * Redistribution and use in source and binary forms, with or without
15 < * modification, are permitted provided that the following conditions
16 < * are met:
17 < *
18 < * 1. Redistributions of source code must retain the above copyright
19 < *         notice, this list of conditions and the following disclaimer.
20 < *
21 < * 2. Redistributions in binary form must reproduce the above copyright
22 < *       notice, this list of conditions and the following disclaimer in
23 < *       the documentation and/or other materials provided with the
24 < *       distribution.
25 < *
26 < * 3. The end-user documentation included with the redistribution,
27 < *           if any, must include the following acknowledgment:
28 < *             "This product includes Radiance software
29 < *                 (http://radsite.lbl.gov/)
30 < *                 developed by the Lawrence Berkeley National Laboratory
31 < *               (http://www.lbl.gov/)."
32 < *       Alternately, this acknowledgment may appear in the software itself,
33 < *       if and wherever such third-party acknowledgments normally appear.
34 < *
35 < * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36 < *       and "The Regents of the University of California" must
37 < *       not be used to endorse or promote products derived from this
38 < *       software without prior written permission. For written
39 < *       permission, please contact [email protected].
40 < *
41 < * 5. Products derived from this software may not be called "Radiance",
42 < *       nor may "Radiance" appear in their name, without prior written
43 < *       permission of Lawrence Berkeley National Laboratory.
44 < *
45 < * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 < * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 < * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 < * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49 < * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 < * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 < * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 < * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 < * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 < * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 < * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 < * SUCH DAMAGE.
57 < * ====================================================================
58 < *
59 < * This software consists of voluntary contributions made by many
60 < * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
61 < * information on Lawrence Berkeley National Laboratory, please see
62 < * <http://www.lbl.gov/>.
63 < */
8 > #include "copyright.h"
9  
10   #include  "ray.h"
11 <
11 > #include  "ambient.h"
12   #include  "data.h"
13 <
13 > #include  "source.h"
14   #include  "otypes.h"
15 <
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 93 | 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 132 | Line 78 | typedef struct {
78   }  BRDFDAT;             /* BRDF material data */
79  
80  
81 + static int setbrdfunc(BRDFDAT *np);
82 +
83 +
84   static void
85 < dirbrdf(cval, np, ldir, omega)          /* compute source contribution */
86 < COLOR  cval;                    /* returned coefficient */
87 < register BRDFDAT  *np;          /* material data */
88 < FVECT  ldir;                    /* light source direction */
89 < double  omega;                  /* light source size */
85 > dirbrdf(                /* compute source contribution */
86 >        COLOR  cval,                    /* returned coefficient */
87 >        void  *nnp,             /* material data */
88 >        FVECT  ldir,                    /* light source direction */
89 >        double  omega                   /* light source size */
90 > )
91   {
92 +        BRDFDAT *np = nnp;
93          double  ldot;
94          double  dtmp;
95          COLOR  ctmp;
96          FVECT  ldx;
97          static double  vldx[5], pt[MAXDIM];
98 <        register char   **sa;
99 <        register int    i;
98 >        char    **sa;
99 >        int     i;
100   #define lddx (vldx+1)
101  
102          setcolor(cval, 0.0, 0.0, 0.0);
# Line 177 | Line 128 | double  omega;                 /* light source size */
128                  scalecolor(ctmp, dtmp);
129                  addcolor(cval, ctmp);
130          }
131 <        if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY)
132 <                return;         /* no specular component */
131 >        if ((ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) ||
132 >                        ambRayInPmap(np->pr))
133 >                return;         /* diffuse only */
134                                          /* set up function */
135          setbrdfunc(np);
136          sa = np->mp->oargs.sarg;
# Line 190 | Line 142 | double  omega;                 /* light source size */
142          lddx[3] = omega;
143                                          /* compute BRTDF */
144          if (np->mp->otype == MAT_BRTDF) {
145 <                if (sa[6][0] == '0')            /* special case */
145 >                if (sa[6][0] == '0' && !sa[6][1])       /* special case */
146                          colval(ctmp,RED) = 0.0;
147                  else
148                          colval(ctmp,RED) = funvalue(sa[6], 4, lddx);
149 <                if (!strcmp(sa[7],sa[6]))
149 >                if (sa[7][0] == '0' && !sa[7][1])
150 >                        colval(ctmp,GRN) = 0.0;
151 >                else if (!strcmp(sa[7],sa[6]))
152                          colval(ctmp,GRN) = colval(ctmp,RED);
153                  else
154                          colval(ctmp,GRN) = funvalue(sa[7], 4, lddx);
155 <                if (!strcmp(sa[8],sa[6]))
155 >                if (sa[8][0] == '0' && !sa[8][1])
156 >                        colval(ctmp,BLU) = 0.0;
157 >                else if (!strcmp(sa[8],sa[6]))
158                          colval(ctmp,BLU) = colval(ctmp,RED);
159                  else if (!strcmp(sa[8],sa[7]))
160                          colval(ctmp,BLU) = colval(ctmp,GRN);
# Line 215 | Line 171 | double  omega;                 /* light source size */
171                  dtmp = funvalue(sa[0], 5, vldx);
172                  setcolor(ctmp, dtmp, dtmp, dtmp);
173          }
174 <        if (errno) {
174 >        if ((errno == EDOM) | (errno == ERANGE)) {
175                  objerror(np->mp, WARNING, "compute error");
176                  return;
177          }
# Line 225 | Line 181 | double  omega;                 /* light source size */
181                  /*
182                   *  Compute reflected non-diffuse component.
183                   */
184 <                if (np->mp->otype == MAT_MFUNC | np->mp->otype == MAT_MDATA)
184 >                if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA))
185                          multcolor(ctmp, np->mcolor);
186                  dtmp = ldot * omega * np->rspec;
187                  scalecolor(ctmp, dtmp);
# Line 234 | Line 190 | double  omega;                 /* light source size */
190                  /*
191                   *  Compute transmitted non-diffuse component.
192                   */
193 <                if (np->mp->otype == MAT_TFUNC | np->mp->otype == MAT_TDATA)
193 >                if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA))
194                          multcolor(ctmp, np->mcolor);
195                  dtmp = -ldot * omega * np->tspec;
196                  scalecolor(ctmp, dtmp);
# Line 245 | Line 201 | double  omega;                 /* light source size */
201  
202  
203   int
204 < m_brdf(m, r)                    /* color a ray that hit a BRDTfunc material */
205 < register OBJREC  *m;
206 < register RAY  *r;
204 > m_brdf(                 /* color a ray that hit a BRDTfunc material */
205 >        OBJREC  *m,
206 >        RAY  *r
207 > )
208   {
209          int  hitfront = 1;
210          BRDFDAT  nd;
211          RAY  sr;
212 <        double  transtest, transdist;
212 >        double  mirtest=0, mirdist;
213 >        double  transtest=0, transdist;
214          int  hasrefl, hastrans;
215 +        int  hastexture;
216          COLOR  ctmp;
217          FVECT  vtmp;
218 <        register MFUNC  *mf;
219 <        register int  i;
218 >        double  d;
219 >        MFUNC  *mf;
220 >        int  i;
221                                                  /* check arguments */
222 <        if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9)
222 >        if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9))
223                  objerror(m, USER, "bad # arguments");
224          nd.mp = m;
225          nd.pr = r;
# Line 279 | Line 239 | register RAY  *r;
239          setcolor(nd.tdiff, m->oargs.farg[6],
240                          m->oargs.farg[7],
241                          m->oargs.farg[8]);
242 <                                        /* get modifiers */
242 >                                                /* get modifiers */
243          raytexture(r, m->omod);
244 <        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
244 >        hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY);
245 >        if (hastexture) {                       /* perturb normal */
246 >                nd.pdot = raynormal(nd.pnorm, r);
247 >        } else {
248 >                VCOPY(nd.pnorm, r->ron);
249 >                nd.pdot = r->rod;
250 >        }
251          if (r->rod < 0.0) {                     /* orient perturbed values */
252                  nd.pdot = -nd.pdot;
253                  for (i = 0; i < 3; i++) {
# Line 293 | Line 259 | register RAY  *r;
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;
262 >        hasrefl = (bright(nd.rdiff) > FTINY);
263 >        hastrans = (bright(nd.tdiff) > FTINY);
264 >        mirdist = transdist = r->rot;
265                                                  /* load cal file */
266          nd.dp = NULL;
267          mf = getfunc(m, 9, 0x3f, 0);
268                                                  /* compute transmitted ray */
269          setbrdfunc(&nd);
303        transtest = 0;
304        transdist = r->rot;
270          errno = 0;
271          setcolor(ctmp, evalue(mf->ep[3]),
272                          evalue(mf->ep[4]),
273                          evalue(mf->ep[5]));
274 <        if (errno)
274 >        if ((errno == EDOM) | (errno == ERANGE))
275                  objerror(m, WARNING, "compute error");
276 <        else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
277 <                if (!(r->crtype & SHADOW) &&
278 <                                DOT(r->pert,r->pert) > FTINY*FTINY) {
279 <                        for (i = 0; i < 3; i++) /* perturb direction */
315 <                                sr.rdir[i] = r->rdir[i] - .75*r->pert[i];
276 >        else if (rayorigin(&sr, TRANS, r, ctmp) == 0) {
277 >                if (hastexture && !(r->crtype & (SHADOW|AMBIENT))) {
278 >                                                /* perturb direction */
279 >                        VSUB(sr.rdir, r->rdir, r->pert);
280                          if (normalize(sr.rdir) == 0.0) {
281                                  objerror(m, WARNING, "illegal perturbation");
282                                  VCOPY(sr.rdir, r->rdir);
283                          }
284                  } else {
285                          VCOPY(sr.rdir, r->rdir);
322                        transtest = 2;
286                  }
287                  rayvalue(&sr);
288 <                multcolor(sr.rcol, ctmp);
288 >                multcolor(sr.rcol, sr.rcoef);
289                  addcolor(r->rcol, sr.rcol);
290 <                transtest *= bright(sr.rcol);
291 <                transdist = r->rot + sr.rt;
290 >                if (!hastexture || r->crtype & (SHADOW|AMBIENT)) {
291 >                        transtest = 2.0*bright(sr.rcol);
292 >                        transdist = r->rot + sr.rt;
293 >                }
294          }
295 <        if (r->crtype & SHADOW)                 /* the rest is shadow */
295 >        if (r->crtype & SHADOW) {               /* the rest is shadow */
296 >                r->rt = transdist;
297                  return(1);
298 +        }
299                                                  /* compute reflected ray */
300          setbrdfunc(&nd);
301          errno = 0;
302          setcolor(ctmp, evalue(mf->ep[0]),
303                          evalue(mf->ep[1]),
304                          evalue(mf->ep[2]));
305 <        if (errno)
305 >        if ((errno == EDOM) | (errno == ERANGE))
306                  objerror(m, WARNING, "compute error");
307 <        else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
308 <                for (i = 0; i < 3; i++)
309 <                        sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
307 >        else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) {
308 >                VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot);
309 >                checknorm(sr.rdir);
310                  rayvalue(&sr);
311 <                multcolor(sr.rcol, ctmp);
311 >                multcolor(sr.rcol, sr.rcoef);
312                  addcolor(r->rcol, sr.rcol);
313 +                if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) {
314 +                        mirtest = 2.0*bright(sr.rcol);
315 +                        mirdist = r->rot + sr.rt;
316 +                }
317          }
318                                                  /* compute ambient */
319          if (hasrefl) {
320                  if (!hitfront)
321                          flipsurface(r);
322 <                ambient(ctmp, r, nd.pnorm);
323 <                multcolor(ctmp, nd.rdiff);
322 >                copycolor(ctmp, nd.rdiff);
323 >                multambient(ctmp, r, nd.pnorm);
324                  addcolor(r->rcol, ctmp);        /* add to returned color */
325                  if (!hitfront)
326                          flipsurface(r);
# Line 360 | Line 331 | register RAY  *r;
331                  vtmp[0] = -nd.pnorm[0];
332                  vtmp[1] = -nd.pnorm[1];
333                  vtmp[2] = -nd.pnorm[2];
334 <                ambient(ctmp, r, vtmp);
335 <                multcolor(ctmp, nd.tdiff);
334 >                copycolor(ctmp, nd.tdiff);
335 >                multambient(ctmp, r, vtmp);
336                  addcolor(r->rcol, ctmp);
337                  if (hitfront)
338                          flipsurface(r);
339          }
340          if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
341                  direct(r, dirbrdf, &nd);        /* add direct component */
342 <                                                /* check distance */
343 <        if (transtest > bright(r->rcol))
342 >
343 >        d = bright(r->rcol);                    /* set effective distance */
344 >        if (transtest > d)
345                  r->rt = transdist;
346 +        else if (mirtest > d)
347 +                r->rt = mirdist;
348  
349          return(1);
350   }
# Line 378 | Line 352 | register RAY  *r;
352  
353  
354   int
355 < m_brdf2(m, r)                   /* color a ray that hit a BRDF material */
356 < register OBJREC  *m;
357 < register RAY  *r;
355 > m_brdf2(                        /* color a ray that hit a BRDF material */
356 >        OBJREC  *m,
357 >        RAY  *r
358 > )
359   {
360          BRDFDAT  nd;
361          COLOR  ctmp;
# Line 390 | Line 365 | register RAY  *r;
365          if (r->crtype & SHADOW)
366                  return(1);
367                                                  /* check arguments */
368 <        if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs <
369 <                        (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4))
368 >        if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
369 >                        ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
370                  objerror(m, USER, "bad # arguments");
371 +                                                /* check for back side */
372 +        if (r->rod < 0.0) {
373 +                if (!backvis) {
374 +                        raytrans(r);
375 +                        return(1);
376 +                }
377 +                raytexture(r, m->omod);
378 +                flipsurface(r);                 /* reorient if backvis */
379 +        } else
380 +                raytexture(r, m->omod);
381 +
382          nd.mp = m;
383          nd.pr = r;
384                                                  /* get material color */
# Line 402 | Line 388 | register RAY  *r;
388                                                  /* get specular component */
389          nd.rspec = m->oargs.farg[3];
390                                                  /* compute transmittance */
391 <        if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) {
391 >        if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
392                  nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
393                  nd.tspec = nd.trans * m->oargs.farg[5];
394                  dtmp = nd.trans - nd.tspec;
# Line 414 | Line 400 | register RAY  *r;
400                                                  /* compute reflectance */
401          dtmp = 1.0 - nd.trans - nd.rspec;
402          setcolor(nd.rdiff, dtmp, dtmp, dtmp);
417                                                /* check for back side */
418        if (r->rod < 0.0) {
419                if (!backvis && m->otype != MAT_TFUNC
420                                && m->otype != MAT_TDATA) {
421                        raytrans(r);
422                        return(1);
423                }
424                flipsurface(r);                 /* reorient if backvis */
425        }
426                                                /* get modifiers */
427        raytexture(r, m->omod);
403          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
404          multcolor(nd.mcolor, r->pcol);          /* modify material color */
405          multcolor(nd.rdiff, nd.mcolor);
# Line 439 | Line 414 | register RAY  *r;
414          }
415                                                  /* compute ambient */
416          if (nd.trans < 1.0-FTINY) {
417 <                ambient(ctmp, r, nd.pnorm);
417 >                copycolor(ctmp, nd.mcolor);     /* modified by material color */
418                  scalecolor(ctmp, 1.0-nd.trans);
419 <                multcolor(ctmp, nd.mcolor);     /* modified by material color */
419 >                multambient(ctmp, r, nd.pnorm);
420                  addcolor(r->rcol, ctmp);        /* add to returned color */
421          }
422          if (nd.trans > FTINY) {         /* from other side */
# Line 449 | Line 424 | register RAY  *r;
424                  vtmp[0] = -nd.pnorm[0];
425                  vtmp[1] = -nd.pnorm[1];
426                  vtmp[2] = -nd.pnorm[2];
427 <                ambient(ctmp, r, vtmp);
427 >                copycolor(ctmp, nd.mcolor);
428                  scalecolor(ctmp, nd.trans);
429 <                multcolor(ctmp, nd.mcolor);
429 >                multambient(ctmp, r, vtmp);
430                  addcolor(r->rcol, ctmp);
431                  flipsurface(r);
432          }
# Line 462 | Line 437 | register RAY  *r;
437   }
438  
439  
440 < int
441 < setbrdfunc(np)                  /* set up brdf function and variables */
442 < register BRDFDAT  *np;
440 > static int
441 > setbrdfunc(                     /* set up brdf function and variables */
442 >        BRDFDAT  *np
443 > )
444   {
445          FVECT  vec;
446  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines