--- ray/src/rt/raytrace.c 2005/04/15 04:44:51 2.48 +++ ray/src/rt/raytrace.c 2021/01/04 19:07:15 2.82 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raytrace.c,v 2.48 2005/04/15 04:44:51 greg Exp $"; +static const char RCSid[] = "$Id: raytrace.c,v 2.82 2021/01/04 19:07:15 greg Exp $"; #endif /* * raytrace.c - routines for tracing and shading rays. @@ -13,16 +13,18 @@ static const char RCSid[] = "$Id: raytrace.c,v 2.48 20 #include "source.h" #include "otypes.h" #include "otspecial.h" +#include "random.h" +#include "pmap.h" #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 */ @@ -34,30 +36,42 @@ 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 */ - register RAY *r, - register RAY *ro, + RAY *r, int rt, - double rw + const RAY *ro, + const COLOR rc ) { - double re; - + double rw, re; + /* assign coefficient/weight */ + if (rc == NULL) { + rw = 1.0; + 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); + } 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 if (ro->rot >= FHUGE) { /* illegal continuation */ - memset(r, 0, sizeof(RAY)); - return(-1); } else { /* spawned ray */ + if (ro->rot >= FHUGE*.99) { + memset(r, 0, sizeof(RAY)); + return(-1); /* illegal continuation */ + } r->rlvl = ro->rlvl; if (rt & RAYREFL) { r->rlvl++; @@ -69,6 +83,7 @@ rayorigin( /* start new ray from old one */ 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; @@ -76,21 +91,52 @@ rayorigin( /* start new ray from old one */ 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 > 0.1) { + if (re > 92.) { + r->rweight = 0.0; + } else { + r->rweight *= exp(-re); + } + } } rayclear(r); - return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); + if (r->rweight <= 0.0) /* check for expiration */ + 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"); + if ((maxdepth < 0) & (r->rlvl > -maxdepth)) + return(-1); /* upper reflection limit */ + if (r->rweight >= minweight) + return(0); + if (frandom() > r->rweight/minweight) + return(-1); + rw = minweight/r->rweight; /* promote survivor */ + scalecolor(r->rcoef, rw); + r->rweight = minweight; + return(0); + } + 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++; @@ -99,16 +145,21 @@ 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; + VCOPY(r->rop, r->rorg); + r->ron[0] = -r->rdir[0]; r->ron[1] = -r->rdir[1]; r->ron[2] = -r->rdir[2]; + r->rod = 1.0; r->pert[0] = r->pert[1] = r->pert[2] = 0.0; + r->rflips = 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 -rayvalue( /* trace a ray and compute its value */ +void +raytrace( /* trace a ray and compute its value */ RAY *r ) { @@ -120,16 +171,16 @@ rayvalue( /* trace a ray and compute its value */ } 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 */ } -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)) || @@ -138,31 +189,51 @@ 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, r, TRANS, 1.0) == 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->mcol, tr.mcol); + copycolor(r->rcol, tr.rcol); + r->rmt = r->rot + tr.rmt; + r->rxt = r->rot + tr.rxt; +} + + +int +raytirrad( /* irradiance hack */ + OBJREC *m, + RAY *r +) +{ + if (ofun[m->otype].flags & (T_M|T_X) && m->otype != MAT_CLIP) { + if (istransp(m->otype) || isBSDFproxy(m)) { + raytrans(r); + return(1); + } + if (!islight(m->otype)) + return((*ofun[Lamb.otype].funp)(&Lamb, r)); } + return(0); /* not a qualifying surface */ } -extern int +int rayshade( /* shade ray r with material mod */ - register RAY *r, + RAY *r, int mod ) { - register OBJREC *m; + int tst_irrad = do_irrad && !(r->crtype & ~(PRIMARY|TRANS)); + OBJREC *m; - r->rt = r->rot; /* set effective ray length */ + r->rxt = r->rot; /* preset effective ray length */ for ( ; mod != OVOID; mod = m->omod) { m = objptr(mod); /****** unnecessary test since modifier() is always called @@ -172,16 +243,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)) { - raytrans(r); - return(1); - } - if (!islight(m->otype)) - m = &Lamb; - } + if (tst_irrad && raytirrad(m, r)) + return(1); + if ((*ofun[m->otype].funp)(m, r)) return(1); /* materials call raytexture() */ } @@ -189,9 +253,9 @@ rayshade( /* shade ray r with material mod */ } -extern void +void rayparticipate( /* compute ray medium participation */ - register RAY *r + RAY *r ) { COLOR ce, ca; @@ -207,28 +271,33 @@ rayparticipate( /* compute ray medium participation 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, - 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); @@ -247,17 +316,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; @@ -267,12 +337,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) @@ -296,15 +372,22 @@ 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; + scalecolor(fr.mcol, coef); + scalecolor(br.mcol, 1.0-coef); + copycolor(r->mcol, fr.mcol); + addcolor(r->mcol, br.mcol); + 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 RAY *r, - register int flags + const RAY *r, + int flags ) { double sum = 0.0; @@ -317,14 +400,41 @@ raydist( /* compute (cumulative) ray distance */ } -extern double +void +raycontrib( /* compute (cumulative) ray contribution */ + RREAL rc[3], + const RAY *r, + int flags +) +{ + static int warnedPM = 0; + + rc[0] = rc[1] = rc[2] = 1.; + + while (r != NULL && r->crtype&flags) { + int i = 3; + while (i--) + rc[i] *= colval(r->rcoef,i); + /* check for participating medium */ + if (!warnedPM && (bright(r->cext) > FTINY) | + (bright(r->albedo) > FTINY)) { + error(WARNING, + "ray contribution calculation does not support participating media"); + warnedPM++; + } + r = r->parent; + } +} + + +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 @@ -353,7 +463,7 @@ raynormal( /* compute perturbed normal for ray */ } -extern void +void newrayxf( /* get new tranformation matrix for ray */ RAY *r ) @@ -362,8 +472,8 @@ newrayxf( /* get new tranformation matrix for ray */ struct xfn *next; FULLXF xf; } xfseed = { &xfseed }, *xflast = &xfseed; - register struct xfn *xp; - register RAY *rp; + struct xfn *xp; + const RAY *rp; /* * Search for transform in circular list that @@ -374,7 +484,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"); @@ -391,9 +501,9 @@ newrayxf( /* get new tranformation matrix for ray */ } -extern void +void flipsurface( /* reverse surface orientation */ - register RAY *r + RAY *r ) { r->rod = -r->rod; @@ -403,10 +513,11 @@ flipsurface( /* reverse surface orientation */ r->pert[0] = -r->pert[0]; r->pert[1] = -r->pert[1]; r->pert[2] = -r->pert[2]; + r->rflips++; } -extern void +void rayhit( /* standard ray hit test */ OBJECT *oset, RAY *r @@ -423,17 +534,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; @@ -444,14 +555,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; @@ -474,8 +586,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); @@ -491,8 +602,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; @@ -500,7 +611,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); @@ -566,16 +677,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 ) @@ -596,12 +705,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;