--- ray/src/rt/raytrace.c 1989/04/11 13:30:31 1.2 +++ ray/src/rt/raytrace.c 1991/01/12 14:05:01 1.13 @@ -1,4 +1,4 @@ -/* Copyright (c) 1986 Regents of the University of California */ +/* Copyright (c) 1990 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -22,7 +22,7 @@ extern double minweight; /* minimum ray weight */ long nrays = 0L; /* number of rays traced */ -#define MAXLOOP 32 /* modifier loop detection */ +#define MAXLOOP 128 /* modifier loop detection */ #define RAYHIT (-1) /* return value for intercepted ray */ @@ -56,35 +56,39 @@ double rw; r->newcset = r->clipset; r->ro = NULL; r->rot = FHUGE; - r->rofs = 1.0; setident4(r->rofx); - r->robs = 1.0; setident4(r->robx); r->pert[0] = r->pert[1] = r->pert[2] = 0.0; 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 */ -register RAY *r; +RAY *r; { extern int (*trace)(); - if (localhit(r, &thescene)) - if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) - raytrans(r); /* object is clipped */ - else - rayshade(r, r->ro->omod); - else if (sourcehit(r)) - rayshade(r, r->ro->omod); + if (localhit(r, &thescene) || sourcehit(r)) + raycont(r); if (trace != NULL) (*trace)(r); /* trace execution */ } +raycont(r) /* check for clipped object and continue */ +register RAY *r; +{ + if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) + raytrans(r); + else + rayshade(r, r->ro->omod); +} + + raytrans(r) /* transmit ray as is */ -RAY *r; +register RAY *r; { RAY tr; @@ -92,6 +96,7 @@ RAY *r; VCOPY(tr.rdir, r->rdir); rayvalue(&tr); copycolor(r->rcol, tr.rcol); + r->rt = r->rot + tr.rt; } } @@ -104,13 +109,15 @@ int mod; register OBJREC *m; /* check for infinite loop */ if (depth++ >= MAXLOOP) - objerror(r->ro, USER, "material loop"); + objerror(r->ro, USER, "possible modifier loop"); for ( ; mod != OVOID; mod = m->omod) { m = objptr(mod); + /****** unnecessary test since modifier() is always called if (!ismodifier(m->otype)) { sprintf(errmsg, "illegal modifier \"%s\"", m->oname); error(USER, errmsg); } + ******/ (*ofun[m->otype].funp)(m, r); /* execute function */ m->lastrno = r->rno; if (ismaterial(m->otype)) { /* materials call raytexture */ @@ -205,7 +212,7 @@ register RAY *r; * still fraught with problems since reflected rays and similar * directions calculated from the surface normal may spawn rays behind * the surface. The only solution is to curb textures at high - * incidence (Rdot << 1). + * incidence (namely, keep DOT(rdir,pert) < Rdot). */ for (i = 0; i < 3; i++) @@ -226,6 +233,42 @@ register RAY *r; } +newrayxf(r) /* get new tranformation matrix for ray */ +RAY *r; +{ + static struct xfn { + struct xfn *next; + FULLXF xf; + } xfseed = { &xfseed }, *xflast = &xfseed; + register struct xfn *xp; + register RAY *rp; + + /* + * Search for transform in circular list that + * has no associated ray in the tree. + */ + xp = xflast; + for (rp = r->parent; rp != NULL; rp = rp->parent) + if (rp->rox == &xp->xf) { /* xp in use */ + xp = xp->next; /* move to next */ + if (xp == xflast) { /* need new one */ + xp = (struct xfn *)malloc(sizeof(struct xfn)); + if (xp == NULL) + error(SYSTEM, + "out of memory in newrayxf"); + /* insert in list */ + xp->next = xflast->next; + xflast->next = xp; + break; /* we're done */ + } + rp = r; /* start check over */ + } + /* got it */ + r->rox = &xp->xf; + xflast = xp; +} + + flipsurface(r) /* reverse surface orientation */ register RAY *r; { @@ -244,28 +287,28 @@ register RAY *r; register CUBE *scene; { FVECT curpos; /* current cube position */ - int mpos, mneg; /* sign flags */ + int sflags; /* sign flags */ double t, dt; register int i; nrays++; /* increment trace counter */ - mpos = mneg = 0; + sflags = 0; for (i = 0; i < 3; i++) { curpos[i] = r->rorg[i]; if (r->rdir[i] > FTINY) - mpos |= 1 << i; + sflags |= 1 << i; else if (r->rdir[i] < -FTINY) - mneg |= 1 << i; + sflags |= 0x10 << i; } t = 0.0; if (!incube(scene, curpos)) { /* find distance to entry */ for (i = 0; i < 3; i++) { /* plane in our direction */ - if (mpos & 1<cuorg[i]; - else if (mneg & 1<cuorg[i] + scene->cusize; else continue; @@ -282,24 +325,23 @@ register CUBE *scene; if (!incube(scene, curpos)) /* non-intersecting ray */ return(0); } - return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT); + return(raymove(curpos, sflags, r, scene) == RAYHIT); } static int -raymove(pos, plus, minus, r, cu) /* check for hit as we move */ +raymove(pos, dirf, r, cu) /* check for hit as we move */ FVECT pos; /* modified */ -int plus, minus; /* direction indicators to speed tests */ +int dirf; /* direction indicators to speed tests */ register RAY *r; register CUBE *cu; { int ax; double dt, t; - register int sgn; if (istree(cu->cutree)) { /* recurse on subcubes */ CUBE cukid; - register int br; + register int br, sgn; cukid.cusize = cu->cusize * 0.5; /* find subcube */ VCOPY(cukid.cuorg, cu->cuorg); @@ -318,45 +360,44 @@ register CUBE *cu; } for ( ; ; ) { cukid.cutree = octkid(cu->cutree, br); - if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT) + if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) return(RAYHIT); sgn = 1 << ax; - if (sgn & minus) /* negative axis? */ - if (sgn & br) { - cukid.cuorg[ax] -= cukid.cusize; - br &= ~sgn; - } else - return(ax); /* underflow */ - else + if (sgn & dirf) /* positive axis? */ if (sgn & br) return(ax); /* overflow */ else { cukid.cuorg[ax] += cukid.cusize; br |= sgn; } + else + if (sgn & br) { + cukid.cuorg[ax] -= cukid.cusize; + br &= ~sgn; + } else + return(ax); /* underflow */ } /*NOTREACHED*/ } if (isfull(cu->cutree) && checkhit(r, cu)) return(RAYHIT); /* advance to next cube */ - sgn = plus | minus; - if (sgn&1) { - dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; + if (dirf&0x11) { + dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; t = (dt - pos[0])/r->rdir[0]; ax = 0; } else t = FHUGE; - if (sgn&2) { - dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; + if (dirf&0x22) { + dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; dt = (dt - pos[1])/r->rdir[1]; if (dt < t) { t = dt; ax = 1; } } - if (sgn&4) { - dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; + if (dirf&0x44) { + dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; dt = (dt - pos[2])/r->rdir[2]; if (dt < t) { t = dt;