--- ray/src/rt/raytrace.c 2003/03/04 19:02:22 2.36 +++ ray/src/rt/raytrace.c 2005/04/19 01:15:06 2.49 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raytrace.c,v 2.36 2003/03/04 19:02:22 greg Exp $"; +static const char RCSid[] = "$Id: raytrace.c,v 2.49 2005/04/19 01:15:06 greg Exp $"; #endif /* * raytrace.c - routines for tracing and shading rays. @@ -10,9 +10,8 @@ static const char RCSid[] = "$Id: raytrace.c,v 2.36 20 #include "copyright.h" #include "ray.h" - +#include "source.h" #include "otypes.h" - #include "otspecial.h" #define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ @@ -20,7 +19,7 @@ static const char RCSid[] = "$Id: raytrace.c,v 2.36 20 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, @@ -28,36 +27,46 @@ OBJREC Lamb = { OBJREC Aftplane; /* aft clipping plane object */ -static int raymove(), checkhit(); -static void checkset(); - -#ifndef MAXLOOP -#define MAXLOOP 0 /* modifier loop detection */ -#endif - #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); -int -rayorigin(r, ro, rt, rw) /* start new ray from old one */ -register RAY *r, *ro; -int rt; -double rw; -{ - double re; +extern int +rayorigin( /* start new ray from old one */ + RAY *r, + int rt, + const RAY *ro, + const COLOR rc +) +{ + double rw, re; + /* assign coefficient/weight */ + if (rc == NULL) { + rw = 1.0; + setcolor(r->rcoef, 1., 1., 1.); + } else { + rw = intens(rc); + if (rc != r->rcoef) + copycolor(r->rcoef, rc); + } if ((r->parent = ro) == NULL) { /* primary ray */ r->rlvl = 0; r->rweight = rw; r->crtype = r->rtype = rt; r->rsrc = -1; r->clipset = NULL; - r->revf = raytrace; copycolor(r->cext, cextinction); copycolor(r->albedo, salbedo); r->gecc = seccg; r->slights = NULL; } else { /* spawned ray */ + if (ro->rot >= FHUGE) { + memset(r, 0, sizeof(RAY)); + return(-1); /* illegal continuation */ + } r->rlvl = ro->rlvl; if (rt & RAYREFL) { r->rlvl++; @@ -69,7 +78,6 @@ double rw; r->clipset = ro->newcset; r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot; } - r->revf = ro->revf; copycolor(r->cext, ro->cext); copycolor(r->albedo, ro->albedo); r->gecc = ro->gecc; @@ -77,21 +85,23 @@ double rw; r->crtype = ro->crtype | (r->rtype = rt); VCOPY(r->rorg, ro->rop); r->rweight = ro->rweight * rw; - /* estimate absorption */ + /* estimate extinction */ 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); + re *= ro->rot; + if (re > .1) + r->rweight *= exp(-re); } rayclear(r); return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); } -void -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; @@ -101,14 +111,16 @@ register RAY *r; 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); } -void -raytrace(r) /* trace a ray and compute its value */ -RAY *r; +extern void +rayvalue( /* trace a ray and compute its value */ + RAY *r +) { if (localhit(r, &thescene)) raycont(r); /* hit local surface, evaluate */ @@ -118,16 +130,17 @@ RAY *r; } else if (sourcehit(r)) rayshade(r, r->ro->omod); /* distant source */ - rayparticipate(r); /* for participating medium */ - if (trace != NULL) (*trace)(r); /* trace execution */ + + rayparticipate(r); /* for participating medium */ } -void -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)) @@ -135,13 +148,14 @@ register RAY *r; } -void -raytrans(r) /* transmit ray as is */ -register RAY *r; +extern void +raytrans( /* transmit ray as is */ + register RAY *r +) { RAY tr; - if (rayorigin(&tr, r, TRANS, 1.0) == 0) { + if (rayorigin(&tr, TRANS, r, NULL) == 0) { VCOPY(tr.rdir, r->rdir); rayvalue(&tr); copycolor(r->rcol, tr.rcol); @@ -150,21 +164,16 @@ register RAY *r; } -int -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 +) { - 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) { + for ( ; mod != OVOID; mod = m->omod) { m = objptr(mod); /****** unnecessary test since modifier() is always called if (!ismodifier(m->otype)) { @@ -173,30 +182,27 @@ 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)) { -#if MAXLOOP - depth--; -#endif raytrans(r); return(1); } if (!islight(m->otype)) m = &Lamb; } - /* materials call raytexture */ - gotmat = (*ofun[m->otype].funp)(m, r); + if ((*ofun[m->otype].funp)(m, r)) + return(1); /* materials call raytexture() */ } -#if MAXLOOP - depth--; -#endif - return(gotmat); + return(0); /* no material! */ } -void -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; @@ -211,10 +217,10 @@ register RAY *r; 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 */ + setcolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re), + ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge), + be<=FTINY ? 1. : be>92. ? 0. : exp(-be)); + multcolor(r->rcol, ce); /* path extinction */ if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) return; /* no scattering */ setcolor(ca, @@ -226,17 +232,13 @@ register RAY *r; } -raytexture(r, mod) /* get material modifiers */ -RAY *r; -int mod; +extern void +raytexture( /* get material modifiers */ + RAY *r, + OBJECT mod +) { 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); @@ -252,17 +254,16 @@ 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; -double coef; +extern int +raymixture( /* mix modifiers */ + register RAY *r, + OBJECT fore, + OBJECT back, + double coef +) { RAY fr, br; int foremat, backmat; @@ -275,19 +276,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 (backmat ^ foremat) { if (backmat && coef > FTINY) raytrans(&fr); 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]; @@ -309,10 +311,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 const RAY *r, + register int flags +) { double sum = 0.0; @@ -324,11 +327,40 @@ register int flags; } -double -raynormal(norm, r) /* compute perturbed normal for ray */ -FVECT norm; -register RAY *r; +extern void +raycontrib( /* compute (cumulative) ray contribution */ + COLOR rc, + const RAY *r, + int flags +) { + COLOR eext, ext1; + + setcolor(eext, 0., 0., 0.); + setcolor(rc, 1., 1., 1.); + + while (r != NULL && r->crtype&flags) { + multcolor(rc, r->rcoef); + copycolor(ext1, r->cext); + scalecolor(ext1, r->rot); + addcolor(eext, ext1); + r = r->parent; + } + if (intens(eext) > FTINY) { + setcolor(ext1, exp(-colval(eext,RED)), + exp(-colval(eext,GRN)), + exp(-colval(eext,BLU))); + multcolor(rc, ext1); + } +} + + +extern double +raynormal( /* compute perturbed normal for ray */ + FVECT norm, + register RAY *r +) +{ double newdot; register int i; @@ -359,16 +391,17 @@ register RAY *r; } -void -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; FULLXF xf; } xfseed = { &xfseed }, *xflast = &xfseed; register struct xfn *xp; - register RAY *rp; + register const RAY *rp; /* * Search for transform in circular list that @@ -396,9 +429,10 @@ RAY *r; } -void -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]; @@ -410,10 +444,11 @@ register RAY *r; } -void -rayhit(oset, r) /* standard ray hit test */ -OBJECT *oset; -RAY *r; +extern void +rayhit( /* standard ray hit test */ + OBJECT *oset, + RAY *r +) { OBJREC *o; int i; @@ -426,10 +461,11 @@ RAY *r; } -int -localhit(r, scene) /* check for hit in the octree */ -register RAY *r; -register CUBE *scene; +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 */ @@ -484,17 +520,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; @@ -575,10 +612,11 @@ register CUBE *cu; static int -checkhit(r, cu, cxs) /* check for hit in full cube */ -register RAY *r; -CUBE *cu; -OBJECT *cxs; +checkhit( /* check for hit in full cube */ + register RAY *r, + CUBE *cu, + OBJECT *cxs +) { OBJECT oset[MAXSET+1]; @@ -595,9 +633,10 @@ OBJECT *cxs; static void -checkset(os, cs) /* modify checked set and set to check */ -register OBJECT *os; /* os' = os - cs */ -register OBJECT *cs; /* cs' = cs + os */ +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;