--- ray/src/rt/source.c 2011/02/14 20:13:38 2.60 +++ ray/src/rt/source.c 2015/02/24 19:39:27 2.64 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.60 2011/02/14 20:13:38 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.64 2015/02/24 19:39:27 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -12,9 +12,9 @@ static const char RCSid[] = "$Id: source.c,v 2.60 2011 #include "rtotypes.h" #include "source.h" #include "random.h" +#include "pmap.h" +#include "pmapsrc.h" -extern double ssampdist; /* scatter sampling distance */ - #ifndef MAXSSAMP #define MAXSSAMP 16 /* maximum samples per ray */ #endif @@ -42,8 +42,8 @@ static int maxcntr = 0; /* size of contribution arra static int cntcmp(const void *p1, const void *p2); -extern OBJREC * /* find an object's actual material */ -findmaterial(register OBJREC *o) +OBJREC * /* find an object's actual material */ +findmaterial(OBJREC *o) { while (!ismaterial(o->otype)) { if (o->otype == MOD_ALIAS && o->oargs.nsargs) { @@ -68,13 +68,13 @@ findmaterial(register OBJREC *o) } -extern void +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 */ @@ -99,14 +99,15 @@ marksources(void) /* find and mark source objects */ m->otype == MAT_SPOT ? 7 : 3)) objerror(m, USER, "bad # arguments"); - 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 */ - + if (m->otype == MAT_GLOW && + o->otype != OBJ_SOURCE && + 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"); @@ -119,8 +120,10 @@ marksources(void) /* 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) { source[ns].sflags |= SSPOT; if ((source[ns].sl.s = makespot(m)) == NULL) @@ -135,14 +138,17 @@ marksources(void) /* find and mark source objects */ #if SHADCACHE initobscache(ns); #endif - 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 */ + + /* 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)); @@ -155,7 +161,7 @@ memerr: } -extern void +void freesources(void) /* free all source structures */ { if (nsources > 0) { @@ -178,15 +184,15 @@ freesources(void) /* free all source structures */ } -extern int +int srcray( /* send a ray to a source, return domega */ - register RAY *sr, /* returned source ray */ + RAY *sr, /* returned source ray */ RAY *r, /* ray which hit object */ SRCINDEX *si /* source sample index */ ) { double d; /* distance to source */ - register SRCREC *srcp; + SRCREC *srcp; rayorigin(sr, SHADOW, r, NULL); /* ignore limits */ @@ -220,12 +226,12 @@ srcray( /* send a ray to a source, return domega */ } -extern void +void srcvalue( /* punch ray to source and compute value */ - register RAY *r + RAY *r ) { - register SRCREC *sp; + SRCREC *sp; sp = &source[r->rsrc]; if (sp->sflags & SVIRTUAL) { /* virtual source */ @@ -277,15 +283,15 @@ transillum( /* check if material is transparent illu } -extern int +int sourcehit( /* check to see if ray hit distant source */ - register RAY *r + 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; @@ -353,8 +359,8 @@ cntcmp( /* contribution compare (descending) */ const void *p2 ) { - register const CNTPTR *sc1 = (const CNTPTR *)p1; - register const CNTPTR *sc2 = (const CNTPTR *)p2; + const CNTPTR *sc1 = (const CNTPTR *)p1; + const CNTPTR *sc2 = (const CNTPTR *)p2; if (sc1->brt > sc2->brt) return(-1); @@ -364,20 +370,28 @@ cntcmp( /* contribution compare (descending) */ } -extern void +void direct( /* add direct component */ RAY *r, /* ray that hit surface */ srcdirf_t *f, /* direct component coefficient function */ void *p /* data for f */ ) { - 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?! */ @@ -420,7 +434,7 @@ direct( /* add direct component */ /* sort contributions */ qsort(cntord, sn, sizeof(CNTPTR), cntcmp); { /* find last */ - register int l, m; + int l, m; ncnts = l = sn; sn = 0; @@ -440,7 +454,10 @@ direct( /* add direct component */ /* compute number to check */ nshadcheck = pow((double)ncnts, shadcert) + .5; /* modify threshold */ - ourthresh = shadthresh / r->rweight; + if (ncnts > MINSHADCNT) + ourthresh = shadthresh / r->rweight; + else + ourthresh = 0; /* test for shadows */ for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts; hwt += (double)source[scp->sno].nhits / @@ -515,9 +532,9 @@ direct( /* add direct component */ } -extern void +void srcscatter( /* compute source scattering into ray */ - register RAY *r + RAY *r ) { int oldsampndx; @@ -526,12 +543,15 @@ srcscatter( /* compute source scattering into ray */ SRCINDEX si; double t, d; double re, ge, be; - COLOR cvext; + COLOR cvext, pmapInscatter; 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) return; + if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) nsamps = 1; #if MAXSSAMP @@ -586,9 +606,17 @@ srcscatter( /* compute source scattering into ray */ } /* 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); + } + 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 */ } @@ -680,10 +708,10 @@ weaksrcmat(OBJECT obj) /* identify material */ distglow(m, r, raydist(r,PRIMARY))) -extern int +int m_light( /* ray hit a light source */ - register OBJREC *m, - register RAY *r + OBJREC *m, + RAY *r ) { /* check for over-counting */