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.57 by greg, Sun Oct 10 22:31:46 2010 UTC vs.
Revision 2.77 by greg, Tue Nov 13 19:58:33 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, d3, d4;
# 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
# 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                  d3 = DOT(vtmp,vtmp);
# Line 140 | Line 152 | dirnorm(               /* compute source contribution */
152                          addcolor(cval, ctmp);
153                  }
154          }
155 <        if (ldot < -FTINY && ltdiff > FTINY) {
156 <                /*
145 <                 *  Compute diffuse transmission.
146 <                 */
147 <                copycolor(ctmp, np->mcolor);
148 <                dtmp = -ldot * omega * ltdiff * (1.0/PI);
149 <                scalecolor(ctmp, dtmp);
150 <                addcolor(cval, ctmp);
151 <        }
155 >        
156 >
157          if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
158                  /*
159                   *  Compute specular transmission.  Specular transmission
# Line 169 | 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;
184          double  fest;
180        double  transtest, transdist;
181        double  mirtest, mirdist;
185          int     hastexture;
186          double  d;
187          COLOR  ctmp;
188 <        register int  i;
188 >        int  i;
189 >
190 >        /* PMAP: skip transmitted shadow ray if accounted for in photon map */
191 >        /* No longer needed?
192 >        if (shadowRayInPmap(r) || ambRayInPmap(r))
193 >                return(1); */          
194 >                
195                                                  /* easy shadow test */
196          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
197                  return(1);
# Line 191 | 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 222 | Line 231 | m_normal(                      /* color a ray that hit something normal *
231          if (nd.pdot < .001)
232                  nd.pdot = .001;                 /* non-zero for dirnorm() */
233          multcolor(nd.mcolor, r->pcol);          /* modify material color */
225        mirtest = transtest = 0;
226        mirdist = transdist = r->rot;
234          nd.rspec = m->oargs.farg[3];
235                                                  /* compute Fresnel approx. */
236          if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) {
237 <                fest = FRESNE(r->rod);
237 >                fest = FRESNE(nd.pdot);
238                  nd.rspec += fest*(1. - nd.rspec);
239          } else
240                  fest = 0.;
# Line 242 | Line 249 | m_normal(                      /* color a ray that hit something normal *
249                          if (!(nd.specfl & SP_PURE) &&
250                                          specthresh >= nd.tspec-FTINY)
251                                  nd.specfl |= SP_TBLT;
252 <                        if (!hastexture || r->crtype & SHADOW) {
252 >                        if (!hastexture || r->crtype & (SHADOW|AMBIENT)) {
253                                  VCOPY(nd.prdir, r->rdir);
247                                transtest = 2;
254                          } else {
255 <                                for (i = 0; i < 3; i++)         /* perturb */
256 <                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
255 >                                                        /* perturb */
256 >                                VSUB(nd.prdir, r->rdir, r->pert);
257                                  if (DOT(nd.prdir, r->ron) < -FTINY)
258                                          normalize(nd.prdir);    /* OK */
259                                  else
# Line 257 | Line 263 | m_normal(                      /* color a ray that hit something normal *
263          } else
264                  nd.tdiff = nd.tspec = nd.trans = 0.0;
265                                                  /* transmitted ray */
266 +
267          if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
268                  RAY  lr;
269                  copycolor(lr.rcoef, nd.mcolor); /* modified by color */
# Line 266 | Line 273 | m_normal(                      /* color a ray that hit something normal *
273                          rayvalue(&lr);
274                          multcolor(lr.rcol, lr.rcoef);
275                          addcolor(r->rcol, lr.rcol);
276 <                        transtest *= bright(lr.rcol);
270 <                        transdist = r->rot + lr.rt;
276 >                        r->rxt = r->rot + raydistance(&lr);
277                  }
278 <        } else
273 <                transtest = 0;
278 >        }
279  
280 <        if (r->crtype & SHADOW) {               /* the rest is shadow */
276 <                r->rt = transdist;
280 >        if (r->crtype & SHADOW)                 /* the rest is shadow */
281                  return(1);
278        }
282                                                  /* get specular reflection */
283          if (nd.rspec > FTINY) {
284                  nd.specfl |= SP_REFL;
# Line 283 | Line 286 | m_normal(                      /* color a ray that hit something normal *
286                  if (m->otype != MAT_METAL) {
287                          setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
288                  } else if (fest > FTINY) {
289 <                        d = nd.rspec*(1. - fest);
289 >                        d = m->oargs.farg[3]*(1. - fest);
290                          for (i = 0; i < 3; i++)
291 <                                nd.scolor[i] = fest + nd.mcolor[i]*d;
291 >                                colval(nd.scolor,i) = fest +
292 >                                                colval(nd.mcolor,i)*d;
293                  } else {
294                          copycolor(nd.scolor, nd.mcolor);
295                          scalecolor(nd.scolor, nd.rspec);
# Line 307 | Line 311 | m_normal(                      /* color a ray that hit something normal *
311                          VCOPY(lr.rdir, nd.vrefl);
312                          rayvalue(&lr);
313                          multcolor(lr.rcol, lr.rcoef);
314 +                        copycolor(r->mcol, lr.rcol);
315                          addcolor(r->rcol, lr.rcol);
316 <                        if (!hastexture && nd.specfl & SP_FLAT) {
317 <                                mirtest = 2.*bright(lr.rcol);
318 <                                mirdist = r->rot + lr.rt;
314 <                        }
316 >                        if (nd.specfl & SP_FLAT &&
317 >                                        !hastexture | (r->crtype & AMBIENT))
318 >                                r->rmt = r->rot + raydistance(&lr);
319                  }
320          }
321                                                  /* diffuse reflection */
# Line 321 | Line 325 | m_normal(                      /* color a ray that hit something normal *
325                  return(1);                      /* 100% pure specular */
326  
327          if (!(nd.specfl & SP_PURE))
328 <                gaussamp(r, &nd);               /* checks *BLT flags */
328 >                gaussamp(&nd);                  /* checks *BLT flags */
329  
330          if (nd.rdiff > FTINY) {         /* ambient from this side */
331                  copycolor(ctmp, nd.mcolor);     /* modified by material color */
332 <                if (nd.specfl & SP_RBLT)
333 <                        scalecolor(ctmp, 1.0-nd.trans);
334 <                else
331 <                        scalecolor(ctmp, nd.rdiff);
332 >                scalecolor(ctmp, nd.rdiff);
333 >                if (nd.specfl & SP_RBLT)        /* add in specular as well? */
334 >                        addcolor(ctmp, nd.scolor);
335                  multambient(ctmp, r, hastexture ? nd.pnorm : r->ron);
336                  addcolor(r->rcol, ctmp);        /* add to returned color */
337          }
# Line 352 | Line 355 | m_normal(                      /* color a ray that hit something normal *
355          }
356                                          /* add direct component */
357          direct(r, dirnorm, &nd);
355                                        /* check distance */
356        d = bright(r->rcol);
357        if (transtest > d)
358                r->rt = transdist;
359        else if (mirtest > d)
360                r->rt = mirdist;
358  
359          return(1);
360   }
# Line 365 | Line 362 | m_normal(                      /* color a ray that hit something normal *
362  
363   static void
364   gaussamp(                       /* sample Gaussian specular */
365 <        RAY  *r,
369 <        register NORMDAT  *np
365 >        NORMDAT  *np
366   )
367   {
368          RAY  sr;
369          FVECT  u, v, h;
370          double  rv[2];
371          double  d, sinp, cosp;
372 <        COLOR   scol;
373 <        int  niter, ns2go;
374 <        register int  i;
372 >        COLOR  scol;
373 >        int  maxiter, ntrials, nstarget, nstaken;
374 >        int  i;
375                                          /* quick test */
376          if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
377                          (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
378                  return;
379                                          /* set up sample coordinates */
380 <        v[0] = v[1] = v[2] = 0.0;
385 <        for (i = 0; i < 3; i++)
386 <                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
387 <                        break;
388 <        v[i] = 1.0;
389 <        fcross(u, v, np->pnorm);
390 <        normalize(u);
380 >        getperpendicular(u, np->pnorm, rand_samp);
381          fcross(v, np->pnorm, u);
382                                          /* compute reflection */
383          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
384 <                        rayorigin(&sr, SPECULAR, r, np->scolor) == 0) {
385 <                copycolor(scol, np->scolor);
396 <                ns2go = 1;
384 >                        rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) {
385 >                nstarget = 1;
386                  if (specjitter > 1.5) { /* multiple samples? */
387 <                        ns2go = specjitter*r->rweight + .5;
388 <                        if (sr.rweight <= minweight*ns2go)
389 <                                ns2go = sr.rweight/minweight;
390 <                        if (ns2go > 1) {
391 <                                d = 1./ns2go;
392 <                                scalecolor(scol, d);
387 >                        nstarget = specjitter*np->rp->rweight + .5;
388 >                        if (sr.rweight <= minweight*nstarget)
389 >                                nstarget = sr.rweight/minweight;
390 >                        if (nstarget > 1) {
391 >                                d = 1./nstarget;
392 >                                scalecolor(sr.rcoef, d);
393                                  sr.rweight *= d;
394                          } else
395 <                                ns2go = 1;
395 >                                nstarget = 1;
396                  }
397 <                dimlist[ndims++] = (int)np->mp;
398 <                for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) {
399 <                        if (specjitter > 1.5)
397 >                setcolor(scol, 0., 0., 0.);
398 >                dimlist[ndims++] = (int)(size_t)np->mp;
399 >                maxiter = MAXITER*nstarget;
400 >                for (nstaken = ntrials = 0; nstaken < nstarget &&
401 >                                                ntrials < maxiter; ntrials++) {
402 >                        if (ntrials)
403                                  d = frandom();
404                          else
405                                  d = urand(ilhash(dimlist,ndims)+samplendx);
# Line 423 | Line 415 | gaussamp(                      /* sample Gaussian specular */
415                                  d = sqrt( np->alpha2 * -log(rv[1]) );
416                          for (i = 0; i < 3; i++)
417                                  h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
418 <                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
419 <                        if (d <= np->pdot + FTINY)
418 >                        d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d);
419 >                        VSUM(sr.rdir, np->rp->rdir, h, d);
420 >                                                /* sample rejection test */
421 >                        if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY)
422                                  continue;
429                        VSUM(sr.rdir, r->rdir, h, d);
430                        if (DOT(sr.rdir, r->ron) <= FTINY)
431                                continue;
423                          checknorm(sr.rdir);
424 <                        if (specjitter > 1.5) { /* adjusted W-G-M-D weight */
425 <                                d = 2.*(1. - np->pdot/d);
426 <                                copycolor(sr.rcoef, scol);
427 <                                scalecolor(sr.rcoef, d);
428 <                                rayclear(&sr);
424 >                        if (nstarget > 1) {     /* W-G-M-D adjustment */
425 >                                if (nstaken) rayclear(&sr);
426 >                                rayvalue(&sr);
427 >                                d = 2./(1. + np->rp->rod/d);
428 >                                scalecolor(sr.rcol, d);
429 >                                addcolor(scol, sr.rcol);
430 >                        } else {
431 >                                rayvalue(&sr);
432 >                                multcolor(sr.rcol, sr.rcoef);
433 >                                addcolor(np->rp->rcol, sr.rcol);
434                          }
435 <                        rayvalue(&sr);
440 <                        multcolor(sr.rcol, sr.rcoef);
441 <                        addcolor(r->rcol, sr.rcol);
442 <                        --ns2go;
435 >                        ++nstaken;
436                  }
437 +                if (nstarget > 1) {             /* final W-G-M-D weighting */
438 +                        multcolor(scol, sr.rcoef);
439 +                        d = (double)nstarget/ntrials;
440 +                        scalecolor(scol, d);
441 +                        addcolor(np->rp->rcol, scol);
442 +                }
443                  ndims--;
444          }
445                                          /* compute transmission */
446          copycolor(sr.rcoef, np->mcolor);        /* modified by color */
447          scalecolor(sr.rcoef, np->tspec);
448          if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
449 <                        rayorigin(&sr, SPECULAR, r, sr.rcoef) == 0) {
450 <                ns2go = 1;
449 >                        rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) {
450 >                nstarget = 1;
451                  if (specjitter > 1.5) { /* multiple samples? */
452 <                        ns2go = specjitter*r->rweight + .5;
453 <                        if (sr.rweight <= minweight*ns2go)
454 <                                ns2go = sr.rweight/minweight;
455 <                        if (ns2go > 1) {
456 <                                d = 1./ns2go;
452 >                        nstarget = specjitter*np->rp->rweight + .5;
453 >                        if (sr.rweight <= minweight*nstarget)
454 >                                nstarget = sr.rweight/minweight;
455 >                        if (nstarget > 1) {
456 >                                d = 1./nstarget;
457                                  scalecolor(sr.rcoef, d);
458                                  sr.rweight *= d;
459                          } else
460 <                                ns2go = 1;
460 >                                nstarget = 1;
461                  }
462 <                dimlist[ndims++] = (int)np->mp;
463 <                for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) {
464 <                        if (specjitter > 1.5)
462 >                dimlist[ndims++] = (int)(size_t)np->mp;
463 >                maxiter = MAXITER*nstarget;
464 >                for (nstaken = ntrials = 0; nstaken < nstarget &&
465 >                                                ntrials < maxiter; ntrials++) {
466 >                        if (ntrials)
467                                  d = frandom();
468                          else
469 <                                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
469 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
470                          multisamp(rv, 2, d);
471                          d = 2.0*PI * rv[0];
472                          cosp = tcos(d);
# Line 478 | Line 479 | gaussamp(                      /* sample Gaussian specular */
479                                  d = sqrt( np->alpha2 * -log(rv[1]) );
480                          for (i = 0; i < 3; i++)
481                                  sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
482 <                        if (DOT(sr.rdir, r->ron) >= -FTINY)
482 >                                                /* sample rejection test */
483 >                        if (DOT(sr.rdir, np->rp->ron) >= -FTINY)
484                                  continue;
485                          normalize(sr.rdir);     /* OK, normalize */
486 <                        if (specjitter > 1.5)   /* multi-sampling */
486 >                        if (nstaken)            /* multi-sampling */
487                                  rayclear(&sr);
488                          rayvalue(&sr);
489                          multcolor(sr.rcol, sr.rcoef);
490 <                        addcolor(r->rcol, sr.rcol);
491 <                        --ns2go;
490 >                        addcolor(np->rp->rcol, sr.rcol);
491 >                        ++nstaken;
492                  }
493                  ndims--;
494          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines