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.50 by greg, Tue Apr 19 01:15:06 2005 UTC vs.
Revision 2.72 by greg, Wed Sep 2 18:59:01 2015 UTC

# Line 19 | Line 19 | static const char RCSid[] = "$Id$";
19   #include  "otypes.h"
20   #include  "rtotypes.h"
21   #include  "random.h"
22 + #include  "pmapmat.h"
23  
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 + #define  FRESTHRESH     0.017999        /* minimum specularity for approx. */
30  
31  
32   /*
# Line 64 | Line 66 | typedef struct {
66          double  pdot;           /* perturbed dot product */
67   }  NORMDAT;             /* normal material data */
68  
69 < static srcdirf_t dirnorm;
68 < static void gaussamp(RAY  *r, NORMDAT  *np);
69 > static void gaussamp(NORMDAT  *np);
70  
71  
72   static void
73   dirnorm(                /* compute source contribution */
74          COLOR  cval,                    /* returned coefficient */
75 <        void  *nnp,             /* material data */
75 >        void  *nnp,                     /* material data */
76          FVECT  ldir,                    /* light source direction */
77          double  omega                   /* light source size */
78   )
79   {
80 <        register NORMDAT *np = nnp;
80 >        NORMDAT *np = nnp;
81          double  ldot;
82          double  lrdiff, ltdiff;
83 <        double  dtmp, d2;
83 >        double  dtmp, d2, d3, d4;
84          FVECT  vtmp;
85          COLOR  ctmp;
86  
# Line 93 | Line 94 | dirnorm(               /* compute source contribution */
94                                  /* Fresnel estimate */
95          lrdiff = np->rdiff;
96          ltdiff = np->tdiff;
97 <        if (np->specfl & SP_PURE && np->rspec > FTINY &&
97 >        if (np->specfl & SP_PURE && np->rspec >= FRESTHRESH &&
98                          (lrdiff > FTINY) | (ltdiff > FTINY)) {
99                  dtmp = 1. - FRESNE(fabs(ldot));
100                  lrdiff *= dtmp;
# Line 111 | Line 112 | dirnorm(               /* compute source contribution */
112                  scalecolor(ctmp, dtmp);
113                  addcolor(cval, ctmp);
114          }
115 +
116 +        if (ldot < -FTINY && ltdiff > FTINY) {
117 +                /*
118 +                 *  Compute diffuse transmission.
119 +                 */
120 +                copycolor(ctmp, np->mcolor);
121 +                dtmp = -ldot * omega * ltdiff * (1.0/PI);
122 +                scalecolor(ctmp, dtmp);
123 +                addcolor(cval, ctmp);
124 +        }
125 +
126 +        if (ambRayInPmap(np->rp))
127 +                return;         /* specular already in photon map */
128 +
129          if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
130                  /*
131                   *  Compute specular reflection coefficient using
132 <                 *  gaussian distribution model.
132 >                 *  Gaussian distribution model.
133                   */
134                                                  /* roughness */
135                  dtmp = np->alpha2;
# Line 122 | Line 137 | dirnorm(               /* compute source contribution */
137                  if (np->specfl & SP_FLAT)
138                          dtmp += omega * (0.25/PI);
139                                                  /* half vector */
140 <                vtmp[0] = ldir[0] - np->rp->rdir[0];
126 <                vtmp[1] = ldir[1] - np->rp->rdir[1];
127 <                vtmp[2] = ldir[2] - np->rp->rdir[2];
140 >                VSUB(vtmp, ldir, np->rp->rdir);
141                  d2 = DOT(vtmp, np->pnorm);
142                  d2 *= d2;
143 <                d2 = (DOT(vtmp,vtmp) - d2) / d2;
144 <                                                /* gaussian */
145 <                dtmp = exp(-d2/dtmp)/(4.*PI * np->pdot * dtmp);
143 >                d3 = DOT(vtmp,vtmp);
144 >                d4 = (d3 - d2) / d2;
145 >                                                /* new W-G-M-D model */
146 >                dtmp = exp(-d4/dtmp) * d3 / (PI * d2*d2 * dtmp);
147                                                  /* worth using? */
148                  if (dtmp > FTINY) {
149                          copycolor(ctmp, np->scolor);
150 <                        dtmp *= omega;
150 >                        dtmp *= ldot * omega;
151                          scalecolor(ctmp, dtmp);
152                          addcolor(cval, ctmp);
153                  }
154          }
155 <        if (ldot < -FTINY && ltdiff > FTINY) {
156 <                /*
143 <                 *  Compute diffuse transmission.
144 <                 */
145 <                copycolor(ctmp, np->mcolor);
146 <                dtmp = -ldot * omega * ltdiff * (1.0/PI);
147 <                scalecolor(ctmp, dtmp);
148 <                addcolor(cval, ctmp);
149 <        }
155 >        
156 >
157          if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
158                  /*
159                   *  Compute specular transmission.  Specular transmission
# Line 154 | Line 161 | dirnorm(               /* compute source contribution */
161                   */
162                                                  /* roughness + source */
163                  dtmp = np->alpha2 + omega*(1.0/PI);
164 <                                                /* gaussian */
165 <                dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp) /
159 <                                        (PI*np->pdot*dtmp);
164 >                                                /* Gaussian */
165 >                dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp);
166                                                  /* worth using? */
167                  if (dtmp > FTINY) {
168                          copycolor(ctmp, np->mcolor);
169 <                        dtmp *= np->tspec * omega;
169 >                        dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
170                          scalecolor(ctmp, dtmp);
171                          addcolor(cval, ctmp);
172                  }
# Line 168 | Line 174 | dirnorm(               /* compute source contribution */
174   }
175  
176  
177 < extern int
177 > int
178   m_normal(                       /* color a ray that hit something normal */
179 <        register OBJREC  *m,
180 <        register RAY  *r
179 >        OBJREC  *m,
180 >        RAY  *r
181   )
182   {
183          NORMDAT  nd;
# Line 181 | Line 187 | m_normal(                      /* color a ray that hit something normal *
187          int     hastexture;
188          double  d;
189          COLOR  ctmp;
190 <        register int  i;
190 >        int  i;
191 >
192 >        /* PMAP: skip transmitted shadow ray if accounted for in photon map */
193 >        if (shadowRayInPmap(r))
194 >                return(1);
195                                                  /* easy shadow test */
196          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
197                  return(1);
# Line 190 | Line 200 | m_normal(                      /* color a ray that hit something normal *
200                  objerror(m, USER, "bad number of arguments");
201                                                  /* check for back side */
202          if (r->rod < 0.0) {
203 <                if (!backvis && m->otype != MAT_TRANS) {
203 >                if (!backvis) {
204                          raytrans(r);
205                          return(1);
206                  }
# Line 225 | Line 235 | m_normal(                      /* color a ray that hit something normal *
235          mirdist = transdist = r->rot;
236          nd.rspec = m->oargs.farg[3];
237                                                  /* compute Fresnel approx. */
238 <        if (nd.specfl & SP_PURE && nd.rspec > FTINY) {
239 <                fest = FRESNE(r->rod);
238 >        if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) {
239 >                fest = FRESNE(nd.pdot);
240                  nd.rspec += fest*(1. - nd.rspec);
241          } else
242                  fest = 0.;
# Line 241 | Line 251 | m_normal(                      /* color a ray that hit something normal *
251                          if (!(nd.specfl & SP_PURE) &&
252                                          specthresh >= nd.tspec-FTINY)
253                                  nd.specfl |= SP_TBLT;
254 <                        if (!hastexture || r->crtype & SHADOW) {
254 >                        if (!hastexture || r->crtype & (SHADOW|AMBIENT)) {
255                                  VCOPY(nd.prdir, r->rdir);
256                                  transtest = 2;
257                          } else {
# Line 256 | Line 266 | m_normal(                      /* color a ray that hit something normal *
266          } else
267                  nd.tdiff = nd.tspec = nd.trans = 0.0;
268                                                  /* transmitted ray */
269 +
270          if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
271                  RAY  lr;
272                  copycolor(lr.rcoef, nd.mcolor); /* modified by color */
# Line 282 | Line 293 | m_normal(                      /* color a ray that hit something normal *
293                  if (m->otype != MAT_METAL) {
294                          setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
295                  } else if (fest > FTINY) {
296 <                        d = nd.rspec*(1. - fest);
296 >                        d = m->oargs.farg[3]*(1. - fest);
297                          for (i = 0; i < 3; i++)
298 <                                nd.scolor[i] = fest + nd.mcolor[i]*d;
298 >                                colval(nd.scolor,i) = fest +
299 >                                                colval(nd.mcolor,i)*d;
300                  } else {
301                          copycolor(nd.scolor, nd.mcolor);
302                          scalecolor(nd.scolor, nd.rspec);
# Line 293 | Line 305 | m_normal(                      /* color a ray that hit something normal *
305                  if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
306                          nd.specfl |= SP_RBLT;
307                                                  /* compute reflected ray */
308 <                for (i = 0; i < 3; i++)
297 <                        nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i];
308 >                VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.*nd.pdot);
309                                                  /* penetration? */
310                  if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
311 <                        for (i = 0; i < 3; i++)         /* safety measure */
312 <                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
311 >                        VSUM(nd.vrefl, r->rdir, r->ron, 2.*r->rod);
312 >                checknorm(nd.vrefl);
313          }
314                                                  /* reflected ray */
315          if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) {
# Line 308 | Line 319 | m_normal(                      /* color a ray that hit something normal *
319                          rayvalue(&lr);
320                          multcolor(lr.rcol, lr.rcoef);
321                          addcolor(r->rcol, lr.rcol);
322 <                        if (!hastexture && nd.specfl & SP_FLAT) {
322 >                        if (nd.specfl & SP_FLAT &&
323 >                                        !hastexture | (r->crtype & AMBIENT)) {
324                                  mirtest = 2.*bright(lr.rcol);
325                                  mirdist = r->rot + lr.rt;
326                          }
# Line 321 | Line 333 | m_normal(                      /* color a ray that hit something normal *
333                  return(1);                      /* 100% pure specular */
334  
335          if (!(nd.specfl & SP_PURE))
336 <                gaussamp(r, &nd);               /* checks *BLT flags */
336 >                gaussamp(&nd);                  /* checks *BLT flags */
337  
338          if (nd.rdiff > FTINY) {         /* ambient from this side */
339                  copycolor(ctmp, nd.mcolor);     /* modified by material color */
340 <                if (nd.specfl & SP_RBLT)
341 <                        scalecolor(ctmp, 1.0-nd.trans);
342 <                else
331 <                        scalecolor(ctmp, nd.rdiff);
340 >                scalecolor(ctmp, nd.rdiff);
341 >                if (nd.specfl & SP_RBLT)        /* add in specular as well? */
342 >                        addcolor(ctmp, nd.scolor);
343                  multambient(ctmp, r, hastexture ? nd.pnorm : r->ron);
344                  addcolor(r->rcol, ctmp);        /* add to returned color */
345          }
# Line 364 | Line 375 | m_normal(                      /* color a ray that hit something normal *
375  
376  
377   static void
378 < gaussamp(                       /* sample gaussian specular */
379 <        RAY  *r,
369 <        register NORMDAT  *np
378 > gaussamp(                       /* sample Gaussian specular */
379 >        NORMDAT  *np
380   )
381   {
382          RAY  sr;
383          FVECT  u, v, h;
384          double  rv[2];
385          double  d, sinp, cosp;
386 <        int  niter;
387 <        register int  i;
386 >        COLOR  scol;
387 >        int  maxiter, ntrials, nstarget, nstaken;
388 >        int  i;
389                                          /* quick test */
390          if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
391                          (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
392                  return;
393                                          /* set up sample coordinates */
394 <        v[0] = v[1] = v[2] = 0.0;
384 <        for (i = 0; i < 3; i++)
385 <                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
386 <                        break;
387 <        v[i] = 1.0;
388 <        fcross(u, v, np->pnorm);
389 <        normalize(u);
394 >        getperpendicular(u, np->pnorm, rand_samp);
395          fcross(v, np->pnorm, u);
396                                          /* compute reflection */
397          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
398 <                        rayorigin(&sr, SPECULAR, r, np->scolor) == 0) {
399 <                dimlist[ndims++] = (int)np->mp;
400 <                for (niter = 0; niter < MAXITER; niter++) {
401 <                        if (niter)
398 >                        rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) {
399 >                nstarget = 1;
400 >                if (specjitter > 1.5) { /* multiple samples? */
401 >                        nstarget = specjitter*np->rp->rweight + .5;
402 >                        if (sr.rweight <= minweight*nstarget)
403 >                                nstarget = sr.rweight/minweight;
404 >                        if (nstarget > 1) {
405 >                                d = 1./nstarget;
406 >                                scalecolor(sr.rcoef, d);
407 >                                sr.rweight *= d;
408 >                        } else
409 >                                nstarget = 1;
410 >                }
411 >                setcolor(scol, 0., 0., 0.);
412 >                dimlist[ndims++] = (int)(size_t)np->mp;
413 >                maxiter = MAXITER*nstarget;
414 >                for (nstaken = ntrials = 0; nstaken < nstarget &&
415 >                                                ntrials < maxiter; ntrials++) {
416 >                        if (ntrials)
417                                  d = frandom();
418                          else
419                                  d = urand(ilhash(dimlist,ndims)+samplendx);
# Line 401 | Line 421 | gaussamp(                      /* sample gaussian specular */
421                          d = 2.0*PI * rv[0];
422                          cosp = tcos(d);
423                          sinp = tsin(d);
424 <                        rv[1] = 1.0 - specjitter*rv[1];
424 >                        if ((0. <= specjitter) & (specjitter < 1.))
425 >                                rv[1] = 1.0 - specjitter*rv[1];
426                          if (rv[1] <= FTINY)
427                                  d = 1.0;
428                          else
429                                  d = sqrt( np->alpha2 * -log(rv[1]) );
430                          for (i = 0; i < 3; i++)
431                                  h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
432 <                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
433 <                        for (i = 0; i < 3; i++)
434 <                                sr.rdir[i] = r->rdir[i] + d*h[i];
435 <                        if (DOT(sr.rdir, r->ron) > FTINY) {
432 >                        d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d);
433 >                        VSUM(sr.rdir, np->rp->rdir, h, d);
434 >                                                /* sample rejection test */
435 >                        if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY)
436 >                                continue;
437 >                        checknorm(sr.rdir);
438 >                        if (nstarget > 1) {     /* W-G-M-D adjustment */
439 >                                if (nstaken) rayclear(&sr);
440                                  rayvalue(&sr);
441 +                                d = 2./(1. + np->rp->rod/d);
442 +                                scalecolor(sr.rcol, d);
443 +                                addcolor(scol, sr.rcol);
444 +                        } else {
445 +                                rayvalue(&sr);
446                                  multcolor(sr.rcol, sr.rcoef);
447 <                                addcolor(r->rcol, sr.rcol);
418 <                                break;
447 >                                addcolor(np->rp->rcol, sr.rcol);
448                          }
449 +                        ++nstaken;
450                  }
451 +                if (nstarget > 1) {             /* final W-G-M-D weighting */
452 +                        multcolor(scol, sr.rcoef);
453 +                        d = (double)nstarget/ntrials;
454 +                        scalecolor(scol, d);
455 +                        addcolor(np->rp->rcol, scol);
456 +                }
457                  ndims--;
458          }
459                                          /* compute transmission */
460          copycolor(sr.rcoef, np->mcolor);        /* modified by color */
461          scalecolor(sr.rcoef, np->tspec);
462          if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
463 <                        rayorigin(&sr, SPECULAR, r, sr.rcoef) == 0) {
464 <                dimlist[ndims++] = (int)np->mp;
465 <                for (niter = 0; niter < MAXITER; niter++) {
466 <                        if (niter)
463 >                        rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) {
464 >                nstarget = 1;
465 >                if (specjitter > 1.5) { /* multiple samples? */
466 >                        nstarget = specjitter*np->rp->rweight + .5;
467 >                        if (sr.rweight <= minweight*nstarget)
468 >                                nstarget = sr.rweight/minweight;
469 >                        if (nstarget > 1) {
470 >                                d = 1./nstarget;
471 >                                scalecolor(sr.rcoef, d);
472 >                                sr.rweight *= d;
473 >                        } else
474 >                                nstarget = 1;
475 >                }
476 >                dimlist[ndims++] = (int)(size_t)np->mp;
477 >                maxiter = MAXITER*nstarget;
478 >                for (nstaken = ntrials = 0; nstaken < nstarget &&
479 >                                                ntrials < maxiter; ntrials++) {
480 >                        if (ntrials)
481                                  d = frandom();
482                          else
483 <                                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
483 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
484                          multisamp(rv, 2, d);
485                          d = 2.0*PI * rv[0];
486                          cosp = tcos(d);
487                          sinp = tsin(d);
488 <                        rv[1] = 1.0 - specjitter*rv[1];
488 >                        if ((0. <= specjitter) & (specjitter < 1.))
489 >                                rv[1] = 1.0 - specjitter*rv[1];
490                          if (rv[1] <= FTINY)
491                                  d = 1.0;
492                          else
493                                  d = sqrt( np->alpha2 * -log(rv[1]) );
494                          for (i = 0; i < 3; i++)
495                                  sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
496 <                        if (DOT(sr.rdir, r->ron) < -FTINY) {
497 <                                normalize(sr.rdir);     /* OK, normalize */
498 <                                rayvalue(&sr);
499 <                                multcolor(sr.rcol, sr.rcoef);
500 <                                addcolor(r->rcol, sr.rcol);
501 <                                break;
502 <                        }
496 >                                                /* sample rejection test */
497 >                        if (DOT(sr.rdir, np->rp->ron) >= -FTINY)
498 >                                continue;
499 >                        normalize(sr.rdir);     /* OK, normalize */
500 >                        if (nstaken)            /* multi-sampling */
501 >                                rayclear(&sr);
502 >                        rayvalue(&sr);
503 >                        multcolor(sr.rcol, sr.rcoef);
504 >                        addcolor(np->rp->rcol, sr.rcol);
505 >                        ++nstaken;
506                  }
507                  ndims--;
508          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines