--- ray/src/rt/source.c 2003/04/23 00:52:34 2.32 +++ ray/src/rt/source.c 2004/01/09 05:37:11 2.41 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.32 2003/04/23 00:52:34 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.41 2004/01/09 05:37:11 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -7,8 +7,6 @@ static const char RCSid[] = "$Id: source.c,v 2.32 2003 * External symbols declared in source.h */ -#include "copyright.h" - #include "ray.h" #include "otypes.h" @@ -44,6 +42,30 @@ static CNTPTR *cntord; /* source ordering in direct static int maxcntr = 0; /* size of contribution arrays */ +OBJREC * /* find an object's actual material */ +findmaterial(register OBJREC *o) +{ + while (!ismaterial(o->otype)) { + if (ismixture(o->otype)) + return(NULL); /* reject mixed materials */ + 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 (o->omod == OVOID) + return(NULL); + o = objptr(o->omod); + } + return(o); +} + + void marksources() /* find and mark source objects */ { @@ -54,17 +76,16 @@ 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)) - continue; + /* find material */ + m = findmaterial(o); + if (m == NULL || !islight(m->otype)) + continue; /* not source modifier */ if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : m->otype == MAT_SPOT ? 7 : 3)) @@ -103,6 +124,9 @@ marksources() /* find and mark source objects */ source[ns].sflags |= SSKIP; } } +#if SHADCACHE + source[ns].obscache = NULL; +#endif if (!(source[ns].sflags & SSKIP)) foundsource++; } @@ -115,7 +139,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: @@ -127,6 +151,10 @@ void freesources() /* free all source structures */ { if (nsources > 0) { +#if SHADCACHE + while (nsources--) + freeobscache(&source[nsources]); +#endif free((void *)source); source = NULL; nsources = 0; @@ -142,10 +170,11 @@ 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 */ +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; @@ -180,8 +209,9 @@ SRCINDEX *si; /* source sample index */ void -srcvalue(r) /* punch ray to source and compute value */ -register RAY *r; +srcvalue( /* punch ray to source and compute value */ +register RAY *r +) { register SRCREC *sp; @@ -221,8 +251,9 @@ nomat: int -sourcehit(r) /* check to see if ray hit distant source */ -register RAY *r; +sourcehit( /* check to see if ray hit distant source */ +register RAY *r +) { int first, last; register int i; @@ -260,9 +291,14 @@ register RAY *r; 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) @@ -272,10 +308,11 @@ 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 */ +direct( /* add direct component */ +RAY *r, /* ray that hit surface */ +void (*f)(), /* direct component coefficient function */ +char *p /* data for f */ +) { extern void (*trace)(); register int sn; @@ -297,7 +334,7 @@ char *p; /* data for f */ maxcntr*sizeof(CONTRIB)); 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; @@ -308,6 +345,13 @@ char *p; /* data for f */ cntord[sn].brt = bright(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); /* compute potential */ sr.revf = srcvalue; @@ -355,7 +399,11 @@ char *p; /* data for f */ rayorigin(&sr, r, SHADOW, 1.0); 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 )) { @@ -364,8 +412,15 @@ char *p; /* data for f */ 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! */ + } copycolor(scp->val, sr.rcol); multcolor(scp->val, scp->coef); } @@ -398,8 +453,9 @@ char *p; /* data for f */ void -srcscatter(r) /* compute source scattering into ray */ -register RAY *r; +srcscatter( /* compute source scattering into ray */ +register RAY *r +) { int oldsampndx; int nsamps; @@ -487,13 +543,19 @@ 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(int obj) /* identify material */ +{ + register OBJREC *o = objptr(obj); + + while (!ismaterial(o->otype)) /* find material */ + o = objptr(o->omod); + return((o->otype==MAT_ILLUM)|(o->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 * * @@ -550,9 +612,10 @@ return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} int -m_light(m, r) /* ray hit a light source */ -register OBJREC *m; -register RAY *r; +m_light( /* ray hit a light source */ +register OBJREC *m, +register RAY *r +) { /* check for over-counting */ if (badcomponent(m, r))