--- ray/src/rt/source.c 2004/03/01 18:11:20 2.43 +++ ray/src/rt/source.c 2005/03/10 22:37:00 2.47 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.43 2004/03/01 18:11:20 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.47 2005/03/10 22:37:00 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -8,11 +8,9 @@ static const char RCSid[] = "$Id: source.c,v 2.43 2004 */ #include "ray.h" - #include "otypes.h" - +#include "rtotypes.h" #include "source.h" - #include "random.h" extern double ssampdist; /* scatter sampling distance */ @@ -41,8 +39,10 @@ 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); -OBJREC * /* find an object's actual material */ + +extern OBJREC * /* find an object's actual material */ findmaterial(register OBJREC *o) { while (!ismaterial(o->otype)) { @@ -66,8 +66,8 @@ findmaterial(register OBJREC *o) } -void -marksources() /* find and mark source objects */ +extern void +marksources(void) /* find and mark source objects */ { int foundsource = 0; int i; @@ -125,7 +125,7 @@ marksources() /* find and mark source objects */ } } #if SHADCACHE - source[ns].obscache = NULL; + initobscache(ns); #endif if (!(source[ns].sflags & SSKIP)) foundsource++; @@ -147,8 +147,8 @@ memerr: } -void -freesources() /* free all source structures */ +extern void +freesources(void) /* free all source structures */ { if (nsources > 0) { #if SHADCACHE @@ -169,11 +169,11 @@ freesources() /* free all source structures */ } -int +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 */ + register RAY *sr, /* returned source ray */ + RAY *r, /* ray which hit object */ + SRCINDEX *si /* source sample index */ ) { double d; /* distance to source */ @@ -208,9 +208,9 @@ SRCINDEX *si /* source sample index */ } -void +extern void srcvalue( /* punch ray to source and compute value */ -register RAY *r + register RAY *r ) { register SRCREC *sp; @@ -250,11 +250,28 @@ nomat: } -int +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 + register RAY *r ) { + int glowsrc = -1; + int transrc = -1; int first, last; register int i; @@ -263,37 +280,63 @@ 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->pert[0] = r->pert[1] = r->pert[2] = 0.0; - r->uv[0] = r->uv[1] = 0.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( /* contribution compare (descending) */ -const void *p1, -const void *p2 + const void *p1, + const void *p2 ) { register const CNTPTR *sc1 = (const CNTPTR *)p1; @@ -307,14 +350,13 @@ const void *p2 } -void +extern void direct( /* add direct component */ -RAY *r, /* ray that hit surface */ -void (*f)(), /* direct component coefficient function */ -char *p /* data for f */ + 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; @@ -452,9 +494,9 @@ char *p /* data for f */ } -void +extern void srcscatter( /* compute source scattering into ray */ -register RAY *r + register RAY *r ) { int oldsampndx; @@ -500,13 +542,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 { @@ -546,10 +596,10 @@ register RAY *r static int weaksrcmat(OBJECT obj) /* identify material */ { - OBJREC *o = findmaterial(objptr(obj)); + OBJREC *m = findmaterial(objptr(obj)); - if (o == NULL) return 0; - return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW)); + if (m == NULL) return(0); + return((m->otype==MAT_ILLUM) | (m->otype==MAT_GLOW)); } #define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ @@ -610,10 +660,10 @@ weaksrcmat(OBJECT obj) /* identify material */ distglow(m, r, raydist(r,PRIMARY))) -int +extern int m_light( /* ray hit a light source */ -register OBJREC *m, -register RAY *r + register OBJREC *m, + register RAY *r ) { /* check for over-counting */