--- ray/src/rt/raytrace.c 2018/01/09 05:01:15 2.72 +++ ray/src/rt/raytrace.c 2023/11/15 18:02:53 2.88 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raytrace.c,v 2.72 2018/01/09 05:01:15 greg Exp $"; +static const char RCSid[] = "$Id: raytrace.c,v 2.88 2023/11/15 18:02:53 greg Exp $"; #endif /* * raytrace.c - routines for tracing and shading rays. @@ -41,20 +41,20 @@ rayorigin( /* start new ray from old one */ RAY *r, int rt, const RAY *ro, - const COLOR rc + const SCOLOR rc ) { double rw, re; /* assign coefficient/weight */ if (rc == NULL) { rw = 1.0; - setcolor(r->rcoef, 1., 1., 1.); + setscolor(r->rcoef, 1., 1., 1.); } else { - rw = intens(rc); + rw = sintens(rc); if (rw > 1.0) rw = 1.0; /* avoid calculation growth */ if (rc != r->rcoef) - copycolor(r->rcoef, rc); + copyscolor(r->rcoef, rc); } if ((r->parent = ro) == NULL) { /* primary ray */ r->rlvl = 0; @@ -68,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 */ } @@ -116,21 +116,21 @@ rayorigin( /* start new ray from old one */ if (photonMapping && rt != TRANS) return(-1); } - if (maxdepth <= 0 && rc != NULL) { /* Russian roulette */ + 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); if (frandom() > r->rweight/minweight) return(-1); rw = minweight/r->rweight; /* promote survivor */ - scalecolor(r->rcoef, rw); + scalescolor(r->rcoef, rw); 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); } @@ -145,11 +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; + 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->rcol, 0.0, 0.0, 0.0); + setscolor(r->pcol, 1.0, 1.0, 1.0); + scolorblack(r->mcol); + scolorblack(r->rcol); } @@ -194,20 +199,43 @@ raytrans( /* transmit ray as is */ rayorigin(&tr, TRANS, r, NULL); /* always continue */ VCOPY(tr.rdir, r->rdir); rayvalue(&tr); - copycolor(r->rcol, tr.rcol); - r->rt = r->rot + tr.rt; + copyscolor(r->mcol, tr.mcol); + copyscolor(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)) { + setscolor(r->pcol, 1.0, 1.0, 1.0); + 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; /* preset 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 @@ -217,16 +245,9 @@ rayshade( /* shade ray r with material mod */ } ******/ /* hack for irradiance calculation */ - if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && - (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)) - m = &Lamb; - } + if (tst_irrad && raytirrad(m, r)) + return(1); + if ((*ofun[m->otype].funp)(m, r)) return(1); /* materials call raytexture() */ } @@ -239,7 +260,7 @@ rayparticipate( /* compute ray medium participation RAY *r ) { - COLOR ce, ca; + SCOLOR ce, ca; double re, ge, be; if (intens(r->cext) <= 1./FHUGE) @@ -252,20 +273,20 @@ rayparticipate( /* compute ray medium participation ge *= 1. - colval(r->albedo,GRN); be *= 1. - colval(r->albedo,BLU); } - setcolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re), + setscolor(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 */ + smultscolor(r->rcol, ce); /* path extinction */ if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) return; /* no scattering */ /* PMAP: indirect inscattering accounted for by volume photons? */ if (!volumePhotonMapping) { - setcolor(ca, + setscolor(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 */ + saddscolor(r->rcol, ca); /* ambient in scattering */ } srcscatter(r); /* source in scattering */ @@ -306,6 +327,7 @@ raymixture( /* mix modifiers */ ) { RAY fr, br; + double mfore, mback; int foremat, backmat; int i; /* bound coefficient */ @@ -319,14 +341,14 @@ raymixture( /* mix modifiers */ fr = *r; if (coef > FTINY) { fr.rweight *= coef; - scalecolor(fr.rcoef, coef); + scalescolor(fr.rcoef, coef); foremat = rayshade(&fr, fore); } /* background */ br = *r; if (coef < 1.0-FTINY) { br.rweight *= 1.0-coef; - scalecolor(br.rcoef, 1.0-coef); + scalescolor(br.rcoef, 1.0-coef); backmat = rayshade(&br, back); } /* check for transparency */ @@ -340,19 +362,26 @@ raymixture( /* mix modifiers */ for (i = 0; i < 3; i++) r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; /* mix pattern colors */ - scalecolor(fr.pcol, coef); - scalecolor(br.pcol, 1.0-coef); - copycolor(r->pcol, fr.pcol); - addcolor(r->pcol, br.pcol); + scalescolor(fr.pcol, coef); + scalescolor(br.pcol, 1.0-coef); + copyscolor(r->pcol, fr.pcol); + saddscolor(r->pcol, br.pcol); /* return value tells if material */ if (!foremat & !backmat) return(0); /* mix returned ray values */ - scalecolor(fr.rcol, coef); - 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; + scalescolor(fr.rcol, coef); + scalescolor(br.rcol, 1.0-coef); + copyscolor(r->rcol, fr.rcol); + saddscolor(r->rcol, br.rcol); + scalescolor(fr.mcol, coef); + scalescolor(br.mcol, 1.0-coef); + copyscolor(r->mcol, fr.mcol); + saddscolor(r->mcol, br.mcol); + mfore = pbright(fr.mcol); mback = pbright(br.mcol); + r->rmt = mfore > mback ? fr.rmt : br.rmt; + r->rxt = pbright(fr.rcol)-mfore > pbright(br.rcol)-mback ? + fr.rxt : br.rxt; return(1); } @@ -375,27 +404,26 @@ raydist( /* compute (cumulative) ray distance */ void raycontrib( /* compute (cumulative) ray contribution */ - RREAL rc[3], + SCOLOR rc, 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.; + setscolor(rc, 1., 1., 1.); while (r != NULL && r->crtype&flags) { - for (i = 3; i--; ) { - rc[i] *= colval(r->rcoef,i); - eext[i] += r->rot * colval(r->cext,i); + smultscolor(rc, r->rcoef); + /* 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]); } @@ -485,8 +513,51 @@ 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, + double rod +) +{ + 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 */ + mnew = findmaterial(o); + mray = findmaterial(r->ro); /* check material transparencies */ + if (mnew == NULL) { + if (mray != NULL) + return(1); /* old has material, new does not */ + } else if (mray == NULL) { + return(0); /* new has material, old does not */ + } else if (istransp(mnew->otype)) { + if (!istransp(mray->otype)) + return(1); /* new is transparent, old is not */ + } else if (istransp(mray->otype)) { + return(0); /* old is transparent, new is not */ + } + if (rod <= 0) { /* check which side we hit */ + if (r->rod > 0) + return(1); /* old hit front, new did not */ + } else if (r->rod <= 0) { + return(0); /* new hit front, old did not */ + } + /* earlier modifier definition wins tie */ + return (r->ro->omod >= o->omod); +} void rayhit( /* standard ray hit test */