--- ray/src/rt/source.c 2003/12/31 01:50:02 2.38 +++ ray/src/rt/source.c 2005/04/15 04:44:51 2.49 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: source.c,v 2.38 2003/12/31 01:50:02 greg Exp $"; +static const char RCSid[] = "$Id: source.c,v 2.49 2005/04/15 04:44:51 greg Exp $"; #endif /* * source.c - routines dealing with illumination sources. @@ -7,16 +7,10 @@ static const char RCSid[] = "$Id: source.c,v 2.38 2003 * External symbols declared in source.h */ -#include "copyright.h" - #include "ray.h" - #include "otypes.h" - -#include "otspecial.h" - +#include "rtotypes.h" #include "source.h" - #include "random.h" extern double ssampdist; /* scatter sampling distance */ @@ -45,13 +39,13 @@ 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)) { - if (ismixture(o->otype)) - return(NULL); /* reject mixed materials */ if (o->otype == MOD_ALIAS && o->oargs.nsargs) { OBJECT aobj; OBJREC *ao; @@ -66,12 +60,12 @@ findmaterial(register OBJREC *o) return(NULL); o = objptr(o->omod); } - return(o); + return(o); /* mixtures will return NULL */ } -void -marksources() /* find and mark source objects */ +extern void +marksources(void) /* find and mark source objects */ { int foundsource = 0; int i; @@ -87,7 +81,7 @@ marksources() /* find and mark source objects */ if (!issurface(o->otype) || o->omod == OVOID) continue; /* find material */ - m = findmaterial(o); + m = findmaterial(objptr(o->omod)); if (m == NULL || !islight(m->otype)) continue; /* not source modifier */ @@ -129,7 +123,7 @@ marksources() /* find and mark source objects */ } } #if SHADCACHE - source[ns].obscache = NULL; + initobscache(ns); #endif if (!(source[ns].sflags & SSKIP)) foundsource++; @@ -151,8 +145,8 @@ memerr: } -void -freesources() /* free all source structures */ +extern void +freesources(void) /* free all source structures */ { if (nsources > 0) { #if SHADCACHE @@ -173,11 +167,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 */ @@ -212,9 +206,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; @@ -254,11 +248,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; @@ -267,224 +278,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); -} - - -#if SHADCACHE /* preemptive shadow checking */ -#define ABS(x) ((x)>0 ? (x) : -(x)) - -static void /* find closest blockers to source */ -initobscache(SRCREC *srcp) -{ - int i; - int cachelen; - - if (srcp->sflags & SDISTANT) - cachelen = 4*SHADCACHE*SHADCACHE; - else if (srcp->sflags & SFLAT) - cachelen = SHADCACHE*SHADCACHE*3 + (SHADCACHE&1)*SHADCACHE*4; - else /* spherical distribution */ - cachelen = SHADCACHE*SHADCACHE*6; - /* allocate cache */ - DCHECK(srcp->obscache != NULL, - CONSISTENCY, "initobscache() called twice"); - srcp->obscache = (OBSCACHE *)malloc(sizeof(OBSCACHE) + - sizeof(OBJECT)*(cachelen-1)); - if (srcp->obscache == NULL) - error(SYSTEM, "out of memory in initobscache()"); - /* set parameters */ - if (srcp->sflags & SDISTANT) { - int ax, ax1, ax2; - RREAL amax = 0; - for (ax1 = 3; ax1--; ) - if (ABS(srcp->sloc[ax1]) > amax) { - amax = ABS(srcp->sloc[ax1]); - ax = ax1; - } - srcp->obscache->p.d.ax = ax; - ax1 = (ax+1)%3; - ax2 = (ax+2)%3; - VCOPY(srcp->obscache->p.d.o, thescene.cuorg); - if (srcp->sloc[ax] > 0) - srcp->obscache->p.d.o[ax] += thescene.cusize; - if (srcp->sloc[ax1] < 0) - srcp->obscache->p.d.o[ax1] += thescene.cusize * - srcp->sloc[ax1] / ABS(srcp->sloc[ax]); - if (srcp->sloc[ax2] < 0) - srcp->obscache->p.d.o[ax2] += thescene.cusize * - srcp->sloc[ax2] / ABS(srcp->sloc[ax]); - srcp->obscache->p.d.e1 = (1.-FTINY) / (thescene.cusize*(1. + - fabs(srcp->sloc[ax1]/srcp->sloc[ax]))); - srcp->obscache->p.d.e2 = (1.-FTINY) / (thescene.cusize*(1. + - fabs(srcp->sloc[ax2]/srcp->sloc[ax]))); - } else if (srcp->sflags & SFLAT) { - VCOPY(srcp->obscache->p.f.u, srcp->ss[SU]); - normalize(srcp->obscache->p.f.u); - fcross(srcp->obscache->p.f.v, - srcp->snorm, srcp->obscache->p.f.u); + /* + * 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 */ } - /* XXX Should cast rays from source */ - for (i = cachelen; i--; ) - srcp->obscache->obs[i] = OVOID; + /* + * 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 OBJECT * /* return occluder cache entry */ -srcobstructp(register RAY *r) -{ - static OBJECT noobs; - SRCREC *srcp; - int ondx; - - DCHECK(r->rsrc < 0, CONSISTENCY, - "srcobstructp() called with unaimed ray"); - noobs = OVOID; - srcp = &source[r->rsrc]; - if (srcp->obscache == NULL) /* initialize cache */ - initobscache(srcp); - /* compute cache index */ - if (srcp->sflags & SDISTANT) { - int ax, ax1, ax2; - double t; - ax = srcp->obscache->p.d.ax; - if ((ax1 = ax+1) >= 3) ax1 -= 3; - if ((ax2 = ax+2) >= 3) ax2 -= 3; - t = (srcp->obscache->p.d.o[ax] - r->rorg[ax]) / srcp->sloc[ax]; - if (t <= FTINY) - return &noobs; /* could happen if ray is outside */ - ondx = 2*SHADCACHE*(int)(2*SHADCACHE*srcp->obscache->p.d.e1 * - (r->rorg[ax1] + t*srcp->sloc[ax1] - - srcp->obscache->p.d.o[ax1])); - ondx += (int)(2*SHADCACHE*srcp->obscache->p.d.e2 * - (r->rorg[ax2] + t*srcp->sloc[ax2] - - srcp->obscache->p.d.o[ax2])); - } else if (srcp->sflags & SFLAT) { - FVECT sd; - RREAL sd0m, sd1m; - sd[0] = -DOT(r->rdir, srcp->obscache->p.f.u); - sd[1] = -DOT(r->rdir, srcp->obscache->p.f.v); - sd[2] = -DOT(r->rdir, srcp->snorm); - if (sd[2] < 0) - return &noobs; /* shouldn't happen */ - sd0m = ABS(sd[0]); - sd1m = ABS(sd[1]); - if (sd[2] >= sd0m && sd[2] >= sd1m) { - ondx = SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * - (1. + sd[0]/sd[2])); - ondx += (int)(SHADCACHE*(.5-FTINY) * - (1. + sd[1]/sd[2])); - } else if (sd0m >= sd1m) { - ondx = SHADCACHE*SHADCACHE; - if (sd[0] < 0) - ondx += ((SHADCACHE+1)>>1)*SHADCACHE; - ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * - sd[2]/sd0m); - ondx += (int)(SHADCACHE*(.5-FTINY) * - (1. + sd[1]/sd0m)); - } else /* sd1m > sd0m */ { - ondx = SHADCACHE*SHADCACHE + - ((SHADCACHE+1)>>1)*SHADCACHE*2; - if (sd[1] < 0) - ondx += ((SHADCACHE+1)>>1)*SHADCACHE; - ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * - sd[2]/sd1m); - ondx += (int)(SHADCACHE*(.5-FTINY) * - (1. + sd[0]/sd1m)); - } - } else /* spherical distribution */ { - int ax, ax1, ax2; - RREAL amax = 0; - for (ax1 = 3; ax1--; ) - if (ABS(r->rdir[ax1]) > amax) { - amax = ABS(r->rdir[ax1]); - ax = ax1; - } - if ((ax1 = ax+1) >= 3) ax1 -= 3; - if ((ax2 = ax+2) >= 3) ax2 -= 3; - ondx = 2*SHADCACHE*SHADCACHE * ax; - if (r->rdir[ax] < 0) - ondx += SHADCACHE*SHADCACHE; - ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * - (1. + r->rdir[ax1]/amax)); - ondx += (int)(SHADCACHE*(.5-FTINY) * - (1. + r->rdir[ax2]/amax)); - } - /* return cache pointer */ - return(&srcp->obscache->obs[ondx]); -} - - -void /* free obstruction cache */ -freeobscache(SRCREC *srcp) -{ - if (srcp->obscache == NULL) - return; - free((void *)srcp->obscache); - srcp->obscache = NULL; -} - - -void /* record a source blocker */ -srcblocker(register RAY *r) -{ - OBJREC *m; - - if (r->robj == OVOID || objptr(r->robj) != r->ro || - isvolume(r->ro->otype)) - return; /* don't record complex blockers */ - m = findmaterial(r->ro); - if (m == NULL) - return; /* no material?! */ - if (!(ofun[m->otype].flags & T_OPAQUE)) - return; /* material not a reliable blocker */ - - *srcobstructp(r) = r->robj; /* else record obstructor */ -} - - -int /* check ray against cached blocker */ -srcblocked(RAY *r) -{ - OBJECT obs = *srcobstructp(r); - OBJREC *op; - - if (obs == OVOID) - return(0); - op = objptr(obs); /* check for intersection */ - return ((*ofun[op->otype].funp)(op, r)); -} - -#endif - - 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; @@ -498,14 +348,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; @@ -545,8 +394,7 @@ char *p /* data for f */ #endif VCOPY(scp->dir, sr.rdir); /* compute potential */ - sr.revf = srcvalue; - rayvalue(&sr); + srcvalue(&sr); copycolor(scp->val, sr.rcol); multcolor(scp->val, scp->coef); cntord[sn].brt = bright(scp->val); @@ -606,7 +454,7 @@ char *p /* data for f */ if (bright(sr.rcol) <= FTINY) { #if SHADCACHE if ((scp <= srccnt || scp[-1].sno != scp->sno) - && (scp >= srccnt+ncnts || + && (scp >= srccnt+ncnts-1 || scp[1].sno != scp->sno)) srcblocker(&sr); #endif @@ -643,9 +491,9 @@ char *p /* data for f */ } -void +extern void srcscatter( /* compute source scattering into ray */ -register RAY *r + register RAY *r ) { int oldsampndx; @@ -691,13 +539,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 { @@ -735,13 +591,12 @@ register RAY *r */ static int -weaksrcmat(int obj) /* identify material */ +weaksrcmat(OBJECT obj) /* identify material */ { - register OBJREC *o = objptr(obj); + OBJREC *m = findmaterial(objptr(obj)); - while (!ismaterial(o->otype)) /* find material */ - o = objptr(o->omod); - 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) && \ @@ -802,10 +657,10 @@ weaksrcmat(int 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 */