--- ray/src/rt/source.c 2014/05/07 02:08:12 2.62 +++ ray/src/rt/source.c 2024/12/25 17:40:27 2.85 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.62 2014/05/07 02:08:12 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.85 2024/12/25 17:40:27 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -9,9 +9,12 @@ static const char RCSid[] = "$Id: source.c,v 2.62 2014 #include "ray.h" #include "otypes.h" +#include "otspecial.h" #include "rtotypes.h" #include "source.h" #include "random.h" +#include "pmapsrc.h" +#include "pmapmat.h" #ifndef MAXSSAMP #define MAXSSAMP 16 /* maximum samples per ray */ @@ -24,8 +27,8 @@ static const char RCSid[] = "$Id: source.c,v 2.62 2014 typedef struct { int sno; /* source number */ FVECT dir; /* source direction */ - COLOR coef; /* material coefficient */ - COLOR val; /* contribution */ + SCOLOR coef; /* material coefficient */ + SCOLOR val; /* contribution */ } CONTRIB; /* direct contribution */ typedef struct { @@ -33,46 +36,23 @@ typedef struct { float brt; /* brightness (for comparison) */ } CNTPTR; /* contribution pointer */ -static CONTRIB *srccnt; /* source contributions in direct() */ -static CNTPTR *cntord; /* source ordering in direct() */ +static CONTRIB *srccnt = NULL; /* source contributions in direct() */ +static CNTPTR *cntord = NULL; /* source ordering in direct() */ static int maxcntr = 0; /* size of contribution arrays */ 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 */ { - int foundsource = 0; + int indirect = 0; int i; OBJREC *o, *m; int ns; + /* call us only once! */ + if (nsources) + error(CONSISTENCY, "Multiple calls to marksources!"); /* initialize dispatch table */ initstypes(); /* find direct sources */ @@ -83,7 +63,7 @@ marksources(void) /* find and mark source objects */ if (!issurface(o->otype) || o->omod == OVOID) continue; /* find material */ - m = findmaterial(objptr(o->omod)); + m = findmaterial(o); if (m == NULL) continue; if (m->otype == MAT_CLIP) { @@ -97,13 +77,13 @@ 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 && m->oargs.farg[3] <= FTINY) { - foundsource += (ambounce > 0); + indirect += (ambounce > 0); continue; /* don't track these */ } if (sfun[o->otype].of == NULL || @@ -120,9 +100,12 @@ marksources(void) /* find and mark source objects */ source[ns].sl.prox = m->oargs.farg[3]; if (source[ns].sflags & SDISTANT) { source[ns].sflags |= SSKIP; - foundsource += (ambounce > 0); + indirect += (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; @@ -133,29 +116,83 @@ marksources(void) /* find and mark source objects */ source[ns].sflags |= SSKIP; } } + maxcntr += !(source[ns].sflags & SSKIP); + } + if (!maxcntr) { + if (!indirect) + error(WARNING, "no light sources found"); + return; /* no direct calculation, it seems */ + } #if SHADCACHE + for (ns = 0; ns < nsources; ns++) /* initialize obstructor cache */ initobscache(ns); #endif - foundsource += !(source[ns].sflags & SSKIP); - } - if (!foundsource) { - error(WARNING, "no light sources found"); - return; - } - markvirtuals(); /* find and add virtual sources */ + /* PMAP: disable virtual sources */ + if (!photonMapping) + markvirtuals(); /* find and add virtual sources */ + /* allocate our contribution arrays */ - maxcntr = nsources + MAXSPART; /* start with this many */ + maxcntr += 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; + /* call us only once! */ + if (nsources) + error(CONSISTENCY, "Multiple calls to distantsources!"); + /* 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(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) { @@ -163,25 +200,28 @@ freesources(void) /* free all source structures */ while (nsources--) freeobscache(&source[nsources]); #endif - free((void *)source); +#ifdef SSKIPOPT + sskip_rsi(NULL); +#endif + free(source); source = NULL; nsources = 0; } markclip(NULL); if (maxcntr <= 0) return; - free((void *)srccnt); + free(srccnt); srccnt = NULL; - free((void *)cntord); + free(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 */ +srcray( /* aim a ray at a source, return domega */ + RAY *sr, /* prepared source ray */ + RAY *r, /* originating ray (or NULL) */ SRCINDEX *si /* source sample index */ ) { @@ -194,7 +234,7 @@ srcray( /* send a ray to a source, return domega */ sr->rmax = 0.0; while ((d = nextssamp(sr, si)) != 0.0) { - sr->rsrc = si->sn; /* remember source */ + sr->rsrc = si->sn; /* remember source */ srcp = source + si->sn; if (srcp->sflags & SDISTANT) { if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) @@ -264,11 +304,10 @@ nomat: static int transillum( /* check if material is transparent illum */ - OBJECT obj + OBJREC *m ) { - OBJREC *m = findmaterial(objptr(obj)); - + m = findmaterial(m); if (m == NULL) return(1); if (m->otype != MAT_ILLUM) @@ -301,8 +340,8 @@ sourcehit( /* check to see if ray hit distant source */ if (2.*PI*(1. - DOT(source[i].sloc,r->rdir)) > source[i].ss2) continue; - /* is it the only possibility? */ - if (first == last) { + /* is it what we aimed for? */ + if (i == r->rsrc) { r->ro = source[i].so; break; } @@ -314,7 +353,7 @@ sourcehit( /* check to see if ray hit distant source glowsrc = i; continue; } - if (transillum(source[i].so->omod)) { + if (transillum(source[i].so)) { if (transrc < 0) transrc = i; continue; @@ -334,15 +373,9 @@ sourcehit( /* check to see if ray hit distant source return(0); /* nothing usable */ } /* - * Make assignments. + * Assign object index */ r->robj = objndx(r->ro); - for (i = 0; i < 3; i++) - r->ron[i] = -r->rdir[i]; - r->rod = 1.0; - r->pert[0] = r->pert[1] = r->pert[2] = 0.0; - r->uv[0] = r->uv[1] = 0.0; - r->rox = NULL; return(1); } @@ -378,11 +411,18 @@ direct( /* add direct component */ 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?! */ - /* potential contributions */ - initsrcindex(&si); + if (maxcntr <= 0) + return; /* no direct?! */ + + initsrcindex(&si); /* potential contributions */ for (sn = 0; srcray(&sr, r, &si); sn++) { if (sn >= maxcntr) { maxcntr = sn + MAXSPART; @@ -396,26 +436,33 @@ direct( /* add direct component */ 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; /* & count as test */ + if (source[scp->sno].ntests++ > 0xfffffff0) { + source[scp->sno].ntests >>= 1; + source[scp->sno].nhits >>= 1; + } + continue; + } +#endif /* compute coefficient */ (*f)(scp->coef, p, sr.rdir, si.dom); - cntord[sn].brt = intens(scp->coef); + cntord[sn].brt = sintens(scp->coef); if (cntord[sn].brt <= 0.0) continue; -#if SHADCACHE - /* check shadow cache */ - if (si.np == 1 && srcblocked(&sr)) { - cntord[sn].brt = 0.0; - continue; - } +#ifdef SSKIPOPT + if (ssf_select != NULL && sskip_chk(ssf_select, scp->sno)) + scalescolor(scp->coef, r->scorr); #endif VCOPY(scp->dir, sr.rdir); - copycolor(sr.rcoef, scp->coef); + copyscolor(sr.rcoef, scp->coef); /* compute potential */ sr.revf = srcvalue; rayvalue(&sr); - multcolor(sr.rcol, sr.rcoef); - copycolor(scp->val, sr.rcol); - cntord[sn].brt = bright(sr.rcol); + smultscolor(sr.rcol, sr.rcoef); + copyscolor(scp->val, sr.rcol); + cntord[sn].brt = pbright(sr.rcol); } /* sort contributions */ qsort(cntord, sn, sizeof(CNTPTR), cntcmp); @@ -447,14 +494,15 @@ direct( /* add direct component */ (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*pbright(r->rcol)) break; scp = srccnt + cntord[sn].sndx; /* test for hit */ - rayorigin(&sr, SHADOW, r, NULL); - copycolor(sr.rcoef, scp->coef); + rayorigin(&sr, thrudir(r,scp->dir) ? TSHADOW : RSHADOW, r, NULL); + copyscolor(sr.rcoef, scp->coef); VCOPY(sr.rdir, scp->dir); sr.rsrc = scp->sno; /* keep statistics */ @@ -469,7 +517,7 @@ direct( /* add direct component */ raycont(&sr); if (trace != NULL) (*trace)(&sr); /* trace execution */ - if (bright(sr.rcol) <= FTINY) { + if (scolor_mean(sr.rcol) <= FTINY) { #if SHADCACHE if ((scp <= srccnt || scp[-1].sno != scp->sno) && (scp >= srccnt+ncnts-1 || @@ -479,8 +527,8 @@ direct( /* add direct component */ continue; /* missed! */ } rayparticipate(&sr); - multcolor(sr.rcol, sr.rcoef); - copycolor(scp->val, sr.rcol); + smultscolor(sr.rcol, sr.rcoef); + copyscolor(scp->val, sr.rcol); } else if (trace != NULL && (source[scp->sno].sflags & (SDISTANT|SVIRTUAL|SFOLLOW)) == (SDISTANT|SFOLLOW) && @@ -489,7 +537,7 @@ direct( /* add direct component */ /* skip call to rayparticipate() & scp->val update */ } /* add contribution if hit */ - addcolor(r->rcol, scp->val); + saddscolor(r->rcol, scp->val); nhits++; source[scp->sno].nhits++; } @@ -509,8 +557,8 @@ direct( /* add direct component */ prob = hwt * (double)source[scp->sno].nhits / (double)source[scp->sno].ntests; if (prob < 1.0) - scalecolor(scp->val, prob); - addcolor(r->rcol, scp->val); + scalescolor(scp->val, prob); + saddscolor(r->rcol, scp->val); } } @@ -529,9 +577,12 @@ srcscatter( /* compute source scattering into ray */ 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 @@ -540,7 +591,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; @@ -551,46 +603,67 @@ srcscatter( /* compute source scattering into ray */ setcolor(cvext, re > 92. ? 0. : exp(-re), ge > 92. ? 0. : exp(-ge), be > 92. ? 0. : exp(-be)); - if (intens(cvext) <= FTINY) + if (intens(cvext) <= FTINY*FTINY) break; /* too far away */ 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) { + sr.parent = r; /* hack for preemptive test */ + if (srcskip(r->slights[i], &sr)) + continue; + 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 (pbright(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); - 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 */ + d *= si.dom * r->rot / (4.*PI*nsamps); +#ifdef SSKIPOPT + if (ssf_select != NULL && sskip_chk(ssf_select, sr.rsrc)) + d *= r->scorr; +#endif + scalescolor(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); + scalescolor(sr.rcol, r -> rot / nsamps); + } + smultcolor(sr.rcol, r->cext); + smultcolor(sr.rcol, r->albedo); + smultcolor(sr.rcol, cvext); + saddscolor(r->rcol, sr.rcol); /* add it in */ } } samplendx = oldsampndx; @@ -614,17 +687,16 @@ srcscatter( /* compute source scattering into ray */ */ static int -weaksrcmat(OBJECT obj) /* identify material */ +weaksrcmat(OBJREC *m) /* identify material */ { - OBJREC *m = findmaterial(objptr(obj)); - + 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 && \ - weaksrcmat(source[r->rsrc].so->omod)) + weaksrcmat(source[r->rsrc].so)) /* wrongsource * * @@ -655,10 +727,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 * * @@ -688,11 +764,11 @@ m_light( /* ray hit a light source */ { /* check for over-counting */ if (badcomponent(m, r)) { - setcolor(r->rcoef, 0.0, 0.0, 0.0); + scolorblack(r->rcoef); return(1); } if (wrongsource(m, r)) { - setcolor(r->rcoef, 0.0, 0.0, 0.0); + scolorblack(r->rcoef); return(1); } /* check for passed illum */ @@ -704,23 +780,25 @@ m_light( /* ray hit a light source */ } /* check for invisibility */ if (srcignore(m, r)) { - setcolor(r->rcoef, 0.0, 0.0, 0.0); + scolorblack(r->rcoef); return(1); } /* otherwise treat as source */ - /* check for behind */ - if (r->rod < 0.0) + if (r->rod < 0.0) { /* check for behind */ + if (!backvis) + raytrans(r); /* used to return black */ return(1); + } /* check for outside spot */ if (m->otype==MAT_SPOT && spotout(r, makespot(m))) return(1); /* get distribution pattern */ raytexture(r, m->omod); /* get source color */ - setcolor(r->rcol, m->oargs.farg[0], + setscolor(r->rcol, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]); /* modify value */ - multcolor(r->rcol, r->pcol); + smultscolor(r->rcol, r->pcol); return(1); }