--- ray/src/rt/raytrace.c 2015/05/21 15:28:24 2.67 +++ ray/src/rt/raytrace.c 2021/01/31 20:55:04 2.84 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raytrace.c,v 2.67 2015/05/21 15:28:24 greg Exp $"; +static const char RCSid[] = "$Id: raytrace.c,v 2.84 2021/01/31 20:55:04 greg Exp $"; #endif /* * raytrace.c - routines for tracing and shading rays. @@ -51,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); } @@ -66,7 +68,7 @@ rayorigin( /* start new ray from old one */ r->gecc = seccg; r->slights = NULL; } else { /* spawned ray */ - if (ro->rot >= FHUGE) { + if (ro->rot >= FHUGE*.99) { memset(r, 0, sizeof(RAY)); return(-1); /* illegal continuation */ } @@ -108,12 +110,16 @@ rayorigin( /* start new ray from old one */ if (r->crtype & SHADOW) /* shadow commitment */ return(0); /* ambient in photon map? */ - if (r->crtype & AMBIENT && photonMapping) - return(-1); - if (maxdepth <= 0 && rc != NULL) { /* Russian roulette */ + 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); @@ -124,7 +130,7 @@ 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); } @@ -139,10 +145,15 @@ 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); } @@ -188,20 +199,41 @@ raytrans( /* transmit ray as is */ 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->rt = r->rot + tr.rt; + 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 */ +} + + +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 @@ -211,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() */ } @@ -300,6 +325,7 @@ raymixture( /* mix modifiers */ ) { RAY fr, br; + double mfore, mback; int foremat, backmat; int i; /* bound coefficient */ @@ -346,7 +372,14 @@ 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); } @@ -374,22 +407,23 @@ raycontrib( /* compute (cumulative) ray contribution 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]); } @@ -479,8 +513,39 @@ flipsurface( /* reverse surface orientation */ r->pert[0] = -r->pert[0]; r->pert[1] = -r->pert[1]; r->pert[2] = -r->pert[2]; + r->rflips++; } + +int +rayreject( /* check if candidate hit is worse than current */ + OBJREC *o, + RAY *r, + double t +) +{ + OBJREC *mnew, *mray; + + if ((t <= FTINY) | (t > r->rot + FTINY)) + return(1); + if (t < r->rot - FTINY) /* is new hit significantly closer? */ + return(0); + /* coincident point, so decide... */ + if (o == r->ro) + return(1); /* shouldn't happen */ + if (r->ro == NULL) + return(0); /* ditto */ + if ((mnew = findmaterial(o)) == NULL) + return(1); /* new has no material */ + if ((mray = findmaterial(r->ro)) == NULL) + return(0); /* old has no material(!) */ + if (istransp(mnew->otype)) + return(1); /* new is transparent */ + if (istransp(mray->otype)) + return(0); /* old is transparent */ + /* weakest priority to later modifier definition */ + return (r->ro->omod >= o->omod); +} void rayhit( /* standard ray hit test */