--- ray/src/rt/source.c 2005/10/28 16:16:33 2.53 +++ ray/src/rt/source.c 2014/09/15 00:54:39 2.63 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.53 2005/10/28 16:16:33 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.63 2014/09/15 00:54:39 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -13,8 +13,6 @@ static const char RCSid[] = "$Id: source.c,v 2.53 2005 #include "source.h" #include "random.h" -extern double ssampdist; /* scatter sampling distance */ - #ifndef MAXSSAMP #define MAXSSAMP 16 /* maximum samples per ray */ #endif @@ -42,8 +40,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) { @@ -55,6 +53,10 @@ findmaterial(register OBJREC *o) ao = objptr(aobj); if (ismaterial(ao->otype)) return(ao); + if (ao->otype == MOD_ALIAS) { + o = ao; + continue; + } } if (o->omod == OVOID) return(NULL); @@ -64,13 +66,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 */ @@ -82,21 +84,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"); @@ -109,8 +118,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) @@ -125,8 +136,7 @@ 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"); @@ -145,7 +155,7 @@ memerr: } -extern void +void freesources(void) /* free all source structures */ { if (nsources > 0) { @@ -157,6 +167,7 @@ freesources(void) /* free all source structures */ source = NULL; nsources = 0; } + markclip(NULL); if (maxcntr <= 0) return; free((void *)srccnt); @@ -167,51 +178,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 */ @@ -263,15 +277,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; @@ -296,11 +310,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 */ @@ -337,8 +353,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); @@ -348,15 +364,15 @@ 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; @@ -399,12 +415,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; @@ -424,7 +440,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 / @@ -453,7 +472,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) @@ -463,6 +481,7 @@ direct( /* add direct component */ #endif continue; /* missed! */ } + rayparticipate(&sr); multcolor(sr.rcol, sr.rcoef); copycolor(scp->val, sr.rcol); } else if (trace != NULL && @@ -499,9 +518,9 @@ direct( /* add direct component */ } -extern void +void srcscatter( /* compute source scattering into ray */ - register RAY *r + RAY *r ) { int oldsampndx; @@ -540,7 +559,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); @@ -665,10 +683,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 */