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.1 by greg, Sat Jan 4 19:52:49 1992 UTC vs.
Revision 2.12 by greg, Thu Apr 16 13:29:06 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 */
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 {
43        RAY  *rp;               /* ray pointer */
46          OBJREC  *mp;            /* material pointer */
47 +        RAY  *rp;               /* ray pointer */
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 87 | 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.
98                   */
99 <                                                /* roughness + source */
100 <                au2 = av2 = omega/(4.0*PI);
99 >                                                /* add source width if flat */
100 >                if (np->specfl & SP_FLAT)
101 >                        au2 = av2 = omega/(4.0*PI);
102 >                else
103 >                        au2 = av2 = 0.0;
104                  au2 += np->u_alpha * np->u_alpha;
105                  av2 += np->v_alpha * np->v_alpha;
106                                                  /* half vector */
# Line 126 | 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 150 | Line 157 | register OBJREC  *m;
157   register RAY  *r;
158   {
159          ANISODAT  nd;
153        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))
168                  objerror(m, USER, "bad number of real arguments");
163        nd.rp = r;
169          nd.mp = m;
170 +        nd.rp = r;
171                                                  /* get material color */
172          setcolor(nd.mcolor, m->oargs.farg[0],
173                             m->oargs.farg[1],
# Line 170 | 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 181 | 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 */
184        transtest = 0;
190                                                  /* get specular component */
191          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
192                  nd.specfl |= SP_REFL;
# Line 191 | 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) {
201 <                        RAY  lr;
202 <                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
203 <                                for (i = 0; i < 3; i++)
204 <                                        lr.rdir[i] = r->rdir[i] +
205 <                                                2.0*nd.pdot*nd.pnorm[i];
206 <                                rayvalue(&lr);
207 <                                multcolor(lr.rcol, nd.scolor);
208 <                                addcolor(r->rcol, lr.rcol);
209 <                        }
210 <                }
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 216 | 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);
222                                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;
232                                                /* transmitted ray */
233        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
234                RAY  lr;
235                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
236                        VCOPY(lr.rdir, nd.prdir);
237                        rayvalue(&lr);
238                        scalecolor(lr.rcol, nd.tspec);
239                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
240                        addcolor(r->rcol, lr.rcol);
241                        transtest *= bright(lr.rcol);
242                        transdist = r->rot + lr.rt;
243                }
244        }
237  
246        if (r->crtype & SHADOW)                 /* the rest is shadow */
247                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);
275                                        /* check distance */
276        if (transtest > bright(r->rcol))
277                r->rt = transdist;
272   }
273  
274  
# Line 316 | Line 310 | register ANISODAT  *np;
310          FVECT  h;
311          double  rv[2];
312          double  d, sinp, cosp;
319        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) {
324                confuse = 0;
317                  dimlist[ndims++] = (int)np->mp;
318 <        refagain:
327 <                dimlist[ndims] = confuse += 3601;
328 <                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 333 | 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