ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
(Generate patch)

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.71 by greg, Fri Apr 11 20:31:37 2014 UTC vs.
Revision 2.80 by greg, Fri Apr 25 18:38:47 2014 UTC

# Line 51 | Line 51 | static int  nunflshed = 0;     /* number of unflushed ambi
51   #define MAX_SORT_INTVL  (SORT_INTVL<<6)
52   #endif
53  
54 +
55 + static double  qambacc = 0.;            /* ambient accuracy to the 1/4 power */
56   static double  avsum = 0.;              /* computed ambient value sum (log) */
57   static unsigned int  navsum = 0;        /* number of values in avsum */
58   static unsigned int  nambvals = 0;      /* total number of indirect values */
# Line 132 | Line 134 | setambacc(                             /* set ambient accuracy */
134          if (newa < 0.0)
135                  newa = 0.0;
136          ambdiff = fabs(newa - ambacc);
137 <        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
138 <                sortambvals(1);                 /* rebuild tree */
137 >        if (ambdiff >= .01 && (ambacc = newa) > FTINY) {
138 >                qambacc = sqrt(sqrt(ambacc));
139 >                if (nambvals > 0)
140 >                        sortambvals(1);         /* rebuild tree */
141 >        }
142   }
143  
144  
# Line 147 | Line 152 | setambient(void)                               /* initialize calculation */
152          ambdone();
153                                                  /* init ambient limits */
154          setambres(ambres);
155 <        setambacc(ambacc);
155 >        qambacc = sqrt(sqrt(ambacc *= (ambacc > FTINY)));
156          if (ambfile == NULL || !ambfile[0])
157                  return;
158          if (ambacc <= FTINY) {
# Line 263 | Line 268 | ambnotify(                     /* record new modifier */
268  
269   #ifdef NEWAMB
270  
271 < #define tfunc(lwr, x, upr)      (((x)-(lwr)/((upr)-(lwr)))
271 > #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
272  
273 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
274 +                                AMBTREE *at, FVECT c0, double s);
275 + static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
276 + static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
277 +                                FVECT uvw[3]);
278  
279   void
280   multambient(            /* compute ambient component & multiply by coef. */
# Line 315 | Line 325 | multambient(           /* compute ambient component & multiply
325          ok = makeambient(acol, r, nrm, rdepth-1);
326          rdepth--;
327          if (ok) {
328 <                multcolor(aval, acol);          /* got new value */
328 >                multcolor(aval, acol);          /* computed new value */
329                  return;
330          }
331   dumbamb:                                        /* return global value */
# Line 338 | Line 348 | dumbamb:                                       /* return global value */
348  
349  
350   double
351 < sumambient(     /* get interpolated ambient value */
351 > sumambient(             /* get interpolated ambient value */
352          COLOR  acol,
353          RAY  *r,
354          FVECT  rn,
# Line 347 | Line 357 | sumambient(    /* get interpolated ambient value */
357          FVECT  c0,
358          double  s
359   )
360 < {                                       /* initial limit is ambacc radians */
361 <        const double    maxangle = (ambacc-PI/2.)*pow(r->rweight,0.13) + PI/2.;
360 > {                       /* initial limit is 10 degrees plus ambacc radians */
361 >        const double    minangle = 10.0 * PI/180.;
362 >        const double    maxangle = (minangle+ambacc-PI/2.)*pow(r->rweight,0.13)
363 >                                        + PI/2.;
364          double          wsum = 0.0;
365          FVECT           ck0;
366          int             i, j;
# Line 362 | Line 374 | sumambient(    /* get interpolated ambient value */
374                  if (tracktime)
375                          av->latick = ambclock;
376                  /*
377 <                 *  Ambient level test.
377 >                 *  Ambient level test
378                   */
379                  if (av->lvl > al)       /* list sorted, so this works */
380                          break;
381                  if (av->weight < 0.9*r->rweight)
382                          continue;
383                  /*
384 <                 *  Direction test using unperturbed normal.
384 >                 *  Direction test using unperturbed normal
385                   */
386                  decodedir(uvw[2], av->ndir);
387                  d = DOT(uvw[2], r->ron);
# Line 379 | Line 391 | sumambient(    /* get interpolated ambient value */
391                  if (delta_r2 >= maxangle*maxangle)
392                          continue;
393                  /*
394 <                 *  Ambient radius test.
394 >                 *  Elliptical radii test based on Hessian
395                   */
396                  decodedir(uvw[0], av->udir);
397                  VCROSS(uvw[1], uvw[2], uvw[0]);
# Line 388 | Line 400 | sumambient(    /* get interpolated ambient value */
400                  delta_t2 = d*d;
401                  d = DOT(ck0, uvw[1]) / av->rad[1];
402                  delta_t2 += d*d;
403 <                if (delta_t2 >= ambacc*ambacc)
403 >                d = DOT(ck0, uvw[2]) / av->rad[0];
404 >                delta_t2 += d*d;
405 >                if (delta_t2 >= qambacc*qambacc)
406                          continue;
407                  /*
408 <                 *  Ray behind test.
408 >                 *  Extrapolate value and compute final weight (hat function)
409                   */
410 <                d = 0.0;
397 <                for (j = 0; j < 3; j++)
398 <                        d += (r->rop[j] - av->pos[j])*(uvw[2][j] + r->ron[j]);
399 <                if (d*0.5 < -minarad*ambacc-.001)
400 <                        continue;
401 <                /*
402 <                 *  Convert to final weight (hat function)
403 <                 */
410 >                extambient(ct, av, r->rop, rn, uvw);
411                  d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
412 <                        tfunc(ambacc, sqrt(delta_t2), 0.0);
406 <                wsum += d;
407 <                extambient(ct, av, uvw, r->rop, rn);
412 >                        tfunc(qambacc, sqrt(delta_t2), 0.0);
413                  scalecolor(ct, d);
414                  addcolor(acol, ct);
415 +                wsum += d;
416          }
417          if (at->kid == NULL)
418                  return(wsum);
# Line 439 | Line 445 | makeambient(           /* make a new ambient value for storage
445   )
446   {
447          AMBVAL  amb;
448 <        FVECT   uv[2];
448 >        FVECT   uvw[3];
449          int     i;
450  
451          amb.weight = 1.0;                       /* compute weight */
# Line 449 | Line 455 | makeambient(           /* make a new ambient value for storage
455                  amb.weight = 1.25*r->rweight;
456          setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
457                                                  /* compute ambient */
458 <        if (!doambient(acol, r, amb.weight, uv, amb.rad, amb.gpos, amb.gdir)) {
453 <                setcolor(acol, 0.0, 0.0, 0.0);
454 <                return(0);
455 <        }
458 >        i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir);
459          scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
460 +        if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
461 +                return(i);
462                                                  /* store value */
463          VCOPY(amb.pos, r->rop);
464          amb.ndir = encodedir(r->ron);
465 <        amb.udir = encodedir(uv[0]);
465 >        amb.udir = encodedir(uvw[0]);
466          amb.lvl = al;
467          copycolor(amb.val, acol);
468                                                  /* insert into tree */
469          avsave(&amb);                           /* and save to file */
470 <        if (rn != r->ron)
471 <                extambient(acol, &amb, r->rop, rn);     /* texture */
470 >        if (rn != r->ron) {                     /* texture */
471 >                VCOPY(uvw[2], r->ron);
472 >                extambient(acol, &amb, r->rop, rn, uvw);
473 >        }
474          return(1);
475   }
476  
# Line 472 | Line 479 | void
479   extambient(             /* extrapolate value at pv, nv */
480          COLOR  cr,
481          AMBVAL   *ap,
475        FVECT  uvw[3],
482          FVECT  pv,
483 <        FVECT  nv
483 >        FVECT  nv,
484 >        FVECT  uvw[3]
485   )
486   {
487 <        FVECT  v1;
488 <        int  i;
489 <        double  d = 1.0;                /* zeroeth order */
487 >        static FVECT    my_uvw[3];
488 >        FVECT           v1;
489 >        int             i;
490 >        double          d = 1.0;        /* zeroeth order */
491  
492 +        if (uvw == NULL) {              /* need local coordinates? */
493 +                decodedir(my_uvw[2], ap->ndir);
494 +                decodedir(my_uvw[0], ap->udir);
495 +                VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]);
496 +                uvw = my_uvw;
497 +        }
498          for (i = 3; i--; )              /* gradient due to translation */
499                  d += (pv[i] - ap->pos[i]) *
500                          (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]);
# Line 516 | Line 530 | avinsert(                              /* insert ambient value in our tree */
530          at = &atrunk;
531          VCOPY(ck0, thescene.cuorg);
532          s = thescene.cusize;
533 <        while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
533 >        while (s*(OCTSCALE/2) > av->rad[1]*qambacc) {
534                  if (at->kid == NULL)
535                          if ((at->kid = newambtree()) == NULL)
536                                  error(SYSTEM, "out of memory in avinsert");
# Line 541 | Line 555 | avinsert(                              /* insert ambient value in our tree */
555  
556   #else /* ! NEWAMB */
557  
558 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
559 +                                AMBTREE *at, FVECT c0, double s);
560 + static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
561 + static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
562  
563 +
564   void
565   multambient(            /* compute ambient component & multiply by coef. */
566          COLOR  aval,
# Line 612 | Line 631 | dumbamb:                                       /* return global value */
631   }
632  
633  
634 < double
634 > static double
635   sumambient(     /* get interpolated ambient value */
636          COLOR  acol,
637          RAY  *r,
# Line 723 | Line 742 | sumambient(    /* get interpolated ambient value */
742   }
743  
744  
745 < double
745 > static double
746   makeambient(            /* make a new ambient value for storage */
747          COLOR  acol,
748          RAY  *r,
# Line 763 | Line 782 | makeambient(           /* make a new ambient value for storage
782   }
783  
784  
785 < void
785 > static void
786   extambient(             /* extrapolate value at pv, nv */
787          COLOR  cr,
788          AMBVAL   *ap,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines