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.4 by greg, Tue Jan 14 16:16:48 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 51 | Line 50 | typedef struct {
50          short  specfl;          /* specularity flags, defined above */
51          COLOR  mcolor;          /* color of this material */
52          COLOR  scolor;          /* color of specular component */
53 +        FVECT  vrefl;           /* vector in reflected direction */
54          FVECT  prdir;           /* vector in transmitted direction */
55          FVECT  u, v;            /* u and v vectors orienting anisotropy */
56          double  u_alpha;        /* u roughness */
# Line 70 | 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 93 | 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 103 | 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 135 | 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 159 | Line 179 | register OBJREC  *m;
179   register RAY  *r;
180   {
181          ANISODAT  nd;
162        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 179 | 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 190 | 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 */
193        transtest = 0;
212                                                  /* get specular component */
213          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
214                  nd.specfl |= SP_REFL;
# Line 206 | Line 224 | register RAY  *r;
224                          colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
225                  nd.rspec += (1.0-nd.rspec)*dtmp;
226                                                  /* check threshold */
227 <                if (nd.rspec <= specthresh+FTINY)
227 >                if (specthresh > FTINY &&
228 >                                (specthresh >= 1.-FTINY ||
229 >                                specthresh + .05 - .1*frandom() > nd.rspec))
230                          nd.specfl |= SP_RBLT;
231 <
232 <                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
233 <                        RAY  lr;
234 <                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
235 <                                for (i = 0; i < 3; i++)
236 <                                        lr.rdir[i] = r->rdir[i] +
217 <                                                2.0*nd.pdot*nd.pnorm[i];
218 <                                rayvalue(&lr);
219 <                                multcolor(lr.rcol, nd.scolor);
220 <                                addcolor(r->rcol, lr.rcol);
221 <                        }
222 <                }
231 >                                                /* compute refl. direction */
232 >                for (i = 0; i < 3; i++)
233 >                        nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
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];
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;
243                  if (nd.tspec > FTINY) {
244                          nd.specfl |= SP_TRAN;
245                                                          /* check threshold */
246 <                        if (nd.tspec <= specthresh+FTINY)
246 >                        if (specthresh > FTINY &&
247 >                                        (specthresh >= 1.-FTINY ||
248 >                                specthresh + .05 - .1*frandom() > nd.tspec))
249                                  nd.specfl |= SP_TBLT;
250 <                        if (r->crtype & SHADOW ||
235 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
250 >                        if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
251                                  VCOPY(nd.prdir, r->rdir);
237                                transtest = 2;
252                          } else {
253                                  for (i = 0; i < 3; i++)         /* perturb */
254 <                                        nd.prdir[i] = r->rdir[i] -
255 <                                                        .75*r->pert[i];
256 <                                normalize(nd.prdir);
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
258 >                                        VCOPY(nd.prdir, r->rdir);
259                          }
260                  }
261          } else
262                  nd.tdiff = nd.tspec = nd.trans = 0.0;
247                                                /* transmitted ray */
248        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
249                RAY  lr;
250                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
251                        VCOPY(lr.rdir, nd.prdir);
252                        rayvalue(&lr);
253                        scalecolor(lr.rcol, nd.tspec);
254                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
255                        addcolor(r->rcol, lr.rcol);
256                        transtest *= bright(lr.rcol);
257                        transdist = r->rot + lr.rt;
258                }
259        }
263  
261        if (r->crtype & SHADOW)                 /* the rest is shadow */
262                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 */
268 <
269 <        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 296 | Line 295 | register RAY  *r;
295          }
296                                          /* add direct component */
297          direct(r, diraniso, &nd);
299                                        /* check distance */
300        if (transtest > bright(r->rcol))
301                r->rt = transdist;
298   }
299  
300  
# Line 320 | 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 340 | Line 337 | register ANISODAT  *np;
337          FVECT  h;
338          double  rv[2];
339          double  d, sinp, cosp;
343        int  ntries;
340          register int  i;
341                                          /* compute reflection */
342          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
343                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
344                  dimlist[ndims++] = (int)np->mp;
345 <                for (ntries = 0; ntries < 10; ntries++) {
346 <                        dimlist[ndims] = ntries * 3601;
347 <                        d = urand(ilhash(dimlist,ndims+1)+samplendx);
348 <                        multisamp(rv, 2, d);
349 <                        d = 2.0*PI * rv[0];
350 <                        cosp = np->u_alpha * cos(d);
351 <                        sinp = np->v_alpha * sin(d);
352 <                        d = sqrt(cosp*cosp + sinp*sinp);
353 <                        cosp /= d;
354 <                        sinp /= d;
355 <                        rv[1] = 1.0 - specjitter*rv[1];
356 <                        if (rv[1] <= FTINY)
357 <                                d = 1.0;
358 <                        else
359 <                                d = sqrt(-log(rv[1]) /
360 <                                        (cosp*cosp/(np->u_alpha*np->u_alpha) +
361 <                                         sinp*sinp/(np->v_alpha*np->v_alpha)));
362 <                        for (i = 0; i < 3; i++)
363 <                                h[i] = np->pnorm[i] +
364 <                                        d*(cosp*np->u[i] + sinp*np->v[i]);
365 <                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
366 <                        for (i = 0; i < 3; i++)
367 <                                sr.rdir[i] = r->rdir[i] + d*h[i];
368 <                        if (DOT(sr.rdir, r->ron) > FTINY) {
369 <                                rayvalue(&sr);
370 <                                multcolor(sr.rcol, np->scolor);
375 <                                addcolor(r->rcol, sr.rcol);
376 <                                break;
377 <                        }
378 <                }
345 >                d = urand(ilhash(dimlist,ndims)+samplendx);
346 >                multisamp(rv, 2, d);
347 >                d = 2.0*PI * rv[0];
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;
353 >                rv[1] = 1.0 - specjitter*rv[1];
354 >                if (rv[1] <= FTINY)
355 >                        d = 1.0;
356 >                else
357 >                        d = sqrt(-log(rv[1]) /
358 >                                (cosp*cosp/(np->u_alpha*np->u_alpha) +
359 >                                 sinp*sinp/(np->v_alpha*np->v_alpha)));
360 >                for (i = 0; i < 3; i++)
361 >                        h[i] = np->pnorm[i] +
362 >                                d*(cosp*np->u[i] + sinp*np->v[i]);
363 >                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
364 >                for (i = 0; i < 3; i++)
365 >                        sr.rdir[i] = r->rdir[i] + d*h[i];
366 >                if (DOT(sr.rdir, r->ron) <= FTINY)      /* penetration? */
367 >                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
368 >                rayvalue(&sr);
369 >                multcolor(sr.rcol, np->scolor);
370 >                addcolor(r->rcol, sr.rcol);
371                  ndims--;
372          }
373                                          /* compute transmission */
374 +        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
375 +                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
376 +                dimlist[ndims++] = (int)np->mp;
377 +                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
378 +                multisamp(rv, 2, d);
379 +                d = 2.0*PI * rv[0];
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/(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]);
395 +                if (DOT(sr.rdir, r->ron) < -FTINY)
396 +                        normalize(sr.rdir);             /* OK, normalize */
397 +                else
398 +                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
399 +                rayvalue(&sr);
400 +                scalecolor(sr.rcol, np->tspec);
401 +                multcolor(sr.rcol, np->mcolor);         /* modify by color */
402 +                addcolor(r->rcol, sr.rcol);
403 +                ndims--;
404 +        }
405   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines