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.67 by greg, Sat May 10 17:43:01 2014 UTC vs.
Revision 2.86 by greg, Thu Dec 19 23:25:28 2024 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)
28 > #define  FRESNE(ci)     (exp(-5.85*(ci)) - 0.00202943064)
29   #define  FRESTHRESH     0.017999        /* minimum specularity for approx. */
30  
31  
# Line 53 | Line 54 | typedef struct {
54          OBJREC  *mp;            /* material pointer */
55          RAY  *rp;               /* ray pointer */
56          short  specfl;          /* specularity flags, defined above */
57 <        COLOR  mcolor;          /* color of this material */
58 <        COLOR  scolor;          /* color of specular component */
58 <        FVECT  vrefl;           /* vector in direction of reflected ray */
57 >        SCOLOR  mcolor;         /* color of this material */
58 >        SCOLOR  scolor;         /* color of specular component */
59          FVECT  prdir;           /* vector in transmitted direction */
60          double  alpha2;         /* roughness squared */
61          double  rdiff, rspec;   /* reflected specular, diffuse */
# Line 70 | Line 70 | static void gaussamp(NORMDAT  *np);
70  
71   static void
72   dirnorm(                /* compute source contribution */
73 <        COLOR  cval,                    /* returned coefficient */
73 >        SCOLOR  scval,                  /* returned coefficient */
74          void  *nnp,                     /* material data */
75          FVECT  ldir,                    /* light source direction */
76          double  omega                   /* light source size */
# Line 81 | Line 81 | dirnorm(               /* compute source contribution */
81          double  lrdiff, ltdiff;
82          double  dtmp, d2, d3, d4;
83          FVECT  vtmp;
84 <        COLOR  ctmp;
84 >        SCOLOR  sctmp;
85  
86 <        setcolor(cval, 0.0, 0.0, 0.0);
86 >        scolorblack(scval);
87  
88          ldot = DOT(np->pnorm, ldir);
89  
# Line 100 | Line 100 | dirnorm(               /* compute source contribution */
100                  ltdiff *= dtmp;
101          }
102  
103 <        if (ldot > FTINY && lrdiff > FTINY) {
103 >        if ((ldot > FTINY) & (lrdiff > FTINY)) {
104                  /*
105                   *  Compute and add diffuse reflected component to returned
106                   *  color.  The diffuse reflected component will always be
107                   *  modified by the color of the material.
108                   */
109 <                copycolor(ctmp, np->mcolor);
109 >                copyscolor(sctmp, np->mcolor);
110                  dtmp = ldot * omega * lrdiff * (1.0/PI);
111 <                scalecolor(ctmp, dtmp);
112 <                addcolor(cval, ctmp);
111 >                scalescolor(sctmp, dtmp);
112 >                saddscolor(scval, sctmp);
113          }
114 <        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
114 >
115 >        if ((ldot < -FTINY) & (ltdiff > FTINY)) {
116                  /*
117 +                 *  Compute diffuse transmission.
118 +                 */
119 +                copyscolor(sctmp, np->mcolor);
120 +                dtmp = -ldot * omega * ltdiff * (1.0/PI);
121 +                scalescolor(sctmp, dtmp);
122 +                saddscolor(scval, sctmp);
123 +        }
124 +
125 +        if (ambRayInPmap(np->rp))
126 +                return;         /* specular already in photon map */
127 +
128 +        if ((ldot > FTINY) & ((np->specfl&(SP_REFL|SP_PURE)) == SP_REFL)) {
129 +                /*
130                   *  Compute specular reflection coefficient using
131                   *  Gaussian distribution model.
132                   */
# Line 131 | Line 145 | dirnorm(               /* compute source contribution */
145                  dtmp = exp(-d4/dtmp) * d3 / (PI * d2*d2 * dtmp);
146                                                  /* worth using? */
147                  if (dtmp > FTINY) {
148 <                        copycolor(ctmp, np->scolor);
148 >                        copyscolor(sctmp, np->scolor);
149                          dtmp *= ldot * omega;
150 <                        scalecolor(ctmp, dtmp);
151 <                        addcolor(cval, ctmp);
150 >                        scalescolor(sctmp, dtmp);
151 >                        saddscolor(scval, sctmp);
152                  }
153          }
154 <        if (ldot < -FTINY && ltdiff > FTINY) {
154 >        
155 >
156 >        if ((ldot < -FTINY) & ((np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN)) {
157                  /*
142                 *  Compute diffuse transmission.
143                 */
144                copycolor(ctmp, np->mcolor);
145                dtmp = -ldot * omega * ltdiff * (1.0/PI);
146                scalecolor(ctmp, dtmp);
147                addcolor(cval, ctmp);
148        }
149        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
150                /*
158                   *  Compute specular transmission.  Specular transmission
159                   *  is always modified by material color.
160                   */
# Line 157 | Line 164 | dirnorm(               /* compute source contribution */
164                  dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp);
165                                                  /* worth using? */
166                  if (dtmp > FTINY) {
167 <                        copycolor(ctmp, np->mcolor);
167 >                        copyscolor(sctmp, np->mcolor);
168                          dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
169 <                        scalecolor(ctmp, dtmp);
170 <                        addcolor(cval, ctmp);
169 >                        scalescolor(sctmp, dtmp);
170 >                        saddscolor(scval, sctmp);
171                  }
172          }
173   }
# Line 174 | Line 181 | m_normal(                      /* color a ray that hit something normal *
181   {
182          NORMDAT  nd;
183          double  fest;
177        double  transtest, transdist;
178        double  mirtest, mirdist;
184          int     hastexture;
185          double  d;
186 <        COLOR  ctmp;
186 >        SCOLOR  sctmp;
187          int  i;
188 +
189 +        /* PMAP: skip transmitted shadow ray if accounted for in photon map */
190 +        /* No longer needed?
191 +        if (shadowRayInPmap(r) || ambRayInPmap(r))
192 +                return(1); */          
193 +                
194                                                  /* easy shadow test */
195          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
196                  return(1);
# Line 199 | Line 210 | m_normal(                      /* color a ray that hit something normal *
210          nd.mp = m;
211          nd.rp = r;
212                                                  /* get material color */
213 <        setcolor(nd.mcolor, m->oargs.farg[0],
213 >        setscolor(nd.mcolor, m->oargs.farg[0],
214                             m->oargs.farg[1],
215                             m->oargs.farg[2]);
216                                                  /* get roughness */
# Line 214 | Line 225 | m_normal(                      /* color a ray that hit something normal *
225                  VCOPY(nd.pnorm, r->ron);
226                  nd.pdot = r->rod;
227          }
228 <        if (r->ro != NULL && isflat(r->ro->otype))
228 >        if (!hastexture && r->ro != NULL && isflat(r->ro->otype))
229                  nd.specfl |= SP_FLAT;
230          if (nd.pdot < .001)
231                  nd.pdot = .001;                 /* non-zero for dirnorm() */
232 <        multcolor(nd.mcolor, r->pcol);          /* modify material color */
222 <        mirtest = transtest = 0;
223 <        mirdist = transdist = r->rot;
232 >        smultscolor(nd.mcolor, r->pcol);        /* modify material color */
233          nd.rspec = m->oargs.farg[3];
234                                                  /* compute Fresnel approx. */
235          if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) {
# Line 241 | Line 250 | m_normal(                      /* color a ray that hit something normal *
250                                  nd.specfl |= SP_TBLT;
251                          if (!hastexture || r->crtype & (SHADOW|AMBIENT)) {
252                                  VCOPY(nd.prdir, r->rdir);
244                                transtest = 2;
253                          } else {
254 <                                for (i = 0; i < 3; i++)         /* perturb */
255 <                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
254 >                                                        /* perturb */
255 >                                VSUB(nd.prdir, r->rdir, r->pert);
256                                  if (DOT(nd.prdir, r->ron) < -FTINY)
257                                          normalize(nd.prdir);    /* OK */
258                                  else
# Line 253 | Line 261 | m_normal(                      /* color a ray that hit something normal *
261                  }
262          } else
263                  nd.tdiff = nd.tspec = nd.trans = 0.0;
264 +                                                /* diffuse reflection */
265 +        nd.rdiff = 1.0 - nd.trans - nd.rspec;
266                                                  /* transmitted ray */
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 */
270 <                scalecolor(lr.rcoef, nd.tspec);
269 >                copyscolor(lr.rcoef, nd.mcolor);        /* modified by color */
270 >                scalescolor(lr.rcoef, nd.tspec);
271                  if (rayorigin(&lr, TRANS, r, lr.rcoef) == 0) {
272                          VCOPY(lr.rdir, nd.prdir);
273                          rayvalue(&lr);
274 <                        multcolor(lr.rcol, lr.rcoef);
275 <                        addcolor(r->rcol, lr.rcol);
276 <                        transtest *= bright(lr.rcol);
277 <                        transdist = r->rot + lr.rt;
274 >                        smultscolor(lr.rcol, lr.rcoef);
275 >                        saddscolor(r->rcol, lr.rcol);
276 >                        if (nd.tspec >= 1.0-FTINY) {
277 >                                                /* completely transparent */
278 >                                smultscolor(lr.mcol, lr.rcoef);
279 >                                copyscolor(r->mcol, lr.mcol);
280 >                                r->rmt = r->rot + lr.rmt;
281 >                                r->rxt = r->rot + lr.rxt;
282 >                        } else if (nd.tspec > nd.tdiff + nd.rdiff)
283 >                                r->rxt = r->rot + raydistance(&lr);
284                  }
285 <        } else
270 <                transtest = 0;
285 >        }
286  
287 <        if (r->crtype & SHADOW) {               /* the rest is shadow */
273 <                r->rt = transdist;
287 >        if (r->crtype & SHADOW)                 /* the rest is shadow */
288                  return(1);
275        }
289                                                  /* get specular reflection */
290          if (nd.rspec > FTINY) {
291                  nd.specfl |= SP_REFL;
292                                                  /* compute specular color */
293                  if (m->otype != MAT_METAL) {
294 <                        setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
294 >                        setscolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
295                  } else if (fest > FTINY) {
296                          d = m->oargs.farg[3]*(1. - fest);
297 <                        for (i = 0; i < 3; i++)
298 <                                colval(nd.scolor,i) = fest +
286 <                                                colval(nd.mcolor,i)*d;
297 >                        for (i = NCSAMP; i--; )
298 >                                nd.scolor[i] = fest + nd.mcolor[i]*d;
299                  } else {
300 <                        copycolor(nd.scolor, nd.mcolor);
301 <                        scalecolor(nd.scolor, nd.rspec);
300 >                        copyscolor(nd.scolor, nd.mcolor);
301 >                        scalescolor(nd.scolor, nd.rspec);
302                  }
303                                                  /* check threshold */
304                  if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
305                          nd.specfl |= SP_RBLT;
294                                                /* compute reflected ray */
295                VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.*nd.pdot);
296                                                /* penetration? */
297                if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
298                        VSUM(nd.vrefl, r->rdir, r->ron, 2.*r->rod);
299                checknorm(nd.vrefl);
306          }
307                                                  /* reflected ray */
308          if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) {
309                  RAY  lr;
310                  if (rayorigin(&lr, REFLECTED, r, nd.scolor) == 0) {
311 <                        VCOPY(lr.rdir, nd.vrefl);
311 >                                                /* compute reflected ray */
312 >                        VSUM(lr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot);
313 >                                                /* penetration? */
314 >                        if (hastexture && DOT(lr.rdir, r->ron) <= FTINY)
315 >                                VSUM(lr.rdir, r->rdir, r->ron, 2.*r->rod);
316 >                        checknorm(lr.rdir);
317                          rayvalue(&lr);
318 <                        multcolor(lr.rcol, lr.rcoef);
319 <                        addcolor(r->rcol, lr.rcol);
320 <                        if (nd.specfl & SP_FLAT &&
321 <                                        !hastexture | (r->crtype & AMBIENT)) {
322 <                                mirtest = 2.*bright(lr.rcol);
323 <                                mirdist = r->rot + lr.rt;
313 <                        }
318 >                        smultscolor(lr.rcol, lr.rcoef);
319 >                        copyscolor(r->mcol, lr.rcol);
320 >                        saddscolor(r->rcol, lr.rcol);
321 >                        r->rmt = r->rot;
322 >                        if (nd.specfl & SP_FLAT && r->crtype & AMBIENT)
323 >                                r->rmt += raydistance(&lr);
324                  }
325          }
316                                                /* diffuse reflection */
317        nd.rdiff = 1.0 - nd.trans - nd.rspec;
326  
327          if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
328                  return(1);                      /* 100% pure specular */
329  
330          if (!(nd.specfl & SP_PURE))
331 <                gaussamp(&nd);          /* checks *BLT flags */
331 >                gaussamp(&nd);                  /* checks *BLT flags */
332  
333          if (nd.rdiff > FTINY) {         /* ambient from this side */
334 <                copycolor(ctmp, nd.mcolor);     /* modified by material color */
335 <                scalecolor(ctmp, nd.rdiff);
334 >                copyscolor(sctmp, nd.mcolor);   /* modified by material color */
335 >                scalescolor(sctmp, nd.rdiff);
336                  if (nd.specfl & SP_RBLT)        /* add in specular as well? */
337 <                        addcolor(ctmp, nd.scolor);
338 <                multambient(ctmp, r, hastexture ? nd.pnorm : r->ron);
339 <                addcolor(r->rcol, ctmp);        /* add to returned color */
337 >                        saddscolor(sctmp, nd.scolor);
338 >                multambient(sctmp, r, nd.pnorm);
339 >                saddscolor(r->rcol, sctmp);     /* add to returned color */
340          }
341          if (nd.tdiff > FTINY) {         /* ambient from other side */
342 <                copycolor(ctmp, nd.mcolor);     /* modified by color */
343 <                if (nd.specfl & SP_TBLT)
344 <                        scalecolor(ctmp, nd.trans);
345 <                else
346 <                        scalecolor(ctmp, nd.tdiff);
347 <                flipsurface(r);
348 <                if (hastexture) {
349 <                        FVECT  bnorm;
350 <                        bnorm[0] = -nd.pnorm[0];
351 <                        bnorm[1] = -nd.pnorm[1];
352 <                        bnorm[2] = -nd.pnorm[2];
353 <                        multambient(ctmp, r, bnorm);
346 <                } else
347 <                        multambient(ctmp, r, r->ron);
348 <                addcolor(r->rcol, ctmp);
349 <                flipsurface(r);
342 >                FVECT  bnorm;
343 >                copyscolor(sctmp, nd.mcolor);   /* modified by color */
344 >                if (nd.specfl & SP_TBLT) {
345 >                        scalescolor(sctmp, nd.trans);
346 >                } else {
347 >                        scalescolor(sctmp, nd.tdiff);
348 >                }
349 >                bnorm[0] = -nd.pnorm[0];
350 >                bnorm[1] = -nd.pnorm[1];
351 >                bnorm[2] = -nd.pnorm[2];
352 >                multambient(sctmp, r, bnorm);
353 >                saddscolor(r->rcol, sctmp);
354          }
355                                          /* add direct component */
356          direct(r, dirnorm, &nd);
353                                        /* check distance */
354        d = bright(r->rcol);
355        if (transtest > d)
356                r->rt = transdist;
357        else if (mirtest > d)
358                r->rt = mirdist;
357  
358          return(1);
359   }
# Line 370 | Line 368 | gaussamp(                      /* sample Gaussian specular */
368          FVECT  u, v, h;
369          double  rv[2];
370          double  d, sinp, cosp;
371 <        COLOR  scol;
371 >        SCOLOR  scol;
372          int  maxiter, ntrials, nstarget, nstaken;
373          int  i;
374                                          /* quick test */
# Line 378 | Line 376 | gaussamp(                      /* sample Gaussian specular */
376                          (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
377                  return;
378                                          /* set up sample coordinates */
379 <        v[0] = v[1] = v[2] = 0.0;
382 <        for (i = 0; i < 3; i++)
383 <                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
384 <                        break;
385 <        v[i] = 1.0;
386 <        fcross(u, v, np->pnorm);
387 <        normalize(u);
379 >        getperpendicular(u, np->pnorm, rand_samp);
380          fcross(v, np->pnorm, u);
381                                          /* compute reflection */
382          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
383 <                        rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) {
383 >                        rayorigin(&sr, RSPECULAR, np->rp, np->scolor) == 0) {
384                  nstarget = 1;
385                  if (specjitter > 1.5) { /* multiple samples? */
386                          nstarget = specjitter*np->rp->rweight + .5;
# Line 396 | Line 388 | gaussamp(                      /* sample Gaussian specular */
388                                  nstarget = sr.rweight/minweight;
389                          if (nstarget > 1) {
390                                  d = 1./nstarget;
391 <                                scalecolor(sr.rcoef, d);
391 >                                scalescolor(sr.rcoef, d);
392                                  sr.rweight *= d;
393                          } else
394                                  nstarget = 1;
395                  }
396 <                setcolor(scol, 0., 0., 0.);
396 >                scolorblack(scol);
397                  dimlist[ndims++] = (int)(size_t)np->mp;
398                  maxiter = MAXITER*nstarget;
399                  for (nstaken = ntrials = 0; nstaken < nstarget &&
# Line 432 | Line 424 | gaussamp(                      /* sample Gaussian specular */
424                                  if (nstaken) rayclear(&sr);
425                                  rayvalue(&sr);
426                                  d = 2./(1. + np->rp->rod/d);
427 <                                scalecolor(sr.rcol, d);
428 <                                addcolor(scol, sr.rcol);
427 >                                scalescolor(sr.rcol, d);
428 >                                saddscolor(scol, sr.rcol);
429                          } else {
430                                  rayvalue(&sr);
431 <                                multcolor(sr.rcol, sr.rcoef);
432 <                                addcolor(np->rp->rcol, sr.rcol);
431 >                                smultscolor(sr.rcol, sr.rcoef);
432 >                                saddscolor(np->rp->rcol, sr.rcol);
433                          }
434                          ++nstaken;
435                  }
436                  if (nstarget > 1) {             /* final W-G-M-D weighting */
437 <                        multcolor(scol, sr.rcoef);
437 >                        smultscolor(scol, sr.rcoef);
438                          d = (double)nstarget/ntrials;
439 <                        scalecolor(scol, d);
440 <                        addcolor(np->rp->rcol, scol);
439 >                        scalescolor(scol, d);
440 >                        saddscolor(np->rp->rcol, scol);
441                  }
442                  ndims--;
443          }
444                                          /* compute transmission */
445 <        copycolor(sr.rcoef, np->mcolor);        /* modified by color */
446 <        scalecolor(sr.rcoef, np->tspec);
445 >        copyscolor(sr.rcoef, np->mcolor);       /* modified by color */
446 >        scalescolor(sr.rcoef, np->tspec);
447          if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
448 <                        rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) {
448 >                        rayorigin(&sr, TSPECULAR, np->rp, sr.rcoef) == 0) {
449                  nstarget = 1;
450                  if (specjitter > 1.5) { /* multiple samples? */
451                          nstarget = specjitter*np->rp->rweight + .5;
# Line 461 | Line 453 | gaussamp(                      /* sample Gaussian specular */
453                                  nstarget = sr.rweight/minweight;
454                          if (nstarget > 1) {
455                                  d = 1./nstarget;
456 <                                scalecolor(sr.rcoef, d);
456 >                                scalescolor(sr.rcoef, d);
457                                  sr.rweight *= d;
458                          } else
459                                  nstarget = 1;
# Line 493 | Line 485 | gaussamp(                      /* sample Gaussian specular */
485                          if (nstaken)            /* multi-sampling */
486                                  rayclear(&sr);
487                          rayvalue(&sr);
488 <                        multcolor(sr.rcol, sr.rcoef);
489 <                        addcolor(np->rp->rcol, sr.rcol);
488 >                        smultscolor(sr.rcol, sr.rcoef);
489 >                        saddscolor(np->rp->rcol, sr.rcol);
490                          ++nstaken;
491                  }
492                  ndims--;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines