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.31 by greg, Mon Nov 6 12:03:17 1995 UTC vs.
Revision 2.39 by greg, Tue Feb 25 02:47:22 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1995 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;
# Line 202 | Line 211 | register RAY  *r;
211          mirtest = transtest = 0;
212          mirdist = transdist = r->rot;
213          nd.rspec = m->oargs.farg[3];
214 +                                                /* compute Fresnel approx. */
215 +        if (nd.specfl & SP_PURE && nd.rspec > FTINY) {
216 +                fest = FRESNE(r->rod);
217 +                nd.rspec += fest*(1. - nd.rspec);
218 +        } else
219 +                fest = 0.;
220                                                  /* compute transmission */
221          if (m->otype == MAT_TRANS) {
222                  nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
# Line 228 | Line 243 | register RAY  *r;
243          } else
244                  nd.tdiff = nd.tspec = nd.trans = 0.0;
245                                                  /* transmitted ray */
246 <        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
246 >        if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
247                  RAY  lr;
248                  if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
249                          VCOPY(lr.rdir, nd.prdir);
# Line 250 | Line 265 | register RAY  *r;
265          if (nd.rspec > FTINY) {
266                  nd.specfl |= SP_REFL;
267                                                  /* compute specular color */
268 <                if (m->otype == MAT_METAL)
268 >                if (m->otype != MAT_METAL) {
269 >                        setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
270 >                } else if (fest > FTINY) {
271 >                        d = nd.rspec*(1. - fest);
272 >                        for (i = 0; i < 3; i++)
273 >                                nd.scolor[i] = fest + nd.mcolor[i]*d;
274 >                } else {
275                          copycolor(nd.scolor, nd.mcolor);
276 <                else
277 <                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
257 <                scalecolor(nd.scolor, nd.rspec);
276 >                        scalecolor(nd.scolor, nd.rspec);
277 >                }
278                                                  /* check threshold */
279                  if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
280                          nd.specfl |= SP_RBLT;
# Line 265 | Line 285 | register RAY  *r;
285                  if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
286                          for (i = 0; i < 3; i++)         /* safety measure */
287                                  nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
288 <
289 <                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
290 <                        RAY  lr;
291 <                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
292 <                                VCOPY(lr.rdir, nd.vrefl);
293 <                                rayvalue(&lr);
294 <                                multcolor(lr.rcol, nd.scolor);
295 <                                addcolor(r->rcol, lr.rcol);
296 <                                if (!hastexture && nd.specfl & SP_FLAT) {
297 <                                        mirtest = 2.*bright(lr.rcol);
298 <                                        mirdist = r->rot + lr.rt;
299 <                                }
288 >        }
289 >                                                /* reflected ray */
290 >        if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) {
291 >                RAY  lr;
292 >                if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
293 >                        VCOPY(lr.rdir, nd.vrefl);
294 >                        rayvalue(&lr);
295 >                        multcolor(lr.rcol, nd.scolor);
296 >                        addcolor(r->rcol, lr.rcol);
297 >                        if (!hastexture && nd.specfl & SP_FLAT) {
298 >                                mirtest = 2.*bright(lr.rcol);
299 >                                mirdist = r->rot + lr.rt;
300                          }
301                  }
302          }
# Line 286 | Line 306 | register RAY  *r;
306          if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
307                  return(1);                      /* 100% pure specular */
308  
309 <        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
310 <                gaussamp(r, &nd);
309 >        if (!(nd.specfl & SP_PURE))
310 >                gaussamp(r, &nd);               /* checks *BLT flags */
311  
312          if (nd.rdiff > FTINY) {         /* ambient from this side */
313                  ambient(ctmp, r, hastexture?nd.pnorm:r->ron);
# Line 300 | Line 320 | register RAY  *r;
320          }
321          if (nd.tdiff > FTINY) {         /* ambient from other side */
322                  flipsurface(r);
323 <                ambient(ctmp, r, hastexture?nd.pnorm:r->ron);
323 >                if (hastexture) {
324 >                        FVECT  bnorm;
325 >                        bnorm[0] = -nd.pnorm[0];
326 >                        bnorm[1] = -nd.pnorm[1];
327 >                        bnorm[2] = -nd.pnorm[2];
328 >                        ambient(ctmp, r, bnorm);
329 >                } else
330 >                        ambient(ctmp, r, r->ron);
331                  if (nd.specfl & SP_TBLT)
332                          scalecolor(ctmp, nd.trans);
333                  else
# Line 322 | Line 349 | register RAY  *r;
349   }
350  
351  
352 < static
352 > static void
353   gaussamp(r, np)                 /* sample gaussian specular */
354   RAY  *r;
355   register NORMDAT  *np;
# Line 331 | Line 358 | register NORMDAT  *np;
358          FVECT  u, v, h;
359          double  rv[2];
360          double  d, sinp, cosp;
361 +        int  niter;
362          register int  i;
363                                          /* quick test */
364          if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
# Line 349 | Line 377 | register NORMDAT  *np;
377          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
378                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
379                  dimlist[ndims++] = (int)np->mp;
380 <                d = urand(ilhash(dimlist,ndims)+samplendx);
381 <                multisamp(rv, 2, d);
382 <                d = 2.0*PI * rv[0];
383 <                cosp = cos(d);
384 <                sinp = sin(d);
385 <                rv[1] = 1.0 - specjitter*rv[1];
386 <                if (rv[1] <= FTINY)
387 <                        d = 1.0;
388 <                else
389 <                        d = sqrt( np->alpha2 * -log(rv[1]) );
390 <                for (i = 0; i < 3; i++)
391 <                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
392 <                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
393 <                for (i = 0; i < 3; i++)
394 <                        sr.rdir[i] = r->rdir[i] + d*h[i];
395 <                if (DOT(sr.rdir, r->ron) <= FTINY)
396 <                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
397 <                rayvalue(&sr);
398 <                multcolor(sr.rcol, np->scolor);
399 <                addcolor(r->rcol, sr.rcol);
380 >                for (niter = 0; niter < MAXITER; niter++) {
381 >                        if (niter)
382 >                                d = frandom();
383 >                        else
384 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
385 >                        multisamp(rv, 2, d);
386 >                        d = 2.0*PI * rv[0];
387 >                        cosp = tcos(d);
388 >                        sinp = tsin(d);
389 >                        rv[1] = 1.0 - specjitter*rv[1];
390 >                        if (rv[1] <= FTINY)
391 >                                d = 1.0;
392 >                        else
393 >                                d = sqrt( np->alpha2 * -log(rv[1]) );
394 >                        for (i = 0; i < 3; i++)
395 >                                h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
396 >                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
397 >                        for (i = 0; i < 3; i++)
398 >                                sr.rdir[i] = r->rdir[i] + d*h[i];
399 >                        if (DOT(sr.rdir, r->ron) > FTINY) {
400 >                                rayvalue(&sr);
401 >                                multcolor(sr.rcol, np->scolor);
402 >                                addcolor(r->rcol, sr.rcol);
403 >                                break;
404 >                        }
405 >                }
406                  ndims--;
407          }
408                                          /* compute transmission */
409          if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
410                          rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
411                  dimlist[ndims++] = (int)np->mp;
412 <                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
413 <                multisamp(rv, 2, d);
414 <                d = 2.0*PI * rv[0];
415 <                cosp = cos(d);
416 <                sinp = sin(d);
417 <                rv[1] = 1.0 - specjitter*rv[1];
418 <                if (rv[1] <= FTINY)
419 <                        d = 1.0;
420 <                else
421 <                        d = sqrt( -log(rv[1]) * np->alpha2 );
422 <                for (i = 0; i < 3; i++)
423 <                        sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
424 <                if (DOT(sr.rdir, r->ron) < -FTINY)
425 <                        normalize(sr.rdir);             /* OK, normalize */
426 <                else
427 <                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
428 <                rayvalue(&sr);
429 <                scalecolor(sr.rcol, np->tspec);
430 <                multcolor(sr.rcol, np->mcolor);         /* modified by color */
431 <                addcolor(r->rcol, sr.rcol);
412 >                for (niter = 0; niter < MAXITER; niter++) {
413 >                        if (niter)
414 >                                d = frandom();
415 >                        else
416 >                                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
417 >                        multisamp(rv, 2, d);
418 >                        d = 2.0*PI * rv[0];
419 >                        cosp = tcos(d);
420 >                        sinp = tsin(d);
421 >                        rv[1] = 1.0 - specjitter*rv[1];
422 >                        if (rv[1] <= FTINY)
423 >                                d = 1.0;
424 >                        else
425 >                                d = sqrt( np->alpha2 * -log(rv[1]) );
426 >                        for (i = 0; i < 3; i++)
427 >                                sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
428 >                        if (DOT(sr.rdir, r->ron) < -FTINY) {
429 >                                normalize(sr.rdir);     /* OK, normalize */
430 >                                rayvalue(&sr);
431 >                                scalecolor(sr.rcol, np->tspec);
432 >                                multcolor(sr.rcol, np->mcolor); /* modified */
433 >                                addcolor(r->rcol, sr.rcol);
434 >                                break;
435 >                        }
436 >                }
437                  ndims--;
438          }
439   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines