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.41 by greg, Wed Mar 12 04:59:05 2003 UTC vs.
Revision 2.72 by greg, Wed Sep 2 18:59:01 2015 UTC

# Line 14 | Line 14 | static const char RCSid[] = "$Id$";
14   #include "copyright.h"
15  
16   #include  "ray.h"
17 <
17 > #include  "ambient.h"
18 > #include  "source.h"
19   #include  "otypes.h"
20 <
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(-6.0*(ci)) - 0.00247875217)
28 > #define  FRESNE(ci)     (exp(-5.85*(ci)) - 0.00287989916)
29 > #define  FRESTHRESH     0.017999        /* minimum specularity for approx. */
30  
28 static void  gaussamp();
31  
32   /*
33   *      This routine implements the isotropic Gaussian
# Line 64 | Line 66 | typedef struct {
66          double  pdot;           /* perturbed dot product */
67   }  NORMDAT;             /* normal material data */
68  
69 + static void gaussamp(NORMDAT  *np);
70  
71 +
72   static void
73 < dirnorm(cval, np, ldir, omega)          /* compute source contribution */
74 < COLOR  cval;                    /* returned coefficient */
75 < register NORMDAT  *np;          /* material data */
76 < FVECT  ldir;                    /* light source direction */
77 < double  omega;                  /* light source size */
73 > dirnorm(                /* compute source contribution */
74 >        COLOR  cval,                    /* returned coefficient */
75 >        void  *nnp,                     /* material data */
76 >        FVECT  ldir,                    /* light source direction */
77 >        double  omega                   /* light source size */
78 > )
79   {
80 +        NORMDAT *np = nnp;
81          double  ldot;
82 <        double  ldiff;
83 <        double  dtmp, d2;
82 >        double  lrdiff, ltdiff;
83 >        double  dtmp, d2, d3, d4;
84          FVECT  vtmp;
85          COLOR  ctmp;
86  
# Line 86 | Line 92 | double  omega;                 /* light source size */
92                  return;         /* wrong side */
93  
94                                  /* Fresnel estimate */
95 <        ldiff = np->rdiff;
96 <        if (np->specfl & SP_PURE && (np->rspec > FTINY & ldiff > FTINY))
97 <                ldiff *= 1. - FRESNE(fabs(ldot));
95 >        lrdiff = np->rdiff;
96 >        ltdiff = np->tdiff;
97 >        if (np->specfl & SP_PURE && np->rspec >= FRESTHRESH &&
98 >                        (lrdiff > FTINY) | (ltdiff > FTINY)) {
99 >                dtmp = 1. - FRESNE(fabs(ldot));
100 >                lrdiff *= dtmp;
101 >                ltdiff *= dtmp;
102 >        }
103  
104 <        if (ldot > FTINY && ldiff > FTINY) {
104 >        if (ldot > FTINY && lrdiff > FTINY) {
105                  /*
106                   *  Compute and add diffuse reflected component to returned
107                   *  color.  The diffuse reflected component will always be
108                   *  modified by the color of the material.
109                   */
110                  copycolor(ctmp, np->mcolor);
111 <                dtmp = ldot * omega * ldiff / PI;
111 >                dtmp = ldot * omega * lrdiff * (1.0/PI);
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;
136                                                  /* + source if flat */
137                  if (np->specfl & SP_FLAT)
138 <                        dtmp += omega/(4.0*PI);
138 >                        dtmp += omega * (0.25/PI);
139                                                  /* half vector */
140 <                vtmp[0] = ldir[0] - np->rp->rdir[0];
116 <                vtmp[1] = ldir[1] - np->rp->rdir[1];
117 <                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*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 * sqrt(ldot/np->pdot);
150 >                        dtmp *= ldot * omega;
151                          scalecolor(ctmp, dtmp);
152                          addcolor(cval, ctmp);
153                  }
154          }
155 <        if (ldot < -FTINY && np->tdiff > FTINY) {
156 <                /*
133 <                 *  Compute diffuse transmission.
134 <                 */
135 <                copycolor(ctmp, np->mcolor);
136 <                dtmp = -ldot * omega * np->tdiff / PI;
137 <                scalecolor(ctmp, dtmp);
138 <                addcolor(cval, ctmp);
139 <        }
155 >        
156 >
157          if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
158                  /*
159                   *  Compute specular transmission.  Specular transmission
160                   *  is always modified by material color.
161                   */
162                                                  /* roughness + source */
163 <                dtmp = np->alpha2 + omega/PI;
164 <                                                /* gaussian */
163 >                dtmp = np->alpha2 + omega*(1.0/PI);
164 >                                                /* Gaussian */
165                  dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp);
166                                                  /* worth using? */
167                  if (dtmp > FTINY) {
# Line 158 | Line 175 | double  omega;                 /* light source size */
175  
176  
177   int
178 < m_normal(m, r)                  /* color a ray that hit something normal */
179 < register OBJREC  *m;
180 < register RAY  *r;
178 > m_normal(                       /* color a ray that hit something normal */
179 >        OBJREC  *m,
180 >        RAY  *r
181 > )
182   {
183          NORMDAT  nd;
184          double  fest;
# Line 169 | Line 187 | register RAY  *r;
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 178 | Line 200 | register RAY  *r;
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 198 | Line 220 | register RAY  *r;
220          if ((nd.alpha2 *= nd.alpha2) <= FTINY)
221                  nd.specfl |= SP_PURE;
222  
223 <        if (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) {
223 >        if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) {
224                  nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
225          } else {
226                  VCOPY(nd.pnorm, r->ron);
227                  nd.pdot = r->rod;
206                if (r->ro != NULL && isflat(r->ro->otype))
207                        nd.specfl |= SP_FLAT;
228          }
229 +        if (r->ro != NULL && isflat(r->ro->otype))
230 +                nd.specfl |= SP_FLAT;
231          if (nd.pdot < .001)
232                  nd.pdot = .001;                 /* non-zero for dirnorm() */
233          multcolor(nd.mcolor, r->pcol);          /* modify material color */
# Line 213 | Line 235 | register RAY  *r;
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 229 | Line 251 | register RAY  *r;
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 244 | Line 266 | register RAY  *r;
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 <                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
272 >                copycolor(lr.rcoef, nd.mcolor); /* modified by color */
273 >                scalecolor(lr.rcoef, nd.tspec);
274 >                if (rayorigin(&lr, TRANS, r, lr.rcoef) == 0) {
275                          VCOPY(lr.rdir, nd.prdir);
276                          rayvalue(&lr);
277 <                        scalecolor(lr.rcol, nd.tspec);
253 <                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
277 >                        multcolor(lr.rcol, lr.rcoef);
278                          addcolor(r->rcol, lr.rcol);
279                          transtest *= bright(lr.rcol);
280                          transdist = r->rot + lr.rt;
# Line 269 | Line 293 | register RAY  *r;
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 280 | Line 305 | register RAY  *r;
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++)
284 <                        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)) {
316                  RAY  lr;
317 <                if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
317 >                if (rayorigin(&lr, REFLECTED, r, nd.scolor) == 0) {
318                          VCOPY(lr.rdir, nd.vrefl);
319                          rayvalue(&lr);
320 <                        multcolor(lr.rcol, nd.scolor);
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 308 | Line 333 | register RAY  *r;
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 <                ambient(ctmp, r, hastexture?nd.pnorm:r->ron);
340 <                if (nd.specfl & SP_RBLT)
341 <                        scalecolor(ctmp, 1.0-nd.trans);
342 <                else
343 <                        scalecolor(ctmp, nd.rdiff);
319 <                multcolor(ctmp, nd.mcolor);     /* modified by material color */
339 >                copycolor(ctmp, nd.mcolor);     /* modified by material color */
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          }
346          if (nd.tdiff > FTINY) {         /* ambient from other side */
347 +                copycolor(ctmp, nd.mcolor);     /* modified by color */
348 +                if (nd.specfl & SP_TBLT)
349 +                        scalecolor(ctmp, nd.trans);
350 +                else
351 +                        scalecolor(ctmp, nd.tdiff);
352                  flipsurface(r);
353                  if (hastexture) {
354                          FVECT  bnorm;
355                          bnorm[0] = -nd.pnorm[0];
356                          bnorm[1] = -nd.pnorm[1];
357                          bnorm[2] = -nd.pnorm[2];
358 <                        ambient(ctmp, r, bnorm);
358 >                        multambient(ctmp, r, bnorm);
359                  } else
360 <                        ambient(ctmp, r, r->ron);
332 <                if (nd.specfl & SP_TBLT)
333 <                        scalecolor(ctmp, nd.trans);
334 <                else
335 <                        scalecolor(ctmp, nd.tdiff);
336 <                multcolor(ctmp, nd.mcolor);     /* modified by color */
360 >                        multambient(ctmp, r, r->ron);
361                  addcolor(r->rcol, ctmp);
362                  flipsurface(r);
363          }
# Line 351 | Line 375 | register RAY  *r;
375  
376  
377   static void
378 < gaussamp(r, np)                 /* sample gaussian specular */
379 < RAY  *r;
380 < 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;
370 <        for (i = 0; i < 3; i++)
371 <                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
372 <                        break;
373 <        v[i] = 1.0;
374 <        fcross(u, v, np->pnorm);
375 <        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, r, SPECULAR, np->rspec) == 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 387 | Line 421 | register NORMDAT  *np;
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 <                                multcolor(sr.rcol, np->scolor);
442 <                                addcolor(r->rcol, sr.rcol);
443 <                                break;
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(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, r, SPECULAR, np->tspec) == 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 <                                scalecolor(sr.rcol, np->tspec);
500 <                                multcolor(sr.rcol, np->mcolor); /* modified */
501 <                                addcolor(r->rcol, sr.rcol);
502 <                                break;
503 <                        }
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