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.5 by greg, Wed Jan 15 11:02:43 1992 UTC vs.
Revision 2.31 by greg, Wed Nov 22 09:27:51 1995 UTC

# Line 19 | Line 19 | static char SCCSid[] = "$SunId$ LBL";
19   extern double  specthresh;              /* specular sampling threshold */
20   extern double  specjitter;              /* specular sampling jitter */
21  
22 + extern int  backvis;                    /* back faces visible? */
23 +
24 + static  agaussamp(), getacoords();
25 +
26   /*
27 < *      This anisotropic reflection model uses a variant on the
28 < *  exponential Gaussian used in normal.c.
27 > *      This routine implements the anisotropic Gaussian
28 > *  model described by Ward in Siggraph `92 article.
29   *      We orient the surface towards the incoming ray, so a single
30   *  surface can be used to represent an infinitely thin object.
31   *
# Line 34 | Line 38 | extern double  specjitter;             /* specular sampling jitte
38   *  8  red      grn     blu     rspec   u-rough v-rough trans   tspec
39   */
40  
37 #define  BSPEC(m)       (6.0)           /* specularity parameter b */
38
41                                  /* specularity flags */
42   #define  SP_REFL        01              /* has reflected specular component */
43   #define  SP_TRAN        02              /* has transmitted specular */
44 < #define  SP_PURE        010             /* purely specular (zero roughness) */
45 < #define  SP_FLAT        020             /* reflecting surface is flat */
46 < #define  SP_RBLT        040             /* reflection below sample threshold */
47 < #define  SP_TBLT        0100            /* transmission below threshold */
46 < #define  SP_BADU        0200            /* bad u direction calculation */
44 > #define  SP_FLAT        04              /* reflecting surface is flat */
45 > #define  SP_RBLT        010             /* reflection below sample threshold */
46 > #define  SP_TBLT        020             /* transmission below threshold */
47 > #define  SP_BADU        040             /* bad u direction calculation */
48  
49   typedef struct {
50          OBJREC  *mp;            /* material pointer */
# Line 51 | Line 52 | typedef struct {
52          short  specfl;          /* specularity flags, defined above */
53          COLOR  mcolor;          /* color of this material */
54          COLOR  scolor;          /* color of specular component */
55 +        FVECT  vrefl;           /* vector in reflected direction */
56          FVECT  prdir;           /* vector in transmitted direction */
57          FVECT  u, v;            /* u and v vectors orienting anisotropy */
58          double  u_alpha;        /* u roughness */
# Line 70 | Line 72 | FVECT  ldir;                   /* light source direction */
72   double  omega;                  /* light source size */
73   {
74          double  ldot;
75 <        double  dtmp, dtmp2;
75 >        double  dtmp, dtmp1, dtmp2;
76          FVECT  h;
77          double  au2, av2;
78          COLOR  ctmp;
# Line 93 | Line 95 | double  omega;                 /* light source size */
95                  scalecolor(ctmp, dtmp);
96                  addcolor(cval, ctmp);
97          }
98 <        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE|SP_BADU)) == SP_REFL) {
98 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) {
99                  /*
100                   *  Compute specular reflection coefficient using
101                   *  anisotropic gaussian distribution model.
# Line 103 | Line 105 | double  omega;                 /* light source size */
105                          au2 = av2 = omega/(4.0*PI);
106                  else
107                          au2 = av2 = 0.0;
108 <                au2 += np->u_alpha * np->u_alpha;
109 <                av2 += np->v_alpha * np->v_alpha;
108 >                au2 += np->u_alpha*np->u_alpha;
109 >                av2 += np->v_alpha*np->v_alpha;
110                                                  /* half vector */
111                  h[0] = ldir[0] - np->rp->rdir[0];
112                  h[1] = ldir[1] - np->rp->rdir[1];
113                  h[2] = ldir[2] - np->rp->rdir[2];
112                normalize(h);
114                                                  /* ellipse */
115 <                dtmp = DOT(np->u, h);
116 <                dtmp *= dtmp / au2;
115 >                dtmp1 = DOT(np->u, h);
116 >                dtmp1 *= dtmp1 / au2;
117                  dtmp2 = DOT(np->v, h);
118                  dtmp2 *= dtmp2 / av2;
119                                                  /* gaussian */
120 <                dtmp = (dtmp + dtmp2) / (1.0 + DOT(np->pnorm, h));
121 <                dtmp = exp(-2.0*dtmp) / (4.0*PI * sqrt(au2*av2));
120 >                dtmp = DOT(np->pnorm, h);
121 >                dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp);
122 >                dtmp = exp(-dtmp) * (0.25/PI)
123 >                                * sqrt(ldot/(np->pdot*au2*av2));
124                                                  /* worth using? */
125                  if (dtmp > FTINY) {
126                          copycolor(ctmp, np->scolor);
127 <                        dtmp *= omega / np->pdot;
127 >                        dtmp *= omega;
128                          scalecolor(ctmp, dtmp);
129                          addcolor(cval, ctmp);
130                  }
# Line 135 | Line 138 | double  omega;                 /* light source size */
138                  scalecolor(ctmp, dtmp);
139                  addcolor(cval, ctmp);
140          }
141 <        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE|SP_BADU)) == SP_TRAN) {
141 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) {
142                  /*
143                   *  Compute specular transmission.  Specular transmission
144                   *  is always modified by material color.
145                   */
146                                                  /* roughness + source */
147 +                au2 = av2 = omega / PI;
148 +                au2 += np->u_alpha*np->u_alpha;
149 +                av2 += np->v_alpha*np->v_alpha;
150 +                                                /* "half vector" */
151 +                h[0] = ldir[0] - np->prdir[0];
152 +                h[1] = ldir[1] - np->prdir[1];
153 +                h[2] = ldir[2] - np->prdir[2];
154 +                dtmp = DOT(h,h);
155 +                if (dtmp > FTINY*FTINY) {
156 +                        dtmp1 = DOT(h,np->pnorm);
157 +                        dtmp = 1.0 - dtmp1*dtmp1/dtmp;
158 +                        if (dtmp > FTINY*FTINY) {
159 +                                dtmp1 = DOT(h,np->u);
160 +                                dtmp1 *= dtmp1 / au2;
161 +                                dtmp2 = DOT(h,np->v);
162 +                                dtmp2 *= dtmp2 / av2;
163 +                                dtmp = (dtmp1 + dtmp2) / dtmp;
164 +                        }
165 +                } else
166 +                        dtmp = 0.0;
167                                                  /* gaussian */
168 <                dtmp = 0.0;
168 >                dtmp = exp(-dtmp) * (1.0/PI)
169 >                                * sqrt(-ldot/(np->pdot*au2*av2));
170                                                  /* worth using? */
171                  if (dtmp > FTINY) {
172                          copycolor(ctmp, np->mcolor);
173 <                        dtmp *= np->tspec * omega / np->pdot;
173 >                        dtmp *= np->tspec * omega;
174                          scalecolor(ctmp, dtmp);
175                          addcolor(cval, ctmp);
176                  }
# Line 159 | Line 183 | register OBJREC  *m;
183   register RAY  *r;
184   {
185          ANISODAT  nd;
162        double  transtest, transdist;
163        double  dtmp;
186          COLOR  ctmp;
187          register int  i;
188                                                  /* easy shadow test */
189 <        if (r->crtype & SHADOW && m->otype != MAT_TRANS2)
190 <                return;
189 >        if (r->crtype & SHADOW)
190 >                return(1);
191  
192          if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
193                  objerror(m, USER, "bad number of real arguments");
# Line 179 | Line 201 | register RAY  *r;
201          nd.specfl = 0;
202          nd.u_alpha = m->oargs.farg[4];
203          nd.v_alpha = m->oargs.farg[5];
204 <        if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY)
205 <                nd.specfl |= SP_PURE;
206 <                                                /* reorient if necessary */
207 <        if (r->rod < 0.0)
208 <                flipsurface(r);
204 >        if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
205 >                objerror(m, USER, "roughness too small");
206 >                                                /* 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);
216          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
217          if (nd.pdot < .001)
218                  nd.pdot = .001;                 /* non-zero for diraniso() */
219          multcolor(nd.mcolor, r->pcol);          /* modify material color */
193        transtest = 0;
220                                                  /* get specular component */
221          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
222                  nd.specfl |= SP_REFL;
# Line 200 | Line 226 | register RAY  *r;
226                  else
227                          setcolor(nd.scolor, 1.0, 1.0, 1.0);
228                  scalecolor(nd.scolor, nd.rspec);
203                                                /* improved model */
204                dtmp = exp(-BSPEC(m)*nd.pdot);
205                for (i = 0; i < 3; i++)
206                        colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
207                nd.rspec += (1.0-nd.rspec)*dtmp;
229                                                  /* check threshold */
230 <                if (specthresh > FTINY &&
210 <                                ((specthresh >= 1.-FTINY ||
211 <                                specthresh + (.1 - .2*urand(8199+samplendx))
212 <                                        > nd.rspec)))
230 >                if (specthresh >= nd.rspec-FTINY)
231                          nd.specfl |= SP_RBLT;
232 <
233 <                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
234 <                        RAY  lr;
235 <                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
236 <                                for (i = 0; i < 3; i++)
237 <                                        lr.rdir[i] = r->rdir[i] +
220 <                                                2.0*nd.pdot*nd.pnorm[i];
221 <                                rayvalue(&lr);
222 <                                multcolor(lr.rcol, nd.scolor);
223 <                                addcolor(r->rcol, lr.rcol);
224 <                        }
225 <                }
232 >                                                /* compute refl. direction */
233 >                for (i = 0; i < 3; i++)
234 >                        nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
235 >                if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
236 >                        for (i = 0; i < 3; i++)         /* safety measure */
237 >                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
238          }
239                                                  /* compute transmission */
240 <        if (m->otype == MAT_TRANS) {
240 >        if (m->otype == MAT_TRANS2) {
241                  nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
242                  nd.tspec = nd.trans * m->oargs.farg[7];
243                  nd.tdiff = nd.trans - nd.tspec;
244                  if (nd.tspec > FTINY) {
245                          nd.specfl |= SP_TRAN;
246                                                          /* check threshold */
247 <                        if (specthresh > FTINY &&
236 <                                        ((specthresh >= 1.-FTINY ||
237 <                                        specthresh +
238 <                                            (.1 - .2*urand(7241+samplendx))
239 <                                                > nd.tspec)))
247 >                        if (specthresh >= nd.tspec-FTINY)
248                                  nd.specfl |= SP_TBLT;
249 <                        if (r->crtype & SHADOW ||
242 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
249 >                        if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
250                                  VCOPY(nd.prdir, r->rdir);
244                                transtest = 2;
251                          } else {
252                                  for (i = 0; i < 3; i++)         /* perturb */
253 <                                        nd.prdir[i] = r->rdir[i] -
254 <                                                        .75*r->pert[i];
255 <                                normalize(nd.prdir);
253 >                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
254 >                                if (DOT(nd.prdir, r->ron) < -FTINY)
255 >                                        normalize(nd.prdir);    /* OK */
256 >                                else
257 >                                        VCOPY(nd.prdir, r->rdir);
258                          }
259                  }
260          } else
261                  nd.tdiff = nd.tspec = nd.trans = 0.0;
254                                                /* transmitted ray */
255        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
256                RAY  lr;
257                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
258                        VCOPY(lr.rdir, nd.prdir);
259                        rayvalue(&lr);
260                        scalecolor(lr.rcol, nd.tspec);
261                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
262                        addcolor(r->rcol, lr.rcol);
263                        transtest *= bright(lr.rcol);
264                        transdist = r->rot + lr.rt;
265                }
266        }
262  
268        if (r->crtype & SHADOW)                 /* the rest is shadow */
269                return;
263                                                  /* diffuse reflection */
264          nd.rdiff = 1.0 - nd.trans - nd.rspec;
265  
266 <        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
274 <                return;                         /* 100% pure specular */
275 <
276 <        if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
266 >        if (r->ro != NULL && isflat(r->ro->otype))
267                  nd.specfl |= SP_FLAT;
268  
269          getacoords(r, &nd);                     /* set up coordinates */
270  
271 <        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU)))
271 >        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
272                  agaussamp(r, &nd);
273  
274          if (nd.rdiff > FTINY) {         /* ambient from this side */
275 <                ambient(ctmp, r);
275 >                ambient(ctmp, r, nd.pnorm);
276                  if (nd.specfl & SP_RBLT)
277                          scalecolor(ctmp, 1.0-nd.trans);
278                  else
# Line 291 | Line 281 | register RAY  *r;
281                  addcolor(r->rcol, ctmp);        /* add to returned color */
282          }
283          if (nd.tdiff > FTINY) {         /* ambient from other side */
284 +                FVECT  bnorm;
285 +
286                  flipsurface(r);
287 <                ambient(ctmp, r);
287 >                bnorm[0] = -nd.pnorm[0];
288 >                bnorm[1] = -nd.pnorm[1];
289 >                bnorm[2] = -nd.pnorm[2];
290 >                ambient(ctmp, r, bnorm);
291                  if (nd.specfl & SP_TBLT)
292                          scalecolor(ctmp, nd.trans);
293                  else
# Line 303 | Line 298 | register RAY  *r;
298          }
299                                          /* add direct component */
300          direct(r, diraniso, &nd);
301 <                                        /* check distance */
302 <        if (transtest > bright(r->rcol))
308 <                r->rt = transdist;
301 >
302 >        return(1);
303   }
304  
305  
# Line 327 | Line 321 | register ANISODAT  *np;
321                  np->specfl |= SP_BADU;
322                  return;
323          }
324 <        multv3(np->u, np->u, mf->f->xfm);
324 >        if (mf->f != &unitxf)
325 >                multv3(np->u, np->u, mf->f->xfm);
326          fcross(np->v, np->pnorm, np->u);
327          if (normalize(np->v) == 0.0) {
328                  objerror(np->mp, WARNING, "illegal orientation vector");
# Line 347 | Line 342 | register ANISODAT  *np;
342          FVECT  h;
343          double  rv[2];
344          double  d, sinp, cosp;
350        int  ntries;
345          register int  i;
346                                          /* compute reflection */
347          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
348                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
349                  dimlist[ndims++] = (int)np->mp;
350 <                for (ntries = 0; ntries < 10; ntries++) {
351 <                        dimlist[ndims] = ntries * 3601;
352 <                        d = urand(ilhash(dimlist,ndims+1)+samplendx);
353 <                        multisamp(rv, 2, d);
354 <                        d = 2.0*PI * rv[0];
355 <                        cosp = np->u_alpha * cos(d);
356 <                        sinp = np->v_alpha * sin(d);
357 <                        d = sqrt(cosp*cosp + sinp*sinp);
358 <                        cosp /= d;
359 <                        sinp /= d;
360 <                        rv[1] = 1.0 - specjitter*rv[1];
361 <                        if (rv[1] <= FTINY)
362 <                                d = 1.0;
363 <                        else
364 <                                d = sqrt(-log(rv[1]) /
365 <                                        (cosp*cosp/(np->u_alpha*np->u_alpha) +
366 <                                         sinp*sinp/(np->v_alpha*np->v_alpha)));
367 <                        for (i = 0; i < 3; i++)
368 <                                h[i] = np->pnorm[i] +
369 <                                        d*(cosp*np->u[i] + sinp*np->v[i]);
370 <                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
371 <                        for (i = 0; i < 3; i++)
372 <                                sr.rdir[i] = r->rdir[i] + d*h[i];
373 <                        if (DOT(sr.rdir, r->ron) > FTINY) {
374 <                                rayvalue(&sr);
375 <                                multcolor(sr.rcol, np->scolor);
382 <                                addcolor(r->rcol, sr.rcol);
383 <                                break;
384 <                        }
385 <                }
350 >                d = urand(ilhash(dimlist,ndims)+samplendx);
351 >                multisamp(rv, 2, d);
352 >                d = 2.0*PI * rv[0];
353 >                cosp = cos(d) * np->u_alpha;
354 >                sinp = sin(d) * np->v_alpha;
355 >                d = sqrt(cosp*cosp + sinp*sinp);
356 >                cosp /= d;
357 >                sinp /= d;
358 >                rv[1] = 1.0 - specjitter*rv[1];
359 >                if (rv[1] <= FTINY)
360 >                        d = 1.0;
361 >                else
362 >                        d = sqrt(-log(rv[1]) /
363 >                                (cosp*cosp/(np->u_alpha*np->u_alpha) +
364 >                                 sinp*sinp/(np->v_alpha*np->v_alpha)));
365 >                for (i = 0; i < 3; i++)
366 >                        h[i] = np->pnorm[i] +
367 >                                d*(cosp*np->u[i] + sinp*np->v[i]);
368 >                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
369 >                for (i = 0; i < 3; i++)
370 >                        sr.rdir[i] = r->rdir[i] + d*h[i];
371 >                if (DOT(sr.rdir, r->ron) <= FTINY)      /* penetration? */
372 >                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
373 >                rayvalue(&sr);
374 >                multcolor(sr.rcol, np->scolor);
375 >                addcolor(r->rcol, sr.rcol);
376                  ndims--;
377          }
378                                          /* compute transmission */
379 +        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
380 +                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
381 +                dimlist[ndims++] = (int)np->mp;
382 +                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
383 +                multisamp(rv, 2, d);
384 +                d = 2.0*PI * rv[0];
385 +                cosp = cos(d) * np->u_alpha;
386 +                sinp = sin(d) * np->v_alpha;
387 +                d = sqrt(cosp*cosp + sinp*sinp);
388 +                cosp /= d;
389 +                sinp /= d;
390 +                rv[1] = 1.0 - specjitter*rv[1];
391 +                if (rv[1] <= FTINY)
392 +                        d = 1.0;
393 +                else
394 +                        d = sqrt(-log(rv[1]) /
395 +                                (cosp*cosp/(np->u_alpha*np->u_alpha) +
396 +                                 sinp*sinp/(np->v_alpha*np->u_alpha)));
397 +                for (i = 0; i < 3; i++)
398 +                        sr.rdir[i] = np->prdir[i] +
399 +                                        d*(cosp*np->u[i] + sinp*np->v[i]);
400 +                if (DOT(sr.rdir, r->ron) < -FTINY)
401 +                        normalize(sr.rdir);             /* OK, normalize */
402 +                else
403 +                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
404 +                rayvalue(&sr);
405 +                scalecolor(sr.rcol, np->tspec);
406 +                multcolor(sr.rcol, np->mcolor);         /* modify by color */
407 +                addcolor(r->rcol, sr.rcol);
408 +                ndims--;
409 +        }
410   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines