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.26 by greg, Thu Nov 18 09:42:55 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(), getacoords();
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 34 | Line 36 | extern double  specjitter;             /* specular sampling jitte
36   *  8  red      grn     blu     rspec   u-rough v-rough trans   tspec
37   */
38  
37 #define  BSPEC(m)       (6.0)           /* specularity parameter b */
38
39                                  /* specularity flags */
40   #define  SP_REFL        01              /* has reflected specular component */
41   #define  SP_TRAN        02              /* has transmitted specular */
42 < #define  SP_PURE        010             /* purely specular (zero roughness) */
43 < #define  SP_FLAT        020             /* reflecting surface is flat */
44 < #define  SP_RBLT        040             /* reflection below sample threshold */
45 < #define  SP_TBLT        0100            /* transmission below threshold */
46 < #define  SP_BADU        0200            /* bad u direction calculation */
42 > #define  SP_FLAT        04              /* reflecting surface is flat */
43 > #define  SP_RBLT        010             /* reflection below sample threshold */
44 > #define  SP_TBLT        020             /* transmission below threshold */
45 > #define  SP_BADU        040             /* bad u direction calculation */
46  
47   typedef struct {
48          OBJREC  *mp;            /* material pointer */
# Line 71 | Line 70 | FVECT  ldir;                   /* light source direction */
70   double  omega;                  /* light source size */
71   {
72          double  ldot;
73 <        double  dtmp, dtmp2;
73 >        double  dtmp, dtmp1, dtmp2;
74          FVECT  h;
75          double  au2, av2;
76          COLOR  ctmp;
# Line 94 | Line 93 | double  omega;                 /* light source size */
93                  scalecolor(ctmp, dtmp);
94                  addcolor(cval, ctmp);
95          }
96 <        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE|SP_BADU)) == SP_REFL) {
96 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) {
97                  /*
98                   *  Compute specular reflection coefficient using
99                   *  anisotropic gaussian distribution model.
# Line 104 | Line 103 | double  omega;                 /* light source size */
103                          au2 = av2 = omega/(4.0*PI);
104                  else
105                          au2 = av2 = 0.0;
106 <                au2 += np->u_alpha * np->u_alpha;
107 <                av2 += np->v_alpha * np->v_alpha;
106 >                au2 += np->u_alpha*np->u_alpha;
107 >                av2 += np->v_alpha*np->v_alpha;
108                                                  /* half vector */
109                  h[0] = ldir[0] - np->rp->rdir[0];
110                  h[1] = ldir[1] - np->rp->rdir[1];
111                  h[2] = ldir[2] - np->rp->rdir[2];
113                normalize(h);
112                                                  /* ellipse */
113 <                dtmp = DOT(np->u, h);
114 <                dtmp *= dtmp / au2;
113 >                dtmp1 = DOT(np->u, h);
114 >                dtmp1 *= dtmp1 / au2;
115                  dtmp2 = DOT(np->v, h);
116                  dtmp2 *= dtmp2 / av2;
117                                                  /* gaussian */
118 <                dtmp = (dtmp + dtmp2) / (1.0 + DOT(np->pnorm, h));
119 <                dtmp = exp(-2.0*dtmp) / (4.0*PI * sqrt(au2*av2));
118 >                dtmp = DOT(np->pnorm, h);
119 >                dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp);
120 >                dtmp = exp(-dtmp) * (0.25/PI)
121 >                                * sqrt(ldot/(np->pdot*au2*av2));
122                                                  /* worth using? */
123                  if (dtmp > FTINY) {
124                          copycolor(ctmp, np->scolor);
125 <                        dtmp *= omega / np->pdot;
125 >                        dtmp *= omega;
126                          scalecolor(ctmp, dtmp);
127                          addcolor(cval, ctmp);
128                  }
# Line 136 | Line 136 | double  omega;                 /* light source size */
136                  scalecolor(ctmp, dtmp);
137                  addcolor(cval, ctmp);
138          }
139 <        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE|SP_BADU)) == SP_TRAN) {
139 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) {
140                  /*
141                   *  Compute specular transmission.  Specular transmission
142                   *  is always modified by material color.
143                   */
144                                                  /* roughness + source */
145 +                au2 = av2 = omega / PI;
146 +                au2 += np->u_alpha*np->u_alpha;
147 +                av2 += np->v_alpha*np->v_alpha;
148 +                                                /* "half vector" */
149 +                h[0] = ldir[0] - np->prdir[0];
150 +                h[1] = ldir[1] - np->prdir[1];
151 +                h[2] = ldir[2] - np->prdir[2];
152 +                dtmp = DOT(h,h);
153 +                if (dtmp > FTINY*FTINY) {
154 +                        dtmp1 = DOT(h,np->pnorm);
155 +                        dtmp = 1.0 - dtmp1*dtmp1/dtmp;
156 +                        if (dtmp > FTINY*FTINY) {
157 +                                dtmp1 = DOT(h,np->u);
158 +                                dtmp1 *= dtmp1 / au2;
159 +                                dtmp2 = DOT(h,np->v);
160 +                                dtmp2 *= dtmp2 / av2;
161 +                                dtmp = (dtmp1 + dtmp2) / dtmp;
162 +                        }
163 +                } else
164 +                        dtmp = 0.0;
165                                                  /* gaussian */
166 <                dtmp = 0.0;
166 >                dtmp = exp(-dtmp) * (1.0/PI)
167 >                                * sqrt(-ldot/(np->pdot*au2*av2));
168                                                  /* worth using? */
169                  if (dtmp > FTINY) {
170                          copycolor(ctmp, np->mcolor);
171 <                        dtmp *= np->tspec * omega / np->pdot;
171 >                        dtmp *= np->tspec * omega;
172                          scalecolor(ctmp, dtmp);
173                          addcolor(cval, ctmp);
174                  }
# Line 160 | Line 181 | register OBJREC  *m;
181   register RAY  *r;
182   {
183          ANISODAT  nd;
163        double  transtest, transdist;
164        double  dtmp;
184          COLOR  ctmp;
185          register int  i;
186                                                  /* easy shadow test */
187 <        if (r->crtype & SHADOW && m->otype != MAT_TRANS2)
187 >        if (r->crtype & SHADOW)
188                  return;
189  
190          if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
# Line 180 | Line 199 | register RAY  *r;
199          nd.specfl = 0;
200          nd.u_alpha = m->oargs.farg[4];
201          nd.v_alpha = m->oargs.farg[5];
202 <        if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY)
203 <                nd.specfl |= SP_PURE;
202 >        if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
203 >                objerror(m, USER, "roughness too small");
204                                                  /* reorient if necessary */
205          if (r->rod < 0.0)
206                  flipsurface(r);
# Line 191 | Line 210 | register RAY  *r;
210          if (nd.pdot < .001)
211                  nd.pdot = .001;                 /* non-zero for diraniso() */
212          multcolor(nd.mcolor, r->pcol);          /* modify material color */
194        transtest = 0;
213                                                  /* get specular component */
214          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
215                  nd.specfl |= SP_REFL;
# Line 201 | Line 219 | register RAY  *r;
219                  else
220                          setcolor(nd.scolor, 1.0, 1.0, 1.0);
221                  scalecolor(nd.scolor, nd.rspec);
204                                                /* improved model */
205                dtmp = exp(-BSPEC(m)*nd.pdot);
206                for (i = 0; i < 3; i++)
207                        colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
208                nd.rspec += (1.0-nd.rspec)*dtmp;
222                                                  /* check threshold */
223 <                if (specthresh > FTINY &&
211 <                                ((specthresh >= 1.-FTINY ||
212 <                                specthresh + (.1 - .2*urand(8199+samplendx))
213 <                                        > nd.rspec)))
223 >                if (specthresh >= nd.rspec-FTINY)
224                          nd.specfl |= SP_RBLT;
225                                                  /* compute refl. direction */
226                  for (i = 0; i < 3; i++)
# Line 218 | Line 228 | register RAY  *r;
228                  if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
229                          for (i = 0; i < 3; i++)         /* safety measure */
230                                  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                }
231          }
232                                                  /* compute transmission */
233 <        if (m->otype == MAT_TRANS) {
233 >        if (m->otype == MAT_TRANS2) {
234                  nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
235                  nd.tspec = nd.trans * m->oargs.farg[7];
236                  nd.tdiff = nd.trans - nd.tspec;
237                  if (nd.tspec > FTINY) {
238                          nd.specfl |= SP_TRAN;
239                                                          /* check threshold */
240 <                        if (specthresh > FTINY &&
241 <                                        ((specthresh >= 1.-FTINY ||
242 <                                        specthresh +
243 <                                            (.1 - .2*urand(7241+samplendx))
244 <                                                > nd.tspec)))
240 >                        if (specthresh >= nd.tspec-FTINY)
241                                  nd.specfl |= SP_TBLT;
242 <                        if (r->crtype & SHADOW ||
247 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
242 >                        if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
243                                  VCOPY(nd.prdir, r->rdir);
249                                transtest = 2;
244                          } else {
245                                  for (i = 0; i < 3; i++)         /* perturb */
246 <                                        nd.prdir[i] = r->rdir[i] -
253 <                                                        0.5*r->pert[i];
246 >                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
247                                  if (DOT(nd.prdir, r->ron) < -FTINY)
248                                          normalize(nd.prdir);    /* OK */
249                                  else
# Line 259 | Line 252 | register RAY  *r;
252                  }
253          } else
254                  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        }
255  
276        if (r->crtype & SHADOW)                 /* the rest is shadow */
277                return;
256                                                  /* diffuse reflection */
257          nd.rdiff = 1.0 - nd.trans - nd.rspec;
258  
259 <        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
260 <                return;                         /* 100% pure specular */
283 <
284 <        if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
259 >        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
260 >                        r->ro->otype == OBJ_RING))
261                  nd.specfl |= SP_FLAT;
262  
263          getacoords(r, &nd);                     /* set up coordinates */
264  
265 <        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU)))
265 >        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
266                  agaussamp(r, &nd);
267  
268          if (nd.rdiff > FTINY) {         /* ambient from this side */
# Line 311 | Line 287 | register RAY  *r;
287          }
288                                          /* add direct component */
289          direct(r, diraniso, &nd);
314                                        /* check distance */
315        if (transtest > bright(r->rcol))
316                r->rt = transdist;
290   }
291  
292  
# Line 335 | Line 308 | register ANISODAT  *np;
308                  np->specfl |= SP_BADU;
309                  return;
310          }
311 <        multv3(np->u, np->u, mf->f->xfm);
311 >        if (mf->f != &unitxf)
312 >                multv3(np->u, np->u, mf->f->xfm);
313          fcross(np->v, np->pnorm, np->u);
314          if (normalize(np->v) == 0.0) {
315                  objerror(np->mp, WARNING, "illegal orientation vector");
# Line 363 | Line 337 | register ANISODAT  *np;
337                  d = urand(ilhash(dimlist,ndims)+samplendx);
338                  multisamp(rv, 2, d);
339                  d = 2.0*PI * rv[0];
340 <                cosp = np->u_alpha * cos(d);
341 <                sinp = np->v_alpha * sin(d);
340 >                cosp = cos(d) * np->u_alpha;
341 >                sinp = sin(d) * np->v_alpha;
342                  d = sqrt(cosp*cosp + sinp*sinp);
343                  cosp /= d;
344                  sinp /= d;
# Line 395 | Line 369 | register ANISODAT  *np;
369                  d = urand(ilhash(dimlist,ndims)+1823+samplendx);
370                  multisamp(rv, 2, d);
371                  d = 2.0*PI * rv[0];
372 <                cosp = cos(d);
373 <                sinp = sin(d);
372 >                cosp = cos(d) * np->u_alpha;
373 >                sinp = sin(d) * np->v_alpha;
374 >                d = sqrt(cosp*cosp + sinp*sinp);
375 >                cosp /= d;
376 >                sinp /= d;
377                  rv[1] = 1.0 - specjitter*rv[1];
378                  if (rv[1] <= FTINY)
379                          d = 1.0;
380                  else
381                          d = sqrt(-log(rv[1]) /
382 <                                (cosp*cosp*4./(np->u_alpha*np->u_alpha) +
383 <                                 sinp*sinp*4./(np->v_alpha*np->v_alpha)));
382 >                                (cosp*cosp/(np->u_alpha*np->u_alpha) +
383 >                                 sinp*sinp/(np->v_alpha*np->u_alpha)));
384                  for (i = 0; i < 3; i++)
385                          sr.rdir[i] = np->prdir[i] +
386                                          d*(cosp*np->u[i] + sinp*np->v[i]);
# Line 412 | Line 389 | register ANISODAT  *np;
389                  else
390                          VCOPY(sr.rdir, np->prdir);      /* else no jitter */
391                  rayvalue(&sr);
392 <                multcolor(sr.rcol, np->scolor);
392 >                scalecolor(sr.rcol, np->tspec);
393 >                multcolor(sr.rcol, np->mcolor);         /* modify by color */
394                  addcolor(r->rcol, sr.rcol);
395                  ndims--;
396          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines