--- ray/src/rt/raytrace.c 1995/01/06 14:00:47 2.18 +++ ray/src/rt/raytrace.c 1997/03/07 16:58:54 2.28 @@ -1,4 +1,4 @@ -/* Copyright (c) 1994 Regents of the University of California */ +/* Copyright (c) 1996 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -24,7 +24,13 @@ extern CUBE thescene; /* our scene */ extern int maxdepth; /* maximum recursion depth */ extern double minweight; /* minimum ray weight */ extern int do_irrad; /* compute irradiance? */ +extern COLOR ambval; /* ambient value */ +extern COLOR cextinction; /* global extinction coefficient */ +extern COLOR salbedo; /* global scattering albedo */ +extern double seccg; /* global scattering eccentricity */ +extern double ssampdist; /* scatter sampling distance */ + unsigned long raynum = 0; /* next unique ray number */ unsigned long nrays = 0; /* number of calls to localhit */ @@ -55,21 +61,30 @@ double rw; r->rsrc = -1; r->clipset = NULL; r->revf = raytrace; + copycolor(r->cext, cextinction); + copycolor(r->albedo, salbedo); + r->gecc = seccg; + r->slights = NULL; } else { /* spawned ray */ r->rlvl = ro->rlvl; if (rt & RAYREFL) { r->rlvl++; r->rsrc = -1; r->clipset = ro->clipset; + r->rmax = 0.0; } else { r->rsrc = ro->rsrc; 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; + r->slights = ro->slights; r->rweight = ro->rweight * rw; r->crtype = ro->crtype | (r->rtype = rt); VCOPY(r->rorg, ro->rop); - r->rmax = 0.0; } rayclear(r); return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); @@ -81,6 +96,7 @@ register RAY *r; { r->rno = raynum++; r->newcset = r->clipset; + r->robj = OVOID; r->ro = NULL; r->rot = FHUGE; r->pert[0] = r->pert[1] = r->pert[2] = 0.0; @@ -94,18 +110,16 @@ raytrace(r) /* trace a ray and compute its value */ RAY *r; { extern int (*trace)(); - int gotmat; if (localhit(r, &thescene)) - gotmat = raycont(r); + raycont(r); /* hit local surface, evaluate */ else if (r->ro == &Aftplane) { - r->ro = NULL; + r->ro = NULL; /* hit aft clipping plane */ r->rot = FHUGE; } else if (sourcehit(r)) - gotmat = rayshade(r, r->ro->omod); + rayshade(r, r->ro->omod); /* distant source */ - if (r->ro != NULL && !gotmat) - objerror(r->ro, USER, "material not found"); + rayparticipate(r); /* for participating medium */ if (trace != NULL) (*trace)(r); /* trace execution */ @@ -116,11 +130,8 @@ raycont(r) /* check for clipped object and continue register RAY *r; { if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || - r->ro->omod == OVOID) { + !rayshade(r, r->ro->omod)) raytrans(r); - return(1); - } - return(rayshade(r, r->ro->omod)); } @@ -131,8 +142,6 @@ register RAY *r; if (rayorigin(&tr, r, TRANS, 1.0) == 0) { VCOPY(tr.rdir, r->rdir); - if (r->rmax > FTINY) - tr.rmax = r->rmax - r->rot; rayvalue(&tr); copycolor(r->rcol, tr.rcol); r->rt = r->rot + tr.rt; @@ -177,6 +186,37 @@ int mod; } +rayparticipate(r) /* compute ray medium participation */ +register RAY *r; +{ + COLOR ce, ca; + double re, ge, be; + + if (intens(r->cext) <= 1./FHUGE) + return; /* no medium */ + re = r->rot*colval(r->cext,RED); + ge = r->rot*colval(r->cext,GRN); + be = r->rot*colval(r->cext,BLU); + if (r->crtype & SHADOW) { /* no scattering for sources */ + re *= 1. - colval(r->albedo,RED); + 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 */ + 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 */ + srcscatter(r); /* source in scattering */ +} + + raytexture(r, mod) /* get material modifiers */ RAY *r; int mod; @@ -195,8 +235,11 @@ int mod; error(USER, errmsg); } ******/ - if ((*ofun[m->otype].funp)(m, r)) - objerror(r->ro, USER, "conflicting materials"); + if ((*ofun[m->otype].funp)(m, r)) { + sprintf(errmsg, "conflicting material \"%s\"", + m->oname); + objerror(r->ro, USER, errmsg); + } } depth--; /* end here */ } @@ -210,31 +253,27 @@ double coef; RAY fr, br; int foremat, backmat; register int i; - /* clip coefficient */ + /* bound coefficient */ if (coef > 1.0) coef = 1.0; else if (coef < 0.0) coef = 0.0; /* compute foreground and background */ - foremat = backmat = -1; + foremat = backmat = 0; /* foreground */ copystruct(&fr, r); - if (fore != OVOID && coef > FTINY) + if (coef > FTINY) foremat = rayshade(&fr, fore); /* background */ copystruct(&br, r); - if (back != OVOID && coef < 1.0-FTINY) + if (coef < 1.0-FTINY) backmat = rayshade(&br, back); - /* check */ - if (foremat < 0) - if (backmat < 0) - foremat = backmat = 0; + /* check for transparency */ + if (backmat ^ foremat) + if (backmat) + raytrans(&fr); else - foremat = backmat; - else if (backmat < 0) - backmat = foremat; - if ((foremat==0) != (backmat==0)) - objerror(r->ro, USER, "mixing material with non-material"); + raytrans(&br); /* mix perturbations */ for (i = 0; i < 3; i++) r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; @@ -243,16 +282,31 @@ double coef; scalecolor(br.pcol, 1.0-coef); copycolor(r->pcol, fr.pcol); addcolor(r->pcol, br.pcol); + /* return value tells if material */ + if (!foremat & !backmat) + return(0); /* mix returned ray values */ - if (foremat) { - 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; + 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; + return(1); +} + + +double +raydist(r, flags) /* compute (cumulative) ray distance */ +register RAY *r; +register int flags; +{ + double sum = 0.0; + + while (r != NULL && r->crtype&flags) { + sum += r->rot; + r = r->parent; } - /* return value tells if material */ - return(foremat); + return(sum); } @@ -396,8 +450,8 @@ register CUBE *scene; return(0); } cxset[0] = 0; - return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT && - r->ro != &Aftplane); + raymove(curpos, cxset, sflags, r, scene); + return(r->ro != NULL & r->ro != &Aftplane); } @@ -501,7 +555,8 @@ OBJECT *cxs; checkset(oset, cxs); /* eliminate double-checking */ for (i = oset[0]; i > 0; i--) { o = objptr(oset[i]); - (*ofun[o->otype].funp)(o, r); + if ((*ofun[o->otype].funp)(o, r)) + r->robj = oset[i]; } if (r->ro == NULL) return(0); /* no scores yet */