--- ray/src/rt/raytrace.c 1990/08/18 04:25:59 1.11 +++ ray/src/rt/raytrace.c 1991/06/17 08:26:18 1.20 @@ -1,4 +1,4 @@ -/* Copyright (c) 1986 Regents of the University of California */ +/* Copyright (c) 1991 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -16,12 +16,22 @@ static char SCCSid[] = "$SunId$ LBL"; #include "otypes.h" +#include "otspecial.h" + extern CUBE thescene; /* our scene */ 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 rayvalue */ +static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; +OBJREC Lamb = { + OVOID, MAT_PLASTIC, "Lambertian", + {0, 5, NULL, Lambfa}, NULL, -1, +}; /* a Lambertian surface */ + #define MAXLOOP 128 /* modifier loop detection */ #define RAYHIT (-1) /* return value for intercepted ray */ @@ -52,7 +62,7 @@ double rw; r->crtype = ro->crtype | (r->rtype = rt); VCOPY(r->rorg, ro->rop); } - r->rno = nrays; + r->rno = raynum++; r->newcset = r->clipset; r->ro = NULL; r->rot = FHUGE; @@ -69,8 +79,11 @@ RAY *r; { extern int (*trace)(); - if (localhit(r, &thescene) || sourcehit(r)) + nrays++; /* increment trace counter */ + if (localhit(r, &thescene)) raycont(r); + else if (sourcehit(r)) + rayshade(r, r->ro->omod); if (trace != NULL) (*trace)(r); /* trace execution */ @@ -110,6 +123,7 @@ int mod; /* 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 @@ -118,6 +132,16 @@ 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 */ @@ -233,6 +257,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 *)bmalloc(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; { @@ -255,8 +315,6 @@ register CUBE *scene; double t, dt; register int i; - nrays++; /* increment trace counter */ - sflags = 0; for (i = 0; i < 3; i++) { curpos[i] = r->rorg[i]; @@ -265,6 +323,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 */