--- ray/src/rt/source.c 2018/11/07 18:22:29 2.69 +++ ray/src/rt/source.c 2021/02/01 16:19:49 2.76 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.69 2018/11/07 18:22:29 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.76 2021/02/01 16:19:49 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -9,6 +9,7 @@ static const char RCSid[] = "$Id: source.c,v 2.69 2018 #include "ray.h" #include "otypes.h" +#include "otspecial.h" #include "rtotypes.h" #include "source.h" #include "random.h" @@ -42,28 +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"); - /* recursive check on alias branch */ - if ((ao = findmaterial(objptr(aobj))) != NULL) - return(ao); - } - if (o->omod == OVOID) - return(NULL); - o = objptr(o->omod); - } - return(o); /* mixtures will return NULL */ -} - - void marksources(void) /* find and mark source objects */ { @@ -81,7 +60,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) { @@ -95,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 && @@ -121,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; @@ -149,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(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) { @@ -266,11 +294,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) @@ -316,7 +343,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; @@ -336,15 +363,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); } @@ -406,18 +427,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 */ @@ -450,19 +470,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 */ @@ -542,7 +560,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)) @@ -645,17 +663,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 * *