ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
(Generate patch)

Comparing ray/src/rt/aniso.c (file contents):
Revision 2.11 by greg, Tue Mar 3 16:20:00 1992 UTC vs.
Revision 2.34 by greg, Sat Feb 22 02:07:28 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  Shading functions for anisotropic materials.
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 + */
64 +
65   #include  "ray.h"
66  
67   #include  "otypes.h"
# Line 16 | Line 70 | static char SCCSid[] = "$SunId$ LBL";
70  
71   #include  "random.h"
72  
73 < extern double  specthresh;              /* specular sampling threshold */
74 < extern double  specjitter;              /* specular sampling jitter */
73 > #ifndef  MAXITER
74 > #define  MAXITER        10              /* maximum # specular ray attempts */
75 > #endif
76  
77   /*
78 < *      This anisotropic reflection model uses a variant on the
79 < *  exponential Gaussian used in normal.c.
78 > *      This routine implements the anisotropic Gaussian
79 > *  model described by Ward in Siggraph `92 article.
80   *      We orient the surface towards the incoming ray, so a single
81   *  surface can be used to represent an infinitely thin object.
82   *
# Line 34 | Line 89 | extern double  specjitter;             /* specular sampling jitte
89   *  8  red      grn     blu     rspec   u-rough v-rough trans   tspec
90   */
91  
37 #define  BSPEC(m)       (6.0)           /* specularity parameter b */
38
92                                  /* specularity flags */
93   #define  SP_REFL        01              /* has reflected specular component */
94   #define  SP_TRAN        02              /* has transmitted specular */
# Line 62 | Line 115 | typedef struct {
115          double  pdot;           /* perturbed dot product */
116   }  ANISODAT;            /* anisotropic material data */
117  
118 + static void     getacoords();
119 + static void     agaussamp();
120  
121 +
122 + static void
123   diraniso(cval, np, ldir, omega)         /* compute source contribution */
124   COLOR  cval;                    /* returned coefficient */
125   register ANISODAT  *np;         /* material data */
# Line 70 | Line 127 | FVECT  ldir;                   /* light source direction */
127   double  omega;                  /* light source size */
128   {
129          double  ldot;
130 <        double  dtmp, dtmp2;
130 >        double  dtmp, dtmp1, dtmp2;
131          FVECT  h;
132          double  au2, av2;
133          COLOR  ctmp;
# Line 103 | Line 160 | double  omega;                 /* light source size */
160                          au2 = av2 = omega/(4.0*PI);
161                  else
162                          au2 = av2 = 0.0;
163 <                au2 += np->u_alpha * np->u_alpha;
164 <                av2 += np->v_alpha * np->v_alpha;
163 >                au2 += np->u_alpha*np->u_alpha;
164 >                av2 += np->v_alpha*np->v_alpha;
165                                                  /* half vector */
166                  h[0] = ldir[0] - np->rp->rdir[0];
167                  h[1] = ldir[1] - np->rp->rdir[1];
168                  h[2] = ldir[2] - np->rp->rdir[2];
112                normalize(h);
169                                                  /* ellipse */
170 <                dtmp = DOT(np->u, h);
171 <                dtmp *= dtmp / au2;
170 >                dtmp1 = DOT(np->u, h);
171 >                dtmp1 *= dtmp1 / au2;
172                  dtmp2 = DOT(np->v, h);
173                  dtmp2 *= dtmp2 / av2;
174                                                  /* gaussian */
175 <                dtmp = (dtmp + dtmp2) / (1.0 + DOT(np->pnorm, h));
176 <                dtmp = exp(-2.0*dtmp) / (4.0*PI * sqrt(au2*av2));
175 >                dtmp = DOT(np->pnorm, h);
176 >                dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp);
177 >                dtmp = exp(-dtmp) * (0.25/PI)
178 >                                * sqrt(ldot/(np->pdot*au2*av2));
179                                                  /* worth using? */
180                  if (dtmp > FTINY) {
181                          copycolor(ctmp, np->scolor);
182 <                        dtmp *= omega / np->pdot;
182 >                        dtmp *= omega;
183                          scalecolor(ctmp, dtmp);
184                          addcolor(cval, ctmp);
185                  }
# Line 141 | Line 199 | double  omega;                 /* light source size */
199                   *  is always modified by material color.
200                   */
201                                                  /* roughness + source */
202 +                au2 = av2 = omega / PI;
203 +                au2 += np->u_alpha*np->u_alpha;
204 +                av2 += np->v_alpha*np->v_alpha;
205 +                                                /* "half vector" */
206 +                h[0] = ldir[0] - np->prdir[0];
207 +                h[1] = ldir[1] - np->prdir[1];
208 +                h[2] = ldir[2] - np->prdir[2];
209 +                dtmp = DOT(h,h);
210 +                if (dtmp > FTINY*FTINY) {
211 +                        dtmp1 = DOT(h,np->pnorm);
212 +                        dtmp = 1.0 - dtmp1*dtmp1/dtmp;
213 +                        if (dtmp > FTINY*FTINY) {
214 +                                dtmp1 = DOT(h,np->u);
215 +                                dtmp1 *= dtmp1 / au2;
216 +                                dtmp2 = DOT(h,np->v);
217 +                                dtmp2 *= dtmp2 / av2;
218 +                                dtmp = (dtmp1 + dtmp2) / dtmp;
219 +                        }
220 +                } else
221 +                        dtmp = 0.0;
222                                                  /* gaussian */
223 <                dtmp = 0.0;
223 >                dtmp = exp(-dtmp) * (1.0/PI)
224 >                                * sqrt(-ldot/(np->pdot*au2*av2));
225                                                  /* worth using? */
226                  if (dtmp > FTINY) {
227                          copycolor(ctmp, np->mcolor);
228 <                        dtmp *= np->tspec * omega / np->pdot;
228 >                        dtmp *= np->tspec * omega;
229                          scalecolor(ctmp, dtmp);
230                          addcolor(cval, ctmp);
231                  }
# Line 154 | Line 233 | double  omega;                 /* light source size */
233   }
234  
235  
236 + int
237   m_aniso(m, r)                   /* shade ray that hit something anisotropic */
238   register OBJREC  *m;
239   register RAY  *r;
240   {
241          ANISODAT  nd;
162        double  dtmp;
242          COLOR  ctmp;
243          register int  i;
244                                                  /* easy shadow test */
245          if (r->crtype & SHADOW)
246 <                return;
246 >                return(1);
247  
248          if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
249                  objerror(m, USER, "bad number of real arguments");
# Line 178 | Line 257 | register RAY  *r;
257          nd.specfl = 0;
258          nd.u_alpha = m->oargs.farg[4];
259          nd.v_alpha = m->oargs.farg[5];
260 <        if (nd.u_alpha < 1e-6 || nd.v_alpha <= 1e-6)
260 >        if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
261                  objerror(m, USER, "roughness too small");
262 <                                                /* reorient if necessary */
263 <        if (r->rod < 0.0)
264 <                flipsurface(r);
262 >                                                /* check for back side */
263 >        if (r->rod < 0.0) {
264 >                if (!backvis && m->otype != MAT_TRANS2) {
265 >                        raytrans(r);
266 >                        return(1);
267 >                }
268 >                flipsurface(r);                 /* reorient if backvis */
269 >        }
270                                                  /* get modifiers */
271          raytexture(r, m->omod);
272          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
# Line 198 | Line 282 | register RAY  *r;
282                  else
283                          setcolor(nd.scolor, 1.0, 1.0, 1.0);
284                  scalecolor(nd.scolor, nd.rspec);
201                                                /* improved model */
202                dtmp = exp(-BSPEC(m)*nd.pdot);
203                for (i = 0; i < 3; i++)
204                        colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
205                nd.rspec += (1.0-nd.rspec)*dtmp;
285                                                  /* check threshold */
286 <                if (specthresh > FTINY &&
208 <                                ((specthresh >= 1.-FTINY ||
209 <                                specthresh + (.05 - .1*frandom()) > nd.rspec)))
286 >                if (specthresh >= nd.rspec-FTINY)
287                          nd.specfl |= SP_RBLT;
288                                                  /* compute refl. direction */
289                  for (i = 0; i < 3; i++)
# Line 216 | Line 293 | register RAY  *r;
293                                  nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
294          }
295                                                  /* compute transmission */
296 <        if (m->otype == MAT_TRANS) {
296 >        if (m->otype == MAT_TRANS2) {
297                  nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
298                  nd.tspec = nd.trans * m->oargs.farg[7];
299                  nd.tdiff = nd.trans - nd.tspec;
300                  if (nd.tspec > FTINY) {
301                          nd.specfl |= SP_TRAN;
302                                                          /* check threshold */
303 <                        if (specthresh > FTINY &&
227 <                                        ((specthresh >= 1.-FTINY ||
228 <                                        specthresh +
229 <                                            (.05 - .1*frandom()) > nd.tspec)))
303 >                        if (specthresh >= nd.tspec-FTINY)
304                                  nd.specfl |= SP_TBLT;
305                          if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
306                                  VCOPY(nd.prdir, r->rdir);
307                          } else {
308                                  for (i = 0; i < 3; i++)         /* perturb */
309 <                                        nd.prdir[i] = r->rdir[i] -
236 <                                                        0.5*r->pert[i];
309 >                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
310                                  if (DOT(nd.prdir, r->ron) < -FTINY)
311                                          normalize(nd.prdir);    /* OK */
312                                  else
# Line 246 | Line 319 | register RAY  *r;
319                                                  /* diffuse reflection */
320          nd.rdiff = 1.0 - nd.trans - nd.rspec;
321  
322 <        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
250 <                        r->ro->otype == OBJ_RING))
322 >        if (r->ro != NULL && isflat(r->ro->otype))
323                  nd.specfl |= SP_FLAT;
324  
325          getacoords(r, &nd);                     /* set up coordinates */
# Line 256 | Line 328 | register RAY  *r;
328                  agaussamp(r, &nd);
329  
330          if (nd.rdiff > FTINY) {         /* ambient from this side */
331 <                ambient(ctmp, r);
331 >                ambient(ctmp, r, nd.pnorm);
332                  if (nd.specfl & SP_RBLT)
333                          scalecolor(ctmp, 1.0-nd.trans);
334                  else
# Line 265 | Line 337 | register RAY  *r;
337                  addcolor(r->rcol, ctmp);        /* add to returned color */
338          }
339          if (nd.tdiff > FTINY) {         /* ambient from other side */
340 +                FVECT  bnorm;
341 +
342                  flipsurface(r);
343 <                ambient(ctmp, r);
343 >                bnorm[0] = -nd.pnorm[0];
344 >                bnorm[1] = -nd.pnorm[1];
345 >                bnorm[2] = -nd.pnorm[2];
346 >                ambient(ctmp, r, bnorm);
347                  if (nd.specfl & SP_TBLT)
348                          scalecolor(ctmp, nd.trans);
349                  else
# Line 277 | Line 354 | register RAY  *r;
354          }
355                                          /* add direct component */
356          direct(r, diraniso, &nd);
357 +
358 +        return(1);
359   }
360  
361  
362 < static
362 > static void
363   getacoords(r, np)               /* set up coordinate system */
364   RAY  *r;
365   register ANISODAT  *np;
# Line 298 | Line 377 | register ANISODAT  *np;
377                  np->specfl |= SP_BADU;
378                  return;
379          }
380 <        multv3(np->u, np->u, mf->f->xfm);
380 >        if (mf->f != &unitxf)
381 >                multv3(np->u, np->u, mf->f->xfm);
382          fcross(np->v, np->pnorm, np->u);
383          if (normalize(np->v) == 0.0) {
384                  objerror(np->mp, WARNING, "illegal orientation vector");
# Line 309 | Line 389 | register ANISODAT  *np;
389   }
390  
391  
392 < static
392 > static void
393   agaussamp(r, np)                /* sample anisotropic gaussian specular */
394   RAY  *r;
395   register ANISODAT  *np;
# Line 318 | Line 398 | register ANISODAT  *np;
398          FVECT  h;
399          double  rv[2];
400          double  d, sinp, cosp;
401 +        int  niter;
402          register int  i;
403                                          /* compute reflection */
404          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
405                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
406                  dimlist[ndims++] = (int)np->mp;
407 <                d = urand(ilhash(dimlist,ndims)+samplendx);
408 <                multisamp(rv, 2, d);
409 <                d = 2.0*PI * rv[0];
410 <                cosp = np->u_alpha * cos(d);
411 <                sinp = np->v_alpha * sin(d);
412 <                d = sqrt(cosp*cosp + sinp*sinp);
413 <                cosp /= d;
414 <                sinp /= d;
415 <                rv[1] = 1.0 - specjitter*rv[1];
416 <                if (rv[1] <= FTINY)
417 <                        d = 1.0;
418 <                else
419 <                        d = sqrt(-log(rv[1]) /
420 <                                (cosp*cosp/(np->u_alpha*np->u_alpha) +
421 <                                 sinp*sinp/(np->v_alpha*np->v_alpha)));
422 <                for (i = 0; i < 3; i++)
423 <                        h[i] = np->pnorm[i] +
424 <                                d*(cosp*np->u[i] + sinp*np->v[i]);
425 <                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
426 <                for (i = 0; i < 3; i++)
427 <                        sr.rdir[i] = r->rdir[i] + d*h[i];
428 <                if (DOT(sr.rdir, r->ron) <= FTINY)      /* penetration? */
429 <                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
430 <                rayvalue(&sr);
431 <                multcolor(sr.rcol, np->scolor);
432 <                addcolor(r->rcol, sr.rcol);
407 >                for (niter = 0; niter < MAXITER; niter++) {
408 >                        if (niter)
409 >                                d = frandom();
410 >                        else
411 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
412 >                        multisamp(rv, 2, d);
413 >                        d = 2.0*PI * rv[0];
414 >                        cosp = tcos(d) * np->u_alpha;
415 >                        sinp = tsin(d) * np->v_alpha;
416 >                        d = sqrt(cosp*cosp + sinp*sinp);
417 >                        cosp /= d;
418 >                        sinp /= d;
419 >                        rv[1] = 1.0 - specjitter*rv[1];
420 >                        if (rv[1] <= FTINY)
421 >                                d = 1.0;
422 >                        else
423 >                                d = sqrt(-log(rv[1]) /
424 >                                        (cosp*cosp/(np->u_alpha*np->u_alpha) +
425 >                                         sinp*sinp/(np->v_alpha*np->v_alpha)));
426 >                        for (i = 0; i < 3; i++)
427 >                                h[i] = np->pnorm[i] +
428 >                                        d*(cosp*np->u[i] + sinp*np->v[i]);
429 >                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
430 >                        for (i = 0; i < 3; i++)
431 >                                sr.rdir[i] = r->rdir[i] + d*h[i];
432 >                        if (DOT(sr.rdir, r->ron) > FTINY) {
433 >                                rayvalue(&sr);
434 >                                multcolor(sr.rcol, np->scolor);
435 >                                addcolor(r->rcol, sr.rcol);
436 >                                break;
437 >                        }
438 >                }
439                  ndims--;
440          }
441                                          /* compute transmission */
442          if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
443                          rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
444                  dimlist[ndims++] = (int)np->mp;
445 <                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
446 <                multisamp(rv, 2, d);
447 <                d = 2.0*PI * rv[0];
448 <                cosp = cos(d);
449 <                sinp = sin(d);
450 <                rv[1] = 1.0 - specjitter*rv[1];
451 <                if (rv[1] <= FTINY)
452 <                        d = 1.0;
453 <                else
454 <                        d = sqrt(-log(rv[1]) /
455 <                                (cosp*cosp*4./(np->u_alpha*np->u_alpha) +
456 <                                 sinp*sinp*4./(np->v_alpha*np->v_alpha)));
457 <                for (i = 0; i < 3; i++)
458 <                        sr.rdir[i] = np->prdir[i] +
459 <                                        d*(cosp*np->u[i] + sinp*np->v[i]);
460 <                if (DOT(sr.rdir, r->ron) < -FTINY)
461 <                        normalize(sr.rdir);             /* OK, normalize */
462 <                else
463 <                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
464 <                rayvalue(&sr);
465 <                scalecolor(sr.rcol, np->tspec);
466 <                multcolor(sr.rcol, np->mcolor);         /* modify by color */
467 <                addcolor(r->rcol, sr.rcol);
445 >                for (niter = 0; niter < MAXITER; niter++) {
446 >                        if (niter)
447 >                                d = frandom();
448 >                        else
449 >                                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
450 >                        multisamp(rv, 2, d);
451 >                        d = 2.0*PI * rv[0];
452 >                        cosp = tcos(d) * np->u_alpha;
453 >                        sinp = tsin(d) * np->v_alpha;
454 >                        d = sqrt(cosp*cosp + sinp*sinp);
455 >                        cosp /= d;
456 >                        sinp /= d;
457 >                        rv[1] = 1.0 - specjitter*rv[1];
458 >                        if (rv[1] <= FTINY)
459 >                                d = 1.0;
460 >                        else
461 >                                d = sqrt(-log(rv[1]) /
462 >                                        (cosp*cosp/(np->u_alpha*np->u_alpha) +
463 >                                         sinp*sinp/(np->v_alpha*np->v_alpha)));
464 >                        for (i = 0; i < 3; i++)
465 >                                sr.rdir[i] = np->prdir[i] +
466 >                                                d*(cosp*np->u[i] + sinp*np->v[i]);
467 >                        if (DOT(sr.rdir, r->ron) < -FTINY) {
468 >                                normalize(sr.rdir);     /* OK, normalize */
469 >                                rayvalue(&sr);
470 >                                scalecolor(sr.rcol, np->tspec);
471 >                                multcolor(sr.rcol, np->mcolor); /* modify */
472 >                                addcolor(r->rcol, sr.rcol);
473 >                                break;
474 >                        }
475 >                }
476                  ndims--;
477          }
478   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines