--- ray/src/rt/source.c 2003/02/25 02:47:23 2.30 +++ ray/src/rt/source.c 2007/07/25 04:12:36 2.56 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.30 2003/02/25 02:47:23 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.56 2007/07/25 04:12:36 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -7,14 +7,10 @@ static const char RCSid[] = "$Id: source.c,v 2.30 2003 * External symbols declared in source.h */ -#include "copyright.h" - #include "ray.h" - #include "otypes.h" - +#include "rtotypes.h" #include "source.h" - #include "random.h" extern double ssampdist; /* scatter sampling distance */ @@ -43,10 +39,38 @@ static CONTRIB *srccnt; /* source contributions in d static CNTPTR *cntord; /* source ordering in direct() */ static int maxcntr = 0; /* size of contribution arrays */ +static int cntcmp(const void *p1, const void *p2); -void -marksources() /* find and mark source objects */ + +extern OBJREC * /* find an object's actual material */ +findmaterial(register 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 */ +} + + +extern void +marksources(void) /* find and mark source objects */ +{ int foundsource = 0; int i; register OBJREC *o, *m; @@ -54,17 +78,22 @@ marksources() /* find and mark source objects */ /* initialize dispatch table */ initstypes(); /* find direct sources */ - for (i = 0; i < nobjects; i++) { + for (i = 0; i < nsceneobjs; i++) { o = objptr(i); if (!issurface(o->otype) || o->omod == OVOID) continue; - - m = objptr(o->omod); - - if (!islight(m->otype)) + /* find material */ + m = findmaterial(objptr(o->omod)); + 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)) @@ -103,6 +132,9 @@ marksources() /* find and mark source objects */ source[ns].sflags |= SSKIP; } } +#if SHADCACHE + initobscache(ns); +#endif if (!(source[ns].sflags & SSKIP)) foundsource++; } @@ -115,7 +147,7 @@ marksources() /* 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) + if ((srccnt == NULL) | (cntord == NULL)) goto memerr; return; memerr: @@ -123,10 +155,14 @@ memerr: } -void -freesources() /* free all source structures */ +extern void +freesources(void) /* free all source structures */ { if (nsources > 0) { +#if SHADCACHE + while (nsources--) + freeobscache(&source[nsources]); +#endif free((void *)source); source = NULL; nsources = 0; @@ -141,16 +177,17 @@ freesources() /* free all source structures */ } -int -srcray(sr, r, si) /* send a ray to a source, return domega */ -register RAY *sr; /* returned source ray */ -RAY *r; /* ray which hit object */ -SRCINDEX *si; /* source sample index */ +extern int +srcray( /* send a ray to a source, return domega */ + register RAY *sr, /* returned source ray */ + RAY *r, /* ray which hit object */ + SRCINDEX *si /* source sample index */ +) { double d; /* distance to source */ register SRCREC *srcp; - rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */ + rayorigin(sr, SHADOW, r, NULL); /* ignore limits */ while ((d = nextssamp(sr, si)) != 0.0) { sr->rsrc = si->sn; /* remember source */ @@ -179,9 +216,10 @@ SRCINDEX *si; /* source sample index */ } -void -srcvalue(r) /* punch ray to source and compute value */ -register RAY *r; +extern void +srcvalue( /* punch ray to source and compute value */ + register RAY *r +) { register SRCREC *sp; @@ -220,10 +258,28 @@ nomat: } -int -sourcehit(r) /* check to see if ray hit distant source */ -register RAY *r; +static int +transillum( /* check if material is transparent illum */ + OBJECT obj +) { + OBJREC *m = findmaterial(objptr(obj)); + + if (m == NULL) + return(1); + if (m->otype != MAT_ILLUM) + return(0); + return(!m->oargs.nsargs || !strcmp(m->oargs.sarg[0], VOIDID)); +} + + +extern int +sourcehit( /* check to see if ray hit distant source */ + register RAY *r +) +{ + int glowsrc = -1; + int transrc = -1; int first, last; register int i; @@ -232,35 +288,68 @@ register RAY *r; } else { /* otherwise check all */ first = 0; last = nsources-1; } - for (i = first; i <= last; i++) - if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT) - /* - * Check to see if ray is within - * solid angle of source. - */ - if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir)) - <= source[i].ss2) { - r->ro = source[i].so; - if (!(source[i].sflags & SSKIP)) - break; - } - - if (r->ro != NULL) { - r->robj = objndx(r->ro); - for (i = 0; i < 3; i++) - r->ron[i] = -r->rdir[i]; - r->rod = 1.0; - r->rox = NULL; - return(1); + for (i = first; i <= last; i++) { + if ((source[i].sflags & (SDISTANT|SVIRTUAL)) != SDISTANT) + continue; + /* + * Check to see if ray is within + * solid angle of source. + */ + if (2.*PI*(1. - DOT(source[i].sloc,r->rdir)) > source[i].ss2) + continue; + /* is it the only possibility? */ + if (first == last) { + r->ro = source[i].so; + break; + } + /* + * If it's a glow or transparent illum, just remember it. + */ + if (source[i].sflags & SSKIP) { + glowsrc = i; + continue; + } + if (transillum(source[i].so->omod)) { + transrc = i; + continue; + } + r->ro = source[i].so; /* otherwise, use first hit */ + break; } - return(0); + /* + * Do we need fallback? + */ + if (r->ro == NULL) { + if (transrc >= 0 && r->crtype & (AMBIENT|SPECULAR)) + return(0); /* avoid overcounting */ + if (glowsrc >= 0) + r->ro = source[glowsrc].so; + else + return(0); /* nothing usable */ + } + /* + * Make assignments. + */ + 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); } static int -cntcmp(sc1, sc2) /* contribution compare (descending) */ -register CNTPTR *sc1, *sc2; +cntcmp( /* contribution compare (descending) */ + const void *p1, + const void *p2 +) { + register const CNTPTR *sc1 = (const CNTPTR *)p1; + register const CNTPTR *sc2 = (const CNTPTR *)p2; + if (sc1->brt > sc2->brt) return(-1); if (sc1->brt < sc2->brt) @@ -269,13 +358,13 @@ register CNTPTR *sc1, *sc2; } -void -direct(r, f, p) /* add direct component */ -RAY *r; /* ray that hit surface */ -void (*f)(); /* direct component coefficient function */ -char *p; /* data for f */ +extern void +direct( /* add direct component */ + RAY *r, /* ray that hit surface */ + srcdirf_t *f, /* direct component coefficient function */ + void *p /* data for f */ +) { - extern void (*trace)(); register int sn; register CONTRIB *scp; SRCINDEX si; @@ -291,11 +380,11 @@ char *p; /* data for f */ for (sn = 0; srcray(&sr, r, &si); sn++) { if (sn >= maxcntr) { maxcntr = sn + MAXSPART; - srccnt = (CONTRIB *)realloc((char *)srccnt, + srccnt = (CONTRIB *)realloc((void *)srccnt, maxcntr*sizeof(CONTRIB)); - cntord = (CNTPTR *)realloc((char *)cntord, + cntord = (CNTPTR *)realloc((void *)cntord, maxcntr*sizeof(CNTPTR)); - if (srccnt == NULL | cntord == NULL) + if ((srccnt == NULL) | (cntord == NULL)) error(SYSTEM, "out of memory in direct"); } cntord[sn].sndx = sn; @@ -303,16 +392,24 @@ char *p; /* data for f */ scp->sno = sr.rsrc; /* compute coefficient */ (*f)(scp->coef, p, sr.rdir, si.dom); - cntord[sn].brt = bright(scp->coef); + cntord[sn].brt = intens(scp->coef); if (cntord[sn].brt <= 0.0) continue; +#if SHADCACHE + /* check shadow cache */ + if (si.np == 1 && srcblocked(&sr)) { + cntord[sn].brt = 0.0; + continue; + } +#endif VCOPY(scp->dir, sr.rdir); + copycolor(sr.rcoef, scp->coef); /* compute potential */ sr.revf = srcvalue; rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); copycolor(scp->val, sr.rcol); - multcolor(scp->val, scp->coef); - cntord[sn].brt = bright(scp->val); + cntord[sn].brt = intens(sr.rcol); } /* sort contributions */ qsort(cntord, sn, sizeof(CNTPTR), cntcmp); @@ -350,22 +447,40 @@ char *p; /* data for f */ break; scp = srccnt + cntord[sn].sndx; /* test for hit */ - rayorigin(&sr, r, SHADOW, 1.0); + rayorigin(&sr, SHADOW, r, NULL); + copycolor(sr.rcoef, scp->coef); VCOPY(sr.rdir, scp->dir); sr.rsrc = scp->sno; - source[scp->sno].ntests++; /* keep statistics */ + /* keep statistics */ + if (source[scp->sno].ntests++ > 0xfffffff0) { + source[scp->sno].ntests >>= 1; + source[scp->sno].nhits >>= 1; + } if (localhit(&sr, &thescene) && ( sr.ro != source[scp->sno].so || source[scp->sno].sflags & SFOLLOW )) { /* follow entire path */ raycont(&sr); - rayparticipate(&sr); if (trace != NULL) (*trace)(&sr); /* trace execution */ - if (bright(sr.rcol) <= FTINY) + if (bright(sr.rcol) <= FTINY) { +#if SHADCACHE + if ((scp <= srccnt || scp[-1].sno != scp->sno) + && (scp >= srccnt+ncnts-1 || + scp[1].sno != scp->sno)) + srcblocker(&sr); +#endif continue; /* missed! */ + } + rayparticipate(&sr); + multcolor(sr.rcol, sr.rcoef); copycolor(scp->val, sr.rcol); - multcolor(scp->val, scp->coef); + } else if (trace != NULL && + (source[scp->sno].sflags & (SDISTANT|SVIRTUAL|SFOLLOW)) + == (SDISTANT|SFOLLOW) && + sourcehit(&sr) && rayshade(&sr, sr.ro->omod)) { + (*trace)(&sr); /* trace execution */ + /* skip call to rayparticipate() & scp->val update */ } /* add contribution if hit */ addcolor(r->rcol, scp->val); @@ -387,17 +502,17 @@ char *p; /* data for f */ scp = srccnt + cntord[sn].sndx; prob = hwt * (double)source[scp->sno].nhits / (double)source[scp->sno].ntests; - if (prob > 1.0) - prob = 1.0; - scalecolor(scp->val, prob); + if (prob < 1.0) + scalecolor(scp->val, prob); addcolor(r->rcol, scp->val); } } -void -srcscatter(r) /* compute source scattering into ray */ -register RAY *r; +extern void +srcscatter( /* compute source scattering into ray */ + register RAY *r +) { int oldsampndx; int nsamps; @@ -442,13 +557,21 @@ register RAY *r; if (!srcray(&sr, NULL, &si) || sr.rsrc != r->slights[i]) continue; /* no path */ +#if SHADCACHE + if (srcblocked(&sr)) /* check shadow cache */ + continue; +#endif copycolor(sr.cext, r->cext); copycolor(sr.albedo, r->albedo); sr.gecc = r->gecc; sr.slights = r->slights; rayvalue(&sr); /* eval. source ray */ - if (bright(sr.rcol) <= FTINY) + if (bright(sr.rcol) <= FTINY) { +#if SHADCACHE + srcblocker(&sr); /* add blocker to cache */ +#endif continue; + } if (r->gecc <= FTINY) /* compute P(theta) */ d = 1.; else { @@ -485,13 +608,18 @@ register RAY *r; * geometry behind (or inside) an effective radiator. */ -static int weaksrcmod(obj) int obj; /* efficiency booster function */ -{register OBJREC *o = objptr(obj); -return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} +static int +weaksrcmat(OBJECT obj) /* identify material */ +{ + OBJREC *m = findmaterial(objptr(obj)); + + 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 && \ - weaksrcmod(source[r->rsrc].so->omod)) + weaksrcmat(source[r->rsrc].so->omod)) /* wrongsource * * @@ -547,16 +675,21 @@ return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} distglow(m, r, raydist(r,PRIMARY))) -int -m_light(m, r) /* ray hit a light source */ -register OBJREC *m; -register RAY *r; +extern int +m_light( /* ray hit a light source */ + register OBJREC *m, + register RAY *r +) { /* check for over-counting */ - if (badcomponent(m, r)) + if (badcomponent(m, r)) { + setcolor(r->rcoef, 0.0, 0.0, 0.0); return(1); - if (wrongsource(m, r)) + } + if (wrongsource(m, r)) { + setcolor(r->rcoef, 0.0, 0.0, 0.0); return(1); + } /* check for passed illum */ if (passillum(m, r)) { if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) @@ -564,12 +697,14 @@ register RAY *r; raytrans(r); return(1); } + /* check for invisibility */ + if (srcignore(m, r)) { + setcolor(r->rcoef, 0.0, 0.0, 0.0); + return(1); + } /* otherwise treat as source */ /* check for behind */ if (r->rod < 0.0) - return(1); - /* check for invisibility */ - if (srcignore(m, r)) return(1); /* check for outside spot */ if (m->otype==MAT_SPOT && spotout(r, makespot(m)))