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.6 by greg, Wed Jan 15 11:41:47 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 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,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 160 | Line 181 | register OBJREC  *m;
181   register RAY  *r;
182   {
183          ANISODAT  nd;
163        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 180 | 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 191 | 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 */
194        transtest = 0;
214                                                  /* get specular component */
215          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
216                  nd.specfl |= SP_REFL;
# Line 208 | Line 227 | register RAY  *r;
227                  nd.rspec += (1.0-nd.rspec)*dtmp;
228                                                  /* check threshold */
229                  if (specthresh > FTINY &&
230 <                                ((specthresh >= 1.-FTINY ||
231 <                                specthresh + (.1 - .2*urand(8199+samplendx))
213 <                                        > nd.rspec)))
230 >                                (specthresh >= 1.-FTINY ||
231 >                                specthresh + .05 - .1*frandom() > nd.rspec))
232                          nd.specfl |= SP_RBLT;
233                                                  /* compute refl. direction */
234                  for (i = 0; i < 3; i++)
# Line 218 | Line 236 | register RAY  *r;
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];
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                }
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;
# Line 238 | Line 246 | register RAY  *r;
246                          nd.specfl |= SP_TRAN;
247                                                          /* check threshold */
248                          if (specthresh > FTINY &&
249 <                                        ((specthresh >= 1.-FTINY ||
250 <                                        specthresh +
243 <                                            (.1 - .2*urand(7241+samplendx))
244 <                                                > nd.tspec)))
249 >                                        (specthresh >= 1.-FTINY ||
250 >                                specthresh + .05 - .1*frandom() > nd.tspec))
251                                  nd.specfl |= SP_TBLT;
252 <                        if (r->crtype & SHADOW ||
247 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
252 >                        if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
253                                  VCOPY(nd.prdir, r->rdir);
249                                transtest = 2;
254                          } else {
255                                  for (i = 0; i < 3; i++)         /* perturb */
256 <                                        nd.prdir[i] = r->rdir[i] -
253 <                                                        .75*r->pert[i];
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
# Line 259 | Line 262 | register RAY  *r;
262                  }
263          } else
264                  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        }
265  
276        if (r->crtype & SHADOW)                 /* the rest is shadow */
277                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 */
283 <
284 <        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 311 | Line 297 | register RAY  *r;
297          }
298                                          /* add direct component */
299          direct(r, diraniso, &nd);
314                                        /* check distance */
315        if (transtest > bright(r->rcol))
316                r->rt = transdist;
300   }
301  
302  
# Line 335 | 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 363 | Line 347 | register ANISODAT  *np;
347                  d = urand(ilhash(dimlist,ndims)+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);
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;
# Line 389 | Line 373 | register ANISODAT  *np;
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