--- ray/src/rt/raytrace.c 2005/06/23 09:11:38 2.53 +++ ray/src/rt/raytrace.c 2012/06/01 19:17:17 2.64 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raytrace.c,v 2.53 2005/06/23 09:11:38 greg Exp $"; +static const char RCSid[] = "$Id: raytrace.c,v 2.64 2012/06/01 19:17:17 greg Exp $"; #endif /* * raytrace.c - routines for tracing and shading rays. @@ -17,13 +17,13 @@ static const char RCSid[] = "$Id: raytrace.c,v 2.53 20 #define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ -unsigned long raynum = 0; /* next unique ray number */ -unsigned long nrays = 0; /* number of calls to localhit */ +RNUMBER raynum = 0; /* next unique ray number */ +RNUMBER nrays = 0; /* number of calls to localhit */ static RREAL Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; OBJREC Lamb = { OVOID, MAT_PLASTIC, "Lambertian", - {0, 5, NULL, Lambfa}, NULL, + {NULL, Lambfa, 0, 5}, NULL }; /* a Lambertian surface */ OBJREC Aftplane; /* aft clipping plane object */ @@ -93,11 +93,13 @@ rayorigin( /* start new ray from old one */ colval(ro->cext,RED) : colval(ro->cext,GRN); if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); re *= ro->rot; - if (re > 0.1) - if (re > 92.) + if (re > 0.1) { + if (re > 92.) { r->rweight = 0.0; - else + } else { r->rweight *= exp(-re); + } + } } rayclear(r); if (r->rweight <= 0.0) /* check for expiration */ @@ -111,20 +113,20 @@ rayorigin( /* start new ray from old one */ return(-1); /* upper reflection limit */ if (r->rweight >= minweight) return(0); - if (frandom() < r->rweight/minweight) + if (frandom() > r->rweight/minweight) return(-1); rw = minweight/r->rweight; /* promote survivor */ scalecolor(r->rcoef, rw); r->rweight = minweight; return(0); } - return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); + return(r->rweight >= minweight && r->rlvl <= abs(maxdepth) ? 0 : -1); } extern void rayclear( /* clear a ray for (re)evaluation */ - register RAY *r + RAY *r ) { r->rno = raynum++; @@ -163,7 +165,7 @@ raytrace( /* trace a ray and compute its value */ extern void raycont( /* check for clipped object and continue */ - register RAY *r + RAY *r ) { if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || @@ -174,7 +176,7 @@ raycont( /* check for clipped object and continue */ extern void raytrans( /* transmit ray as is */ - register RAY *r + RAY *r ) { RAY tr; @@ -190,11 +192,11 @@ raytrans( /* transmit ray as is */ extern int rayshade( /* shade ray r with material mod */ - register RAY *r, + RAY *r, int mod ) { - register OBJREC *m; + OBJREC *m; r->rt = r->rot; /* set effective ray length */ for ( ; mod != OVOID; mod = m->omod) { @@ -225,7 +227,7 @@ rayshade( /* shade ray r with material mod */ extern void rayparticipate( /* compute ray medium participation */ - register RAY *r + RAY *r ) { COLOR ce, ca; @@ -262,7 +264,7 @@ raytexture( /* get material modifiers */ OBJECT mod ) { - register OBJREC *m; + OBJREC *m; /* execute textures and patterns */ for ( ; mod != OVOID; mod = m->omod) { m = objptr(mod); @@ -283,7 +285,7 @@ raytexture( /* get material modifiers */ extern int raymixture( /* mix modifiers */ - register RAY *r, + RAY *r, OBJECT fore, OBJECT back, double coef @@ -291,7 +293,7 @@ raymixture( /* mix modifiers */ { RAY fr, br; int foremat, backmat; - register int i; + int i; /* bound coefficient */ if (coef > 1.0) coef = 1.0; @@ -301,12 +303,18 @@ raymixture( /* mix modifiers */ foremat = backmat = 0; /* foreground */ fr = *r; - if (coef > FTINY) + if (coef > FTINY) { + fr.rweight *= coef; + scalecolor(fr.rcoef, coef); foremat = rayshade(&fr, fore); + } /* background */ br = *r; - if (coef < 1.0-FTINY) + if (coef < 1.0-FTINY) { + br.rweight *= 1.0-coef; + scalecolor(br.rcoef, 1.0-coef); backmat = rayshade(&br, back); + } /* check for transparency */ if (backmat ^ foremat) { if (backmat && coef > FTINY) @@ -337,8 +345,8 @@ raymixture( /* mix modifiers */ extern double raydist( /* compute (cumulative) ray distance */ - register const RAY *r, - register int flags + const RAY *r, + int flags ) { double sum = 0.0; @@ -353,7 +361,7 @@ raydist( /* compute (cumulative) ray distance */ extern void raycontrib( /* compute (cumulative) ray contribution */ - double rc[3], + RREAL rc[3], const RAY *r, int flags ) @@ -380,11 +388,11 @@ raycontrib( /* compute (cumulative) ray contribution extern double raynormal( /* compute perturbed normal for ray */ FVECT norm, - register RAY *r + RAY *r ) { double newdot; - register int i; + int i; /* The perturbation is added to the surface normal to obtain * the new normal. If the new normal would affect the surface @@ -422,8 +430,8 @@ newrayxf( /* get new tranformation matrix for ray */ struct xfn *next; FULLXF xf; } xfseed = { &xfseed }, *xflast = &xfseed; - register struct xfn *xp; - register const RAY *rp; + struct xfn *xp; + const RAY *rp; /* * Search for transform in circular list that @@ -434,7 +442,7 @@ newrayxf( /* get new tranformation matrix for ray */ if (rp->rox == &xp->xf) { /* xp in use */ xp = xp->next; /* move to next */ if (xp == xflast) { /* need new one */ - xp = (struct xfn *)malloc(sizeof(struct xfn)); + xp = (struct xfn *)bmalloc(sizeof(struct xfn)); if (xp == NULL) error(SYSTEM, "out of memory in newrayxf"); @@ -453,7 +461,7 @@ newrayxf( /* get new tranformation matrix for ray */ extern void flipsurface( /* reverse surface orientation */ - register RAY *r + RAY *r ) { r->rod = -r->rod; @@ -485,15 +493,15 @@ rayhit( /* standard ray hit test */ extern int localhit( /* check for hit in the octree */ - register RAY *r, - register CUBE *scene + RAY *r, + CUBE *scene ) { OBJECT cxset[MAXCSET+1]; /* set of checked objects */ FVECT curpos; /* current cube position */ int sflags; /* sign flags */ double t, dt; - register int i; + int i; nrays++; /* increment trace counter */ sflags = 0; @@ -504,14 +512,15 @@ localhit( /* check for hit in the octree */ else if (r->rdir[i] < -1e-7) sflags |= 0x10 << i; } - if (sflags == 0) - error(CONSISTENCY, "zero ray direction in localhit"); + if (!sflags) { + error(WARNING, "zero ray direction in localhit"); + return(0); + } /* start off assuming nothing hit */ if (r->rmax > FTINY) { /* except aft plane if one */ r->ro = &Aftplane; r->rot = r->rmax; - for (i = 0; i < 3; i++) - r->rop[i] = r->rorg[i] + r->rot*r->rdir[i]; + VSUM(r->rop, r->rorg, r->rdir, r->rot); } /* find global cube entrance point */ t = 0.0; @@ -534,8 +543,7 @@ localhit( /* check for hit in the octree */ if (t >= r->rot) /* clipped already */ return(0); /* advance position */ - for (i = 0; i < 3; i++) - curpos[i] += r->rdir[i]*t; + VSUM(curpos, curpos, r->rdir, t); if (!incube(scene, curpos)) /* non-intersecting ray */ return(0); @@ -551,8 +559,8 @@ 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 + RAY *r, + CUBE *cu ) { int ax; @@ -560,7 +568,7 @@ raymove( /* check for hit as we move */ if (istree(cu->cutree)) { /* recurse on subcubes */ CUBE cukid; - register int br, sgn; + int br, sgn; cukid.cusize = cu->cusize * 0.5; /* find subcube */ VCOPY(cukid.cuorg, cu->cuorg); @@ -626,16 +634,14 @@ raymove( /* check for hit as we move */ ax = 2; } } - pos[0] += r->rdir[0]*t; - pos[1] += r->rdir[1]*t; - pos[2] += r->rdir[2]*t; + VSUM(pos, pos, r->rdir, t); return(ax); } static int checkhit( /* check for hit in full cube */ - register RAY *r, + RAY *r, CUBE *cu, OBJECT *cxs ) @@ -656,12 +662,12 @@ checkhit( /* check for hit in full cube */ static void checkset( /* modify checked set and set to check */ - register OBJECT *os, /* os' = os - cs */ - register OBJECT *cs /* cs' = cs + os */ + OBJECT *os, /* os' = os - cs */ + OBJECT *cs /* cs' = cs + os */ ) { OBJECT cset[MAXCSET+MAXSET+1]; - register int i, j; + int i, j; int k; /* copy os in place, cset <- cs */ cset[0] = 0;