--- ray/src/rt/source.c 2021/11/24 19:08:51 2.77 +++ ray/src/rt/source.c 2024/04/05 01:10:26 2.82 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.77 2021/11/24 19:08:51 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.82 2024/04/05 01:10:26 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -27,8 +27,8 @@ static const char RCSid[] = "$Id: source.c,v 2.77 2021 typedef struct { int sno; /* source number */ FVECT dir; /* source direction */ - COLOR coef; /* material coefficient */ - COLOR val; /* contribution */ + SCOLOR coef; /* material coefficient */ + SCOLOR val; /* contribution */ } CONTRIB; /* direct contribution */ typedef struct { @@ -36,8 +36,8 @@ typedef struct { float brt; /* brightness (for comparison) */ } CNTPTR; /* contribution pointer */ -static CONTRIB *srccnt; /* source contributions in direct() */ -static CNTPTR *cntord; /* source ordering in direct() */ +static CONTRIB *srccnt = NULL; /* source contributions in direct() */ +static CNTPTR *cntord = NULL; /* source ordering in direct() */ static int maxcntr = 0; /* size of contribution arrays */ static int cntcmp(const void *p1, const void *p2); @@ -46,10 +46,13 @@ static int cntcmp(const void *p1, const void *p2); void marksources(void) /* find and mark source objects */ { - int foundsource = 0; + int indirect = 0; int i; OBJREC *o, *m; int ns; + /* call us only once! */ + if (nsources) + error(CONSISTENCY, "Multiple calls to marksources!"); /* initialize dispatch table */ initstypes(); /* find direct sources */ @@ -80,7 +83,7 @@ marksources(void) /* find and mark source objects */ if (m->otype == MAT_GLOW && o->otype != OBJ_SOURCE && m->oargs.farg[3] <= FTINY) { - foundsource += (ambounce > 0); + indirect += (ambounce > 0); continue; /* don't track these */ } if (sfun[o->otype].of == NULL || @@ -97,7 +100,7 @@ marksources(void) /* find and mark source objects */ source[ns].sl.prox = m->oargs.farg[3]; if (source[ns].sflags & SDISTANT) { source[ns].sflags |= SSKIP; - foundsource += (ambounce > 0); + indirect += (ambounce > 0); } } else if (m->otype == MAT_SPOT) { if (source[ns].sflags & SDISTANT) @@ -113,11 +116,12 @@ marksources(void) /* find and mark source objects */ source[ns].sflags |= SSKIP; } } - foundsource += !(source[ns].sflags & SSKIP); + maxcntr += !(source[ns].sflags & SSKIP); } - if (!foundsource) { - error(WARNING, "no light sources found"); - return; + if (!maxcntr) { + if (!indirect) + error(WARNING, "no light sources found"); + return; /* no direct calculation, it seems */ } #if SHADCACHE for (ns = 0; ns < nsources; ns++) /* initialize obstructor cache */ @@ -128,7 +132,7 @@ marksources(void) /* find and mark source objects */ markvirtuals(); /* find and add virtual sources */ /* allocate our contribution arrays */ - maxcntr = nsources + MAXSPART; /* start with this many */ + maxcntr += MAXSPART; /* start with this many */ srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB)); cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR)); if ((srccnt != NULL) & (cntord != NULL)) @@ -144,6 +148,9 @@ distantsources(void) /* only mark distant sources */ int i; OBJREC *o, *m; int ns; + /* call us only once! */ + if (nsources) + error(CONSISTENCY, "Multiple calls to distantsources!"); /* initialize dispatch table */ initstypes(); /* sources needed for sourcehit() */ @@ -193,16 +200,16 @@ freesources(void) /* free all source structures */ while (nsources--) freeobscache(&source[nsources]); #endif - free((void *)source); + free(source); source = NULL; nsources = 0; } markclip(NULL); if (maxcntr <= 0) return; - free((void *)srccnt); + free(srccnt); srccnt = NULL; - free((void *)cntord); + free(cntord); cntord = NULL; maxcntr = 0; } @@ -408,12 +415,11 @@ direct( /* add direct component */ multDirectPmap(r); return; } - /* NOTE: srccnt and cntord global so no recursion */ - if (nsources <= 0) - return; /* no sources?! */ - /* potential contributions */ - initsrcindex(&si); + if (maxcntr <= 0) + return; /* no direct?! */ + + initsrcindex(&si); /* potential contributions */ for (sn = 0; srcray(&sr, r, &si); sn++) { if (sn >= maxcntr) { maxcntr = sn + MAXSPART; @@ -429,23 +435,27 @@ direct( /* add direct component */ scp->sno = sr.rsrc; #if SHADCACHE /* check shadow cache */ if (si.np == 1 && srcblocked(&sr)) { - cntord[sn].brt = 0.0; + cntord[sn].brt = 0.0; /* & count as test */ + if (source[scp->sno].ntests++ > 0xfffffff0) { + source[scp->sno].ntests >>= 1; + source[scp->sno].nhits >>= 1; + } continue; } #endif /* compute coefficient */ (*f)(scp->coef, p, sr.rdir, si.dom); - cntord[sn].brt = intens(scp->coef); + cntord[sn].brt = sintens(scp->coef); if (cntord[sn].brt <= 0.0) continue; VCOPY(scp->dir, sr.rdir); - copycolor(sr.rcoef, scp->coef); + copyscolor(sr.rcoef, scp->coef); /* compute potential */ sr.revf = srcvalue; rayvalue(&sr); - multcolor(sr.rcol, sr.rcoef); - copycolor(scp->val, sr.rcol); - cntord[sn].brt = bright(sr.rcol); + smultscolor(sr.rcol, sr.rcoef); + copyscolor(scp->val, sr.rcol); + cntord[sn].brt = pbright(sr.rcol); } /* sort contributions */ qsort(cntord, sn, sizeof(CNTPTR), cntcmp); @@ -480,12 +490,12 @@ direct( /* add direct component */ if (sn >= MINSHADCNT && (sn+nshadcheck>=ncnts ? cntord[sn].brt : cntord[sn].brt-cntord[sn+nshadcheck].brt) - < ourthresh*bright(r->rcol)) + < ourthresh*pbright(r->rcol)) break; scp = srccnt + cntord[sn].sndx; /* test for hit */ - rayorigin(&sr, SHADOW, r, NULL); - copycolor(sr.rcoef, scp->coef); + rayorigin(&sr, thrudir(r,scp->dir) ? TSHADOW : RSHADOW, r, NULL); + copyscolor(sr.rcoef, scp->coef); VCOPY(sr.rdir, scp->dir); sr.rsrc = scp->sno; /* keep statistics */ @@ -500,7 +510,7 @@ direct( /* add direct component */ raycont(&sr); if (trace != NULL) (*trace)(&sr); /* trace execution */ - if (bright(sr.rcol) <= FTINY) { + if (scolor_mean(sr.rcol) <= FTINY) { #if SHADCACHE if ((scp <= srccnt || scp[-1].sno != scp->sno) && (scp >= srccnt+ncnts-1 || @@ -510,8 +520,8 @@ direct( /* add direct component */ continue; /* missed! */ } rayparticipate(&sr); - multcolor(sr.rcol, sr.rcoef); - copycolor(scp->val, sr.rcol); + smultscolor(sr.rcol, sr.rcoef); + copyscolor(scp->val, sr.rcol); } else if (trace != NULL && (source[scp->sno].sflags & (SDISTANT|SVIRTUAL|SFOLLOW)) == (SDISTANT|SFOLLOW) && @@ -520,7 +530,7 @@ direct( /* add direct component */ /* skip call to rayparticipate() & scp->val update */ } /* add contribution if hit */ - addcolor(r->rcol, scp->val); + saddscolor(r->rcol, scp->val); nhits++; source[scp->sno].nhits++; } @@ -540,8 +550,8 @@ direct( /* add direct component */ prob = hwt * (double)source[scp->sno].nhits / (double)source[scp->sno].ntests; if (prob < 1.0) - scalecolor(scp->val, prob); - addcolor(r->rcol, scp->val); + scalescolor(scp->val, prob); + saddscolor(r->rcol, scp->val); } } @@ -608,7 +618,7 @@ srcscatter( /* compute source scattering into ray */ sr.gecc = r->gecc; sr.slights = r->slights; rayvalue(&sr); /* eval. source ray */ - if (bright(sr.rcol) <= FTINY) { + if (pbright(sr.rcol) <= FTINY) { #if SHADCACHE srcblocker(&sr); /* add blocker to cache */ #endif @@ -623,7 +633,7 @@ srcscatter( /* compute source scattering into ray */ } /* other factors */ d *= si.dom * r->rot / (4.*PI*nsamps); - scalecolor(sr.rcol, d); + scalescolor(sr.rcol, d); } else { /* PMAP: Add ambient inscattering from * volume photons; note we reverse the @@ -634,12 +644,12 @@ srcscatter( /* compute source scattering into ray */ sr.rdir [2] = -r -> rdir [2]; sr.gecc = r -> gecc; inscatterVolumePmap(&sr, sr.rcol); - scalecolor(sr.rcol, r -> rot / nsamps); + scalescolor(sr.rcol, r -> rot / nsamps); } - multcolor(sr.rcol, r->cext); - multcolor(sr.rcol, r->albedo); - multcolor(sr.rcol, cvext); - addcolor(r->rcol, sr.rcol); /* add it in */ + smultcolor(sr.rcol, r->cext); + smultcolor(sr.rcol, r->albedo); + smultcolor(sr.rcol, cvext); + saddscolor(r->rcol, sr.rcol); /* add it in */ } } samplendx = oldsampndx; @@ -740,11 +750,11 @@ m_light( /* ray hit a light source */ { /* check for over-counting */ if (badcomponent(m, r)) { - setcolor(r->rcoef, 0.0, 0.0, 0.0); + scolorblack(r->rcoef); return(1); } if (wrongsource(m, r)) { - setcolor(r->rcoef, 0.0, 0.0, 0.0); + scolorblack(r->rcoef); return(1); } /* check for passed illum */ @@ -756,23 +766,25 @@ m_light( /* ray hit a light source */ } /* check for invisibility */ if (srcignore(m, r)) { - setcolor(r->rcoef, 0.0, 0.0, 0.0); + scolorblack(r->rcoef); return(1); } /* otherwise treat as source */ - /* check for behind */ - if (r->rod < 0.0) + if (r->rod < 0.0) { /* check for behind */ + if (!backvis) + raytrans(r); /* used to return black */ return(1); + } /* check for outside spot */ if (m->otype==MAT_SPOT && spotout(r, makespot(m))) return(1); /* get distribution pattern */ raytexture(r, m->omod); /* get source color */ - setcolor(r->rcol, m->oargs.farg[0], + setscolor(r->rcol, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]); /* modify value */ - multcolor(r->rcol, r->pcol); + smultscolor(r->rcol, r->pcol); return(1); }