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.8 by greg, Thu Jan 16 12:05:32 1992 UTC vs.
Revision 2.18 by greg, Tue May 19 17:09:06 1992 UTC

# Line 39 | Line 39 | extern double  specjitter;             /* specular sampling jitte
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];
112                  normalize(h);
113                                                  /* ellipse */
114 <                dtmp = DOT(np->u, h);
115 <                dtmp *= dtmp / au2;
114 >                dtmp1 = DOT(np->u, h);
115 >                dtmp1 *= dtmp1 / au2;
116                  dtmp2 = DOT(np->v, h);
117                  dtmp2 *= dtmp2 / av2;
118                                                  /* gaussian */
119 <                dtmp = (dtmp + dtmp2) / (1.0 + DOT(np->pnorm, h));
120 <                dtmp = exp(-2.0*dtmp) / (4.0*PI * sqrt(au2*av2));
119 >                dtmp = (dtmp1 + dtmp2) / (1.0 + DOT(np->pnorm, h));
120 >                dtmp = exp(-2.0*dtmp) * 1.0/(4.0*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,np->pnorm);
153 +                dtmp = DOT(h,h) - dtmp*dtmp;
154 +                if (dtmp > FTINY*FTINY) {
155 +                        dtmp1 = DOT(h,np->u);
156 +                        dtmp1 = dtmp1*dtmp1 / (au2*dtmp);
157 +                        dtmp2 = DOT(h,np->v);
158 +                        dtmp2 = dtmp2*dtmp2 / (av2*dtmp);
159 +                        dtmp = 2. - 2.*DOT(ldir,np->prdir);
160 +                        dtmp *= dtmp1 + dtmp2;
161 +                } else
162 +                        dtmp = 0.0;
163                                                  /* gaussian */
164 <                dtmp = 0.0;
164 >                dtmp = exp(-dtmp) * 1.0/(4.0*PI)
165 >                                * sqrt(-ldot/(np->pdot*au2*av2));
166                                                  /* worth using? */
167                  if (dtmp > FTINY) {
168                          copycolor(ctmp, np->mcolor);
169 <                        dtmp *= np->tspec * omega / np->pdot;
169 >                        dtmp *= np->tspec * omega;
170                          scalecolor(ctmp, dtmp);
171                          addcolor(cval, ctmp);
172                  }
# Line 160 | Line 179 | register OBJREC  *m;
179   register RAY  *r;
180   {
181          ANISODAT  nd;
163        double  transtest, transdist;
182          double  dtmp;
183          COLOR  ctmp;
184          register int  i;
185                                                  /* easy shadow test */
186 <        if (r->crtype & SHADOW && m->otype != MAT_TRANS2)
186 >        if (r->crtype & SHADOW)
187                  return;
188  
189          if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
# Line 180 | Line 198 | register RAY  *r;
198          nd.specfl = 0;
199          nd.u_alpha = m->oargs.farg[4];
200          nd.v_alpha = m->oargs.farg[5];
201 <        if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY)
202 <                nd.specfl |= SP_PURE;
201 >        if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
202 >                objerror(m, USER, "roughness too small");
203                                                  /* reorient if necessary */
204          if (r->rod < 0.0)
205                  flipsurface(r);
# Line 191 | Line 209 | register RAY  *r;
209          if (nd.pdot < .001)
210                  nd.pdot = .001;                 /* non-zero for diraniso() */
211          multcolor(nd.mcolor, r->pcol);          /* modify material color */
194        transtest = 0;
212                                                  /* get specular component */
213          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
214                  nd.specfl |= SP_REFL;
# Line 208 | Line 225 | register RAY  *r;
225                  nd.rspec += (1.0-nd.rspec)*dtmp;
226                                                  /* check threshold */
227                  if (specthresh > FTINY &&
228 <                                ((specthresh >= 1.-FTINY ||
229 <                                specthresh + (.05 - .1*urand(8199+samplendx))
213 <                                        > nd.rspec)))
228 >                                (specthresh >= 1.-FTINY ||
229 >                                specthresh + .05 - .1*frandom() > nd.rspec))
230                          nd.specfl |= SP_RBLT;
231                                                  /* compute refl. direction */
232                  for (i = 0; i < 3; i++)
# Line 218 | Line 234 | register RAY  *r;
234                  if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
235                          for (i = 0; i < 3; i++)         /* safety measure */
236                                  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                }
237          }
238                                                  /* compute transmission */
239 <        if (m->otype == MAT_TRANS) {
239 >        if (m->otype == MAT_TRANS2) {
240                  nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
241                  nd.tspec = nd.trans * m->oargs.farg[7];
242                  nd.tdiff = nd.trans - nd.tspec;
# Line 238 | Line 244 | register RAY  *r;
244                          nd.specfl |= SP_TRAN;
245                                                          /* check threshold */
246                          if (specthresh > FTINY &&
247 <                                        ((specthresh >= 1.-FTINY ||
248 <                                        specthresh +
243 <                                            (.05 - .1*urand(7241+samplendx))
244 <                                                > nd.tspec)))
247 >                                        (specthresh >= 1.-FTINY ||
248 >                                specthresh + .05 - .1*frandom() > nd.tspec))
249                                  nd.specfl |= SP_TBLT;
250 <                        if (r->crtype & SHADOW ||
247 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
250 >                        if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
251                                  VCOPY(nd.prdir, r->rdir);
249                                transtest = 2;
252                          } else {
253                                  for (i = 0; i < 3; i++)         /* perturb */
254 <                                        nd.prdir[i] = r->rdir[i] -
253 <                                                        0.5*r->pert[i];
254 >                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
255                                  if (DOT(nd.prdir, r->ron) < -FTINY)
256                                          normalize(nd.prdir);    /* OK */
257                                  else
# Line 259 | Line 260 | register RAY  *r;
260                  }
261          } else
262                  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        }
263  
276        if (r->crtype & SHADOW)                 /* the rest is shadow */
277                return;
264                                                  /* diffuse reflection */
265          nd.rdiff = 1.0 - nd.trans - nd.rspec;
266  
267 <        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
268 <                return;                         /* 100% pure specular */
283 <
284 <        if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
267 >        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
268 >                        r->ro->otype == OBJ_RING))
269                  nd.specfl |= SP_FLAT;
270  
271          getacoords(r, &nd);                     /* set up coordinates */
272  
273 <        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU)))
273 >        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
274                  agaussamp(r, &nd);
275  
276          if (nd.rdiff > FTINY) {         /* ambient from this side */
# Line 311 | Line 295 | register RAY  *r;
295          }
296                                          /* add direct component */
297          direct(r, diraniso, &nd);
314                                        /* check distance */
315        if (transtest > bright(r->rcol))
316                r->rt = transdist;
298   }
299  
300  
# Line 335 | Line 316 | register ANISODAT  *np;
316                  np->specfl |= SP_BADU;
317                  return;
318          }
319 <        multv3(np->u, np->u, mf->f->xfm);
319 >        if (mf->f != &unitxf)
320 >                multv3(np->u, np->u, mf->f->xfm);
321          fcross(np->v, np->pnorm, np->u);
322          if (normalize(np->v) == 0.0) {
323                  objerror(np->mp, WARNING, "illegal orientation vector");
# Line 363 | Line 345 | register ANISODAT  *np;
345                  d = urand(ilhash(dimlist,ndims)+samplendx);
346                  multisamp(rv, 2, d);
347                  d = 2.0*PI * rv[0];
348 <                cosp = np->u_alpha * cos(d);
349 <                sinp = np->v_alpha * sin(d);
348 >                cosp = cos(d) * np->u_alpha;
349 >                sinp = sin(d) * np->v_alpha;
350                  d = sqrt(cosp*cosp + sinp*sinp);
351                  cosp /= d;
352                  sinp /= d;
# Line 395 | Line 377 | register ANISODAT  *np;
377                  d = urand(ilhash(dimlist,ndims)+1823+samplendx);
378                  multisamp(rv, 2, d);
379                  d = 2.0*PI * rv[0];
380 <                cosp = cos(d);
381 <                sinp = sin(d);
380 >                cosp = cos(d) * np->u_alpha;
381 >                sinp = sin(d) * np->v_alpha;
382 >                d = sqrt(cosp*cosp + sinp*sinp);
383 >                cosp /= d;
384 >                sinp /= d;
385                  rv[1] = 1.0 - specjitter*rv[1];
386                  if (rv[1] <= FTINY)
387                          d = 1.0;
388                  else
389                          d = sqrt(-log(rv[1]) /
390 <                                (cosp*cosp*4./(np->u_alpha*np->u_alpha) +
391 <                                 sinp*sinp*4./(np->v_alpha*np->v_alpha)));
390 >                                (cosp*cosp/(np->u_alpha*np->u_alpha) +
391 >                                 sinp*sinp/(np->v_alpha*np->u_alpha)));
392                  for (i = 0; i < 3; i++)
393                          sr.rdir[i] = np->prdir[i] +
394                                          d*(cosp*np->u[i] + sinp*np->v[i]);
# Line 412 | Line 397 | register ANISODAT  *np;
397                  else
398                          VCOPY(sr.rdir, np->prdir);      /* else no jitter */
399                  rayvalue(&sr);
400 <                multcolor(sr.rcol, np->scolor);
400 >                scalecolor(sr.rcol, np->tspec);
401 >                multcolor(sr.rcol, np->mcolor);         /* modify by color */
402                  addcolor(r->rcol, sr.rcol);
403                  ndims--;
404          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines