ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/normal.c
(Generate patch)

Comparing ray/src/rt/normal.c (file contents):
Revision 2.28 by greg, Wed Dec 21 09:51:49 1994 UTC vs.
Revision 2.41 by greg, Wed Mar 12 04:59:05 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  normal.c - shading function for normal materials.
6   *
# Line 14 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11   *     Later changes described in delta comments.
12   */
13  
14 + #include "copyright.h"
15 +
16   #include  "ray.h"
17  
18   #include  "otypes.h"
19  
20   #include  "random.h"
21  
22 < extern double  specthresh;              /* specular sampling threshold */
23 < extern double  specjitter;              /* specular sampling jitter */
22 > #ifndef  MAXITER
23 > #define  MAXITER        10              /* maximum # specular ray attempts */
24 > #endif
25 >                                        /* estimate of Fresnel function */
26 > #define  FRESNE(ci)     (exp(-6.0*(ci)) - 0.00247875217)
27  
28 < extern int  backvis;                    /* back faces visible? */
28 > static void  gaussamp();
29  
28 static  gaussamp();
29
30   /*
31   *      This routine implements the isotropic Gaussian
32   *  model described by Ward in Siggraph `92 article.
# Line 65 | Line 65 | typedef struct {
65   }  NORMDAT;             /* normal material data */
66  
67  
68 + static void
69   dirnorm(cval, np, ldir, omega)          /* compute source contribution */
70   COLOR  cval;                    /* returned coefficient */
71   register NORMDAT  *np;          /* material data */
# Line 72 | Line 73 | FVECT  ldir;                   /* light source direction */
73   double  omega;                  /* light source size */
74   {
75          double  ldot;
76 +        double  ldiff;
77          double  dtmp, d2;
78          FVECT  vtmp;
79          COLOR  ctmp;
# Line 83 | Line 85 | double  omega;                 /* light source size */
85          if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
86                  return;         /* wrong side */
87  
88 <        if (ldot > FTINY && np->rdiff > FTINY) {
88 >                                /* Fresnel estimate */
89 >        ldiff = np->rdiff;
90 >        if (np->specfl & SP_PURE && (np->rspec > FTINY & ldiff > FTINY))
91 >                ldiff *= 1. - FRESNE(fabs(ldot));
92 >
93 >        if (ldot > FTINY && ldiff > FTINY) {
94                  /*
95                   *  Compute and add diffuse reflected component to returned
96                   *  color.  The diffuse reflected component will always be
97                   *  modified by the color of the material.
98                   */
99                  copycolor(ctmp, np->mcolor);
100 <                dtmp = ldot * omega * np->rdiff / PI;
100 >                dtmp = ldot * omega * ldiff / PI;
101                  scalecolor(ctmp, dtmp);
102                  addcolor(cval, ctmp);
103          }
# Line 150 | Line 157 | double  omega;                 /* light source size */
157   }
158  
159  
160 + int
161   m_normal(m, r)                  /* color a ray that hit something normal */
162   register OBJREC  *m;
163   register RAY  *r;
164   {
165          NORMDAT  nd;
166 +        double  fest;
167          double  transtest, transdist;
168 +        double  mirtest, mirdist;
169 +        int     hastexture;
170 +        double  d;
171          COLOR  ctmp;
172          register int  i;
173                                                  /* easy shadow test */
# Line 164 | Line 176 | register RAY  *r;
176  
177          if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
178                  objerror(m, USER, "bad number of arguments");
179 +                                                /* check for back side */
180 +        if (r->rod < 0.0) {
181 +                if (!backvis && m->otype != MAT_TRANS) {
182 +                        raytrans(r);
183 +                        return(1);
184 +                }
185 +                raytexture(r, m->omod);
186 +                flipsurface(r);                 /* reorient if backvis */
187 +        } else
188 +                raytexture(r, m->omod);
189          nd.mp = m;
190          nd.rp = r;
191                                                  /* get material color */
# Line 175 | Line 197 | register RAY  *r;
197          nd.alpha2 = m->oargs.farg[4];
198          if ((nd.alpha2 *= nd.alpha2) <= FTINY)
199                  nd.specfl |= SP_PURE;
200 <                                                /* check for back side */
201 <        if (r->rod < 0.0) {
202 <                if (!backvis && m->otype != MAT_TRANS) {
203 <                        raytrans(r);
204 <                        return(1);
205 <                }
206 <                flipsurface(r);                 /* reorient if backvis */
200 >
201 >        if (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) {
202 >                nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
203 >        } else {
204 >                VCOPY(nd.pnorm, r->ron);
205 >                nd.pdot = r->rod;
206 >                if (r->ro != NULL && isflat(r->ro->otype))
207 >                        nd.specfl |= SP_FLAT;
208          }
186                                                /* get modifiers */
187        raytexture(r, m->omod);
188        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
209          if (nd.pdot < .001)
210                  nd.pdot = .001;                 /* non-zero for dirnorm() */
211          multcolor(nd.mcolor, r->pcol);          /* modify material color */
212 <        transtest = 0;
213 <        transdist = r->rot;
214 <                                                /* get specular component */
215 <        if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
216 <                nd.specfl |= SP_REFL;
217 <                                                /* compute specular color */
218 <                if (m->otype == MAT_METAL)
219 <                        copycolor(nd.scolor, nd.mcolor);
220 <                else
201 <                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
202 <                scalecolor(nd.scolor, nd.rspec);
203 <                                                /* check threshold */
204 <                if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
205 <                        nd.specfl |= SP_RBLT;
206 <                                                /* compute reflected ray */
207 <                for (i = 0; i < 3; i++)
208 <                        nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
209 <                if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
210 <                        for (i = 0; i < 3; i++)         /* safety measure */
211 <                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
212 <
213 <                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
214 <                        RAY  lr;
215 <                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
216 <                                VCOPY(lr.rdir, nd.vrefl);
217 <                                rayvalue(&lr);
218 <                                multcolor(lr.rcol, nd.scolor);
219 <                                addcolor(r->rcol, lr.rcol);
220 <                        }
221 <                }
222 <        }
212 >        mirtest = transtest = 0;
213 >        mirdist = transdist = r->rot;
214 >        nd.rspec = m->oargs.farg[3];
215 >                                                /* compute Fresnel approx. */
216 >        if (nd.specfl & SP_PURE && nd.rspec > FTINY) {
217 >                fest = FRESNE(r->rod);
218 >                nd.rspec += fest*(1. - nd.rspec);
219 >        } else
220 >                fest = 0.;
221                                                  /* compute transmission */
222          if (m->otype == MAT_TRANS) {
223                  nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
# Line 231 | Line 229 | register RAY  *r;
229                          if (!(nd.specfl & SP_PURE) &&
230                                          specthresh >= nd.tspec-FTINY)
231                                  nd.specfl |= SP_TBLT;
232 <                        if (r->crtype & SHADOW ||
235 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
232 >                        if (!hastexture || r->crtype & SHADOW) {
233                                  VCOPY(nd.prdir, r->rdir);
234                                  transtest = 2;
235                          } else {
# Line 247 | Line 244 | register RAY  *r;
244          } else
245                  nd.tdiff = nd.tspec = nd.trans = 0.0;
246                                                  /* transmitted ray */
247 <        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
247 >        if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
248                  RAY  lr;
249                  if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
250                          VCOPY(lr.rdir, nd.prdir);
# Line 261 | Line 258 | register RAY  *r;
258          } else
259                  transtest = 0;
260  
261 <        if (r->crtype & SHADOW)                 /* the rest is shadow */
261 >        if (r->crtype & SHADOW) {               /* the rest is shadow */
262 >                r->rt = transdist;
263                  return(1);
264 +        }
265 +                                                /* get specular reflection */
266 +        if (nd.rspec > FTINY) {
267 +                nd.specfl |= SP_REFL;
268 +                                                /* compute specular color */
269 +                if (m->otype != MAT_METAL) {
270 +                        setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
271 +                } else if (fest > FTINY) {
272 +                        d = nd.rspec*(1. - fest);
273 +                        for (i = 0; i < 3; i++)
274 +                                nd.scolor[i] = fest + nd.mcolor[i]*d;
275 +                } else {
276 +                        copycolor(nd.scolor, nd.mcolor);
277 +                        scalecolor(nd.scolor, nd.rspec);
278 +                }
279 +                                                /* check threshold */
280 +                if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
281 +                        nd.specfl |= SP_RBLT;
282 +                                                /* compute reflected ray */
283 +                for (i = 0; i < 3; i++)
284 +                        nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i];
285 +                                                /* penetration? */
286 +                if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
287 +                        for (i = 0; i < 3; i++)         /* safety measure */
288 +                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
289 +        }
290 +                                                /* reflected ray */
291 +        if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) {
292 +                RAY  lr;
293 +                if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
294 +                        VCOPY(lr.rdir, nd.vrefl);
295 +                        rayvalue(&lr);
296 +                        multcolor(lr.rcol, nd.scolor);
297 +                        addcolor(r->rcol, lr.rcol);
298 +                        if (!hastexture && nd.specfl & SP_FLAT) {
299 +                                mirtest = 2.*bright(lr.rcol);
300 +                                mirdist = r->rot + lr.rt;
301 +                        }
302 +                }
303 +        }
304                                                  /* diffuse reflection */
305          nd.rdiff = 1.0 - nd.trans - nd.rspec;
306  
307          if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
308                  return(1);                      /* 100% pure specular */
309  
310 <        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
311 <                        r->ro->otype == OBJ_RING))
274 <                nd.specfl |= SP_FLAT;
310 >        if (!(nd.specfl & SP_PURE))
311 >                gaussamp(r, &nd);               /* checks *BLT flags */
312  
276        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
277                gaussamp(r, &nd);
278
313          if (nd.rdiff > FTINY) {         /* ambient from this side */
314 <                ambient(ctmp, r);
314 >                ambient(ctmp, r, hastexture?nd.pnorm:r->ron);
315                  if (nd.specfl & SP_RBLT)
316                          scalecolor(ctmp, 1.0-nd.trans);
317                  else
# Line 287 | Line 321 | register RAY  *r;
321          }
322          if (nd.tdiff > FTINY) {         /* ambient from other side */
323                  flipsurface(r);
324 <                ambient(ctmp, r);
324 >                if (hastexture) {
325 >                        FVECT  bnorm;
326 >                        bnorm[0] = -nd.pnorm[0];
327 >                        bnorm[1] = -nd.pnorm[1];
328 >                        bnorm[2] = -nd.pnorm[2];
329 >                        ambient(ctmp, r, bnorm);
330 >                } else
331 >                        ambient(ctmp, r, r->ron);
332                  if (nd.specfl & SP_TBLT)
333                          scalecolor(ctmp, nd.trans);
334                  else
# Line 299 | Line 340 | register RAY  *r;
340                                          /* add direct component */
341          direct(r, dirnorm, &nd);
342                                          /* check distance */
343 <        if (transtest > bright(r->rcol))
343 >        d = bright(r->rcol);
344 >        if (transtest > d)
345                  r->rt = transdist;
346 +        else if (mirtest > d)
347 +                r->rt = mirdist;
348  
349          return(1);
350   }
351  
352  
353 < static
353 > static void
354   gaussamp(r, np)                 /* sample gaussian specular */
355   RAY  *r;
356   register NORMDAT  *np;
# Line 315 | Line 359 | register NORMDAT  *np;
359          FVECT  u, v, h;
360          double  rv[2];
361          double  d, sinp, cosp;
362 +        int  niter;
363          register int  i;
364                                          /* quick test */
365          if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
# Line 333 | Line 378 | register NORMDAT  *np;
378          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
379                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
380                  dimlist[ndims++] = (int)np->mp;
381 <                d = urand(ilhash(dimlist,ndims)+samplendx);
382 <                multisamp(rv, 2, d);
383 <                d = 2.0*PI * rv[0];
384 <                cosp = cos(d);
385 <                sinp = sin(d);
386 <                rv[1] = 1.0 - specjitter*rv[1];
387 <                if (rv[1] <= FTINY)
388 <                        d = 1.0;
389 <                else
390 <                        d = sqrt( np->alpha2 * -log(rv[1]) );
391 <                for (i = 0; i < 3; i++)
392 <                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
393 <                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
394 <                for (i = 0; i < 3; i++)
395 <                        sr.rdir[i] = r->rdir[i] + d*h[i];
396 <                if (DOT(sr.rdir, r->ron) <= FTINY)
397 <                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
398 <                rayvalue(&sr);
399 <                multcolor(sr.rcol, np->scolor);
400 <                addcolor(r->rcol, sr.rcol);
381 >                for (niter = 0; niter < MAXITER; niter++) {
382 >                        if (niter)
383 >                                d = frandom();
384 >                        else
385 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
386 >                        multisamp(rv, 2, d);
387 >                        d = 2.0*PI * rv[0];
388 >                        cosp = tcos(d);
389 >                        sinp = tsin(d);
390 >                        rv[1] = 1.0 - specjitter*rv[1];
391 >                        if (rv[1] <= FTINY)
392 >                                d = 1.0;
393 >                        else
394 >                                d = sqrt( np->alpha2 * -log(rv[1]) );
395 >                        for (i = 0; i < 3; i++)
396 >                                h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
397 >                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
398 >                        for (i = 0; i < 3; i++)
399 >                                sr.rdir[i] = r->rdir[i] + d*h[i];
400 >                        if (DOT(sr.rdir, r->ron) > FTINY) {
401 >                                rayvalue(&sr);
402 >                                multcolor(sr.rcol, np->scolor);
403 >                                addcolor(r->rcol, sr.rcol);
404 >                                break;
405 >                        }
406 >                }
407                  ndims--;
408          }
409                                          /* compute transmission */
410          if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
411                          rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
412                  dimlist[ndims++] = (int)np->mp;
413 <                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
414 <                multisamp(rv, 2, d);
415 <                d = 2.0*PI * rv[0];
416 <                cosp = cos(d);
417 <                sinp = sin(d);
418 <                rv[1] = 1.0 - specjitter*rv[1];
419 <                if (rv[1] <= FTINY)
420 <                        d = 1.0;
421 <                else
422 <                        d = sqrt( -log(rv[1]) * np->alpha2 );
423 <                for (i = 0; i < 3; i++)
424 <                        sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
425 <                if (DOT(sr.rdir, r->ron) < -FTINY)
426 <                        normalize(sr.rdir);             /* OK, normalize */
427 <                else
428 <                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
429 <                rayvalue(&sr);
430 <                scalecolor(sr.rcol, np->tspec);
431 <                multcolor(sr.rcol, np->mcolor);         /* modified by color */
432 <                addcolor(r->rcol, sr.rcol);
413 >                for (niter = 0; niter < MAXITER; niter++) {
414 >                        if (niter)
415 >                                d = frandom();
416 >                        else
417 >                                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
418 >                        multisamp(rv, 2, d);
419 >                        d = 2.0*PI * rv[0];
420 >                        cosp = tcos(d);
421 >                        sinp = tsin(d);
422 >                        rv[1] = 1.0 - specjitter*rv[1];
423 >                        if (rv[1] <= FTINY)
424 >                                d = 1.0;
425 >                        else
426 >                                d = sqrt( np->alpha2 * -log(rv[1]) );
427 >                        for (i = 0; i < 3; i++)
428 >                                sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
429 >                        if (DOT(sr.rdir, r->ron) < -FTINY) {
430 >                                normalize(sr.rdir);     /* OK, normalize */
431 >                                rayvalue(&sr);
432 >                                scalecolor(sr.rcol, np->tspec);
433 >                                multcolor(sr.rcol, np->mcolor); /* modified */
434 >                                addcolor(r->rcol, sr.rcol);
435 >                                break;
436 >                        }
437 >                }
438                  ndims--;
439          }
440   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines