--- ray/src/rt/source.c 2018/06/11 15:10:49 2.68 +++ ray/src/rt/source.c 2020/12/17 03:30:37 2.74 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.68 2018/06/11 15:10:49 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.74 2020/12/17 03:30:37 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -9,6 +9,7 @@ static const char RCSid[] = "$Id: source.c,v 2.68 2018 #include "ray.h" #include "otypes.h" +#include "otspecial.h" #include "rtotypes.h" #include "source.h" #include "random.h" @@ -42,32 +43,6 @@ static int maxcntr = 0; /* size of contribution arra 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 */ { @@ -99,8 +74,8 @@ 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 && @@ -125,6 +100,9 @@ marksources(void) /* find and mark source objects */ 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; @@ -153,15 +131,61 @@ marksources(void) /* find and mark source objects */ 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"); } void +distantsources(void) /* only mark distant sources */ +{ + int i; + OBJREC *o, *m; + int ns; + /* 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(objptr(o->omod)); + 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) { @@ -410,18 +434,17 @@ direct( /* add direct component */ cntord[sn].sndx = sn; scp = srccnt + sn; scp->sno = sr.rsrc; - /* compute coefficient */ - (*f)(scp->coef, p, sr.rdir, si.dom); - cntord[sn].brt = intens(scp->coef); - if (cntord[sn].brt <= 0.0) - continue; -#if SHADCACHE - /* check shadow cache */ +#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 = intens(scp->coef); + if (cntord[sn].brt <= 0.0) + continue; VCOPY(scp->dir, sr.rdir); copycolor(sr.rcoef, scp->coef); /* compute potential */ @@ -454,19 +477,17 @@ direct( /* add direct component */ /* compute number to check */ nshadcheck = pow((double)ncnts, shadcert) + .5; /* modify threshold */ - if (ncnts > MINSHADCNT) - ourthresh = shadthresh / r->rweight; - else - ourthresh = 0; + ourthresh = shadthresh / r->rweight; /* test for shadows */ for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts; hwt += (double)source[scp->sno].nhits / (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 */ @@ -546,7 +567,7 @@ srcscatter( /* compute source scattering into ray */ COLOR cvext; int i, j; - if (r->rot >= FHUGE || r->gecc >= 1.-FTINY) + 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))