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.101 by schorsch, Sun Mar 6 01:13:17 2016 UTC vs.
Revision 2.111 by greg, Thu Nov 18 19:38:21 2021 UTC

# Line 12 | Line 12 | static const char      RCSid[] = "$Id$";
12   #include  "platform.h"
13   #include  "ray.h"
14   #include  "otypes.h"
15 + #include  "otspecial.h"
16   #include  "resolu.h"
17   #include  "ambient.h"
18   #include  "random.h"
# Line 76 | Line 77 | static long  lastpos = -1;             /* last flush position */
77   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
78  
79   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79 #define  freeav(av)     free((void *)av);
80  
81 + #define  tfunc(lwr, x, upr)     (((x)-(lwr))/((upr)-(lwr)))
82 +
83   static void initambfile(int creat);
84   static void avsave(AMBVAL *av);
85   static AMBVAL *avstore(AMBVAL  *aval);
# Line 94 | Line 96 | static int aposcmp(const void *avp1, const void *avp2)
96   static int avlmemi(AMBVAL *avaddr);
97   static void sortambvals(int always);
98  
99 + static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
100 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
101 +                                AMBTREE *at, FVECT c0, double s);
102 + static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
103 + static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
104 +                                FVECT uvw[3]);
105 +
106   #ifdef  F_SETLKW
107   static void aflock(int  typ);
108   #endif
# Line 132 | Line 141 | setambacc(                             /* set ambient accuracy */
141          newa *= (newa > 0);
142          if (fabs(newa - olda) >= .05*(newa + olda)) {
143                  ambacc = newa;
144 <                if (nambvals > 0)
144 >                if (ambacc > FTINY && nambvals > 0)
145                          sortambvals(1);         /* rebuild tree */
146          }
147   }
# Line 255 | Line 264 | ambnotify(                     /* record new modifier */
264                  }
265   }
266  
258 /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
267  
260 #ifndef OLDAMB
261
262 #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
263
264 static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
265 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
266                                AMBTREE *at, FVECT c0, double s);
267 static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
268 static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
269                                FVECT uvw[3]);
270
268   void
269   multambient(            /* compute ambient component & multiply by coef. */
270          COLOR  aval,
# Line 275 | Line 272 | multambient(           /* compute ambient component & multiply
272          FVECT  nrm
273   )
274   {
275 +        static double  logAvgAbsorp = 1;
276          static int  rdepth = 0;                 /* ambient recursion */
277          COLOR   acol, caustic;
278 <        int     ok;
278 >        int     i, ok;
279          double  d, l;
280  
281          /* PMAP: Factor in ambient from photon map, if enabled and ray is
# Line 286 | Line 284 | multambient(           /* compute ambient component & multiply
284          if (ambPmap(aval, r, rdepth))
285                  return;
286  
287 +        if (logAvgAbsorp > 0)                   /* exclude in -aw to avoid growth */
288 +                logAvgAbsorp = log(1.-AVGREFL);
289 +
290          /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
291           * map, if enabled and ray is primary, else caustic is zero.  Continue
292           * with RADIANCE ambient calculation */
# Line 303 | Line 304 | multambient(           /* compute ambient component & multiply
304                  goto dumbamb;
305  
306          if (ambacc <= FTINY) {                  /* no ambient storage */
307 +                FVECT   uvd[2];
308 +                float   dgrad[2], *dgp = NULL;
309 +
310 +                if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999)
311 +                        dgp = dgrad;            /* compute rotational grad. */
312                  copycolor(acol, aval);
313                  rdepth++;
314                  ok = doambient(acol, r, r->rweight,
315 <                                NULL, NULL, NULL, NULL, NULL);
315 >                                uvd, NULL, NULL, dgp, NULL);
316                  rdepth--;
317                  if (!ok)
318                          goto dumbamb;
319 +                if ((ok > 0) & (dgp != NULL)) { /* apply texture */
320 +                        FVECT   v1;
321 +                        VCROSS(v1, r->ron, nrm);
322 +                        d = 1.0;
323 +                        for (i = 3; i--; )
324 +                                d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]);
325 +                        if (d >= 0.05)
326 +                                scalecolor(acol, d);
327 +                }
328                  copycolor(aval, acol);
329  
330                  /* PMAP: add in caustic */
# Line 357 | Line 372 | dumbamb:                                       /* return global value */
372          
373          l = bright(ambval);                     /* average in computations */  
374          if (l > FTINY) {
375 <                d = (log(l)*(double)ambvwt + avsum) /
375 >                d = (log(l)*(double)ambvwt + avsum + logAvgAbsorp*navsum) /
376                                  (double)(ambvwt + navsum);
377                  d = exp(d) / l;
378                  scalecolor(aval, d);
379                  multcolor(aval, ambval);        /* apply color of ambval */
380          } else {
381 <                d = exp( avsum / (double)navsum );
381 >                d = exp( avsum/(double)navsum + logAvgAbsorp );
382                  scalecolor(aval, d);            /* neutral color */
383          }
384   }
# Line 402 | Line 417 | plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
417          VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
418          rtst.rmax = normalize(rtst.rdir);       /* short ray test */
419          while (localhit(&rtst, &thescene)) {    /* check for occluder */
420 <                if (rtst.ro->omod != OVOID &&
420 >                OBJREC  *m = findmaterial(rtst.ro);
421 >                if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) &&
422                                  (rtst.clipset == NULL ||
423                                          !inset(rtst.clipset, rtst.ro->omod)))
424                          return(1);              /* plug light leak */
# Line 571 | Line 587 | extambient(            /* extrapolate value at pv, nv */
587   )
588   {
589          const double    min_d = 0.05;
590 +        const double    max_d = 20.;
591          static FVECT    my_uvw[3];
592          FVECT           v1;
593          int             i;
# Line 590 | Line 607 | extambient(            /* extrapolate value at pv, nv */
607          for (i = 3; i--; )
608                  d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
609          
610 <        if (d < min_d)                  /* should not use if we can avoid it */
610 >        if (d < min_d)                  /* clamp min/max scaling */
611                  d = min_d;
612 +        else if (d > max_d)
613 +                d = max_d;
614          copycolor(cr, ap->val);
615          scalecolor(cr, d);
616          return(d > min_d);
# Line 641 | Line 660 | avinsert(                              /* insert ambient value in our tree */
660   }
661  
662  
644 #else /* ! NEWAMB */
645
646 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
647                                AMBTREE *at, FVECT c0, double s);
648 static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
649 static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
650
651
652 void
653 multambient(            /* compute ambient component & multiply by coef. */
654        COLOR  aval,
655        RAY  *r,
656        FVECT  nrm
657 )
658 {
659        static int  rdepth = 0;                 /* ambient recursion */
660        COLOR   acol, caustic;
661        double  d, l;
662
663        /* PMAP: Factor in ambient from global photon map (if enabled) and return
664         * as all ambient components accounted for */
665        if (ambPmap(aval, r, rdepth))
666                return;
667
668        /* PMAP: Otherwise factor in ambient from caustic photon map
669         * (ambPmapCaustic() returns zero if caustic photons disabled) and
670         * continue with RADIANCE ambient calculation */
671        copycolor(caustic, aval);
672        ambPmapCaustic(caustic, r, rdepth);
673        
674        if (ambdiv <= 0)                        /* no ambient calculation */
675                goto dumbamb;
676                                                /* check number of bounces */
677        if (rdepth >= ambounce)
678                goto dumbamb;
679                                                /* check ambient list */
680        if (ambincl != -1 && r->ro != NULL &&
681                        ambincl != inset(ambset, r->ro->omod))
682                goto dumbamb;
683
684        if (ambacc <= FTINY) {                  /* no ambient storage */
685                copycolor(acol, aval);
686                rdepth++;
687                d = doambient(acol, r, r->rweight, NULL, NULL);
688                rdepth--;
689                if (d <= FTINY)
690                        goto dumbamb;
691                copycolor(aval, acol);          
692        
693           /* PMAP: add in caustic */
694                addcolor(aval, caustic);        
695                return;
696        }
697
698        if (tracktime)                          /* sort to minimize thrashing */
699                sortambvals(0);
700                                                /* interpolate ambient value */
701        setcolor(acol, 0.0, 0.0, 0.0);
702        d = sumambient(acol, r, nrm, rdepth,
703                        &atrunk, thescene.cuorg, thescene.cusize);
704                        
705        if (d > FTINY) {
706                d = 1.0/d;
707                scalecolor(acol, d);
708                multcolor(aval, acol);
709                
710                /* PMAP: add in caustic */
711                addcolor(aval, caustic);        
712                return;
713        }
714        
715        rdepth++;                               /* need to cache new value */
716        d = makeambient(acol, r, nrm, rdepth-1);
717        rdepth--;
718        
719        if (d > FTINY) {
720                multcolor(aval, acol);          /* got new value */
721
722                /* PMAP: add in caustic */
723                addcolor(aval, caustic);                        
724                return;
725        }
726        
727 dumbamb:                                        /* return global value */
728        if ((ambvwt <= 0) | (navsum == 0)) {
729                multcolor(aval, ambval);
730
731                /* PMAP: add in caustic */
732                addcolor(aval, caustic);        
733                return;
734        }
735        
736        l = bright(ambval);                     /* average in computations */
737        if (l > FTINY) {
738                d = (log(l)*(double)ambvwt + avsum) /
739                                (double)(ambvwt + navsum);
740                d = exp(d) / l;
741                scalecolor(aval, d);
742                multcolor(aval, ambval);        /* apply color of ambval */
743        } else {
744                d = exp( avsum / (double)navsum );
745                scalecolor(aval, d);            /* neutral color */
746        }
747 }
748
749
750 static double
751 sumambient(     /* get interpolated ambient value */
752        COLOR  acol,
753        RAY  *r,
754        FVECT  rn,
755        int  al,
756        AMBTREE  *at,
757        FVECT  c0,
758        double  s
759 )
760 {
761        double  d, e1, e2, wt, wsum;
762        COLOR  ct;
763        FVECT  ck0;
764        int  i;
765        int  j;
766        AMBVAL   *av;
767
768        wsum = 0.0;
769                                        /* do this node */
770        for (av = at->alist; av != NULL; av = av->next) {
771                double  rn_dot = -2.0;
772                if (tracktime)
773                        av->latick = ambclock;
774                /*
775                 *  Ambient level test.
776                 */
777                if (av->lvl > al ||     /* list sorted, so this works */
778                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
779                        break;
780                /*
781                 *  Ambient radius test.
782                 */
783                VSUB(ck0, av->pos, r->rop);
784                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
785                if (e1 > ambacc*ambacc*1.21)
786                        continue;
787                /*
788                 *  Direction test using closest normal.
789                 */
790                d = DOT(av->dir, r->ron);
791                if (rn != r->ron) {
792                        rn_dot = DOT(av->dir, rn);
793                        if (rn_dot > 1.0-FTINY)
794                                rn_dot = 1.0-FTINY;
795                        if (rn_dot >= d-FTINY) {
796                                d = rn_dot;
797                                rn_dot = -2.0;
798                        }
799                }
800                e2 = (1.0 - d) * r->rweight;
801                if (e2 < 0.0)
802                        e2 = 0.0;
803                else if (e1 + e2 > ambacc*ambacc*1.21)
804                        continue;
805                /*
806                 *  Ray behind test.
807                 */
808                d = 0.0;
809                for (j = 0; j < 3; j++)
810                        d += (r->rop[j] - av->pos[j]) *
811                                        (av->dir[j] + r->ron[j]);
812                if (d*0.5 < -minarad*ambacc-.001)
813                        continue;
814                /*
815                 *  Jittering final test reduces image artifacts.
816                 */
817                e1 = sqrt(e1);
818                e2 = sqrt(e2);
819                wt = e1 + e2;
820                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
821                        continue;
822                /*
823                 *  Recompute directional error using perturbed normal
824                 */
825                if (rn_dot > 0.0) {
826                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
827                        wt = e1 + e2;
828                }
829                if (wt <= 1e-3)
830                        wt = 1e3;
831                else
832                        wt = 1.0 / wt;
833                wsum += wt;
834                extambient(ct, av, r->rop, rn);
835                scalecolor(ct, wt);
836                addcolor(acol, ct);
837        }
838        if (at->kid == NULL)
839                return(wsum);
840                                        /* do children */
841        s *= 0.5;
842        for (i = 0; i < 8; i++) {
843                for (j = 0; j < 3; j++) {
844                        ck0[j] = c0[j];
845                        if (1<<j & i)
846                                ck0[j] += s;
847                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
848                                break;
849                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
850                                break;
851                }
852                if (j == 3)
853                        wsum += sumambient(acol, r, rn, al,
854                                                at->kid+i, ck0, s);
855        }
856        return(wsum);
857 }
858
859
860 static double
861 makeambient(            /* make a new ambient value for storage */
862        COLOR  acol,
863        RAY  *r,
864        FVECT  rn,
865        int  al
866 )
867 {
868        AMBVAL  amb;
869        FVECT   gp, gd;
870        int     i;
871
872        amb.weight = 1.0;                       /* compute weight */
873        for (i = al; i-- > 0; )
874                amb.weight *= AVGREFL;
875        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
876                amb.weight = 1.25*r->rweight;
877        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
878                                                /* compute ambient */
879        amb.rad = doambient(acol, r, amb.weight, gp, gd);
880        if (amb.rad <= FTINY) {
881                setcolor(acol, 0.0, 0.0, 0.0);
882                return(0.0);
883        }
884        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
885                                                /* store value */
886        VCOPY(amb.pos, r->rop);
887        VCOPY(amb.dir, r->ron);
888        amb.lvl = al;
889        copycolor(amb.val, acol);
890        VCOPY(amb.gpos, gp);
891        VCOPY(amb.gdir, gd);
892                                                /* insert into tree */
893        avsave(&amb);                           /* and save to file */
894        if (rn != r->ron)
895                extambient(acol, &amb, r->rop, rn);     /* texture */
896        return(amb.rad);
897 }
898
899
663   static void
901 extambient(             /* extrapolate value at pv, nv */
902        COLOR  cr,
903        AMBVAL   *ap,
904        FVECT  pv,
905        FVECT  nv
906 )
907 {
908        FVECT  v1;
909        int  i;
910        double  d;
911
912        d = 1.0;                        /* zeroeth order */
913                                        /* gradient due to translation */
914        for (i = 0; i < 3; i++)
915                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
916                                        /* gradient due to rotation */
917        VCROSS(v1, ap->dir, nv);
918        d += DOT(ap->gdir, v1);
919        if (d <= 0.0) {
920                setcolor(cr, 0.0, 0.0, 0.0);
921                return;
922        }
923        copycolor(cr, ap->val);
924        scalecolor(cr, d);
925 }
926
927
928 static void
929 avinsert(                               /* insert ambient value in our tree */
930        AMBVAL *av
931 )
932 {
933        AMBTREE  *at;
934        AMBVAL  *ap;
935        AMBVAL  avh;
936        FVECT  ck0;
937        double  s;
938        int  branch;
939        int  i;
940
941        if (av->rad <= FTINY)
942                error(CONSISTENCY, "zero ambient radius in avinsert");
943        at = &atrunk;
944        VCOPY(ck0, thescene.cuorg);
945        s = thescene.cusize;
946        while (s*(OCTSCALE/2) > av->rad*ambacc) {
947                if (at->kid == NULL)
948                        if ((at->kid = newambtree()) == NULL)
949                                error(SYSTEM, "out of memory in avinsert");
950                s *= 0.5;
951                branch = 0;
952                for (i = 0; i < 3; i++)
953                        if (av->pos[i] > ck0[i] + s) {
954                                ck0[i] += s;
955                                branch |= 1 << i;
956                        }
957                at = at->kid + branch;
958        }
959        avh.next = at->alist;           /* order by increasing level */
960        for (ap = &avh; ap->next != NULL; ap = ap->next)
961                if ( ap->next->lvl > av->lvl ||
962                                (ap->next->lvl == av->lvl) &
963                                (ap->next->weight <= av->weight) )
964                        break;
965        av->next = ap->next;
966        ap->next = (AMBVAL*)av;
967        at->alist = avh.next;
968 }
969
970 #endif  /* ! NEWAMB */
971
972 /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
973
974 static void
664   initambfile(            /* initialize ambient file */
665          int  cre8
666   )
# Line 1072 | Line 761 | newambtree(void)                               /* allocate 8 ambient tree structs
761          }
762          atp = atfreelist;
763          atfreelist = atp->kid;
764 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
764 >        memset(atp, 0, 8*sizeof(AMBTREE));
765          return(atp);
766   }
767  
# Line 1098 | Line 787 | unloadatree(                   /* unload an ambient value tree */
787                                          /* transfer values at this node */
788          for (av = at->alist; av != NULL; av = at->alist) {
789                  at->alist = av->next;
790 +                av->next = NULL;
791                  (*f)(av);
792          }
793          if (at->kid == NULL)
# Line 1175 | Line 865 | avlmemi(                               /* find list position from address */
865   {
866          AMBVAL  **avlpp;
867  
868 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
869 <                        nambvals, sizeof(AMBVAL *), &aposcmp);
868 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
869 >                        nambvals, sizeof(AMBVAL *), aposcmp);
870          if (avlpp == NULL)
871                  error(CONSISTENCY, "address not found in avlmemi");
872          return(avlpp - avlist2);
# Line 1219 | Line 909 | sortambvals(                   /* resort ambient values */
909          }
910          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
911                  if (avlist2 != NULL)
912 <                        free((void *)avlist2);
912 >                        free(avlist2);
913                  if (always) {           /* rebuild without sorting */
914                          oldatrunk = atrunk;
915                          atrunk.alist = NULL;
# Line 1246 | Line 936 | sortambvals(                   /* resort ambient values */
936                  if (i_avlist < nambvals)
937                          error(CONSISTENCY, "missing ambient values in sortambvals");
938   #endif
939 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
940 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
939 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
940 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
941                  for (i = 0; i < nambvals; i++) {
942                          if (avlist1[i].p == NULL)
943                                  continue;
# Line 1263 | Line 953 | sortambvals(                   /* resort ambient values */
953                          avinsert(avlist2[j]);
954                          avlist1[j].p = NULL;
955                  }
956 <                free((void *)avlist1);
957 <                free((void *)avlist2);
956 >                free(avlist1);
957 >                free(avlist2);
958                                                  /* compute new sort interval */
959                  sortintvl = ambclock - lastsort;
960                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 1292 | Line 982 | aflock(                        /* lock/unlock ambient file */
982  
983          if (typ == fls.l_type)          /* already called? */
984                  return;
985 +
986          fls.l_type = typ;
987 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
988 <                error(SYSTEM, "cannot (un)lock ambient file");
987 >        do
988 >                if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1)
989 >                        return;
990 >        while (errno == EINTR);
991 >        
992 >        error(SYSTEM, "cannot (un)lock ambient file");
993   }
994  
995  
# Line 1313 | Line 1008 | ambsync(void)                  /* synchronize ambient file */
1008          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1009                  goto seekerr;
1010          if ((n = flen - lastpos) > 0) {         /* file has grown */
1011 <                if (ambinp == NULL) {           /* use duplicate filedes */
1012 <                        ambinp = fdopen(dup(fileno(ambfp)), "r");
1011 >                if (ambinp == NULL) {           /* get new file pointer */
1012 >                        ambinp = fopen(ambfile, "rb");
1013                          if (ambinp == NULL)
1014 <                                error(SYSTEM, "fdopen failed in ambsync");
1014 >                                error(SYSTEM, "fopen failed in ambsync");
1015                  }
1016                  if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1017                          goto seekerr;
# Line 1331 | Line 1026 | ambsync(void)                  /* synchronize ambient file */
1026                          avstore(&avs);
1027                          n -= AMBVALSIZ;
1028                  }
1029 <                lastpos = flen - n;
1030 <                /*** seek always as safety measure
1031 <                if (n) ***/                     /* alignment */
1337 <                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1338 <                                goto seekerr;
1029 >                lastpos = flen - n;             /* check alignment */
1030 >                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1031 >                        goto seekerr;
1032          }
1033          n = fflush(ambfp);                      /* calls write() at last */
1034 <        if (n != EOF)
1342 <                lastpos += (long)nunflshed*AMBVALSIZ;
1343 <        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1344 <                goto seekerr;
1345 <                
1034 >        lastpos += (long)nunflshed*AMBVALSIZ;
1035          aflock(F_UNLCK);                        /* release file */
1036          nunflshed = 0;
1037          return(n);
1038   seekerr:
1039          error(SYSTEM, "seek failed in ambsync");
1040 <        return -1; /* pro forma return */
1040 >        return(EOF);    /* pro forma return */
1041   }
1042  
1043   #else   /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines