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.104 by greg, Fri Jan 27 22:00:49 2017 UTC

# Line 76 | Line 76 | static long  lastpos = -1;             /* last flush position */
76   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
77  
78   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79 #define  freeav(av)     free((void *)av);
79  
80   static void initambfile(int creat);
81   static void avsave(AMBVAL *av);
# Line 184 | Line 183 | setambient(void)                               /* initialize calculation */
183                                          (flen - lastpos)/AMBVALSIZ);
184                          error(WARNING, errmsg);
185                          fseek(ambfp, lastpos, SEEK_SET);
187 #ifndef _WIN32 /* XXX we need a replacement for that one */
186                          ftruncate(fileno(ambfp), (off_t)lastpos);
189 #endif
187                  }
188          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
189                  initambfile(1);                 /* else create new file */
# Line 196 | Line 193 | setambient(void)                               /* initialize calculation */
193                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
194                  error(SYSTEM, errmsg);
195          }
199 #if 0
200        flockfile(ambfp);                       /* application-level lock */
201 #endif
196   #ifdef  F_SETLKW
197          aflock(F_UNLCK);                        /* release file */
198   #endif
# Line 219 | Line 213 | ambdone(void)                  /* close ambient file and free memory
213                  lastpos = -1;
214          }
215                                          /* free ambient tree */
216 <        unloadatree(&atrunk, &avfree);
216 >        unloadatree(&atrunk, avfree);
217                                          /* reset state variables */
218          avsum = 0.;
219          navsum = 0;
# Line 282 | Line 276 | multambient(           /* compute ambient component & multiply
276   {
277          static int  rdepth = 0;                 /* ambient recursion */
278          COLOR   acol, caustic;
279 <        int     ok;
279 >        int     i, ok;
280          double  d, l;
281  
282          /* PMAP: Factor in ambient from photon map, if enabled and ray is
# Line 308 | Line 302 | multambient(           /* compute ambient component & multiply
302                  goto dumbamb;
303  
304          if (ambacc <= FTINY) {                  /* no ambient storage */
305 +                FVECT   uvd[2];
306 +                float   dgrad[2], *dgp = NULL;
307 +
308 +                if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999)
309 +                        dgp = dgrad;            /* compute rotational grad. */
310                  copycolor(acol, aval);
311                  rdepth++;
312                  ok = doambient(acol, r, r->rweight,
313 <                                NULL, NULL, NULL, NULL, NULL);
313 >                                uvd, NULL, NULL, dgp, NULL);
314                  rdepth--;
315                  if (!ok)
316                          goto dumbamb;
317 +                if ((ok > 0) & (dgp != NULL)) { /* apply texture */
318 +                        FVECT   v1;
319 +                        VCROSS(v1, r->ron, nrm);
320 +                        d = 1.0;
321 +                        for (i = 3; i--; )
322 +                                d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]);
323 +                        if (d >= 0.05)
324 +                                scalecolor(acol, d);
325 +                }
326                  copycolor(aval, acol);
327  
328                  /* PMAP: add in caustic */
# Line 1077 | Line 1085 | newambtree(void)                               /* allocate 8 ambient tree structs
1085          }
1086          atp = atfreelist;
1087          atfreelist = atp->kid;
1088 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
1088 >        memset(atp, 0, 8*sizeof(AMBTREE));
1089          return(atp);
1090   }
1091  
# Line 1103 | Line 1111 | unloadatree(                   /* unload an ambient value tree */
1111                                          /* transfer values at this node */
1112          for (av = at->alist; av != NULL; av = at->alist) {
1113                  at->alist = av->next;
1114 +                av->next = NULL;
1115                  (*f)(av);
1116          }
1117          if (at->kid == NULL)
# Line 1180 | Line 1189 | avlmemi(                               /* find list position from address */
1189   {
1190          AMBVAL  **avlpp;
1191  
1192 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1193 <                        nambvals, sizeof(AMBVAL *), &aposcmp);
1192 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1193 >                        nambvals, sizeof(AMBVAL *), aposcmp);
1194          if (avlpp == NULL)
1195                  error(CONSISTENCY, "address not found in avlmemi");
1196          return(avlpp - avlist2);
# Line 1224 | Line 1233 | sortambvals(                   /* resort ambient values */
1233          }
1234          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1235                  if (avlist2 != NULL)
1236 <                        free((void *)avlist2);
1236 >                        free(avlist2);
1237                  if (always) {           /* rebuild without sorting */
1238                          oldatrunk = atrunk;
1239                          atrunk.alist = NULL;
1240                          atrunk.kid = NULL;
1241 <                        unloadatree(&oldatrunk, &avinsert);
1241 >                        unloadatree(&oldatrunk, avinsert);
1242                  }
1243          } else {                        /* sort memory by last access time */
1244                  /*
# Line 1246 | Line 1255 | sortambvals(                   /* resort ambient values */
1255                  eputs(errmsg);
1256   #endif
1257                  i_avlist = 0;
1258 <                unloadatree(&atrunk, &av2list); /* empty current tree */
1258 >                unloadatree(&atrunk, av2list);  /* empty current tree */
1259   #ifdef DEBUG
1260                  if (i_avlist < nambvals)
1261                          error(CONSISTENCY, "missing ambient values in sortambvals");
1262   #endif
1263 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
1264 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1263 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1264 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1265                  for (i = 0; i < nambvals; i++) {
1266                          if (avlist1[i].p == NULL)
1267                                  continue;
# Line 1268 | Line 1277 | sortambvals(                   /* resort ambient values */
1277                          avinsert(avlist2[j]);
1278                          avlist1[j].p = NULL;
1279                  }
1280 <                free((void *)avlist1);
1281 <                free((void *)avlist2);
1280 >                free(avlist1);
1281 >                free(avlist2);
1282                                                  /* compute new sort interval */
1283                  sortintvl = ambclock - lastsort;
1284                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 1318 | Line 1327 | ambsync(void)                  /* synchronize ambient file */
1327          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1328                  goto seekerr;
1329          if ((n = flen - lastpos) > 0) {         /* file has grown */
1330 <                if (ambinp == NULL) {           /* use duplicate filedes */
1331 <                        ambinp = fdopen(dup(fileno(ambfp)), "r");
1330 >                if (ambinp == NULL) {           /* get new file pointer */
1331 >                        ambinp = fopen(ambfile, "rb");
1332                          if (ambinp == NULL)
1333 <                                error(SYSTEM, "fdopen failed in ambsync");
1333 >                                error(SYSTEM, "fopen failed in ambsync");
1334                  }
1335                  if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1336                          goto seekerr;
# Line 1336 | Line 1345 | ambsync(void)                  /* synchronize ambient file */
1345                          avstore(&avs);
1346                          n -= AMBVALSIZ;
1347                  }
1348 <                lastpos = flen - n;
1349 <                /*** seek always as safety measure
1350 <                if (n) ***/                     /* alignment */
1342 <                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1343 <                                goto seekerr;
1348 >                lastpos = flen - n;             /* check alignment */
1349 >                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1350 >                        goto seekerr;
1351          }
1352          n = fflush(ambfp);                      /* calls write() at last */
1353 <        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 <                
1353 >        lastpos += (long)nunflshed*AMBVALSIZ;
1354          aflock(F_UNLCK);                        /* release file */
1355          nunflshed = 0;
1356          return(n);
1357   seekerr:
1358          error(SYSTEM, "seek failed in ambsync");
1359 <        return -1; /* pro forma return */
1359 >        return(EOF);    /* pro forma return */
1360   }
1361  
1362   #else   /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines