--- ray/src/rt/srcobstr.c 2004/01/01 19:31:46 2.2 +++ ray/src/rt/srcobstr.c 2004/09/08 06:07:52 2.7 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: srcobstr.c,v 2.2 2004/01/01 19:31:46 greg Exp $"; +static const char RCSid[] = "$Id: srcobstr.c,v 2.7 2004/09/08 06:07:52 greg Exp $"; #endif /* * Source occlusion caching routines @@ -19,7 +19,7 @@ static const char RCSid[] = "$Id: srcobstr.c,v 2.2 200 #if SHADCACHE /* preemptive shadow checking */ -static void /* cast source ray to first blocker */ +static int /* cast source ray to first blocker */ castshadow(int sn, FVECT rorg, FVECT rdir) { RAY rt; @@ -28,19 +28,25 @@ castshadow(int sn, FVECT rorg, FVECT rdir) VCOPY(rt.rdir, rdir); rt.rmax = 0; rayorigin(&rt, NULL, PRIMARY, 1.0); - if (!localhit(&rt, &thescene)) - return; - /* pretend we were aimed at source */ - rt.crtype |= rt.rtype = SHADOW; - rt.rdir[0] = -rt.rdir[0]; - rt.rdir[1] = -rt.rdir[1]; - rt.rdir[2] = -rt.rdir[2]; - rt.rod = -rt.rod; - VSUB(rt.rorg, rt.rop, rt.rdir); - rt.rot = 1.; - rt.rsrc = sn; + /* check for intersection */ + while (localhit(&rt, &thescene)) { + RAY rt1 = rt; /* pretend we were aimed at source */ + rt1.crtype |= rt1.rtype = SHADOW; + rt1.rdir[0] = -rt.rdir[0]; + rt1.rdir[1] = -rt.rdir[1]; + rt1.rdir[2] = -rt.rdir[2]; + rt1.rod = -rt.rod; + VSUB(rt1.rorg, rt.rop, rt.rdir); + rt1.rot = 1.; + rt1.rsrc = sn; /* record blocker */ - srcblocker(&rt); + if (srcblocker(&rt1)) + return(1); + /* move past failed blocker */ + VSUM(rt.rorg, rt.rop, rt.rdir, FTINY); + rayclear(&rt); /* & try again... */ + } + return(0); /* found no blockers */ } @@ -136,7 +142,7 @@ initobscache(int sn) else VSUM(rdir, srcp->obscache->p.f.v, srcp->obscache->p.f.u, d); - d = 2./SHADCACHE*(j+.5); + d = 1. - 2./SHADCACHE*(j+.5); VSUM(rdir, rdir, srcp->snorm, d); normalize(rdir); castshadow(sn, rorg, rdir); @@ -169,13 +175,19 @@ initobscache(int sn) static OBJECT * /* return occluder cache entry */ srcobstructp(register RAY *r) { + static unsigned long lastrno = ~0; static OBJECT noobs; + static OBJECT *lastobjp; SRCREC *srcp; int ondx; + noobs = OVOID; + if (r->rno == lastrno) + return lastobjp; /* just recall last pointer */ DCHECK(r->rsrc < 0, CONSISTENCY, "srcobstructp() called with unaimed ray"); - noobs = OVOID; + lastrno = r->rno; + lastobjp = &noobs; srcp = &source[r->rsrc]; if (srcp->sflags & (SSKIP|SPROX|SSPOT|SVIRTUAL)) return(&noobs); /* don't cache these */ @@ -197,7 +209,7 @@ srcobstructp(register RAY *r) ondx += (int)(2*SHADCACHE*srcp->obscache->p.d.e2 * (r->rorg[ax2] + t*srcp->sloc[ax2] - srcp->obscache->p.d.o[ax2])); - if (ondx < 0 | ondx >= 4*SHADCACHE*SHADCACHE) + if ((ondx < 0) | (ondx >= 4*SHADCACHE*SHADCACHE)) return(&noobs); /* could happen if ray is outside */ } else if (srcp->sflags & SFLAT) { FVECT sd; @@ -219,7 +231,7 @@ srcobstructp(register RAY *r) if (sd[0] < 0) ondx += ((SHADCACHE+1)>>1)*SHADCACHE; ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * - sd[2]/sd0m); + (1. - sd[2]/sd0m)); ondx += (int)(SHADCACHE*(.5-FTINY) * (1. + sd[1]/sd0m)); } else /* sd1m > sd0m */ { @@ -228,10 +240,13 @@ srcobstructp(register RAY *r) if (sd[1] < 0) ondx += ((SHADCACHE+1)>>1)*SHADCACHE; ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * - sd[2]/sd1m); + (1. - sd[2]/sd1m)); ondx += (int)(SHADCACHE*(.5-FTINY) * (1. + sd[0]/sd1m)); } + DCHECK((ondx < 0) | (ondx >= SHADCACHE*SHADCACHE*3 + + (SHADCACHE&1)*SHADCACHE*4), CONSISTENCY, + "flat source cache index out of bounds"); } else /* spherical distribution */ { int ax, ax1, ax2; RREAL amax = 0; @@ -249,9 +264,11 @@ srcobstructp(register RAY *r) (1. + r->rdir[ax1]/amax)); ondx += (int)(SHADCACHE*(.5-FTINY) * (1. + r->rdir[ax2]/amax)); + DCHECK((ondx < 0) | (ondx >= SHADCACHE*SHADCACHE*6), CONSISTENCY, + "radial source cache index out of bounds"); } /* return cache pointer */ - return(&srcp->obscache->obs[ondx]); + return(lastobjp = &srcp->obscache->obs[ondx]); } @@ -265,20 +282,21 @@ freeobscache(SRCREC *srcp) } -void /* record a source blocker */ +int /* 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 */ + return(0); /* 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 */ + return(0); /* no material?! */ + if (!isopaque(m->otype)) + return(0); /* material not a reliable blocker */ *srcobstructp(r) = r->robj; /* else record obstructor */ + return(1); }