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.52 by greg, Fri May 7 15:44:52 2010 UTC vs.
Revision 2.76 by greg, Wed Jan 10 04:08:50 2018 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 */
# Line 65 | Line 66 | typedef struct {
66          double  pdot;           /* perturbed dot product */
67   }  NORMDAT;             /* normal material data */
68  
69 < static srcdirf_t dirnorm;
69 < 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 112 | 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 123 | 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];
127 <                vtmp[1] = ldir[1] - np->rp->rdir[1];
128 <                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 <                /*
144 <                 *  Compute diffuse transmission.
145 <                 */
146 <                copycolor(ctmp, np->mcolor);
147 <                dtmp = -ldot * omega * ltdiff * (1.0/PI);
148 <                scalecolor(ctmp, dtmp);
149 <                addcolor(cval, ctmp);
150 <        }
155 >        
156 >
157          if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
158                  /*
159                   *  Compute specular transmission.  Specular transmission
# Line 155 | 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)/(PI*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);
# 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 >        /* No longer needed?
194 >        if (shadowRayInPmap(r) || ambRayInPmap(r))
195 >                return(1); */          
196 >                
197                                                  /* easy shadow test */
198          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
199                  return(1);
# Line 190 | Line 202 | m_normal(                      /* color a ray that hit something normal *
202                  objerror(m, USER, "bad number of arguments");
203                                                  /* check for back side */
204          if (r->rod < 0.0) {
205 <                if (!backvis && m->otype != MAT_TRANS) {
205 >                if (!backvis) {
206                          raytrans(r);
207                          return(1);
208                  }
# Line 226 | Line 238 | m_normal(                      /* color a ray that hit something normal *
238          nd.rspec = m->oargs.farg[3];
239                                                  /* compute Fresnel approx. */
240          if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) {
241 <                fest = FRESNE(r->rod);
241 >                fest = FRESNE(nd.pdot);
242                  nd.rspec += fest*(1. - nd.rspec);
243          } else
244                  fest = 0.;
# Line 241 | Line 253 | m_normal(                      /* color a ray that hit something normal *
253                          if (!(nd.specfl & SP_PURE) &&
254                                          specthresh >= nd.tspec-FTINY)
255                                  nd.specfl |= SP_TBLT;
256 <                        if (!hastexture || r->crtype & SHADOW) {
256 >                        if (!hastexture || r->crtype & (SHADOW|AMBIENT)) {
257                                  VCOPY(nd.prdir, r->rdir);
258                                  transtest = 2;
259                          } else {
260 <                                for (i = 0; i < 3; i++)         /* perturb */
261 <                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
260 >                                                        /* perturb */
261 >                                VSUB(nd.prdir, r->rdir, r->pert);
262                                  if (DOT(nd.prdir, r->ron) < -FTINY)
263                                          normalize(nd.prdir);    /* OK */
264                                  else
# Line 256 | Line 268 | m_normal(                      /* color a ray that hit something normal *
268          } else
269                  nd.tdiff = nd.tspec = nd.trans = 0.0;
270                                                  /* transmitted ray */
271 +
272          if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
273                  RAY  lr;
274                  copycolor(lr.rcoef, nd.mcolor); /* modified by color */
# Line 282 | Line 295 | m_normal(                      /* color a ray that hit something normal *
295                  if (m->otype != MAT_METAL) {
296                          setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
297                  } else if (fest > FTINY) {
298 <                        d = nd.rspec*(1. - fest);
298 >                        d = m->oargs.farg[3]*(1. - fest);
299                          for (i = 0; i < 3; i++)
300 <                                nd.scolor[i] = fest + nd.mcolor[i]*d;
300 >                                colval(nd.scolor,i) = fest +
301 >                                                colval(nd.mcolor,i)*d;
302                  } else {
303                          copycolor(nd.scolor, nd.mcolor);
304                          scalecolor(nd.scolor, nd.rspec);
# Line 293 | Line 307 | m_normal(                      /* color a ray that hit something normal *
307                  if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
308                          nd.specfl |= SP_RBLT;
309                                                  /* compute reflected ray */
310 <                for (i = 0; i < 3; i++)
297 <                        nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i];
310 >                VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.*nd.pdot);
311                                                  /* penetration? */
312                  if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
313 <                        for (i = 0; i < 3; i++)         /* safety measure */
314 <                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
313 >                        VSUM(nd.vrefl, r->rdir, r->ron, 2.*r->rod);
314 >                checknorm(nd.vrefl);
315          }
316                                                  /* reflected ray */
317          if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) {
# Line 308 | Line 321 | m_normal(                      /* color a ray that hit something normal *
321                          rayvalue(&lr);
322                          multcolor(lr.rcol, lr.rcoef);
323                          addcolor(r->rcol, lr.rcol);
324 <                        if (!hastexture && nd.specfl & SP_FLAT) {
324 >                        if (nd.specfl & SP_FLAT &&
325 >                                        !hastexture | (r->crtype & AMBIENT)) {
326                                  mirtest = 2.*bright(lr.rcol);
327                                  mirdist = r->rot + lr.rt;
328                          }
# Line 317 | Line 331 | m_normal(                      /* color a ray that hit something normal *
331                                                  /* diffuse reflection */
332          nd.rdiff = 1.0 - nd.trans - nd.rspec;
333  
334 <        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
334 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) {
335 >                if (mirtest > transtest+FTINY)
336 >                        r->rt = mirdist;
337 >                else if (transtest > FTINY)
338 >                        r->rt = transdist;
339                  return(1);                      /* 100% pure specular */
340 <
340 >        }
341          if (!(nd.specfl & SP_PURE))
342 <                gaussamp(r, &nd);               /* checks *BLT flags */
342 >                gaussamp(&nd);                  /* checks *BLT flags */
343  
344          if (nd.rdiff > FTINY) {         /* ambient from this side */
345                  copycolor(ctmp, nd.mcolor);     /* modified by material color */
346 <                if (nd.specfl & SP_RBLT)
347 <                        scalecolor(ctmp, 1.0-nd.trans);
348 <                else
331 <                        scalecolor(ctmp, nd.rdiff);
346 >                scalecolor(ctmp, nd.rdiff);
347 >                if (nd.specfl & SP_RBLT)        /* add in specular as well? */
348 >                        addcolor(ctmp, nd.scolor);
349                  multambient(ctmp, r, hastexture ? nd.pnorm : r->ron);
350                  addcolor(r->rcol, ctmp);        /* add to returned color */
351          }
# Line 364 | Line 381 | m_normal(                      /* color a ray that hit something normal *
381  
382  
383   static void
384 < gaussamp(                       /* sample gaussian specular */
385 <        RAY  *r,
369 <        register NORMDAT  *np
384 > gaussamp(                       /* sample Gaussian specular */
385 >        NORMDAT  *np
386   )
387   {
388          RAY  sr;
389          FVECT  u, v, h;
390          double  rv[2];
391          double  d, sinp, cosp;
392 <        int  niter;
393 <        register int  i;
392 >        COLOR  scol;
393 >        int  maxiter, ntrials, nstarget, nstaken;
394 >        int  i;
395                                          /* quick test */
396          if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
397                          (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
398                  return;
399                                          /* set up sample coordinates */
400 <        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);
400 >        getperpendicular(u, np->pnorm, rand_samp);
401          fcross(v, np->pnorm, u);
402                                          /* compute reflection */
403          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
404 <                        rayorigin(&sr, SPECULAR, r, np->scolor) == 0) {
405 <                dimlist[ndims++] = (int)np->mp;
406 <                for (niter = 0; niter < MAXITER; niter++) {
407 <                        if (niter)
404 >                        rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) {
405 >                nstarget = 1;
406 >                if (specjitter > 1.5) { /* multiple samples? */
407 >                        nstarget = specjitter*np->rp->rweight + .5;
408 >                        if (sr.rweight <= minweight*nstarget)
409 >                                nstarget = sr.rweight/minweight;
410 >                        if (nstarget > 1) {
411 >                                d = 1./nstarget;
412 >                                scalecolor(sr.rcoef, d);
413 >                                sr.rweight *= d;
414 >                        } else
415 >                                nstarget = 1;
416 >                }
417 >                setcolor(scol, 0., 0., 0.);
418 >                dimlist[ndims++] = (int)(size_t)np->mp;
419 >                maxiter = MAXITER*nstarget;
420 >                for (nstaken = ntrials = 0; nstaken < nstarget &&
421 >                                                ntrials < maxiter; ntrials++) {
422 >                        if (ntrials)
423                                  d = frandom();
424                          else
425                                  d = urand(ilhash(dimlist,ndims)+samplendx);
# Line 401 | Line 427 | gaussamp(                      /* sample gaussian specular */
427                          d = 2.0*PI * rv[0];
428                          cosp = tcos(d);
429                          sinp = tsin(d);
430 <                        rv[1] = 1.0 - specjitter*rv[1];
430 >                        if ((0. <= specjitter) & (specjitter < 1.))
431 >                                rv[1] = 1.0 - specjitter*rv[1];
432                          if (rv[1] <= FTINY)
433                                  d = 1.0;
434                          else
435                                  d = sqrt( np->alpha2 * -log(rv[1]) );
436                          for (i = 0; i < 3; i++)
437                                  h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
438 <                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
439 <                        for (i = 0; i < 3; i++)
440 <                                sr.rdir[i] = r->rdir[i] + d*h[i];
441 <                        if (DOT(sr.rdir, r->ron) > FTINY) {
438 >                        d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d);
439 >                        VSUM(sr.rdir, np->rp->rdir, h, d);
440 >                                                /* sample rejection test */
441 >                        if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY)
442 >                                continue;
443 >                        checknorm(sr.rdir);
444 >                        if (nstarget > 1) {     /* W-G-M-D adjustment */
445 >                                if (nstaken) rayclear(&sr);
446                                  rayvalue(&sr);
447 +                                d = 2./(1. + np->rp->rod/d);
448 +                                scalecolor(sr.rcol, d);
449 +                                addcolor(scol, sr.rcol);
450 +                        } else {
451 +                                rayvalue(&sr);
452                                  multcolor(sr.rcol, sr.rcoef);
453 <                                addcolor(r->rcol, sr.rcol);
418 <                                break;
453 >                                addcolor(np->rp->rcol, sr.rcol);
454                          }
455 +                        ++nstaken;
456                  }
457 +                if (nstarget > 1) {             /* final W-G-M-D weighting */
458 +                        multcolor(scol, sr.rcoef);
459 +                        d = (double)nstarget/ntrials;
460 +                        scalecolor(scol, d);
461 +                        addcolor(np->rp->rcol, scol);
462 +                }
463                  ndims--;
464          }
465                                          /* compute transmission */
466          copycolor(sr.rcoef, np->mcolor);        /* modified by color */
467          scalecolor(sr.rcoef, np->tspec);
468          if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
469 <                        rayorigin(&sr, SPECULAR, r, sr.rcoef) == 0) {
470 <                dimlist[ndims++] = (int)np->mp;
471 <                for (niter = 0; niter < MAXITER; niter++) {
472 <                        if (niter)
469 >                        rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) {
470 >                nstarget = 1;
471 >                if (specjitter > 1.5) { /* multiple samples? */
472 >                        nstarget = specjitter*np->rp->rweight + .5;
473 >                        if (sr.rweight <= minweight*nstarget)
474 >                                nstarget = sr.rweight/minweight;
475 >                        if (nstarget > 1) {
476 >                                d = 1./nstarget;
477 >                                scalecolor(sr.rcoef, d);
478 >                                sr.rweight *= d;
479 >                        } else
480 >                                nstarget = 1;
481 >                }
482 >                dimlist[ndims++] = (int)(size_t)np->mp;
483 >                maxiter = MAXITER*nstarget;
484 >                for (nstaken = ntrials = 0; nstaken < nstarget &&
485 >                                                ntrials < maxiter; ntrials++) {
486 >                        if (ntrials)
487                                  d = frandom();
488                          else
489 <                                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
489 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
490                          multisamp(rv, 2, d);
491                          d = 2.0*PI * rv[0];
492                          cosp = tcos(d);
493                          sinp = tsin(d);
494 <                        rv[1] = 1.0 - specjitter*rv[1];
494 >                        if ((0. <= specjitter) & (specjitter < 1.))
495 >                                rv[1] = 1.0 - specjitter*rv[1];
496                          if (rv[1] <= FTINY)
497                                  d = 1.0;
498                          else
499                                  d = sqrt( np->alpha2 * -log(rv[1]) );
500                          for (i = 0; i < 3; i++)
501                                  sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
502 <                        if (DOT(sr.rdir, r->ron) < -FTINY) {
503 <                                normalize(sr.rdir);     /* OK, normalize */
504 <                                rayvalue(&sr);
505 <                                multcolor(sr.rcol, sr.rcoef);
506 <                                addcolor(r->rcol, sr.rcol);
507 <                                break;
508 <                        }
502 >                                                /* sample rejection test */
503 >                        if (DOT(sr.rdir, np->rp->ron) >= -FTINY)
504 >                                continue;
505 >                        normalize(sr.rdir);     /* OK, normalize */
506 >                        if (nstaken)            /* multi-sampling */
507 >                                rayclear(&sr);
508 >                        rayvalue(&sr);
509 >                        multcolor(sr.rcol, sr.rcoef);
510 >                        addcolor(np->rp->rcol, sr.rcol);
511 >                        ++nstaken;
512                  }
513                  ndims--;
514          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines