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.7 by greg, Wed Jan 15 16:59:55 1992 UTC vs.
Revision 2.24 by greg, Mon Mar 8 12:37:18 1993 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 + static  agaussamp();
23 +
24   /*
25 < *      This anisotropic reflection model uses a variant on the
26 < *  exponential Gaussian used in normal.c.
25 > *      This routine implements the anisotropic Gaussian
26 > *  model described by Ward in Siggraph `92 article.
27   *      We orient the surface towards the incoming ray, so a single
28   *  surface can be used to represent an infinitely thin object.
29   *
# Line 39 | Line 41 | extern double  specjitter;             /* specular sampling jitte
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 71 | 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 94 | 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 104 | 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];
113                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 136 | 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 160 | Line 183 | register OBJREC  *m;
183   register RAY  *r;
184   {
185          ANISODAT  nd;
163        double  transtest, transdist;
186          double  dtmp;
187          COLOR  ctmp;
188          register int  i;
189                                                  /* easy shadow test */
190 <        if (r->crtype & SHADOW && m->otype != MAT_TRANS2)
190 >        if (r->crtype & SHADOW)
191                  return;
192  
193          if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
# Line 180 | Line 202 | register RAY  *r;
202          nd.specfl = 0;
203          nd.u_alpha = m->oargs.farg[4];
204          nd.v_alpha = m->oargs.farg[5];
205 <        if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY)
206 <                nd.specfl |= SP_PURE;
205 >        if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
206 >                objerror(m, USER, "roughness too small");
207                                                  /* reorient if necessary */
208          if (r->rod < 0.0)
209                  flipsurface(r);
# Line 191 | Line 213 | register RAY  *r;
213          if (nd.pdot < .001)
214                  nd.pdot = .001;                 /* non-zero for diraniso() */
215          multcolor(nd.mcolor, r->pcol);          /* modify material color */
194        transtest = 0;
216                                                  /* get specular component */
217          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
218                  nd.specfl |= SP_REFL;
# Line 208 | Line 229 | register RAY  *r;
229                  nd.rspec += (1.0-nd.rspec)*dtmp;
230                                                  /* check threshold */
231                  if (specthresh > FTINY &&
232 <                                ((specthresh >= 1.-FTINY ||
233 <                                specthresh + (.1 - .2*urand(8199+samplendx))
213 <                                        > nd.rspec)))
232 >                                (specthresh >= 1.-FTINY ||
233 >                                specthresh + .05 - .1*frandom() > nd.rspec))
234                          nd.specfl |= SP_RBLT;
235                                                  /* compute refl. direction */
236                  for (i = 0; i < 3; i++)
# Line 218 | Line 238 | register RAY  *r;
238                  if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
239                          for (i = 0; i < 3; i++)         /* safety measure */
240                                  nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
221
222                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
223                        RAY  lr;
224                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
225                                VCOPY(lr.rdir, nd.vrefl);
226                                rayvalue(&lr);
227                                multcolor(lr.rcol, nd.scolor);
228                                addcolor(r->rcol, lr.rcol);
229                        }
230                }
241          }
242                                                  /* compute transmission */
243 <        if (m->otype == MAT_TRANS) {
243 >        if (m->otype == MAT_TRANS2) {
244                  nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
245                  nd.tspec = nd.trans * m->oargs.farg[7];
246                  nd.tdiff = nd.trans - nd.tspec;
# Line 238 | Line 248 | register RAY  *r;
248                          nd.specfl |= SP_TRAN;
249                                                          /* check threshold */
250                          if (specthresh > FTINY &&
251 <                                        ((specthresh >= 1.-FTINY ||
252 <                                        specthresh +
243 <                                            (.1 - .2*urand(7241+samplendx))
244 <                                                > nd.tspec)))
251 >                                        (specthresh >= 1.-FTINY ||
252 >                                specthresh + .05 - .1*frandom() > nd.tspec))
253                                  nd.specfl |= SP_TBLT;
254 <                        if (r->crtype & SHADOW ||
247 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
254 >                        if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
255                                  VCOPY(nd.prdir, r->rdir);
249                                transtest = 2;
256                          } else {
257                                  for (i = 0; i < 3; i++)         /* perturb */
258 <                                        nd.prdir[i] = r->rdir[i] -
253 <                                                        0.5*r->pert[i];
258 >                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
259                                  if (DOT(nd.prdir, r->ron) < -FTINY)
260                                          normalize(nd.prdir);    /* OK */
261                                  else
# Line 259 | Line 264 | register RAY  *r;
264                  }
265          } else
266                  nd.tdiff = nd.tspec = nd.trans = 0.0;
262                                                /* transmitted ray */
263        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
264                RAY  lr;
265                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
266                        VCOPY(lr.rdir, nd.prdir);
267                        rayvalue(&lr);
268                        scalecolor(lr.rcol, nd.tspec);
269                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
270                        addcolor(r->rcol, lr.rcol);
271                        transtest *= bright(lr.rcol);
272                        transdist = r->rot + lr.rt;
273                }
274        }
267  
276        if (r->crtype & SHADOW)                 /* the rest is shadow */
277                return;
268                                                  /* diffuse reflection */
269          nd.rdiff = 1.0 - nd.trans - nd.rspec;
270  
271 <        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
272 <                return;                         /* 100% pure specular */
283 <
284 <        if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
271 >        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
272 >                        r->ro->otype == OBJ_RING))
273                  nd.specfl |= SP_FLAT;
274  
275          getacoords(r, &nd);                     /* set up coordinates */
276  
277 <        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU)))
277 >        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
278                  agaussamp(r, &nd);
279  
280          if (nd.rdiff > FTINY) {         /* ambient from this side */
# Line 311 | Line 299 | register RAY  *r;
299          }
300                                          /* add direct component */
301          direct(r, diraniso, &nd);
314                                        /* check distance */
315        if (transtest > bright(r->rcol))
316                r->rt = transdist;
302   }
303  
304  
# Line 335 | Line 320 | register ANISODAT  *np;
320                  np->specfl |= SP_BADU;
321                  return;
322          }
323 <        multv3(np->u, np->u, mf->f->xfm);
323 >        if (mf->f != &unitxf)
324 >                multv3(np->u, np->u, mf->f->xfm);
325          fcross(np->v, np->pnorm, np->u);
326          if (normalize(np->v) == 0.0) {
327                  objerror(np->mp, WARNING, "illegal orientation vector");
# Line 363 | Line 349 | register ANISODAT  *np;
349                  d = urand(ilhash(dimlist,ndims)+samplendx);
350                  multisamp(rv, 2, d);
351                  d = 2.0*PI * rv[0];
352 <                cosp = np->u_alpha * cos(d);
353 <                sinp = np->v_alpha * sin(d);
352 >                cosp = cos(d) * np->u_alpha;
353 >                sinp = sin(d) * np->v_alpha;
354                  d = sqrt(cosp*cosp + sinp*sinp);
355                  cosp /= d;
356                  sinp /= d;
# Line 395 | Line 381 | register ANISODAT  *np;
381                  d = urand(ilhash(dimlist,ndims)+1823+samplendx);
382                  multisamp(rv, 2, d);
383                  d = 2.0*PI * rv[0];
384 <                cosp = cos(d);
385 <                sinp = sin(d);
384 >                cosp = cos(d) * np->u_alpha;
385 >                sinp = sin(d) * np->v_alpha;
386 >                d = sqrt(cosp*cosp + sinp*sinp);
387 >                cosp /= d;
388 >                sinp /= d;
389                  rv[1] = 1.0 - specjitter*rv[1];
390                  if (rv[1] <= FTINY)
391                          d = 1.0;
392                  else
393                          d = sqrt(-log(rv[1]) /
394 <                                (cosp*cosp*4./(np->u_alpha*np->u_alpha) +
395 <                                 sinp*sinp*4./(np->v_alpha*np->v_alpha)));
394 >                                (cosp*cosp/(np->u_alpha*np->u_alpha) +
395 >                                 sinp*sinp/(np->v_alpha*np->u_alpha)));
396                  for (i = 0; i < 3; i++)
397                          sr.rdir[i] = np->prdir[i] +
398                                          d*(cosp*np->u[i] + sinp*np->v[i]);
# Line 412 | Line 401 | register ANISODAT  *np;
401                  else
402                          VCOPY(sr.rdir, np->prdir);      /* else no jitter */
403                  rayvalue(&sr);
404 <                multcolor(sr.rcol, np->scolor);
404 >                scalecolor(sr.rcol, np->tspec);
405 >                multcolor(sr.rcol, np->mcolor);         /* modify by color */
406                  addcolor(r->rcol, sr.rcol);
407                  ndims--;
408          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines