--- ray/src/rt/ambient.c 2015/07/29 18:57:03 2.96 +++ ray/src/rt/ambient.c 2019/02/16 00:09:53 2.107 @@ -1,4 +1,4 @@ -static const char RCSid[] = "$Id: ambient.c,v 2.96 2015/07/29 18:57:03 rschregle Exp $"; +static const char RCSid[] = "$Id: ambient.c,v 2.107 2019/02/16 00:09:53 greg Exp $"; /* * ambient.c - routines dealing with ambient (inter-reflected) component. * @@ -12,6 +12,7 @@ static const char RCSid[] = "$Id: ambient.c,v 2.96 201 #include "platform.h" #include "ray.h" #include "otypes.h" +#include "otspecial.h" #include "resolu.h" #include "ambient.h" #include "random.h" @@ -76,7 +77,6 @@ static long lastpos = -1; /* last flush position */ #define AMBFLUSH (BUFSIZ/AMBVALSIZ) #define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) -#define freeav(av) free((void *)av); static void initambfile(int creat); static void avsave(AMBVAL *av); @@ -132,7 +132,7 @@ setambacc( /* set ambient accuracy */ newa *= (newa > 0); if (fabs(newa - olda) >= .05*(newa + olda)) { ambacc = newa; - if (nambvals > 0) + if (ambacc > FTINY && nambvals > 0) sortambvals(1); /* rebuild tree */ } } @@ -184,9 +184,7 @@ setambient(void) /* initialize calculation */ (flen - lastpos)/AMBVALSIZ); error(WARNING, errmsg); fseek(ambfp, lastpos, SEEK_SET); -#ifndef _WIN32 /* XXX we need a replacement for that one */ ftruncate(fileno(ambfp), (off_t)lastpos); -#endif } } else if ((ambfp = fopen(ambfile, "w+")) != NULL) { initambfile(1); /* else create new file */ @@ -196,9 +194,6 @@ setambient(void) /* initialize calculation */ sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); error(SYSTEM, errmsg); } -#ifdef getc_unlocked - flockfile(ambfp); /* application-level lock */ -#endif #ifdef F_SETLKW aflock(F_UNLCK); /* release file */ #endif @@ -219,7 +214,7 @@ ambdone(void) /* close ambient file and free memory lastpos = -1; } /* free ambient tree */ - unloadatree(&atrunk, &avfree); + unloadatree(&atrunk, avfree); /* reset state variables */ avsum = 0.; navsum = 0; @@ -282,7 +277,7 @@ multambient( /* compute ambient component & multiply { static int rdepth = 0; /* ambient recursion */ COLOR acol, caustic; - int ok; + int i, ok; double d, l; /* PMAP: Factor in ambient from photon map, if enabled and ray is @@ -308,13 +303,27 @@ multambient( /* compute ambient component & multiply goto dumbamb; if (ambacc <= FTINY) { /* no ambient storage */ + FVECT uvd[2]; + float dgrad[2], *dgp = NULL; + + if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999) + dgp = dgrad; /* compute rotational grad. */ copycolor(acol, aval); rdepth++; ok = doambient(acol, r, r->rweight, - NULL, NULL, NULL, NULL, NULL); + uvd, NULL, NULL, dgp, NULL); rdepth--; if (!ok) goto dumbamb; + if ((ok > 0) & (dgp != NULL)) { /* apply texture */ + FVECT v1; + VCROSS(v1, r->ron, nrm); + d = 1.0; + for (i = 3; i--; ) + d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]); + if (d >= 0.05) + scalecolor(acol, d); + } copycolor(aval, acol); /* PMAP: add in caustic */ @@ -407,7 +416,8 @@ plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang) VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */ rtst.rmax = normalize(rtst.rdir); /* short ray test */ while (localhit(&rtst, &thescene)) { /* check for occluder */ - if (rtst.ro->omod != OVOID && + OBJREC *m = findmaterial(rtst.ro); + if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) && (rtst.clipset == NULL || !inset(rtst.clipset, rtst.ro->omod))) return(1); /* plug light leak */ @@ -1077,7 +1087,7 @@ newambtree(void) /* allocate 8 ambient tree structs } atp = atfreelist; atfreelist = atp->kid; - memset((char *)atp, '\0', 8*sizeof(AMBTREE)); + memset(atp, 0, 8*sizeof(AMBTREE)); return(atp); } @@ -1103,6 +1113,7 @@ unloadatree( /* unload an ambient value tree */ /* transfer values at this node */ for (av = at->alist; av != NULL; av = at->alist) { at->alist = av->next; + av->next = NULL; (*f)(av); } if (at->kid == NULL) @@ -1180,8 +1191,8 @@ avlmemi( /* find list position from address */ { AMBVAL **avlpp; - avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, - nambvals, sizeof(AMBVAL *), &aposcmp); + avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, + nambvals, sizeof(AMBVAL *), aposcmp); if (avlpp == NULL) error(CONSISTENCY, "address not found in avlmemi"); return(avlpp - avlist2); @@ -1224,12 +1235,12 @@ sortambvals( /* resort ambient values */ } if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ if (avlist2 != NULL) - free((void *)avlist2); + free(avlist2); if (always) { /* rebuild without sorting */ oldatrunk = atrunk; atrunk.alist = NULL; atrunk.kid = NULL; - unloadatree(&oldatrunk, &avinsert); + unloadatree(&oldatrunk, avinsert); } } else { /* sort memory by last access time */ /* @@ -1246,13 +1257,13 @@ sortambvals( /* resort ambient values */ eputs(errmsg); #endif i_avlist = 0; - unloadatree(&atrunk, &av2list); /* empty current tree */ + unloadatree(&atrunk, av2list); /* empty current tree */ #ifdef DEBUG if (i_avlist < nambvals) error(CONSISTENCY, "missing ambient values in sortambvals"); #endif - qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); - qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); + qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); + qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); for (i = 0; i < nambvals; i++) { if (avlist1[i].p == NULL) continue; @@ -1268,8 +1279,8 @@ sortambvals( /* resort ambient values */ avinsert(avlist2[j]); avlist1[j].p = NULL; } - free((void *)avlist1); - free((void *)avlist2); + free(avlist1); + free(avlist2); /* compute new sort interval */ sortintvl = ambclock - lastsort; if (sortintvl >= MAX_SORT_INTVL/2) @@ -1318,10 +1329,10 @@ ambsync(void) /* synchronize ambient file */ if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) goto seekerr; if ((n = flen - lastpos) > 0) { /* file has grown */ - if (ambinp == NULL) { /* use duplicate filedes */ - ambinp = fdopen(dup(fileno(ambfp)), "r"); + if (ambinp == NULL) { /* get new file pointer */ + ambinp = fopen(ambfile, "rb"); if (ambinp == NULL) - error(SYSTEM, "fdopen failed in ambsync"); + error(SYSTEM, "fopen failed in ambsync"); } if (fseek(ambinp, lastpos, SEEK_SET) < 0) goto seekerr; @@ -1336,24 +1347,18 @@ ambsync(void) /* synchronize ambient file */ avstore(&avs); n -= AMBVALSIZ; } - lastpos = flen - n; - /*** seek always as safety measure - if (n) ***/ /* alignment */ - if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) - goto seekerr; + lastpos = flen - n; /* check alignment */ + if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) + goto seekerr; } n = fflush(ambfp); /* calls write() at last */ - if (n != EOF) - lastpos += (long)nunflshed*AMBVALSIZ; - else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) - goto seekerr; - + lastpos += (long)nunflshed*AMBVALSIZ; aflock(F_UNLCK); /* release file */ nunflshed = 0; return(n); seekerr: error(SYSTEM, "seek failed in ambsync"); - return -1; /* pro forma return */ + return(EOF); /* pro forma return */ } #else /* ! F_SETLKW */