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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines