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.35 by greg, Tue Jan 7 16:44:04 1997 UTC vs.
Revision 2.46 by greg, Thu Aug 28 03:22:16 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 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  "ambient.h"
19 +
20   #include  "otypes.h"
21  
22   #include  "random.h"
23  
23 extern double  specthresh;              /* specular sampling threshold */
24 extern double  specjitter;              /* specular sampling jitter */
25
26 extern int  backvis;                    /* back faces visible? */
27
24   #ifndef  MAXITER
25   #define  MAXITER        10              /* maximum # specular ray attempts */
26   #endif
27 +                                        /* estimate of Fresnel function */
28 + #define  FRESNE(ci)     (exp(-5.85*(ci)) - 0.00287989916)
29  
30 < static  gaussamp();
30 > static void  gaussamp();
31  
32   /*
33   *      This routine implements the isotropic Gaussian
# Line 69 | Line 67 | typedef struct {
67   }  NORMDAT;             /* normal material data */
68  
69  
70 + static void
71   dirnorm(cval, np, ldir, omega)          /* compute source contribution */
72   COLOR  cval;                    /* returned coefficient */
73   register NORMDAT  *np;          /* material data */
# Line 76 | Line 75 | FVECT  ldir;                   /* light source direction */
75   double  omega;                  /* light source size */
76   {
77          double  ldot;
78 +        double  ldiff;
79          double  dtmp, d2;
80          FVECT  vtmp;
81          COLOR  ctmp;
# Line 87 | Line 87 | double  omega;                 /* light source size */
87          if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
88                  return;         /* wrong side */
89  
90 <        if (ldot > FTINY && np->rdiff > FTINY) {
90 >                                /* Fresnel estimate */
91 >        ldiff = np->rdiff;
92 >        if (np->specfl & SP_PURE && (np->rspec > FTINY) & (ldiff > FTINY))
93 >                ldiff *= 1. - FRESNE(fabs(ldot));
94 >
95 >        if (ldot > FTINY && ldiff > FTINY) {
96                  /*
97                   *  Compute and add diffuse reflected component to returned
98                   *  color.  The diffuse reflected component will always be
99                   *  modified by the color of the material.
100                   */
101                  copycolor(ctmp, np->mcolor);
102 <                dtmp = ldot * omega * np->rdiff / PI;
102 >                dtmp = ldot * omega * ldiff / PI;
103                  scalecolor(ctmp, dtmp);
104                  addcolor(cval, ctmp);
105          }
# Line 154 | Line 159 | double  omega;                 /* light source size */
159   }
160  
161  
162 + int
163   m_normal(m, r)                  /* color a ray that hit something normal */
164   register OBJREC  *m;
165   register RAY  *r;
166   {
167          NORMDAT  nd;
168 +        double  fest;
169          double  transtest, transdist;
170          double  mirtest, mirdist;
171          int     hastexture;
# Line 177 | Line 184 | register RAY  *r;
184                          raytrans(r);
185                          return(1);
186                  }
187 +                raytexture(r, m->omod);
188                  flipsurface(r);                 /* reorient if backvis */
189 <        }
189 >        } else
190 >                raytexture(r, m->omod);
191          nd.mp = m;
192          nd.rp = r;
193                                                  /* get material color */
# Line 190 | Line 199 | register RAY  *r;
199          nd.alpha2 = m->oargs.farg[4];
200          if ((nd.alpha2 *= nd.alpha2) <= FTINY)
201                  nd.specfl |= SP_PURE;
202 <        if (r->ro != NULL && isflat(r->ro->otype))
203 <                nd.specfl |= SP_FLAT;
195 <                                                /* get modifiers */
196 <        raytexture(r, m->omod);
197 <        if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY)
202 >
203 >        if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) {
204                  nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
205 <        else {
205 >        } else {
206                  VCOPY(nd.pnorm, r->ron);
207                  nd.pdot = r->rod;
208          }
209 +        if (r->ro != NULL && isflat(r->ro->otype))
210 +                nd.specfl |= SP_FLAT;
211          if (nd.pdot < .001)
212                  nd.pdot = .001;                 /* non-zero for dirnorm() */
213          multcolor(nd.mcolor, r->pcol);          /* modify material color */
214          mirtest = transtest = 0;
215          mirdist = transdist = r->rot;
216          nd.rspec = m->oargs.farg[3];
217 +                                                /* compute Fresnel approx. */
218 +        if (nd.specfl & SP_PURE && nd.rspec > FTINY) {
219 +                fest = FRESNE(r->rod);
220 +                nd.rspec += fest*(1. - nd.rspec);
221 +        } else
222 +                fest = 0.;
223                                                  /* compute transmission */
224          if (m->otype == MAT_TRANS) {
225                  nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
# Line 232 | Line 246 | register RAY  *r;
246          } else
247                  nd.tdiff = nd.tspec = nd.trans = 0.0;
248                                                  /* transmitted ray */
249 <        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
249 >        if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
250                  RAY  lr;
251                  if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
252                          VCOPY(lr.rdir, nd.prdir);
# Line 254 | Line 268 | register RAY  *r;
268          if (nd.rspec > FTINY) {
269                  nd.specfl |= SP_REFL;
270                                                  /* compute specular color */
271 <                if (m->otype == MAT_METAL)
271 >                if (m->otype != MAT_METAL) {
272 >                        setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
273 >                } else if (fest > FTINY) {
274 >                        d = nd.rspec*(1. - fest);
275 >                        for (i = 0; i < 3; i++)
276 >                                nd.scolor[i] = fest + nd.mcolor[i]*d;
277 >                } else {
278                          copycolor(nd.scolor, nd.mcolor);
279 <                else
280 <                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
261 <                scalecolor(nd.scolor, nd.rspec);
279 >                        scalecolor(nd.scolor, nd.rspec);
280 >                }
281                                                  /* check threshold */
282                  if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
283                          nd.specfl |= SP_RBLT;
# Line 269 | Line 288 | register RAY  *r;
288                  if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
289                          for (i = 0; i < 3; i++)         /* safety measure */
290                                  nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
291 <
292 <                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
293 <                        RAY  lr;
294 <                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
295 <                                VCOPY(lr.rdir, nd.vrefl);
296 <                                rayvalue(&lr);
297 <                                multcolor(lr.rcol, nd.scolor);
298 <                                addcolor(r->rcol, lr.rcol);
299 <                                if (!hastexture && nd.specfl & SP_FLAT) {
300 <                                        mirtest = 2.*bright(lr.rcol);
301 <                                        mirdist = r->rot + lr.rt;
302 <                                }
291 >        }
292 >                                                /* reflected ray */
293 >        if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) {
294 >                RAY  lr;
295 >                if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
296 >                        VCOPY(lr.rdir, nd.vrefl);
297 >                        rayvalue(&lr);
298 >                        multcolor(lr.rcol, nd.scolor);
299 >                        addcolor(r->rcol, lr.rcol);
300 >                        if (!hastexture && nd.specfl & SP_FLAT) {
301 >                                mirtest = 2.*bright(lr.rcol);
302 >                                mirdist = r->rot + lr.rt;
303                          }
304                  }
305          }
# Line 290 | Line 309 | register RAY  *r;
309          if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
310                  return(1);                      /* 100% pure specular */
311  
312 <        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
313 <                gaussamp(r, &nd);
312 >        if (!(nd.specfl & SP_PURE))
313 >                gaussamp(r, &nd);               /* checks *BLT flags */
314  
315          if (nd.rdiff > FTINY) {         /* ambient from this side */
316                  ambient(ctmp, r, hastexture?nd.pnorm:r->ron);
# Line 333 | Line 352 | register RAY  *r;
352   }
353  
354  
355 < static
355 > static void
356   gaussamp(r, np)                 /* sample gaussian specular */
357   RAY  *r;
358   register NORMDAT  *np;
# Line 368 | Line 387 | register NORMDAT  *np;
387                                  d = urand(ilhash(dimlist,ndims)+samplendx);
388                          multisamp(rv, 2, d);
389                          d = 2.0*PI * rv[0];
390 <                        cosp = cos(d);
391 <                        sinp = sin(d);
390 >                        cosp = tcos(d);
391 >                        sinp = tsin(d);
392                          rv[1] = 1.0 - specjitter*rv[1];
393                          if (rv[1] <= FTINY)
394                                  d = 1.0;
# Line 400 | Line 419 | register NORMDAT  *np;
419                                  d = urand(ilhash(dimlist,ndims)+1823+samplendx);
420                          multisamp(rv, 2, d);
421                          d = 2.0*PI * rv[0];
422 <                        cosp = cos(d);
423 <                        sinp = sin(d);
422 >                        cosp = tcos(d);
423 >                        sinp = tsin(d);
424                          rv[1] = 1.0 - specjitter*rv[1];
425                          if (rv[1] <= FTINY)
426                                  d = 1.0;
427                          else
428 <                                d = sqrt( -log(rv[1]) * np->alpha2 );
428 >                                d = sqrt( np->alpha2 * -log(rv[1]) );
429                          for (i = 0; i < 3; i++)
430                                  sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
431                          if (DOT(sr.rdir, r->ron) < -FTINY) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines