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.31 by greg, Wed Nov 22 09:27:51 1995 UTC vs.
Revision 2.39 by greg, Wed Mar 12 17:26:58 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 + #include "copyright.h"
9 +
10   #include  "ray.h"
11  
12   #include  "otypes.h"
# Line 16 | Line 15 | static char SCCSid[] = "$SunId$ LBL";
15  
16   #include  "random.h"
17  
18 < extern double  specthresh;              /* specular sampling threshold */
19 < extern double  specjitter;              /* specular sampling jitter */
18 > #ifndef  MAXITER
19 > #define  MAXITER        10              /* maximum # specular ray attempts */
20 > #endif
21  
22 extern int  backvis;                    /* back faces visible? */
23
24 static  agaussamp(), getacoords();
25
22   /*
23   *      This routine implements the anisotropic Gaussian
24   *  model described by Ward in Siggraph `92 article.
# Line 64 | Line 60 | typedef struct {
60          double  pdot;           /* perturbed dot product */
61   }  ANISODAT;            /* anisotropic material data */
62  
63 + static void     getacoords();
64 + static void     agaussamp();
65  
66 +
67 + static void
68   diraniso(cval, np, ldir, omega)         /* compute source contribution */
69   COLOR  cval;                    /* returned coefficient */
70   register ANISODAT  *np;         /* material data */
# Line 178 | Line 178 | double  omega;                 /* light source size */
178   }
179  
180  
181 + int
182   m_aniso(m, r)                   /* shade ray that hit something anisotropic */
183   register OBJREC  *m;
184   register RAY  *r;
# Line 191 | Line 192 | register RAY  *r;
192  
193          if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
194                  objerror(m, USER, "bad number of real arguments");
195 +                                                /* check for back side */
196 +        if (r->rod < 0.0) {
197 +                if (!backvis && m->otype != MAT_TRANS2) {
198 +                        raytrans(r);
199 +                        return(1);
200 +                }
201 +                raytexture(r, m->omod);
202 +                flipsurface(r);                 /* reorient if backvis */
203 +        } else
204 +                raytexture(r, m->omod);
205 +                                                /* get material color */
206          nd.mp = m;
207          nd.rp = r;
196                                                /* get material color */
208          setcolor(nd.mcolor, m->oargs.farg[0],
209                             m->oargs.farg[1],
210                             m->oargs.farg[2]);
# Line 203 | Line 214 | register RAY  *r;
214          nd.v_alpha = m->oargs.farg[5];
215          if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
216                  objerror(m, USER, "roughness too small");
217 <                                                /* check for back side */
207 <        if (r->rod < 0.0) {
208 <                if (!backvis && m->otype != MAT_TRANS2) {
209 <                        raytrans(r);
210 <                        return(1);
211 <                }
212 <                flipsurface(r);                 /* reorient if backvis */
213 <        }
214 <                                                /* get modifiers */
215 <        raytexture(r, m->omod);
217 >
218          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
219          if (nd.pdot < .001)
220                  nd.pdot = .001;                 /* non-zero for diraniso() */
# Line 303 | Line 305 | register RAY  *r;
305   }
306  
307  
308 < static
308 > static void
309   getacoords(r, np)               /* set up coordinate system */
310   RAY  *r;
311   register ANISODAT  *np;
# Line 316 | Line 318 | register ANISODAT  *np;
318          errno = 0;
319          for (i = 0; i < 3; i++)
320                  np->u[i] = evalue(mf->ep[i]);
321 <        if (errno) {
321 >        if (errno == EDOM || errno == ERANGE) {
322                  objerror(np->mp, WARNING, "compute error");
323                  np->specfl |= SP_BADU;
324                  return;
# Line 333 | Line 335 | register ANISODAT  *np;
335   }
336  
337  
338 < static
338 > static void
339   agaussamp(r, np)                /* sample anisotropic gaussian specular */
340   RAY  *r;
341   register ANISODAT  *np;
# Line 342 | Line 344 | register ANISODAT  *np;
344          FVECT  h;
345          double  rv[2];
346          double  d, sinp, cosp;
347 +        int  niter;
348          register int  i;
349                                          /* compute reflection */
350          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
351                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
352                  dimlist[ndims++] = (int)np->mp;
353 <                d = urand(ilhash(dimlist,ndims)+samplendx);
354 <                multisamp(rv, 2, d);
355 <                d = 2.0*PI * rv[0];
356 <                cosp = cos(d) * np->u_alpha;
357 <                sinp = sin(d) * np->v_alpha;
358 <                d = sqrt(cosp*cosp + sinp*sinp);
359 <                cosp /= d;
360 <                sinp /= d;
361 <                rv[1] = 1.0 - specjitter*rv[1];
362 <                if (rv[1] <= FTINY)
363 <                        d = 1.0;
364 <                else
365 <                        d = sqrt(-log(rv[1]) /
366 <                                (cosp*cosp/(np->u_alpha*np->u_alpha) +
367 <                                 sinp*sinp/(np->v_alpha*np->v_alpha)));
368 <                for (i = 0; i < 3; i++)
369 <                        h[i] = np->pnorm[i] +
370 <                                d*(cosp*np->u[i] + sinp*np->v[i]);
371 <                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
372 <                for (i = 0; i < 3; i++)
373 <                        sr.rdir[i] = r->rdir[i] + d*h[i];
374 <                if (DOT(sr.rdir, r->ron) <= FTINY)      /* penetration? */
375 <                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
376 <                rayvalue(&sr);
377 <                multcolor(sr.rcol, np->scolor);
378 <                addcolor(r->rcol, sr.rcol);
353 >                for (niter = 0; niter < MAXITER; niter++) {
354 >                        if (niter)
355 >                                d = frandom();
356 >                        else
357 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
358 >                        multisamp(rv, 2, d);
359 >                        d = 2.0*PI * rv[0];
360 >                        cosp = tcos(d) * np->u_alpha;
361 >                        sinp = tsin(d) * np->v_alpha;
362 >                        d = sqrt(cosp*cosp + sinp*sinp);
363 >                        cosp /= d;
364 >                        sinp /= d;
365 >                        rv[1] = 1.0 - specjitter*rv[1];
366 >                        if (rv[1] <= FTINY)
367 >                                d = 1.0;
368 >                        else
369 >                                d = sqrt(-log(rv[1]) /
370 >                                        (cosp*cosp/(np->u_alpha*np->u_alpha) +
371 >                                         sinp*sinp/(np->v_alpha*np->v_alpha)));
372 >                        for (i = 0; i < 3; i++)
373 >                                h[i] = np->pnorm[i] +
374 >                                        d*(cosp*np->u[i] + sinp*np->v[i]);
375 >                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
376 >                        for (i = 0; i < 3; i++)
377 >                                sr.rdir[i] = r->rdir[i] + d*h[i];
378 >                        if (DOT(sr.rdir, r->ron) > FTINY) {
379 >                                rayvalue(&sr);
380 >                                multcolor(sr.rcol, np->scolor);
381 >                                addcolor(r->rcol, sr.rcol);
382 >                                break;
383 >                        }
384 >                }
385                  ndims--;
386          }
387                                          /* compute transmission */
388          if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
389                          rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
390                  dimlist[ndims++] = (int)np->mp;
391 <                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
392 <                multisamp(rv, 2, d);
393 <                d = 2.0*PI * rv[0];
394 <                cosp = cos(d) * np->u_alpha;
395 <                sinp = sin(d) * np->v_alpha;
396 <                d = sqrt(cosp*cosp + sinp*sinp);
397 <                cosp /= d;
398 <                sinp /= d;
399 <                rv[1] = 1.0 - specjitter*rv[1];
400 <                if (rv[1] <= FTINY)
401 <                        d = 1.0;
402 <                else
403 <                        d = sqrt(-log(rv[1]) /
404 <                                (cosp*cosp/(np->u_alpha*np->u_alpha) +
405 <                                 sinp*sinp/(np->v_alpha*np->u_alpha)));
406 <                for (i = 0; i < 3; i++)
407 <                        sr.rdir[i] = np->prdir[i] +
408 <                                        d*(cosp*np->u[i] + sinp*np->v[i]);
409 <                if (DOT(sr.rdir, r->ron) < -FTINY)
410 <                        normalize(sr.rdir);             /* OK, normalize */
411 <                else
412 <                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
413 <                rayvalue(&sr);
414 <                scalecolor(sr.rcol, np->tspec);
415 <                multcolor(sr.rcol, np->mcolor);         /* modify by color */
416 <                addcolor(r->rcol, sr.rcol);
391 >                for (niter = 0; niter < MAXITER; niter++) {
392 >                        if (niter)
393 >                                d = frandom();
394 >                        else
395 >                                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
396 >                        multisamp(rv, 2, d);
397 >                        d = 2.0*PI * rv[0];
398 >                        cosp = tcos(d) * np->u_alpha;
399 >                        sinp = tsin(d) * np->v_alpha;
400 >                        d = sqrt(cosp*cosp + sinp*sinp);
401 >                        cosp /= d;
402 >                        sinp /= d;
403 >                        rv[1] = 1.0 - specjitter*rv[1];
404 >                        if (rv[1] <= FTINY)
405 >                                d = 1.0;
406 >                        else
407 >                                d = sqrt(-log(rv[1]) /
408 >                                        (cosp*cosp/(np->u_alpha*np->u_alpha) +
409 >                                         sinp*sinp/(np->v_alpha*np->v_alpha)));
410 >                        for (i = 0; i < 3; i++)
411 >                                sr.rdir[i] = np->prdir[i] +
412 >                                                d*(cosp*np->u[i] + sinp*np->v[i]);
413 >                        if (DOT(sr.rdir, r->ron) < -FTINY) {
414 >                                normalize(sr.rdir);     /* OK, normalize */
415 >                                rayvalue(&sr);
416 >                                scalecolor(sr.rcol, np->tspec);
417 >                                multcolor(sr.rcol, np->mcolor); /* modify */
418 >                                addcolor(r->rcol, sr.rcol);
419 >                                break;
420 >                        }
421 >                }
422                  ndims--;
423          }
424   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines