--- ray/src/rt/raytrace.c 1998/08/06 10:30:38 2.32 +++ ray/src/rt/raytrace.c 2004/03/30 16:13:01 2.45 @@ -1,40 +1,25 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: raytrace.c,v 2.45 2004/03/30 16:13:01 schorsch Exp $"; #endif - /* * raytrace.c - routines for tracing and shading rays. * - * 8/7/85 + * External symbols declared in ray.h */ -#include "ray.h" +#include "copyright.h" -#include "octree.h" - +#include "ray.h" +#include "source.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 */ -static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; +static RREAL Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; OBJREC Lamb = { OVOID, MAT_PLASTIC, "Lambertian", {0, 5, NULL, Lambfa}, NULL, @@ -42,17 +27,20 @@ OBJREC Lamb = { OBJREC Aftplane; /* aft clipping plane object */ -static int raymove(), checkset(), checkhit(); - -#define MAXLOOP 128 /* modifier loop detection */ - #define RAYHIT (-1) /* return value for intercepted ray */ +static int raymove(FVECT pos, OBJECT *cxs, int dirf, RAY *r, CUBE *cu); +static int checkhit(RAY *r, CUBE *cu, OBJECT *cxs); +static void checkset(OBJECT *os, OBJECT *cs); -rayorigin(r, ro, rt, rw) /* start new ray from old one */ -register RAY *r, *ro; -int rt; -double rw; + +extern int +rayorigin( /* start new ray from old one */ + register RAY *r, + register RAY *ro, + int rt, + double rw +) { double re; @@ -99,25 +87,30 @@ double rw; } -rayclear(r) /* clear a ray for (re)evaluation */ -register RAY *r; +extern void +rayclear( /* 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->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); } -raytrace(r) /* trace a ray and compute its value */ -RAY *r; +extern void +raytrace( /* 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) { @@ -133,8 +126,10 @@ RAY *r; } -raycont(r) /* check for clipped object and continue */ -register RAY *r; +extern void +raycont( /* check for clipped object and continue */ + register RAY *r +) { if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || !rayshade(r, r->ro->omod)) @@ -142,8 +137,10 @@ register RAY *r; } -raytrans(r) /* transmit ray as is */ -register RAY *r; +extern void +raytrans( /* transmit ray as is */ + register RAY *r +) { RAY tr; @@ -156,16 +153,14 @@ register RAY *r; } -rayshade(r, mod) /* shade ray r with material mod */ -register RAY *r; -int mod; +extern int +rayshade( /* shade ray r with material mod */ + register RAY *r, + int mod +) { - static int depth = 0; int gotmat; register OBJREC *m; - /* check for infinite loop */ - if (depth++ >= MAXLOOP) - objerror(r->ro, USER, "possible modifier loop"); r->rt = r->rot; /* set effective ray length */ for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { m = objptr(mod); @@ -176,9 +171,10 @@ int mod; } ******/ /* hack for irradiance calculation */ - if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { + if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && + m->otype != MAT_CLIP && + (ofun[m->otype].flags & (T_M|T_X))) { if (irr_ignore(m->otype)) { - depth--; raytrans(r); return(1); } @@ -188,13 +184,14 @@ int mod; /* materials call raytexture */ gotmat = (*ofun[m->otype].funp)(m, r); } - depth--; return(gotmat); } -rayparticipate(r) /* compute ray medium participation */ -register RAY *r; +extern void +rayparticipate( /* compute ray medium participation */ + register RAY *r +) { COLOR ce, ca; double re, ge, be; @@ -224,15 +221,13 @@ register RAY *r; } -raytexture(r, mod) /* get material modifiers */ -RAY *r; -int mod; +extern void +raytexture( /* get material modifiers */ + RAY *r, + OBJECT mod +) { - static int depth = 0; register OBJREC *m; - /* check for infinite loop */ - if (depth++ >= MAXLOOP) - objerror(r->ro, USER, "modifier loop"); /* execute textures and patterns */ for ( ; mod != OVOID; mod = m->omod) { m = objptr(mod); @@ -248,14 +243,16 @@ int mod; objerror(r->ro, USER, errmsg); } } - depth--; /* end here */ } -raymixture(r, fore, back, coef) /* mix modifiers */ -register RAY *r; -OBJECT fore, back; -double coef; +extern int +raymixture( /* mix modifiers */ + register RAY *r, + OBJECT fore, + OBJECT back, + double coef +) { RAY fr, br; int foremat, backmat; @@ -268,19 +265,20 @@ double coef; /* compute foreground and background */ foremat = backmat = 0; /* foreground */ - copystruct(&fr, r); + fr = *r; if (coef > FTINY) foremat = rayshade(&fr, fore); /* background */ - copystruct(&br, r); + br = *r; if (coef < 1.0-FTINY) backmat = rayshade(&br, back); /* check for transparency */ - if (backmat ^ foremat) - if (!foremat && coef > FTINY) + if (backmat ^ foremat) { + if (backmat && coef > FTINY) raytrans(&fr); - else if (!backmat && coef < 1.0-FTINY) + else if (foremat && coef < 1.0-FTINY) raytrans(&br); + } /* mix perturbations */ for (i = 0; i < 3; i++) r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; @@ -302,10 +300,11 @@ double coef; } -double -raydist(r, flags) /* compute (cumulative) ray distance */ -register RAY *r; -register int flags; +extern double +raydist( /* compute (cumulative) ray distance */ + register RAY *r, + register int flags +) { double sum = 0.0; @@ -317,10 +316,11 @@ register int flags; } -double -raynormal(norm, r) /* compute perturbed normal for ray */ -FVECT norm; -register RAY *r; +extern double +raynormal( /* compute perturbed normal for ray */ + FVECT norm, + register RAY *r +) { double newdot; register int i; @@ -352,8 +352,10 @@ register RAY *r; } -newrayxf(r) /* get new tranformation matrix for ray */ -RAY *r; +extern void +newrayxf( /* get new tranformation matrix for ray */ + RAY *r +) { static struct xfn { struct xfn *next; @@ -371,7 +373,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"); @@ -388,8 +390,10 @@ RAY *r; } -flipsurface(r) /* reverse surface orientation */ -register RAY *r; +extern void +flipsurface( /* reverse surface orientation */ + register RAY *r +) { r->rod = -r->rod; r->ron[0] = -r->ron[0]; @@ -401,10 +405,29 @@ register RAY *r; } -localhit(r, scene) /* check for hit in the octree */ -register RAY *r; -register CUBE *scene; +extern void +rayhit( /* 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]; + } +} + + +extern int +localhit( /* check for hit in the octree */ + register RAY *r, + register CUBE *scene +) +{ OBJECT cxset[MAXCSET+1]; /* set of checked objects */ FVECT curpos; /* current cube position */ int sflags; /* sign flags */ @@ -458,17 +481,18 @@ register CUBE *scene; } cxset[0] = 0; raymove(curpos, cxset, sflags, r, scene); - return(r->ro != NULL & r->ro != &Aftplane); + return((r->ro != NULL) & (r->ro != &Aftplane)); } static int -raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ -FVECT pos; /* current position, modified herein */ -OBJECT *cxs; /* checked objects, modified by checkhit */ -int dirf; /* direction indicators to speed tests */ -register RAY *r; -register CUBE *cu; +raymove( /* check for hit as we move */ + FVECT pos, /* current position, modified herein */ + OBJECT *cxs, /* checked objects, modified by checkhit */ + int dirf, /* direction indicators to speed tests */ + register RAY *r, + register CUBE *cu +) { int ax; double dt, t; @@ -548,34 +572,32 @@ register CUBE *cu; } -static -checkhit(r, cu, cxs) /* check for hit in full cube */ -register RAY *r; -CUBE *cu; -OBJECT *cxs; +static int +checkhit( /* 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 -checkset(os, cs) /* modify checked set and set to check */ -register OBJECT *os; /* os' = os - cs */ -register OBJECT *cs; /* cs' = cs + os */ +static void +checkset( /* modify checked set and set to check */ + register OBJECT *os, /* os' = os - cs */ + register OBJECT *cs /* cs' = cs + os */ +) { OBJECT cset[MAXCSET+MAXSET+1]; register int i, j;