--- ray/src/rt/raytrace.c 2010/09/26 15:51:15 2.61 +++ ray/src/rt/raytrace.c 2018/11/13 19:58:33 2.73 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raytrace.c,v 2.61 2010/09/26 15:51:15 greg Exp $"; +static const char RCSid[] = "$Id: raytrace.c,v 2.73 2018/11/13 19:58:33 greg Exp $"; #endif /* * raytrace.c - routines for tracing and shading rays. @@ -14,6 +14,7 @@ static const char RCSid[] = "$Id: raytrace.c,v 2.61 20 #include "otypes.h" #include "otspecial.h" #include "random.h" +#include "pmap.h" #define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ @@ -35,7 +36,7 @@ static int checkhit(RAY *r, CUBE *cu, OBJECT *cxs); static void checkset(OBJECT *os, OBJECT *cs); -extern int +int rayorigin( /* start new ray from old one */ RAY *r, int rt, @@ -50,6 +51,8 @@ rayorigin( /* start new ray from old one */ setcolor(r->rcoef, 1., 1., 1.); } else { rw = intens(rc); + if (rw > 1.0) + rw = 1.0; /* avoid calculation growth */ if (rc != r->rcoef) copycolor(r->rcoef, rc); } @@ -106,6 +109,13 @@ rayorigin( /* start new ray from old one */ return(-1); if (r->crtype & SHADOW) /* shadow commitment */ return(0); + /* ambient in photon map? */ + if (ro != NULL && ro->crtype & AMBIENT) { + if (causticPhotonMapping) + return(-1); + if (photonMapping && rt != TRANS) + return(-1); + } if (maxdepth <= 0 && rc != NULL) { /* Russian roulette */ if (minweight <= 0.0) error(USER, "zero ray weight in Russian roulette"); @@ -120,13 +130,13 @@ rayorigin( /* start new ray from old one */ r->rweight = minweight; return(0); } - return(r->rlvl <= abs(maxdepth) && r->rweight >= minweight ? 0 : -1); + return(r->rweight >= minweight && r->rlvl <= abs(maxdepth) ? 0 : -1); } -extern void +void rayclear( /* clear a ray for (re)evaluation */ - register RAY *r + RAY *r ) { r->rno = raynum++; @@ -135,15 +145,16 @@ rayclear( /* clear a ray for (re)evaluation */ r->robj = OVOID; r->ro = NULL; r->rox = NULL; - r->rt = r->rot = FHUGE; + r->rxt = r->rmt = 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->mcol, 0.0, 0.0, 0.0); setcolor(r->rcol, 0.0, 0.0, 0.0); } -extern void +void raytrace( /* trace a ray and compute its value */ RAY *r ) @@ -163,9 +174,9 @@ raytrace( /* trace a ray and compute its value */ } -extern void +void raycont( /* check for clipped object and continue */ - register RAY *r + RAY *r ) { if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || @@ -174,31 +185,31 @@ raycont( /* check for clipped object and continue */ } -extern void +void raytrans( /* transmit ray as is */ - register RAY *r + RAY *r ) { RAY tr; - if (rayorigin(&tr, TRANS, r, NULL) == 0) { - VCOPY(tr.rdir, r->rdir); - rayvalue(&tr); - copycolor(r->rcol, tr.rcol); - r->rt = r->rot + tr.rt; - } + rayorigin(&tr, TRANS, r, NULL); /* always continue */ + VCOPY(tr.rdir, r->rdir); + rayvalue(&tr); + copycolor(r->rcol, tr.rcol); + r->rmt = r->rot + tr.rmt; + r->rxt = r->rot + tr.rxt; } -extern int +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 */ + r->rxt = r->rmt = r->rot; /* preset effective ray length */ for ( ; mod != OVOID; mod = m->omod) { m = objptr(mod); /****** unnecessary test since modifier() is always called @@ -209,9 +220,9 @@ rayshade( /* shade ray r with material mod */ ******/ /* hack for irradiance calculation */ if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && - m->otype != MAT_CLIP && - (ofun[m->otype].flags & (T_M|T_X))) { - if (irr_ignore(m->otype)) { + (ofun[m->otype].flags & (T_M|T_X)) && + m->otype != MAT_CLIP) { + if (istransp(m->otype) || isBSDFproxy(m)) { raytrans(r); return(1); } @@ -225,9 +236,9 @@ rayshade( /* shade ray r with material mod */ } -extern void +void rayparticipate( /* compute ray medium participation */ - register RAY *r + RAY *r ) { COLOR ce, ca; @@ -249,22 +260,27 @@ rayparticipate( /* compute ray medium participation multcolor(r->rcol, ce); /* path extinction */ if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) return; /* no scattering */ - 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 */ + + /* PMAP: indirect inscattering accounted for by volume photons? */ + if (!volumePhotonMapping) { + 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 */ } -extern void +void raytexture( /* get material modifiers */ RAY *r, OBJECT mod ) { - register OBJREC *m; + OBJREC *m; /* execute textures and patterns */ for ( ; mod != OVOID; mod = m->omod) { m = objptr(mod); @@ -283,17 +299,18 @@ raytexture( /* get material modifiers */ } -extern int +int raymixture( /* mix modifiers */ - register RAY *r, + RAY *r, OBJECT fore, OBJECT back, double coef ) { RAY fr, br; + double mfore, mback; int foremat, backmat; - register int i; + int i; /* bound coefficient */ if (coef > 1.0) coef = 1.0; @@ -338,15 +355,18 @@ raymixture( /* mix modifiers */ scalecolor(br.rcol, 1.0-coef); copycolor(r->rcol, fr.rcol); addcolor(r->rcol, br.rcol); - r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; + mfore = bright(fr.mcol); mback = bright(br.mcol); + r->rmt = mfore > mback ? fr.rmt : br.rmt; + r->rxt = bright(fr.rcol)-mfore > bright(br.rcol)-mback ? + fr.rxt : br.rxt; return(1); } -extern double +double raydist( /* compute (cumulative) ray distance */ - register const RAY *r, - register int flags + const RAY *r, + int flags ) { double sum = 0.0; @@ -359,9 +379,9 @@ raydist( /* compute (cumulative) ray distance */ } -extern void +void raycontrib( /* compute (cumulative) ray contribution */ - double rc[3], + RREAL rc[3], const RAY *r, int flags ) @@ -385,14 +405,14 @@ raycontrib( /* compute (cumulative) ray contribution } -extern double +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 @@ -421,7 +441,7 @@ raynormal( /* compute perturbed normal for ray */ } -extern void +void newrayxf( /* get new tranformation matrix for ray */ RAY *r ) @@ -430,8 +450,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 @@ -442,7 +462,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"); @@ -459,9 +479,9 @@ newrayxf( /* get new tranformation matrix for ray */ } -extern void +void flipsurface( /* reverse surface orientation */ - register RAY *r + RAY *r ) { r->rod = -r->rod; @@ -474,7 +494,7 @@ flipsurface( /* reverse surface orientation */ } -extern void +void rayhit( /* standard ray hit test */ OBJECT *oset, RAY *r @@ -491,17 +511,17 @@ rayhit( /* standard ray hit test */ } -extern int +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; @@ -520,8 +540,7 @@ localhit( /* check for hit in the octree */ 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; @@ -544,8 +563,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); @@ -561,8 +579,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; @@ -570,7 +588,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); @@ -636,16 +654,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 ) @@ -666,12 +682,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;