--- ray/src/rt/source.c 2003/09/12 22:35:54 2.37 +++ ray/src/rt/source.c 2004/03/30 16:13:01 2.44 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.37 2003/09/12 22:35:54 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.44 2004/03/30 16:13:01 schorsch Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -7,14 +7,10 @@ static const char RCSid[] = "$Id: source.c,v 2.37 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,36 @@ 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 (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); +} + + +extern void +marksources(void) /* find and mark source objects */ +{ int foundsource = 0; int i; register OBJREC *o, *m; @@ -61,13 +83,7 @@ marksources() /* find and mark source objects */ if (!issurface(o->otype) || o->omod == OVOID) continue; /* find material */ - m = objptr(o->omod); - while (!ismaterial(m->otype)) - if (ismixture(m->otype) || m->omod == OVOID) { - m = NULL; - break; - } else - m = objptr(m->omod); + m = findmaterial(objptr(o->omod)); if (m == NULL || !islight(m->otype)) continue; /* not source modifier */ @@ -108,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++; } @@ -128,10 +147,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; @@ -146,11 +169,12 @@ 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; @@ -184,9 +208,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; @@ -225,9 +250,10 @@ nomat: } -int -sourcehit(r) /* check to see if ray hit distant source */ -register RAY *r; +extern int +sourcehit( /* check to see if ray hit distant source */ + register RAY *r +) { int first, last; register int i; @@ -265,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) @@ -276,13 +307,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; @@ -313,6 +344,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; @@ -373,8 +411,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); } @@ -406,9 +451,10 @@ char *p; /* data for f */ } -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; @@ -497,12 +543,11 @@ register RAY *r; */ static int -weaksrcmat(int obj) /* identify material */ +weaksrcmat(OBJECT obj) /* identify material */ { - register OBJREC *o = objptr(obj); + OBJREC *o = findmaterial(objptr(obj)); - while (!ismaterial(o->otype)) /* find material */ - o = objptr(o->omod); + if (o == NULL) return 0; return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW)); } @@ -564,10 +609,11 @@ weaksrcmat(int obj) /* identify material */ 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))