--- ray/src/rt/raytrace.c 2012/06/01 19:17:17 2.64 +++ ray/src/rt/raytrace.c 2019/07/25 16:50:54 2.79 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raytrace.c,v 2.64 2012/06/01 19:17:17 greg Exp $"; +static const char RCSid[] = "$Id: raytrace.c,v 2.79 2019/07/25 16:50:54 greg Exp $"; #endif /* * raytrace.c - routines for tracing and shading rays. @@ -14,6 +14,7 @@ static const char RCSid[] = "$Id: raytrace.c,v 2.64 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,10 +109,17 @@ rayorigin( /* start new ray from old one */ return(-1); if (r->crtype & SHADOW) /* shadow commitment */ return(0); - if (maxdepth <= 0 && rc != NULL) { /* Russian roulette */ + /* 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) + if ((maxdepth < 0) & (r->rlvl > -maxdepth)) return(-1); /* upper reflection limit */ if (r->rweight >= minweight) return(0); @@ -120,11 +130,11 @@ rayorigin( /* start new ray from old one */ r->rweight = minweight; return(0); } - return(r->rweight >= minweight && r->rlvl <= abs(maxdepth) ? 0 : -1); + return((r->rweight >= minweight) & (r->rlvl <= abs(maxdepth)) ? 0 : -1); } -extern void +void rayclear( /* clear a ray for (re)evaluation */ RAY *r ) @@ -135,15 +145,17 @@ 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->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 +void raytrace( /* trace a ray and compute its value */ RAY *r ) @@ -163,7 +175,7 @@ raytrace( /* trace a ray and compute its value */ } -extern void +void raycont( /* check for clipped object and continue */ RAY *r ) @@ -174,31 +186,51 @@ raycont( /* check for clipped object and continue */ } -extern void +void raytrans( /* transmit ray as is */ 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->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 */ RAY *r, int mod ) { + 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 @@ -208,16 +240,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() */ } @@ -225,7 +250,7 @@ rayshade( /* shade ray r with material mod */ } -extern void +void rayparticipate( /* compute ray medium participation */ RAY *r ) @@ -249,16 +274,21 @@ 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 @@ -283,7 +313,7 @@ raytexture( /* get material modifiers */ } -extern int +int raymixture( /* mix modifiers */ RAY *r, OBJECT fore, @@ -292,6 +322,7 @@ raymixture( /* mix modifiers */ ) { RAY fr, br; + double mfore, mback; int foremat, backmat; int i; /* bound coefficient */ @@ -338,12 +369,19 @@ 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 */ const RAY *r, int flags @@ -359,33 +397,34 @@ raydist( /* compute (cumulative) ray distance */ } -extern void +void raycontrib( /* compute (cumulative) ray contribution */ RREAL rc[3], const RAY *r, int flags ) { - double eext[3]; - int i; + static int warnedPM = 0; - eext[0] = eext[1] = eext[2] = 0.; rc[0] = rc[1] = rc[2] = 1.; while (r != NULL && r->crtype&flags) { - for (i = 3; i--; ) { + int i = 3; + while (i--) rc[i] *= colval(r->rcoef,i); - eext[i] += r->rot * colval(r->cext,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; } - for (i = 3; i--; ) - rc[i] *= (eext[i] <= FTINY) ? 1. : - (eext[i] > 92.) ? 0. : exp(-eext[i]); } -extern double +double raynormal( /* compute perturbed normal for ray */ FVECT norm, RAY *r @@ -421,7 +460,7 @@ raynormal( /* compute perturbed normal for ray */ } -extern void +void newrayxf( /* get new tranformation matrix for ray */ RAY *r ) @@ -459,7 +498,7 @@ newrayxf( /* get new tranformation matrix for ray */ } -extern void +void flipsurface( /* reverse surface orientation */ RAY *r ) @@ -471,10 +510,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 @@ -491,7 +531,7 @@ rayhit( /* standard ray hit test */ } -extern int +int localhit( /* check for hit in the octree */ RAY *r, CUBE *scene