--- ray/src/rt/raytrace.c 1996/03/30 11:18:36 2.25 +++ ray/src/rt/raytrace.c 2003/02/25 02:47:23 2.35 @@ -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.35 2003/02/25 02:47:23 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 double 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; @@ -62,7 +54,7 @@ double rw; r->clipset = NULL; r->revf = raytrace; copycolor(r->cext, cextinction); - r->albedo = salbedo; + copycolor(r->albedo, salbedo); r->gecc = seccg; r->slights = NULL; } else { /* spawned ray */ @@ -79,37 +71,44 @@ double rw; } r->revf = ro->revf; copycolor(r->cext, ro->cext); - r->albedo = ro->albedo; + 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->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; 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) { @@ -125,6 +124,7 @@ RAY *r; } +void raycont(r) /* check for clipped object and continue */ register RAY *r; { @@ -134,6 +134,7 @@ register RAY *r; } +void raytrans(r) /* transmit ray as is */ register RAY *r; { @@ -148,16 +149,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); @@ -170,7 +174,9 @@ int mod; /* hack for irradiance calculation */ if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { if (irr_ignore(m->otype)) { +#if MAXLOOP depth--; +#endif raytrans(r); return(1); } @@ -180,38 +186,40 @@ 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; { COLOR ce, ca; - double dist; double re, ge, be; if (intens(r->cext) <= 1./FHUGE) return; /* no medium */ - if ((dist = r->rot) >= FHUGE) - dist = 2.*thescene.cusize; /* what to use for infinity? */ - if (r->crtype & SHADOW) - dist *= 1. - r->albedo; /* no scattering for sources */ - if (dist <= FTINY) - return; /* no effective ray travel */ - re = dist*colval(r->cext,RED); - ge = dist*colval(r->cext,GRN); - be = dist*colval(r->cext,BLU); - setcolor(ce, re>92. ? 0. : exp(-re), - ge>92. ? 0. : exp(-ge), - be>92. ? 0. : exp(-be)); + re = r->rot*colval(r->cext,RED); + ge = r->rot*colval(r->cext,GRN); + be = r->rot*colval(r->cext,BLU); + if (r->crtype & SHADOW) { /* no scattering for sources */ + re *= 1. - colval(r->albedo,RED); + ge *= 1. - colval(r->albedo,GRN); + be *= 1. - colval(r->albedo,BLU); + } + setcolor(ce, re<=0. ? 1. : re>92. ? 0. : exp(-re), + ge<=0. ? 1. : ge>92. ? 0. : exp(-ge), + be<=0. ? 1. : be>92. ? 0. : exp(-be)); multcolor(r->rcol, ce); /* path absorption */ - if (r->albedo <= FTINY || r->crtype & SHADOW) + if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) return; /* no scattering */ - setcolor(ca, r->albedo*colval(ambval,RED)*(1.-colval(ce,RED)), - r->albedo*colval(ambval,GRN)*(1.-colval(ce,GRN)), - r->albedo*colval(ambval,BLU)*(1.-colval(ce,BLU))); + setcolor(ca, + colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)), + colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)), + colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU))); addcolor(r->rcol, ca); /* ambient in scattering */ srcscatter(r); /* source in scattering */ } @@ -221,11 +229,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 +251,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 +283,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 +358,7 @@ register RAY *r; } +void newrayxf(r) /* get new tranformation matrix for ray */ RAY *r; { @@ -364,7 +378,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 +395,7 @@ RAY *r; } +void flipsurface(r) /* reverse surface orientation */ register RAY *r; { @@ -394,6 +409,7 @@ register RAY *r; } +int localhit(r, scene) /* check for hit in the octree */ register RAY *r; register CUBE *scene; @@ -541,7 +557,7 @@ register CUBE *cu; } -static +static int checkhit(r, cu, cxs) /* check for hit in full cube */ register RAY *r; CUBE *cu; @@ -555,7 +571,8 @@ OBJECT *cxs; checkset(oset, cxs); /* eliminate double-checking */ for (i = oset[0]; i > 0; i--) { o = objptr(oset[i]); - (*ofun[o->otype].funp)(o, r); + if ((*ofun[o->otype].funp)(o, r)) + r->robj = oset[i]; } if (r->ro == NULL) return(0); /* no scores yet */ @@ -564,7 +581,7 @@ OBJECT *cxs; } -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 */