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.97 by greg, Fri Aug 21 18:21:05 2015 UTC vs.
Revision 2.105 by greg, Tue Jan 9 05:01:15 2018 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  "source.h"
19   #include  "random.h"
20   #include  "pmapamb.h"
21  
# Line 76 | Line 78 | static long  lastpos = -1;             /* last flush position */
78   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
79  
80   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79 #define  freeav(av)     free((void *)av);
81  
82   static void initambfile(int creat);
83   static void avsave(AMBVAL *av);
# Line 184 | Line 185 | setambient(void)                               /* initialize calculation */
185                                          (flen - lastpos)/AMBVALSIZ);
186                          error(WARNING, errmsg);
187                          fseek(ambfp, lastpos, SEEK_SET);
187 #ifndef _WIN32 /* XXX we need a replacement for that one */
188                          ftruncate(fileno(ambfp), (off_t)lastpos);
189 #endif
189                  }
190          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
191                  initambfile(1);                 /* else create new file */
# Line 196 | Line 195 | setambient(void)                               /* initialize calculation */
195                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
196                  error(SYSTEM, errmsg);
197          }
199 #if 0
200        flockfile(ambfp);                       /* application-level lock */
201 #endif
198   #ifdef  F_SETLKW
199          aflock(F_UNLCK);                        /* release file */
200   #endif
# Line 219 | Line 215 | ambdone(void)                  /* close ambient file and free memory
215                  lastpos = -1;
216          }
217                                          /* free ambient tree */
218 <        unloadatree(&atrunk, &avfree);
218 >        unloadatree(&atrunk, avfree);
219                                          /* reset state variables */
220          avsum = 0.;
221          navsum = 0;
# Line 282 | Line 278 | multambient(           /* compute ambient component & multiply
278   {
279          static int  rdepth = 0;                 /* ambient recursion */
280          COLOR   acol, caustic;
281 <        int     ok;
281 >        int     i, ok;
282          double  d, l;
283  
284          /* PMAP: Factor in ambient from photon map, if enabled and ray is
# Line 308 | 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 407 | 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 1077 | Line 1088 | newambtree(void)                               /* allocate 8 ambient tree structs
1088          }
1089          atp = atfreelist;
1090          atfreelist = atp->kid;
1091 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
1091 >        memset(atp, 0, 8*sizeof(AMBTREE));
1092          return(atp);
1093   }
1094  
# Line 1103 | Line 1114 | unloadatree(                   /* unload an ambient value tree */
1114                                          /* transfer values at this node */
1115          for (av = at->alist; av != NULL; av = at->alist) {
1116                  at->alist = av->next;
1117 +                av->next = NULL;
1118                  (*f)(av);
1119          }
1120          if (at->kid == NULL)
# Line 1180 | Line 1192 | avlmemi(                               /* find list position from address */
1192   {
1193          AMBVAL  **avlpp;
1194  
1195 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1196 <                        nambvals, sizeof(AMBVAL *), &aposcmp);
1195 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1196 >                        nambvals, sizeof(AMBVAL *), aposcmp);
1197          if (avlpp == NULL)
1198                  error(CONSISTENCY, "address not found in avlmemi");
1199          return(avlpp - avlist2);
# Line 1224 | Line 1236 | sortambvals(                   /* resort ambient values */
1236          }
1237          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1238                  if (avlist2 != NULL)
1239 <                        free((void *)avlist2);
1239 >                        free(avlist2);
1240                  if (always) {           /* rebuild without sorting */
1241                          oldatrunk = atrunk;
1242                          atrunk.alist = NULL;
1243                          atrunk.kid = NULL;
1244 <                        unloadatree(&oldatrunk, &avinsert);
1244 >                        unloadatree(&oldatrunk, avinsert);
1245                  }
1246          } else {                        /* sort memory by last access time */
1247                  /*
# Line 1246 | Line 1258 | sortambvals(                   /* resort ambient values */
1258                  eputs(errmsg);
1259   #endif
1260                  i_avlist = 0;
1261 <                unloadatree(&atrunk, &av2list); /* empty current tree */
1261 >                unloadatree(&atrunk, av2list);  /* empty current tree */
1262   #ifdef DEBUG
1263                  if (i_avlist < nambvals)
1264                          error(CONSISTENCY, "missing ambient values in sortambvals");
1265   #endif
1266 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
1267 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1266 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1267 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1268                  for (i = 0; i < nambvals; i++) {
1269                          if (avlist1[i].p == NULL)
1270                                  continue;
# Line 1268 | Line 1280 | sortambvals(                   /* resort ambient values */
1280                          avinsert(avlist2[j]);
1281                          avlist1[j].p = NULL;
1282                  }
1283 <                free((void *)avlist1);
1284 <                free((void *)avlist2);
1283 >                free(avlist1);
1284 >                free(avlist2);
1285                                                  /* compute new sort interval */
1286                  sortintvl = ambclock - lastsort;
1287                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 1318 | Line 1330 | ambsync(void)                  /* synchronize ambient file */
1330          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1331                  goto seekerr;
1332          if ((n = flen - lastpos) > 0) {         /* file has grown */
1333 <                if (ambinp == NULL) {           /* use duplicate filedes */
1334 <                        ambinp = fdopen(dup(fileno(ambfp)), "r");
1333 >                if (ambinp == NULL) {           /* get new file pointer */
1334 >                        ambinp = fopen(ambfile, "rb");
1335                          if (ambinp == NULL)
1336 <                                error(SYSTEM, "fdopen failed in ambsync");
1336 >                                error(SYSTEM, "fopen failed in ambsync");
1337                  }
1338                  if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1339                          goto seekerr;
# Line 1336 | Line 1348 | ambsync(void)                  /* synchronize ambient file */
1348                          avstore(&avs);
1349                          n -= AMBVALSIZ;
1350                  }
1351 <                lastpos = flen - n;
1352 <                /*** seek always as safety measure
1353 <                if (n) ***/                     /* alignment */
1342 <                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1343 <                                goto seekerr;
1351 >                lastpos = flen - n;             /* check alignment */
1352 >                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1353 >                        goto seekerr;
1354          }
1355          n = fflush(ambfp);                      /* calls write() at last */
1356 <        if (n != EOF)
1347 <                lastpos += (long)nunflshed*AMBVALSIZ;
1348 <        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1349 <                goto seekerr;
1350 <                
1356 >        lastpos += (long)nunflshed*AMBVALSIZ;
1357          aflock(F_UNLCK);                        /* release file */
1358          nunflshed = 0;
1359          return(n);
1360   seekerr:
1361          error(SYSTEM, "seek failed in ambsync");
1362 <        return -1; /* pro forma return */
1362 >        return(EOF);    /* pro forma return */
1363   }
1364  
1365   #else   /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines