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.2 by greg, Sat Jan 4 23:36:40 1992 UTC vs.
Revision 2.13 by greg, Mon Apr 20 09:25:47 1992 UTC

# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ LBL";
16  
17   #include  "random.h"
18  
19 + extern double  specthresh;              /* specular sampling threshold */
20 + extern double  specjitter;              /* specular sampling jitter */
21 +
22   /*
23   *      This anisotropic reflection model uses a variant on the
24   *  exponential Gaussian used in normal.c.
# Line 31 | Line 34 | static char SCCSid[] = "$SunId$ LBL";
34   *  8  red      grn     blu     rspec   u-rough v-rough trans   tspec
35   */
36  
34 #define  BSPEC(m)       (6.0)           /* specularity parameter b */
35
37                                  /* specularity flags */
38   #define  SP_REFL        01              /* has reflected specular component */
39   #define  SP_TRAN        02              /* has transmitted specular */
40 < #define  SP_PURE        010             /* purely specular (zero roughness) */
41 < #define  SP_BADU        020             /* bad u direction calculation */
42 < #define  SP_FLAT        040             /* reflecting surface is flat */
40 > #define  SP_FLAT        04              /* reflecting surface is flat */
41 > #define  SP_RBLT        010             /* reflection below sample threshold */
42 > #define  SP_TBLT        020             /* transmission below threshold */
43 > #define  SP_BADU        040             /* bad u direction calculation */
44  
45   typedef struct {
46          OBJREC  *mp;            /* material pointer */
# Line 46 | Line 48 | typedef struct {
48          short  specfl;          /* specularity flags, defined above */
49          COLOR  mcolor;          /* color of this material */
50          COLOR  scolor;          /* color of specular component */
51 +        FVECT  vrefl;           /* vector in reflected direction */
52          FVECT  prdir;           /* vector in transmitted direction */
53          FVECT  u, v;            /* u and v vectors orienting anisotropy */
54          double  u_alpha;        /* u roughness */
# Line 88 | Line 91 | double  omega;                 /* light source size */
91                  scalecolor(ctmp, dtmp);
92                  addcolor(cval, ctmp);
93          }
94 <        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE|SP_BADU)) == SP_REFL) {
94 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) {
95                  /*
96                   *  Compute specular reflection coefficient using
97                   *  anisotropic gaussian distribution model.
# Line 116 | Line 119 | double  omega;                 /* light source size */
119                                                  /* worth using? */
120                  if (dtmp > FTINY) {
121                          copycolor(ctmp, np->scolor);
122 <                        dtmp *= omega / np->pdot;
122 >                        dtmp *= omega * sqrt(ldot/np->pdot);
123                          scalecolor(ctmp, dtmp);
124                          addcolor(cval, ctmp);
125                  }
# Line 130 | Line 133 | double  omega;                 /* light source size */
133                  scalecolor(ctmp, dtmp);
134                  addcolor(cval, ctmp);
135          }
136 <        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE|SP_BADU)) == SP_TRAN) {
136 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) {
137                  /*
138                   *  Compute specular transmission.  Specular transmission
139                   *  is always modified by material color.
# Line 141 | Line 144 | double  omega;                 /* light source size */
144                                                  /* worth using? */
145                  if (dtmp > FTINY) {
146                          copycolor(ctmp, np->mcolor);
147 <                        dtmp *= np->tspec * omega / np->pdot;
147 >                        dtmp *= np->tspec * omega * sqrt(ldot/np->pdot);
148                          scalecolor(ctmp, dtmp);
149                          addcolor(cval, ctmp);
150                  }
# Line 154 | Line 157 | register OBJREC  *m;
157   register RAY  *r;
158   {
159          ANISODAT  nd;
157        double  transtest, transdist;
160          double  dtmp;
161          COLOR  ctmp;
162          register int  i;
163                                                  /* easy shadow test */
164 <        if (r->crtype & SHADOW && m->otype != MAT_TRANS2)
164 >        if (r->crtype & SHADOW)
165                  return;
166  
167          if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
# Line 174 | Line 176 | register RAY  *r;
176          nd.specfl = 0;
177          nd.u_alpha = m->oargs.farg[4];
178          nd.v_alpha = m->oargs.farg[5];
179 <        if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY)
180 <                nd.specfl |= SP_PURE;
179 >        if (nd.u_alpha < 1e-6 || nd.v_alpha <= 1e-6)
180 >                objerror(m, USER, "roughness too small");
181                                                  /* reorient if necessary */
182          if (r->rod < 0.0)
183                  flipsurface(r);
# Line 185 | Line 187 | register RAY  *r;
187          if (nd.pdot < .001)
188                  nd.pdot = .001;                 /* non-zero for diraniso() */
189          multcolor(nd.mcolor, r->pcol);          /* modify material color */
188        transtest = 0;
190                                                  /* get specular component */
191          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
192                  nd.specfl |= SP_REFL;
# Line 195 | Line 196 | register RAY  *r;
196                  else
197                          setcolor(nd.scolor, 1.0, 1.0, 1.0);
198                  scalecolor(nd.scolor, nd.rspec);
199 <                                                /* improved model */
200 <                dtmp = exp(-BSPEC(m)*nd.pdot);
199 >                                                /* check threshold */
200 >                if (specthresh > FTINY &&
201 >                                (specthresh >= 1.-FTINY ||
202 >                                specthresh > nd.rspec))
203 >                        nd.specfl |= SP_RBLT;
204 >                                                /* compute refl. direction */
205                  for (i = 0; i < 3; i++)
206 <                        colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
207 <                nd.rspec += (1.0-nd.rspec)*dtmp;
208 <
209 <                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
205 <                        RAY  lr;
206 <                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
207 <                                for (i = 0; i < 3; i++)
208 <                                        lr.rdir[i] = r->rdir[i] +
209 <                                                2.0*nd.pdot*nd.pnorm[i];
210 <                                rayvalue(&lr);
211 <                                multcolor(lr.rcol, nd.scolor);
212 <                                addcolor(r->rcol, lr.rcol);
213 <                        }
214 <                }
206 >                        nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
207 >                if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
208 >                        for (i = 0; i < 3; i++)         /* safety measure */
209 >                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
210          }
211                                                  /* compute transmission */
212          if (m->otype == MAT_TRANS) {
# Line 220 | Line 215 | register RAY  *r;
215                  nd.tdiff = nd.trans - nd.tspec;
216                  if (nd.tspec > FTINY) {
217                          nd.specfl |= SP_TRAN;
218 <                        if (r->crtype & SHADOW ||
219 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
218 >                                                        /* check threshold */
219 >                        if (specthresh > FTINY &&
220 >                                        (specthresh >= 1.-FTINY ||
221 >                                        specthresh > nd.tspec))
222 >                                nd.specfl |= SP_TBLT;
223 >                        if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
224                                  VCOPY(nd.prdir, r->rdir);
226                                transtest = 2;
225                          } else {
226                                  for (i = 0; i < 3; i++)         /* perturb */
227                                          nd.prdir[i] = r->rdir[i] -
228 <                                                        .75*r->pert[i];
229 <                                normalize(nd.prdir);
228 >                                                        0.5*r->pert[i];
229 >                                if (DOT(nd.prdir, r->ron) < -FTINY)
230 >                                        normalize(nd.prdir);    /* OK */
231 >                                else
232 >                                        VCOPY(nd.prdir, r->rdir);
233                          }
234                  }
235          } else
236                  nd.tdiff = nd.tspec = nd.trans = 0.0;
236                                                /* transmitted ray */
237        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
238                RAY  lr;
239                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
240                        VCOPY(lr.rdir, nd.prdir);
241                        rayvalue(&lr);
242                        scalecolor(lr.rcol, nd.tspec);
243                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
244                        addcolor(r->rcol, lr.rcol);
245                        transtest *= bright(lr.rcol);
246                        transdist = r->rot + lr.rt;
247                }
248        }
237  
250        if (r->crtype & SHADOW)                 /* the rest is shadow */
251                return;
238                                                  /* diffuse reflection */
239          nd.rdiff = 1.0 - nd.trans - nd.rspec;
240  
241 <        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
242 <                return;                         /* 100% pure specular */
241 >        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
242 >                        r->ro->otype == OBJ_RING))
243 >                nd.specfl |= SP_FLAT;
244  
245          getacoords(r, &nd);                     /* set up coordinates */
246  
247 <        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU)))
247 >        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
248                  agaussamp(r, &nd);
249  
250          if (nd.rdiff > FTINY) {         /* ambient from this side */
251                  ambient(ctmp, r);
252 <                scalecolor(ctmp, nd.rdiff);
252 >                if (nd.specfl & SP_RBLT)
253 >                        scalecolor(ctmp, 1.0-nd.trans);
254 >                else
255 >                        scalecolor(ctmp, nd.rdiff);
256                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
257                  addcolor(r->rcol, ctmp);        /* add to returned color */
258          }
259          if (nd.tdiff > FTINY) {         /* ambient from other side */
260                  flipsurface(r);
261                  ambient(ctmp, r);
262 <                scalecolor(ctmp, nd.tdiff);
262 >                if (nd.specfl & SP_TBLT)
263 >                        scalecolor(ctmp, nd.trans);
264 >                else
265 >                        scalecolor(ctmp, nd.tdiff);
266                  multcolor(ctmp, nd.mcolor);     /* modified by color */
267                  addcolor(r->rcol, ctmp);
268                  flipsurface(r);
269          }
270                                          /* add direct component */
271          direct(r, diraniso, &nd);
279                                        /* check distance */
280        if (transtest > bright(r->rcol))
281                r->rt = transdist;
272   }
273  
274  
# Line 320 | Line 310 | register ANISODAT  *np;
310          FVECT  h;
311          double  rv[2];
312          double  d, sinp, cosp;
323        int  confuse;
313          register int  i;
314                                          /* compute reflection */
315 <        if (np->specfl & SP_REFL &&
315 >        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
316                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
328                confuse = 0;
317                  dimlist[ndims++] = (int)np->mp;
318 <        refagain:
331 <                dimlist[ndims] = confuse += 3601;
332 <                d = urand(ilhash(dimlist,ndims+1)+samplendx);
318 >                d = urand(ilhash(dimlist,ndims)+samplendx);
319                  multisamp(rv, 2, d);
320                  d = 2.0*PI * rv[0];
321                  cosp = np->u_alpha * cos(d);
# Line 337 | Line 323 | register ANISODAT  *np;
323                  d = sqrt(cosp*cosp + sinp*sinp);
324                  cosp /= d;
325                  sinp /= d;
326 +                rv[1] = 1.0 - specjitter*rv[1];
327                  if (rv[1] <= FTINY)
328                          d = 1.0;
329                  else
330 <                        d = sqrt( -log(rv[1]) /
330 >                        d = sqrt(-log(rv[1]) /
331                                  (cosp*cosp/(np->u_alpha*np->u_alpha) +
332 <                                 sinp*sinp/(np->v_alpha*np->v_alpha)) );
332 >                                 sinp*sinp/(np->v_alpha*np->v_alpha)));
333                  for (i = 0; i < 3; i++)
334                          h[i] = np->pnorm[i] +
335 <                                        d*(cosp*np->u[i] + sinp*np->v[i]);
335 >                                d*(cosp*np->u[i] + sinp*np->v[i]);
336                  d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
337                  for (i = 0; i < 3; i++)
338                          sr.rdir[i] = r->rdir[i] + d*h[i];
339 <                if (DOT(sr.rdir, r->ron) <= FTINY)      /* oops! */
340 <                        goto refagain;
339 >                if (DOT(sr.rdir, r->ron) <= FTINY)      /* penetration? */
340 >                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
341                  rayvalue(&sr);
342                  multcolor(sr.rcol, np->scolor);
343                  addcolor(r->rcol, sr.rcol);
344                  ndims--;
345          }
346                                          /* compute transmission */
347 +        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
348 +                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
349 +                dimlist[ndims++] = (int)np->mp;
350 +                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
351 +                multisamp(rv, 2, d);
352 +                d = 2.0*PI * rv[0];
353 +                cosp = cos(d);
354 +                sinp = sin(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*4./(np->u_alpha*np->u_alpha) +
361 +                                 sinp*sinp*4./(np->v_alpha*np->v_alpha)));
362 +                for (i = 0; i < 3; i++)
363 +                        sr.rdir[i] = np->prdir[i] +
364 +                                        d*(cosp*np->u[i] + sinp*np->v[i]);
365 +                if (DOT(sr.rdir, r->ron) < -FTINY)
366 +                        normalize(sr.rdir);             /* OK, normalize */
367 +                else
368 +                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
369 +                rayvalue(&sr);
370 +                scalecolor(sr.rcol, np->tspec);
371 +                multcolor(sr.rcol, np->mcolor);         /* modify by color */
372 +                addcolor(r->rcol, sr.rcol);
373 +                ndims--;
374 +        }
375   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines