--- ray/src/rt/source.c 1997/08/15 10:24:24 2.27 +++ ray/src/rt/source.c 2021/02/01 16:19:49 2.76 @@ -1,27 +1,21 @@ -/* Copyright (c) 1995 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: source.c,v 2.76 2021/02/01 16:19:49 greg Exp $"; #endif - /* * source.c - routines dealing with illumination sources. * - * 8/20/85 + * External symbols declared in source.h */ #include "ray.h" - -#include "octree.h" - #include "otypes.h" - +#include "otspecial.h" +#include "rtotypes.h" #include "source.h" - #include "random.h" +#include "pmapsrc.h" +#include "pmapmat.h" -extern double ssampdist; /* scatter sampling distance */ - #ifndef MAXSSAMP #define MAXSSAMP 16 /* maximum samples per ray */ #endif @@ -46,40 +40,49 @@ static CONTRIB *srccnt; /* source contributions in d static CNTPTR *cntord; /* source ordering in direct() */ static int maxcntr = 0; /* size of contribution arrays */ +static int cntcmp(const void *p1, const void *p2); -marksources() /* find and mark source objects */ + +void +marksources(void) /* find and mark source objects */ { int foundsource = 0; int i; - register OBJREC *o, *m; - register int ns; + OBJREC *o, *m; + int ns; /* initialize dispatch table */ initstypes(); /* find direct sources */ - for (i = 0; i < nobjects; i++) { + for (i = 0; i < nsceneobjs; i++) { o = objptr(i); if (!issurface(o->otype) || o->omod == OVOID) continue; - - m = objptr(o->omod); - - if (!islight(m->otype)) + /* find material */ + m = findmaterial(o); + if (m == NULL) continue; + if (m->otype == MAT_CLIP) { + markclip(m); /* special case for antimatter */ + 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 (m->otype == MAT_GLOW && o->otype != OBJ_SOURCE && - m->oargs.farg[3] <= FTINY) - continue; /* don't bother */ - if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY && - m->oargs.farg[2] <= FTINY) - continue; /* don't bother */ - + m->oargs.farg[3] <= FTINY) { + foundsource += (ambounce > 0); + continue; /* don't track these */ + } if (sfun[o->otype].of == NULL || sfun[o->otype].of->setsrc == NULL) objerror(o, USER, "illegal material"); @@ -92,9 +95,14 @@ marksources() /* find and mark source objects */ if (m->otype == MAT_GLOW) { source[ns].sflags |= SPROX; source[ns].sl.prox = m->oargs.farg[3]; - if (source[ns].sflags & SDISTANT) + if (source[ns].sflags & SDISTANT) { source[ns].sflags |= SSKIP; + 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; @@ -105,67 +113,149 @@ marksources() /* find and mark source objects */ source[ns].sflags |= SSKIP; } } - if (!(source[ns].sflags & SSKIP)) - foundsource++; + foundsource += !(source[ns].sflags & SSKIP); } if (!foundsource) { error(WARNING, "no light sources found"); return; } - markvirtuals(); /* find and add virtual sources */ +#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 */ + /* allocate our contribution arrays */ 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"); } -srcray(sr, r, si) /* send a ray to a source, return domega */ -register RAY *sr; /* returned source ray */ -RAY *r; /* ray which hit object */ -SRCINDEX *si; /* source sample index */ +void +distantsources(void) /* only mark distant sources */ { - double d; /* distance to source */ - register SRCREC *srcp; + int i; + OBJREC *o, *m; + int ns; + /* initialize dispatch table */ + initstypes(); + /* sources needed for sourcehit() */ + for (i = 0; i < nsceneobjs; i++) { + + o = objptr(i); - rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */ - - while ((d = nextssamp(sr, si)) != 0.0) { - sr->rsrc = si->sn; /* remember source */ - srcp = source + si->sn; - if (srcp->sflags & SDISTANT) { - if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) + if ((o->otype != OBJ_SOURCE) | (o->omod == OVOID)) continue; - return(1); /* sample OK */ + /* find material */ + m = findmaterial(o); + 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) { +#if SHADCACHE + while (nsources--) + freeobscache(&source[nsources]); +#endif + free((void *)source); + source = NULL; + nsources = 0; + } + markclip(NULL); + if (maxcntr <= 0) + return; + free((void *)srccnt); + srccnt = NULL; + free((void *)cntord); + cntord = NULL; + maxcntr = 0; +} + + +int +srcray( /* send a ray to a source, return domega */ + RAY *sr, /* returned source ray */ + RAY *r, /* ray which hit object */ + SRCINDEX *si /* source sample index */ +) +{ + double d; /* distance to source */ + SRCREC *srcp; + + rayorigin(sr, SHADOW, r, NULL); /* ignore limits */ + + if (r == NULL) + sr->rmax = 0.0; + + while ((d = nextssamp(sr, si)) != 0.0) { + sr->rsrc = si->sn; /* remember source */ + srcp = source + si->sn; + if (srcp->sflags & SDISTANT) { + if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) + continue; + return(1); /* sample OK */ + } /* local source */ /* check proximity */ - if (srcp->sflags & SPROX && d > srcp->sl.prox) - continue; - /* check angle */ - if (srcp->sflags & SSPOT) { - if (spotout(sr, srcp->sl.s)) + if (srcp->sflags & SPROX && d > srcp->sl.prox) continue; + /* check angle */ + if (srcp->sflags & SSPOT) { + if (spotout(sr, srcp->sl.s)) + continue; /* adjust solid angle */ - si->dom *= d*d; - d += srcp->sl.s->flen; - si->dom /= d*d; + si->dom *= d*d; + d += srcp->sl.s->flen; + si->dom /= d*d; + } + return(1); /* sample OK */ } - return(1); /* sample OK */ - } - return(0); /* no more samples */ + return(0); /* no more samples */ } -srcvalue(r) /* punch ray to source and compute value */ -register RAY *r; +void +srcvalue( /* punch ray to source and compute value */ + RAY *r +) { - register SRCREC *sp; + SRCREC *sp; sp = &source[r->rsrc]; if (sp->sflags & SVIRTUAL) { /* virtual source */ @@ -202,46 +292,93 @@ nomat: } -sourcehit(r) /* check to see if ray hit distant source */ -register RAY *r; +static int +transillum( /* check if material is transparent illum */ + OBJREC *m +) { + m = findmaterial(m); + if (m == NULL) + return(1); + if (m->otype != MAT_ILLUM) + return(0); + return(!m->oargs.nsargs || !strcmp(m->oargs.sarg[0], VOIDID)); +} + + +int +sourcehit( /* check to see if ray hit distant source */ + RAY *r +) +{ + int glowsrc = -1; + int transrc = -1; int first, last; - register int i; + int i; if (r->rsrc >= 0) { /* check only one if aimed */ first = last = r->rsrc; } else { /* otherwise check all */ first = 0; last = nsources-1; } - for (i = first; i <= last; i++) - if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT) - /* - * Check to see if ray is within - * solid angle of source. - */ - if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir)) - <= source[i].ss2) { - r->ro = source[i].so; - if (!(source[i].sflags & SSKIP)) - break; - } - - if (r->ro != NULL) { - r->robj = objndx(r->ro); - for (i = 0; i < 3; i++) - r->ron[i] = -r->rdir[i]; - r->rod = 1.0; - r->rox = NULL; - return(1); + for (i = first; i <= last; i++) { + if ((source[i].sflags & (SDISTANT|SVIRTUAL)) != SDISTANT) + continue; + /* + * Check to see if ray is within + * solid angle of source. + */ + if (2.*PI*(1. - DOT(source[i].sloc,r->rdir)) > source[i].ss2) + continue; + /* is it the only possibility? */ + if (first == last) { + r->ro = source[i].so; + break; + } + /* + * If it's a glow or transparent illum, just remember it. + */ + if (source[i].sflags & SSKIP) { + if (glowsrc < 0) + glowsrc = i; + continue; + } + if (transillum(source[i].so)) { + if (transrc < 0) + transrc = i; + continue; + } + r->ro = source[i].so; /* otherwise, use first hit */ + break; } - return(0); + /* + * Do we need fallback? + */ + if (r->ro == NULL) { + if (transrc >= 0 && r->crtype & (AMBIENT|SPECULAR)) + return(0); /* avoid overcounting */ + if (glowsrc >= 0) + r->ro = source[glowsrc].so; + else + return(0); /* nothing usable */ + } + /* + * Assign object index + */ + r->robj = objndx(r->ro); + return(1); } static int -cntcmp(sc1, sc2) /* contribution compare (descending) */ -register CNTPTR *sc1, *sc2; +cntcmp( /* contribution compare (descending) */ + const void *p1, + const void *p2 +) { + const CNTPTR *sc1 = (const CNTPTR *)p1; + const CNTPTR *sc2 = (const CNTPTR *)p2; + if (sc1->brt > sc2->brt) return(-1); if (sc1->brt < sc2->brt) @@ -250,19 +387,28 @@ register CNTPTR *sc1, *sc2; } -direct(r, f, p) /* add direct component */ -RAY *r; /* ray that hit surface */ -int (*f)(); /* direct component coefficient function */ -char *p; /* data for f */ +void +direct( /* add direct component */ + RAY *r, /* ray that hit surface */ + srcdirf_t *f, /* direct component coefficient function */ + void *p /* data for f */ +) { - extern int (*trace)(); - register int sn; - register CONTRIB *scp; + int sn; + CONTRIB *scp; SRCINDEX si; int nshadcheck, ncnts; int nhits; double prob, ourthresh, hwt; RAY sr; + + /* PMAP: Factor in direct photons (primarily for debugging/validation) */ + if (directPhotonMapping) { + (*f)(r -> rcol, p, r -> ron, PI); + multDirectPmap(r); + return; + } + /* NOTE: srccnt and cntord global so no recursion */ if (nsources <= 0) return; /* no sources?! */ @@ -271,33 +417,40 @@ char *p; /* data for f */ for (sn = 0; srcray(&sr, r, &si); sn++) { if (sn >= maxcntr) { maxcntr = sn + MAXSPART; - srccnt = (CONTRIB *)realloc((char *)srccnt, + srccnt = (CONTRIB *)realloc((void *)srccnt, maxcntr*sizeof(CONTRIB)); - cntord = (CNTPTR *)realloc((char *)cntord, + cntord = (CNTPTR *)realloc((void *)cntord, maxcntr*sizeof(CNTPTR)); - if (srccnt == NULL | cntord == NULL) + if ((srccnt == NULL) | (cntord == NULL)) error(SYSTEM, "out of memory in direct"); } cntord[sn].sndx = sn; scp = srccnt + sn; scp->sno = sr.rsrc; +#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 = bright(scp->coef); + 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 */ sr.revf = srcvalue; rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); copycolor(scp->val, sr.rcol); - multcolor(scp->val, scp->coef); - cntord[sn].brt = bright(scp->val); + cntord[sn].brt = bright(sr.rcol); } /* sort contributions */ qsort(cntord, sn, sizeof(CNTPTR), cntcmp); { /* find last */ - register int l, m; + int l, m; ncnts = l = sn; sn = 0; @@ -324,28 +477,47 @@ char *p; /* data for f */ (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 */ - rayorigin(&sr, r, SHADOW, 1.0); + rayorigin(&sr, SHADOW, r, NULL); + copycolor(sr.rcoef, scp->coef); VCOPY(sr.rdir, scp->dir); sr.rsrc = scp->sno; - source[scp->sno].ntests++; /* keep statistics */ + /* keep statistics */ + if (source[scp->sno].ntests++ > 0xfffffff0) { + source[scp->sno].ntests >>= 1; + source[scp->sno].nhits >>= 1; + } if (localhit(&sr, &thescene) && ( sr.ro != source[scp->sno].so || source[scp->sno].sflags & SFOLLOW )) { /* follow entire path */ raycont(&sr); - rayparticipate(&sr); if (trace != NULL) (*trace)(&sr); /* trace execution */ - if (bright(sr.rcol) <= FTINY) + if (bright(sr.rcol) <= FTINY) { +#if SHADCACHE + if ((scp <= srccnt || scp[-1].sno != scp->sno) + && (scp >= srccnt+ncnts-1 || + scp[1].sno != scp->sno)) + srcblocker(&sr); +#endif continue; /* missed! */ + } + rayparticipate(&sr); + multcolor(sr.rcol, sr.rcoef); copycolor(scp->val, sr.rcol); - multcolor(scp->val, scp->coef); + } else if (trace != NULL && + (source[scp->sno].sflags & (SDISTANT|SVIRTUAL|SFOLLOW)) + == (SDISTANT|SFOLLOW) && + sourcehit(&sr) && rayshade(&sr, sr.ro->omod)) { + (*trace)(&sr); /* trace execution */ + /* skip call to rayparticipate() & scp->val update */ } /* add contribution if hit */ addcolor(r->rcol, scp->val); @@ -367,16 +539,17 @@ char *p; /* data for f */ scp = srccnt + cntord[sn].sndx; prob = hwt * (double)source[scp->sno].nhits / (double)source[scp->sno].ntests; - if (prob > 1.0) - prob = 1.0; - scalecolor(scp->val, prob); + if (prob < 1.0) + scalecolor(scp->val, prob); addcolor(r->rcol, scp->val); } } -srcscatter(r) /* compute source scattering into ray */ -register RAY *r; +void +srcscatter( /* compute source scattering into ray */ + RAY *r +) { int oldsampndx; int nsamps; @@ -387,9 +560,12 @@ register RAY *r; COLOR cvext; int i, j; - 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) nsamps = 1; #if MAXSSAMP @@ -398,7 +574,8 @@ register RAY *r; #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; @@ -414,32 +591,53 @@ register RAY *r; 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; - sr.rmax = 0.; - 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 */ - 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) - 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)); - } + + 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; +#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) { +#if SHADCACHE + 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)); + } /* other factors */ - d *= si.dom * r->rot / (4.*PI*nsamps); + 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); - scalecolor(sr.rcol, d); multcolor(sr.rcol, cvext); addcolor(r->rcol, sr.rcol); /* add it in */ } @@ -464,13 +662,17 @@ register RAY *r; * geometry behind (or inside) an effective radiator. */ -static int weaksrcmod(obj) int obj; /* efficiency booster function */ -{register OBJREC *o = objptr(obj); -return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} +static int +weaksrcmat(OBJREC *m) /* identify material */ +{ + m = findmaterial(m); + if (m == NULL) return(0); + return((m->otype==MAT_ILLUM) | (m->otype==MAT_GLOW)); +} #define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ r->rod > 0.0 && \ - weaksrcmod(source[r->rsrc].so->omod)) + weaksrcmat(source[r->rsrc].so)) /* wrongsource * * @@ -501,10 +703,14 @@ return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} * 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 * * @@ -526,28 +732,36 @@ return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} distglow(m, r, raydist(r,PRIMARY))) -m_light(m, r) /* ray hit a light source */ -register OBJREC *m; -register RAY *r; +int +m_light( /* ray hit a light source */ + OBJREC *m, + RAY *r +) { /* check for over-counting */ - if (badcomponent(m, r)) + if (badcomponent(m, r)) { + setcolor(r->rcoef, 0.0, 0.0, 0.0); return(1); - if (wrongsource(m, r)) + } + if (wrongsource(m, r)) { + setcolor(r->rcoef, 0.0, 0.0, 0.0); return(1); + } /* check for passed illum */ if (passillum(m, r)) { if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) - return(rayshade(r, modifier(m->oargs.sarg[0]))); + return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0]))); raytrans(r); return(1); } + /* check for invisibility */ + if (srcignore(m, r)) { + setcolor(r->rcoef, 0.0, 0.0, 0.0); + return(1); + } /* otherwise treat as source */ /* check for behind */ if (r->rod < 0.0) - return(1); - /* check for invisibility */ - if (srcignore(m, r)) return(1); /* check for outside spot */ if (m->otype==MAT_SPOT && spotout(r, makespot(m)))