--- ray/src/rt/ambient.c 2016/03/06 01:13:17 2.101 +++ 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.101 2016/03/06 01:13:17 schorsch 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.101 20 #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 */ } } @@ -277,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 @@ -303,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 */ @@ -402,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 */ @@ -1072,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); } @@ -1098,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) @@ -1175,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); @@ -1219,7 +1235,7 @@ 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; @@ -1246,8 +1262,8 @@ sortambvals( /* resort ambient values */ 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; @@ -1263,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) @@ -1313,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; @@ -1331,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 */