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.19 by greg, Wed May 20 11:01:40 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,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*dtmp1 / au2;
159 +                                dtmp2 = DOT(h,np->v);
160 +                                dtmp2 = 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/(4.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 159 | Line 181 | register OBJREC  *m;
181   register RAY  *r;
182   {
183          ANISODAT  nd;
162        double  transtest, transdist;
184          double  dtmp;
185          COLOR  ctmp;
186          register int  i;
187                                                  /* easy shadow test */
188 <        if (r->crtype & SHADOW && m->otype != MAT_TRANS2)
188 >        if (r->crtype & SHADOW)
189                  return;
190  
191          if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
# Line 179 | Line 200 | register RAY  *r;
200          nd.specfl = 0;
201          nd.u_alpha = m->oargs.farg[4];
202          nd.v_alpha = m->oargs.farg[5];
203 <        if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY)
204 <                nd.specfl |= SP_PURE;
203 >        if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
204 >                objerror(m, USER, "roughness too small");
205                                                  /* reorient if necessary */
206          if (r->rod < 0.0)
207                  flipsurface(r);
# Line 190 | Line 211 | register RAY  *r;
211          if (nd.pdot < .001)
212                  nd.pdot = .001;                 /* non-zero for diraniso() */
213          multcolor(nd.mcolor, r->pcol);          /* modify material color */
193        transtest = 0;
214                                                  /* get specular component */
215          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
216                  nd.specfl |= SP_REFL;
# Line 206 | Line 226 | register RAY  *r;
226                          colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
227                  nd.rspec += (1.0-nd.rspec)*dtmp;
228                                                  /* check threshold */
229 <                if (nd.rspec <= specthresh+FTINY)
229 >                if (specthresh > FTINY &&
230 >                                (specthresh >= 1.-FTINY ||
231 >                                specthresh + .05 - .1*frandom() > nd.rspec))
232                          nd.specfl |= SP_RBLT;
233 <
234 <                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
235 <                        RAY  lr;
236 <                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
237 <                                for (i = 0; i < 3; i++)
238 <                                        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 <                }
233 >                                                /* compute refl. direction */
234 >                for (i = 0; i < 3; i++)
235 >                        nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
236 >                if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
237 >                        for (i = 0; i < 3; i++)         /* safety measure */
238 >                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
239          }
240                                                  /* compute transmission */
241 <        if (m->otype == MAT_TRANS) {
241 >        if (m->otype == MAT_TRANS2) {
242                  nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
243                  nd.tspec = nd.trans * m->oargs.farg[7];
244                  nd.tdiff = nd.trans - nd.tspec;
245                  if (nd.tspec > FTINY) {
246                          nd.specfl |= SP_TRAN;
247                                                          /* check threshold */
248 <                        if (nd.tspec <= specthresh+FTINY)
248 >                        if (specthresh > FTINY &&
249 >                                        (specthresh >= 1.-FTINY ||
250 >                                specthresh + .05 - .1*frandom() > nd.tspec))
251                                  nd.specfl |= SP_TBLT;
252 <                        if (r->crtype & SHADOW ||
235 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
252 >                        if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
253                                  VCOPY(nd.prdir, r->rdir);
237                                transtest = 2;
254                          } else {
255                                  for (i = 0; i < 3; i++)         /* perturb */
256 <                                        nd.prdir[i] = r->rdir[i] -
257 <                                                        .75*r->pert[i];
258 <                                normalize(nd.prdir);
256 >                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
257 >                                if (DOT(nd.prdir, r->ron) < -FTINY)
258 >                                        normalize(nd.prdir);    /* OK */
259 >                                else
260 >                                        VCOPY(nd.prdir, r->rdir);
261                          }
262                  }
263          } else
264                  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        }
265  
261        if (r->crtype & SHADOW)                 /* the rest is shadow */
262                return;
266                                                  /* diffuse reflection */
267          nd.rdiff = 1.0 - nd.trans - nd.rspec;
268  
269 <        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
270 <                return;                         /* 100% pure specular */
268 <
269 <        if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
269 >        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
270 >                        r->ro->otype == OBJ_RING))
271                  nd.specfl |= SP_FLAT;
272  
273          getacoords(r, &nd);                     /* set up coordinates */
274  
275 <        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU)))
275 >        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
276                  agaussamp(r, &nd);
277  
278          if (nd.rdiff > FTINY) {         /* ambient from this side */
# Line 296 | Line 297 | register RAY  *r;
297          }
298                                          /* add direct component */
299          direct(r, diraniso, &nd);
299                                        /* check distance */
300        if (transtest > bright(r->rcol))
301                r->rt = transdist;
300   }
301  
302  
# Line 320 | Line 318 | register ANISODAT  *np;
318                  np->specfl |= SP_BADU;
319                  return;
320          }
321 <        multv3(np->u, np->u, mf->f->xfm);
321 >        if (mf->f != &unitxf)
322 >                multv3(np->u, np->u, mf->f->xfm);
323          fcross(np->v, np->pnorm, np->u);
324          if (normalize(np->v) == 0.0) {
325                  objerror(np->mp, WARNING, "illegal orientation vector");
# Line 340 | Line 339 | register ANISODAT  *np;
339          FVECT  h;
340          double  rv[2];
341          double  d, sinp, cosp;
343        int  ntries;
342          register int  i;
343                                          /* compute reflection */
344          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
345                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
346                  dimlist[ndims++] = (int)np->mp;
347 <                for (ntries = 0; ntries < 10; ntries++) {
348 <                        dimlist[ndims] = ntries * 3601;
349 <                        d = urand(ilhash(dimlist,ndims+1)+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);
354 <                        d = sqrt(cosp*cosp + sinp*sinp);
355 <                        cosp /= d;
356 <                        sinp /= d;
357 <                        rv[1] = 1.0 - specjitter*rv[1];
358 <                        if (rv[1] <= FTINY)
359 <                                d = 1.0;
360 <                        else
361 <                                d = sqrt(-log(rv[1]) /
362 <                                        (cosp*cosp/(np->u_alpha*np->u_alpha) +
363 <                                         sinp*sinp/(np->v_alpha*np->v_alpha)));
364 <                        for (i = 0; i < 3; i++)
365 <                                h[i] = np->pnorm[i] +
366 <                                        d*(cosp*np->u[i] + sinp*np->v[i]);
367 <                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
368 <                        for (i = 0; i < 3; i++)
369 <                                sr.rdir[i] = r->rdir[i] + d*h[i];
370 <                        if (DOT(sr.rdir, r->ron) > FTINY) {
371 <                                rayvalue(&sr);
372 <                                multcolor(sr.rcol, np->scolor);
375 <                                addcolor(r->rcol, sr.rcol);
376 <                                break;
377 <                        }
378 <                }
347 >                d = urand(ilhash(dimlist,ndims)+samplendx);
348 >                multisamp(rv, 2, d);
349 >                d = 2.0*PI * rv[0];
350 >                cosp = cos(d) * np->u_alpha;
351 >                sinp = sin(d) * np->v_alpha;
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)      /* penetration? */
369 >                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
370 >                rayvalue(&sr);
371 >                multcolor(sr.rcol, np->scolor);
372 >                addcolor(r->rcol, sr.rcol);
373                  ndims--;
374          }
375                                          /* compute transmission */
376 +        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
377 +                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
378 +                dimlist[ndims++] = (int)np->mp;
379 +                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
380 +                multisamp(rv, 2, d);
381 +                d = 2.0*PI * rv[0];
382 +                cosp = cos(d) * np->u_alpha;
383 +                sinp = sin(d) * np->v_alpha;
384 +                d = sqrt(cosp*cosp + sinp*sinp);
385 +                cosp /= d;
386 +                sinp /= d;
387 +                rv[1] = 1.0 - specjitter*rv[1];
388 +                if (rv[1] <= FTINY)
389 +                        d = 1.0;
390 +                else
391 +                        d = sqrt(-log(rv[1]) /
392 +                                (cosp*cosp/(np->u_alpha*np->u_alpha) +
393 +                                 sinp*sinp/(np->v_alpha*np->u_alpha)));
394 +                for (i = 0; i < 3; i++)
395 +                        sr.rdir[i] = np->prdir[i] +
396 +                                        d*(cosp*np->u[i] + sinp*np->v[i]);
397 +                if (DOT(sr.rdir, r->ron) < -FTINY)
398 +                        normalize(sr.rdir);             /* OK, normalize */
399 +                else
400 +                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
401 +                rayvalue(&sr);
402 +                scalecolor(sr.rcol, np->tspec);
403 +                multcolor(sr.rcol, np->mcolor);         /* modify by color */
404 +                addcolor(r->rcol, sr.rcol);
405 +                ndims--;
406 +        }
407   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines