--- ray/src/rt/raytrace.c 1997/03/07 16:58:54 2.28 +++ ray/src/rt/raytrace.c 2003/03/11 19:29:05 2.38 @@ -1,36 +1,22 @@ -/* Copyright (c) 1996 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: raytrace.c,v 2.38 2003/03/11 19:29:05 greg Exp $"; #endif - /* * raytrace.c - routines for tracing and shading rays. * - * 8/7/85 + * External symbols declared in ray.h */ +#include "copyright.h" + #include "ray.h" -#include "octree.h" - #include "otypes.h" #include "otspecial.h" #define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ -extern CUBE thescene; /* our scene */ -extern int maxdepth; /* maximum recursion depth */ -extern double minweight; /* minimum ray weight */ -extern int do_irrad; /* compute irradiance? */ -extern COLOR ambval; /* ambient value */ - -extern COLOR cextinction; /* global extinction coefficient */ -extern COLOR salbedo; /* global scattering albedo */ -extern double seccg; /* global scattering eccentricity */ -extern double ssampdist; /* scatter sampling distance */ - unsigned long raynum = 0; /* next unique ray number */ unsigned long nrays = 0; /* number of calls to localhit */ @@ -42,18 +28,24 @@ OBJREC Lamb = { OBJREC Aftplane; /* aft clipping plane object */ -static int raymove(), checkset(), checkhit(); +static int raymove(), checkhit(); +static void checkset(); -#define MAXLOOP 128 /* modifier loop detection */ +#ifndef MAXLOOP +#define MAXLOOP 0 /* modifier loop detection */ +#endif #define RAYHIT (-1) /* return value for intercepted ray */ +int rayorigin(r, ro, rt, rw) /* start new ray from old one */ register RAY *r, *ro; int rt; double rw; { + double re; + if ((r->parent = ro) == NULL) { /* primary ray */ r->rlvl = 0; r->rweight = rw; @@ -82,35 +74,43 @@ double rw; copycolor(r->albedo, ro->albedo); r->gecc = ro->gecc; r->slights = ro->slights; - r->rweight = ro->rweight * rw; r->crtype = ro->crtype | (r->rtype = rt); VCOPY(r->rorg, ro->rop); + r->rweight = ro->rweight * rw; + /* estimate absorption */ + re = colval(ro->cext,RED) < colval(ro->cext,GRN) ? + colval(ro->cext,RED) : colval(ro->cext,GRN); + if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); + if (re > 0.) + r->rweight *= exp(-re*ro->rot); } rayclear(r); return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); } +void rayclear(r) /* clear a ray for (re)evaluation */ register RAY *r; { r->rno = raynum++; r->newcset = r->clipset; + r->hitf = rayhit; r->robj = OVOID; r->ro = NULL; - r->rot = FHUGE; + r->rox = NULL; + r->rt = r->rot = FHUGE; r->pert[0] = r->pert[1] = r->pert[2] = 0.0; + r->uv[0] = r->uv[1] = 0.0; setcolor(r->pcol, 1.0, 1.0, 1.0); setcolor(r->rcol, 0.0, 0.0, 0.0); - r->rt = 0.0; } +void raytrace(r) /* trace a ray and compute its value */ RAY *r; { - extern int (*trace)(); - if (localhit(r, &thescene)) raycont(r); /* hit local surface, evaluate */ else if (r->ro == &Aftplane) { @@ -126,6 +126,7 @@ RAY *r; } +void raycont(r) /* check for clipped object and continue */ register RAY *r; { @@ -135,6 +136,7 @@ register RAY *r; } +void raytrans(r) /* transmit ray as is */ register RAY *r; { @@ -149,16 +151,19 @@ register RAY *r; } +int rayshade(r, mod) /* shade ray r with material mod */ register RAY *r; int mod; { - static int depth = 0; int gotmat; register OBJREC *m; +#if MAXLOOP + static int depth = 0; /* check for infinite loop */ if (depth++ >= MAXLOOP) objerror(r->ro, USER, "possible modifier loop"); +#endif r->rt = r->rot; /* set effective ray length */ for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { m = objptr(mod); @@ -169,9 +174,12 @@ int mod; } ******/ /* hack for irradiance calculation */ - if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { + if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && + (ofun[m->otype].flags & (T_M|T_X))) { if (irr_ignore(m->otype)) { +#if MAXLOOP depth--; +#endif raytrans(r); return(1); } @@ -181,11 +189,14 @@ int mod; /* materials call raytexture */ gotmat = (*ofun[m->otype].funp)(m, r); } +#if MAXLOOP depth--; +#endif return(gotmat); } +void rayparticipate(r) /* compute ray medium participation */ register RAY *r; { @@ -221,11 +232,13 @@ raytexture(r, mod) /* get material modifiers */ RAY *r; int mod; { - static int depth = 0; register OBJREC *m; +#if MAXLOOP + static int depth = 0; /* check for infinite loop */ if (depth++ >= MAXLOOP) objerror(r->ro, USER, "modifier loop"); +#endif /* execute textures and patterns */ for ( ; mod != OVOID; mod = m->omod) { m = objptr(mod); @@ -241,10 +254,13 @@ int mod; objerror(r->ro, USER, errmsg); } } +#if MAXLOOP depth--; /* end here */ +#endif } +int raymixture(r, fore, back, coef) /* mix modifiers */ register RAY *r; OBJECT fore, back; @@ -270,9 +286,9 @@ double coef; backmat = rayshade(&br, back); /* check for transparency */ if (backmat ^ foremat) - if (backmat) + if (backmat && coef > FTINY) raytrans(&fr); - else + else if (foremat && coef < 1.0-FTINY) raytrans(&br); /* mix perturbations */ for (i = 0; i < 3; i++) @@ -345,6 +361,7 @@ register RAY *r; } +void newrayxf(r) /* get new tranformation matrix for ray */ RAY *r; { @@ -364,7 +381,7 @@ RAY *r; 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)); + xp = (struct xfn *)malloc(sizeof(struct xfn)); if (xp == NULL) error(SYSTEM, "out of memory in newrayxf"); @@ -381,6 +398,7 @@ RAY *r; } +void flipsurface(r) /* reverse surface orientation */ register RAY *r; { @@ -394,6 +412,23 @@ register RAY *r; } +void +rayhit(oset, r) /* standard ray hit test */ +OBJECT *oset; +RAY *r; +{ + OBJREC *o; + int i; + + for (i = oset[0]; i > 0; i--) { + o = objptr(oset[i]); + if ((*ofun[o->otype].funp)(o, r)) + r->robj = oset[i]; + } +} + + +int localhit(r, scene) /* check for hit in the octree */ register RAY *r; register CUBE *scene; @@ -541,31 +576,27 @@ register CUBE *cu; } -static +static int checkhit(r, cu, cxs) /* check for hit in full cube */ register RAY *r; CUBE *cu; OBJECT *cxs; { OBJECT oset[MAXSET+1]; - register OBJREC *o; - register int i; objset(oset, cu->cutree); - checkset(oset, cxs); /* eliminate double-checking */ - for (i = oset[0]; i > 0; i--) { - o = objptr(oset[i]); - if ((*ofun[o->otype].funp)(o, r)) - r->robj = oset[i]; - } - if (r->ro == NULL) + checkset(oset, cxs); /* avoid double-checking */ + + (*r->hitf)(oset, r); /* test for hit in set */ + + if (r->robj == OVOID) return(0); /* no scores yet */ return(incube(cu, r->rop)); /* hit OK if in current cube */ } -static +static void checkset(os, cs) /* modify checked set and set to check */ register OBJECT *os; /* os' = os - cs */ register OBJECT *cs; /* cs' = cs + os */