--- ray/src/rt/raytrace.c 1991/05/01 11:17:01 1.15 +++ ray/src/rt/raytrace.c 1993/01/25 15:16:59 2.2 @@ -1,4 +1,4 @@ -/* Copyright (c) 1990 Regents of the University of California */ +/* Copyright (c) 1991 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -23,12 +23,13 @@ extern int maxdepth; /* maximum recursion depth */ extern double minweight; /* minimum ray weight */ extern int do_irrad; /* compute irradiance? */ -long nrays = 0L; /* number of rays traced */ +long raynum = 0L; /* next unique ray number */ +long nrays = 0L; /* number of calls to localhit */ -static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; +static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; OBJREC Lamb = { OVOID, MAT_PLASTIC, "Lambertian", - {0, 5, NULL, Lambfa}, NULL, -1, + {0, 5, NULL, Lambfa}, NULL, }; /* a Lambertian surface */ #define MAXLOOP 128 /* modifier loop detection */ @@ -47,6 +48,7 @@ double rw; r->crtype = r->rtype = rt; r->rsrc = -1; r->clipset = NULL; + r->revf = raytrace; } else { /* spawned ray */ r->rlvl = ro->rlvl; if (rt & RAYREFL) { @@ -57,11 +59,21 @@ double rw; r->rsrc = ro->rsrc; r->clipset = ro->newcset; } + r->revf = ro->revf; r->rweight = ro->rweight * rw; r->crtype = ro->crtype | (r->rtype = rt); VCOPY(r->rorg, ro->rop); } - r->rno = nrays; + rayclear(r); + return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); +} + + +rayclear(r) /* clear a ray for (re)evaluation */ +register RAY *r; +{ + r->rno = raynum++; + r->cxs[0] = 0; r->newcset = r->clipset; r->ro = NULL; r->rot = FHUGE; @@ -69,11 +81,10 @@ double rw; setcolor(r->pcol, 1.0, 1.0, 1.0); setcolor(r->rcol, 0.0, 0.0, 0.0); r->rt = 0.0; - return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); } -rayvalue(r) /* compute a ray's value */ +raytrace(r) /* trace a ray and compute its value */ RAY *r; { extern int (*trace)(); @@ -118,17 +129,10 @@ int mod; { static int depth = 0; register OBJREC *m; - /* check for irradiance calc. */ - if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { - if (irr_ignore(objptr(mod)->otype)) - raytrans(r); - else - (*ofun[Lamb.otype].funp)(&Lamb, r); - return; - } /* check for infinite loop */ if (depth++ >= MAXLOOP) objerror(r->ro, USER, "possible modifier loop"); + r->rt = r->rot; /* set effective ray length */ for ( ; mod != OVOID; mod = m->omod) { m = objptr(mod); /****** unnecessary test since modifier() is always called @@ -137,8 +141,17 @@ int mod; error(USER, errmsg); } ******/ + /* hack for irradiance calculation */ + if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { + if (irr_ignore(m->otype)) { + depth--; + raytrans(r); + return; + } + if (!islight(m->otype)) + m = &Lamb; + } (*ofun[m->otype].funp)(m, r); /* execute function */ - m->lastrno = r->rno; if (ismaterial(m->otype)) { /* materials call raytexture */ depth--; return; /* we're done */ @@ -165,7 +178,6 @@ int mod; error(USER, errmsg); } (*ofun[m->otype].funp)(m, r); - m->lastrno = r->rno; } depth--; /* end here */ } @@ -311,7 +323,6 @@ register CUBE *scene; register int i; nrays++; /* increment trace counter */ - sflags = 0; for (i = 0; i < 3; i++) { curpos[i] = r->rorg[i]; @@ -320,6 +331,8 @@ register CUBE *scene; else if (r->rdir[i] < -FTINY) sflags |= 0x10 << i; } + if (sflags == 0) + error(CONSISTENCY, "zero ray direction in localhit"); t = 0.0; if (!incube(scene, curpos)) { /* find distance to entry */ @@ -440,15 +453,40 @@ CUBE *cu; register int i; objset(oset, cu->cutree); + checkset(oset, r->cxs); /* eliminate double-checking */ for (i = oset[0]; i > 0; i--) { o = objptr(oset[i]); - if (o->lastrno == r->rno) /* checked already? */ - continue; (*ofun[o->otype].funp)(o, r); - o->lastrno = r->rno; } if (r->ro == NULL) return(0); /* no scores yet */ return(incube(cu, r->rop)); /* hit OK if in current cube */ +} + + +static +checkset(os, cs) /* modify checked set and set to check */ +register OBJECT os[MAXSET+1]; /* os' = os - cs */ +register OBJECT cs[MAXCSET+1]; /* cs' = cs + os */ +{ + OBJECT cset[MAXCSET+MAXSET+1]; + register int i, j, k; + /* copy os in place, cset <- cs */ + cset[0] = 0; + k = 0; + for (i = j = 1; i <= os[0]; i++) { + while (j <= cs[0] && cs[j] < os[i]) + cset[++cset[0]] = cs[j++]; + if (j > cs[0] || os[i] != cs[j]) { /* object to check */ + os[++k] = os[i]; + cset[++cset[0]] = os[i]; + } + } + while (j <= cs[0]) /* get the rest of cs */ + cset[++cset[0]] = cs[j++]; + if (cset[0] > MAXCSET) /* truncate if necessary */ + cset[0] = MAXCSET; + setcopy(cs, cset); /* copy new "checked" set back */ + os[0] = k; /* new "to check" set size */ }