--- ray/src/rt/source.c 2006/07/12 05:47:05 2.54 +++ 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.54 2006/07/12 05:47:05 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.54 2006 #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 */ @@ -86,21 +86,28 @@ marksources(void) /* find and mark source objects */ continue; /* find material */ m = findmaterial(objptr(o->omod)); - if (m == NULL || !islight(m->otype)) + 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->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"); @@ -113,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) @@ -129,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)); @@ -149,7 +161,7 @@ memerr: } -extern void +void freesources(void) /* free all source structures */ { if (nsources > 0) { @@ -161,6 +173,7 @@ freesources(void) /* free all source structures */ source = NULL; nsources = 0; } + markclip(NULL); if (maxcntr <= 0) return; free((void *)srccnt); @@ -171,51 +184,54 @@ 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; + double d; /* distance to source */ + SRCREC *srcp; - rayorigin(sr, SHADOW, r, NULL); /* ignore limits */ + rayorigin(sr, SHADOW, r, NULL); /* 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)) - continue; - return(1); /* sample OK */ - } + 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 */ } -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 */ @@ -267,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; @@ -300,11 +316,13 @@ sourcehit( /* check to see if ray hit distant source * If it's a glow or transparent illum, just remember it. */ if (source[i].sflags & SSKIP) { - glowsrc = i; + if (glowsrc < 0) + glowsrc = i; continue; } if (transillum(source[i].so->omod)) { - transrc = i; + if (transrc < 0) + transrc = i; continue; } r->ro = source[i].so; /* otherwise, use first hit */ @@ -341,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); @@ -352,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?! */ @@ -403,12 +429,12 @@ direct( /* add direct component */ rayvalue(&sr); multcolor(sr.rcol, sr.rcoef); copycolor(scp->val, sr.rcol); - cntord[sn].brt = intens(sr.rcol); + 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; @@ -428,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 / @@ -457,7 +486,6 @@ direct( /* add direct component */ raycont(&sr); if (trace != NULL) (*trace)(&sr); /* trace execution */ - rayparticipate(&sr); if (bright(sr.rcol) <= FTINY) { #if SHADCACHE if ((scp <= srccnt || scp[-1].sno != scp->sno) @@ -467,6 +495,7 @@ direct( /* add direct component */ #endif continue; /* missed! */ } + rayparticipate(&sr); multcolor(sr.rcol, sr.rcoef); copycolor(scp->val, sr.rcol); } else if (trace != NULL && @@ -503,9 +532,9 @@ direct( /* add direct component */ } -extern void +void srcscatter( /* compute source scattering into ray */ - register RAY *r + RAY *r ) { int oldsampndx; @@ -514,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 @@ -544,7 +576,6 @@ 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; - sr.rmax = 0.; initsrcindex(&si); /* sample ray to this source */ si.sn = r->slights[i]; nopart(&si, &sr); @@ -575,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 */ } @@ -669,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 */