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.5 by greg, Wed Jan 15 11:02:43 1992 UTC vs.
Revision 2.12 by greg, Thu Apr 16 13:29:06 1992 UTC

# Line 34 | Line 34 | extern double  specjitter;             /* specular sampling jitte
34   *  8  red      grn     blu     rspec   u-rough v-rough trans   tspec
35   */
36  
37 #define  BSPEC(m)       (6.0)           /* specularity parameter b */
38
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_FLAT        020             /* reflecting surface is flat */
42 < #define  SP_RBLT        040             /* reflection below sample threshold */
43 < #define  SP_TBLT        0100            /* transmission below threshold */
46 < #define  SP_BADU        0200            /* 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 {
46          OBJREC  *mp;            /* material pointer */
# Line 51 | 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 93 | 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 135 | 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 159 | Line 157 | register OBJREC  *m;
157   register RAY  *r;
158   {
159          ANISODAT  nd;
162        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 179 | 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 190 | 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 */
193        transtest = 0;
190                                                  /* get specular component */
191          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
192                  nd.specfl |= SP_REFL;
# Line 200 | Line 196 | register RAY  *r;
196                  else
197                          setcolor(nd.scolor, 1.0, 1.0, 1.0);
198                  scalecolor(nd.scolor, nd.rspec);
203                                                /* improved model */
204                dtmp = exp(-BSPEC(m)*nd.pdot);
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;
199                                                  /* check threshold */
200                  if (specthresh > FTINY &&
201 <                                ((specthresh >= 1.-FTINY ||
202 <                                specthresh + (.1 - .2*urand(8199+samplendx))
212 <                                        > nd.rspec)))
201 >                                (specthresh >= 1.-FTINY ||
202 >                                specthresh > nd.rspec))
203                          nd.specfl |= SP_RBLT;
204 <
205 <                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
206 <                        RAY  lr;
207 <                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
208 <                                for (i = 0; i < 3; i++)
209 <                                        lr.rdir[i] = r->rdir[i] +
220 <                                                2.0*nd.pdot*nd.pnorm[i];
221 <                                rayvalue(&lr);
222 <                                multcolor(lr.rcol, nd.scolor);
223 <                                addcolor(r->rcol, lr.rcol);
224 <                        }
225 <                }
204 >                                                /* compute refl. direction */
205 >                for (i = 0; i < 3; i++)
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 233 | Line 217 | register RAY  *r;
217                          nd.specfl |= SP_TRAN;
218                                                          /* check threshold */
219                          if (specthresh > FTINY &&
220 <                                        ((specthresh >= 1.-FTINY ||
221 <                                        specthresh +
238 <                                            (.1 - .2*urand(7241+samplendx))
239 <                                                > nd.tspec)))
220 >                                        (specthresh >= 1.-FTINY ||
221 >                                        specthresh > nd.tspec))
222                                  nd.specfl |= SP_TBLT;
223 <                        if (r->crtype & SHADOW ||
242 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
223 >                        if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
224                                  VCOPY(nd.prdir, r->rdir);
244                                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;
254                                                /* transmitted ray */
255        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
256                RAY  lr;
257                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
258                        VCOPY(lr.rdir, nd.prdir);
259                        rayvalue(&lr);
260                        scalecolor(lr.rcol, nd.tspec);
261                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
262                        addcolor(r->rcol, lr.rcol);
263                        transtest *= bright(lr.rcol);
264                        transdist = r->rot + lr.rt;
265                }
266        }
237  
268        if (r->crtype & SHADOW)                 /* the rest is shadow */
269                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 */
275 <
276 <        if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
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 */
# Line 303 | Line 269 | register RAY  *r;
269          }
270                                          /* add direct component */
271          direct(r, diraniso, &nd);
306                                        /* check distance */
307        if (transtest > bright(r->rcol))
308                r->rt = transdist;
272   }
273  
274  
# Line 347 | Line 310 | register ANISODAT  *np;
310          FVECT  h;
311          double  rv[2];
312          double  d, sinp, cosp;
350        int  ntries;
313          register int  i;
314                                          /* compute reflection */
315          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
316                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
317                  dimlist[ndims++] = (int)np->mp;
318 <                for (ntries = 0; ntries < 10; ntries++) {
319 <                        dimlist[ndims] = ntries * 3601;
320 <                        d = urand(ilhash(dimlist,ndims+1)+samplendx);
321 <                        multisamp(rv, 2, d);
322 <                        d = 2.0*PI * rv[0];
323 <                        cosp = np->u_alpha * cos(d);
324 <                        sinp = np->v_alpha * sin(d);
325 <                        d = sqrt(cosp*cosp + sinp*sinp);
326 <                        cosp /= d;
327 <                        sinp /= d;
328 <                        rv[1] = 1.0 - specjitter*rv[1];
329 <                        if (rv[1] <= FTINY)
330 <                                d = 1.0;
331 <                        else
332 <                                d = sqrt(-log(rv[1]) /
333 <                                        (cosp*cosp/(np->u_alpha*np->u_alpha) +
334 <                                         sinp*sinp/(np->v_alpha*np->v_alpha)));
335 <                        for (i = 0; i < 3; i++)
336 <                                h[i] = np->pnorm[i] +
337 <                                        d*(cosp*np->u[i] + sinp*np->v[i]);
338 <                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
339 <                        for (i = 0; i < 3; i++)
340 <                                sr.rdir[i] = r->rdir[i] + d*h[i];
341 <                        if (DOT(sr.rdir, r->ron) > FTINY) {
342 <                                rayvalue(&sr);
343 <                                multcolor(sr.rcol, np->scolor);
382 <                                addcolor(r->rcol, sr.rcol);
383 <                                break;
384 <                        }
385 <                }
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);
322 >                sinp = np->v_alpha * sin(d);
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]) /
331 >                                (cosp*cosp/(np->u_alpha*np->u_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]);
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)      /* 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