--- ray/src/rt/source.c 2015/02/24 19:39:27 2.64 +++ ray/src/rt/source.c 2020/12/17 03:30:37 2.74 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.64 2015/02/24 19:39:27 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.74 2020/12/17 03:30:37 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -9,11 +9,12 @@ static const char RCSid[] = "$Id: source.c,v 2.64 2015 #include "ray.h" #include "otypes.h" +#include "otspecial.h" #include "rtotypes.h" #include "source.h" #include "random.h" -#include "pmap.h" #include "pmapsrc.h" +#include "pmapmat.h" #ifndef MAXSSAMP #define MAXSSAMP 16 /* maximum samples per ray */ @@ -42,32 +43,6 @@ static int maxcntr = 0; /* size of contribution arra static int cntcmp(const void *p1, const void *p2); -OBJREC * /* find an object's actual material */ -findmaterial(OBJREC *o) -{ - while (!ismaterial(o->otype)) { - if (o->otype == MOD_ALIAS && o->oargs.nsargs) { - OBJECT aobj; - OBJREC *ao; - aobj = lastmod(objndx(o), o->oargs.sarg[0]); - if (aobj < 0) - objerror(o, USER, "bad reference"); - ao = objptr(aobj); - if (ismaterial(ao->otype)) - return(ao); - if (ao->otype == MOD_ALIAS) { - o = ao; - continue; - } - } - if (o->omod == OVOID) - return(NULL); - o = objptr(o->omod); - } - return(o); /* mixtures will return NULL */ -} - - void marksources(void) /* find and mark source objects */ { @@ -99,8 +74,8 @@ marksources(void) /* find and mark source objects */ m->otype == MAT_SPOT ? 7 : 3)) objerror(m, USER, "bad # arguments"); - if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY && - m->oargs.farg[2] <= FTINY) + if (m->oargs.farg[0] <= FTINY && (m->oargs.farg[1] <= FTINY) & + (m->oargs.farg[2] <= FTINY)) continue; /* don't bother */ if (m->otype == MAT_GLOW && o->otype != OBJ_SOURCE && @@ -125,6 +100,9 @@ marksources(void) /* find and mark source objects */ foundsource += (ambounce > 0); } } else if (m->otype == MAT_SPOT) { + if (source[ns].sflags & SDISTANT) + objerror(o, WARNING, + "distant source is a spotlight"); source[ns].sflags |= SSPOT; if ((source[ns].sl.s = makespot(m)) == NULL) goto memerr; @@ -135,16 +113,16 @@ marksources(void) /* find and mark source objects */ source[ns].sflags |= SSKIP; } } -#if SHADCACHE - initobscache(ns); -#endif foundsource += !(source[ns].sflags & SSKIP); } if (!foundsource) { error(WARNING, "no light sources found"); return; } - +#if SHADCACHE + for (ns = 0; ns < nsources; ns++) /* initialize obstructor cache */ + initobscache(ns); +#endif /* PMAP: disable virtual sources */ if (!photonMapping) markvirtuals(); /* find and add virtual sources */ @@ -153,15 +131,61 @@ marksources(void) /* find and mark source objects */ maxcntr = nsources + MAXSPART; /* start with this many */ srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB)); cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR)); - if ((srccnt == NULL) | (cntord == NULL)) - goto memerr; - return; + if ((srccnt != NULL) & (cntord != NULL)) + return; memerr: error(SYSTEM, "out of memory in marksources"); } void +distantsources(void) /* only mark distant sources */ +{ + int i; + OBJREC *o, *m; + int ns; + /* initialize dispatch table */ + initstypes(); + /* sources needed for sourcehit() */ + for (i = 0; i < nsceneobjs; i++) { + + o = objptr(i); + + if ((o->otype != OBJ_SOURCE) | (o->omod == OVOID)) + continue; + /* find material */ + m = findmaterial(objptr(o->omod)); + if (m == NULL) + continue; + if (!islight(m->otype)) + continue; /* not source modifier */ + + if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : + m->otype == MAT_SPOT ? 7 : 3)) + objerror(m, USER, "bad # arguments"); + + if (m->oargs.farg[0] <= FTINY && (m->oargs.farg[1] <= FTINY) & + (m->oargs.farg[2] <= FTINY)) + continue; /* don't bother */ + if (sfun[o->otype].of == NULL || + sfun[o->otype].of->setsrc == NULL) + objerror(o, USER, "illegal material"); + + if ((ns = newsource()) < 0) + error(SYSTEM, "out of memory in distantsources"); + + setsource(&source[ns], o); + + if (m->otype == MAT_GLOW) { + source[ns].sflags |= SPROX|SSKIP; + source[ns].sl.prox = m->oargs.farg[3]; + } else if (m->otype == MAT_SPOT) + objerror(o, WARNING, "distant source is a spotlight"); + } +} + + +void freesources(void) /* free all source structures */ { if (nsources > 0) { @@ -410,18 +434,17 @@ direct( /* add direct component */ cntord[sn].sndx = sn; scp = srccnt + sn; scp->sno = sr.rsrc; - /* compute coefficient */ - (*f)(scp->coef, p, sr.rdir, si.dom); - cntord[sn].brt = intens(scp->coef); - if (cntord[sn].brt <= 0.0) - continue; -#if SHADCACHE - /* check shadow cache */ +#if SHADCACHE /* check shadow cache */ if (si.np == 1 && srcblocked(&sr)) { cntord[sn].brt = 0.0; continue; } #endif + /* compute coefficient */ + (*f)(scp->coef, p, sr.rdir, si.dom); + cntord[sn].brt = intens(scp->coef); + if (cntord[sn].brt <= 0.0) + continue; VCOPY(scp->dir, sr.rdir); copycolor(sr.rcoef, scp->coef); /* compute potential */ @@ -454,19 +477,17 @@ direct( /* add direct component */ /* compute number to check */ nshadcheck = pow((double)ncnts, shadcert) + .5; /* modify threshold */ - if (ncnts > MINSHADCNT) - ourthresh = shadthresh / r->rweight; - else - ourthresh = 0; + ourthresh = shadthresh / r->rweight; /* test for shadows */ for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts; hwt += (double)source[scp->sno].nhits / (double)source[scp->sno].ntests, sn++) { /* check threshold */ - if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : + if (sn >= MINSHADCNT && + (sn+nshadcheck>=ncnts ? cntord[sn].brt : cntord[sn].brt-cntord[sn+nshadcheck].brt) - < ourthresh*bright(r->rcol)) + < ourthresh*bright(r->rcol)) break; scp = srccnt + cntord[sn].sndx; /* test for hit */ @@ -543,13 +564,13 @@ srcscatter( /* compute source scattering into ray */ SRCINDEX si; double t, d; double re, ge, be; - COLOR cvext, pmapInscatter; + COLOR cvext; int i, j; - /* PMAP: do unconditional inscattering for volume photons ? */ - /* if (!volumePhotonMapping) */ - if (r->slights == NULL || r->slights[0] == 0 - || r->gecc >= 1.-FTINY || r->rot >= FHUGE) + if (r->rot >= FHUGE*.99 || r->gecc >= 1.-FTINY) + return; /* this can never work */ + /* PMAP: do unconditional inscattering for volume photons */ + if (!volumePhotonMapping && (r->slights == NULL || r->slights[0] == 0)) return; if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) @@ -560,7 +581,8 @@ srcscatter( /* compute source scattering into ray */ #endif oldsampndx = samplendx; samplendx = random()&0x7fff; /* randomize */ - for (i = r->slights[0]; i > 0; i--) { /* for each source */ + for (i = volumePhotonMapping ? 1 : r->slights[0]; i > 0; i--) { + /* for each source OR once if volume photon map enabled */ for (j = 0; j < nsamps; j++) { /* for each sample position */ samplendx++; t = r->rot * (j+frandom())/nsamps; @@ -576,45 +598,51 @@ srcscatter( /* compute source scattering into ray */ sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; - initsrcindex(&si); /* sample ray to this source */ - si.sn = r->slights[i]; - nopart(&si, &sr); - if (!srcray(&sr, NULL, &si) || - sr.rsrc != r->slights[i]) - continue; /* no path */ + + if (!volumePhotonMapping) { + initsrcindex(&si); /* sample ray to this source */ + si.sn = r->slights[i]; + nopart(&si, &sr); + if (!srcray(&sr, NULL, &si) || + sr.rsrc != r->slights[i]) + continue; /* no path */ #if SHADCACHE - if (srcblocked(&sr)) /* check shadow cache */ - continue; + if (srcblocked(&sr)) /* check shadow cache */ + continue; #endif - copycolor(sr.cext, r->cext); - copycolor(sr.albedo, r->albedo); - sr.gecc = r->gecc; - sr.slights = r->slights; - rayvalue(&sr); /* eval. source ray */ - if (bright(sr.rcol) <= FTINY) { + copycolor(sr.cext, r->cext); + copycolor(sr.albedo, r->albedo); + sr.gecc = r->gecc; + sr.slights = r->slights; + rayvalue(&sr); /* eval. source ray */ + if (bright(sr.rcol) <= FTINY) { #if SHADCACHE - srcblocker(&sr); /* add blocker to cache */ + srcblocker(&sr); /* add blocker to cache */ #endif - continue; - } - if (r->gecc <= FTINY) /* compute P(theta) */ - d = 1.; - else { - d = DOT(r->rdir, sr.rdir); - d = 1. + r->gecc*r->gecc - 2.*r->gecc*d; - d = (1. - r->gecc*r->gecc) / (d*sqrt(d)); - } + continue; + } + if (r->gecc <= FTINY) /* compute P(theta) */ + d = 1.; + else { + d = DOT(r->rdir, sr.rdir); + d = 1. + r->gecc*r->gecc - 2.*r->gecc*d; + d = (1. - r->gecc*r->gecc) / (d*sqrt(d)); + } /* other factors */ - d *= si.dom * r->rot / (4.*PI*nsamps); - scalecolor(sr.rcol, d); - - /* PMAP: Add ambient inscattering from volume photons once only */ - if (volumePhotonMapping && i == 1) { - inscatterVolumePmap(&sr, pmapInscatter); - scalecolor(pmapInscatter, r -> rot / nsamps); - addcolor(sr.rcol, pmapInscatter); - } - + d *= si.dom * r->rot / (4.*PI*nsamps); + scalecolor(sr.rcol, d); + } else { + /* PMAP: Add ambient inscattering from + * volume photons; note we reverse the + * incident ray direction since we're + * now in *backward* raytracing mode! */ + sr.rdir [0] = -r -> rdir [0]; + sr.rdir [1] = -r -> rdir [1]; + sr.rdir [2] = -r -> rdir [2]; + sr.gecc = r -> gecc; + inscatterVolumePmap(&sr, sr.rcol); + scalecolor(sr.rcol, r -> rot / nsamps); + } multcolor(sr.rcol, r->cext); multcolor(sr.rcol, r->albedo); multcolor(sr.rcol, cvext); @@ -683,10 +711,14 @@ weaksrcmat(OBJECT obj) /* identify material */ * The same is true for stray specular samples, since the specular * contribution from light sources is calculated separately. */ - -#define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \ +/* PMAP: Also avoid counting sources via transferred ambient rays (e.g. + * through glass) when photon mapping is enabled, as these indirect + * components are already accounted for. + */ +#define badcomponent(m, r) (srcRayInPmap(r) || \ + (r->crtype&(AMBIENT|SPECULAR) && \ !(r->crtype&SHADOW || r->rod < 0.0 || \ - /* not 100% correct */ distglow(m, r, r->rot))) + /* not 100% correct */ distglow(m, r, r->rot)))) /* passillum * *