--- ray/src/rt/srcobstr.c 2004/03/30 16:13:01 2.4 +++ ray/src/rt/srcobstr.c 2004/09/10 17:08:36 2.10 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: srcobstr.c,v 2.4 2004/03/30 16:13:01 schorsch Exp $"; +static const char RCSid[] = "$Id: srcobstr.c,v 2.10 2004/09/10 17:08:36 greg Exp $"; #endif /* * Source occlusion caching routines @@ -19,7 +19,7 @@ static const char RCSid[] = "$Id: srcobstr.c,v 2.4 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,23 +28,29 @@ 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 */ } -static void /* initialize occlusion cache */ +void /* initialize occlusion cache */ initobscache(int sn) { register SRCREC *srcp = &source[sn]; @@ -54,6 +60,8 @@ initobscache(int sn) int i, j, k; int ax, ax1, ax2; + if (srcp->sflags & (SSKIP|SPROX|SSPOT|SVIRTUAL)) + return; /* don't cache these */ if (srcp->sflags & SDISTANT) cachelen = 4*SHADCACHE*SHADCACHE; else if (srcp->sflags & SFLAT) @@ -61,8 +69,6 @@ initobscache(int sn) 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) @@ -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 */ @@ -232,8 +244,8 @@ srcobstructp(register RAY *r) ondx += (int)(SHADCACHE*(.5-FTINY) * (1. + sd[0]/sd1m)); } - DCHECK(ondx < 0 | ondx >= SHADCACHE*SHADCACHE*3 + - (SHADCACHE&1)*SHADCACHE*4, CONSISTENCY, + 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; @@ -252,11 +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, + 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]); } @@ -270,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); } @@ -295,8 +308,16 @@ srcblocked(RAY *r) if (obs == OVOID) return(0); - op = objptr(obs); /* check for intersection */ - return((*ofun[op->otype].funp)(op, r)); + op = objptr(obs); /* check blocker intersection */ + if (!(*ofun[op->otype].funp)(op, r)) + return(0); + if (source[r->rsrc].sflags & SDISTANT) + return(1); + op = source[r->rsrc].so; /* check source intersection */ + if (!(*ofun[op->otype].funp)(op, r)) + return(1); + rayclear(r); + return(0); /* source in front */ }