--- ray/src/rt/raytrace.c 1994/01/12 16:52:08 2.9 +++ ray/src/rt/raytrace.c 1996/04/17 14:06:35 2.26 @@ -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 */ @@ -34,6 +40,8 @@ OBJREC Lamb = { {0, 5, NULL, Lambfa}, NULL, }; /* a Lambertian surface */ +OBJREC Aftplane; /* aft clipping plane object */ + static int raymove(), checkset(), checkhit(); #define MAXLOOP 128 /* modifier loop detection */ @@ -53,17 +61,27 @@ 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); @@ -91,15 +109,16 @@ raytrace(r) /* trace a ray and compute its value */ RAY *r; { extern int (*trace)(); - int gotmat; if (localhit(r, &thescene)) - gotmat = raycont(r); - else if (sourcehit(r)) - gotmat = rayshade(r, r->ro->omod); + raycont(r); /* hit local surface, evaluate */ + else if (r->ro == &Aftplane) { + r->ro = NULL; /* hit aft clipping plane */ + r->rot = FHUGE; + } else if (sourcehit(r)) + rayshade(r, r->ro->omod); /* distant source */ - if (!gotmat) - objerror(r->ro, USER, "material not found"); + rayparticipate(r); /* for participating medium */ if (trace != NULL) (*trace)(r); /* trace execution */ @@ -110,11 +129,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)); } @@ -156,7 +172,7 @@ int mod; if (irr_ignore(m->otype)) { depth--; raytrans(r); - return; + return(1); } if (!islight(m->otype)) m = &Lamb; @@ -169,6 +185,40 @@ int mod; } +rayparticipate(r) /* compute ray medium participation */ +register RAY *r; +{ + COLOR ce, ca; + double dist; + double re, ge, be; + + if (intens(r->cext) <= 1./FHUGE) + return; /* no medium */ + if ((dist = r->rot) >= FHUGE) + dist = 2.*thescene.cusize; /* what to use for infinity? */ + re = dist*colval(r->cext,RED); + ge = dist*colval(r->cext,GRN); + be = dist*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; @@ -187,8 +237,11 @@ int mod; error(USER, errmsg); } ******/ - if ((*ofun[m->otype].funp)(m, r)) - objerror(USER, r->ro, "conflicting materials"); + if ((*ofun[m->otype].funp)(m, r)) { + sprintf(errmsg, "conflicting material \"%s\"", + m->oname); + objerror(r->ro, USER, errmsg); + } } depth--; /* end here */ } @@ -200,55 +253,66 @@ OBJECT fore, back; double coef; { RAY fr, br; - COLOR ctmp; 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 = 0; /* foreground */ copystruct(&fr, r); - fr.pert[0] = fr.pert[1] = fr.pert[2] = 0.0; - setcolor(fr.pcol, 1.0, 1.0, 1.0); - setcolor(fr.rcol, 0.0, 0.0, 0.0); - if (fore != OVOID && coef > FTINY) + if (coef > FTINY) foremat = rayshade(&fr, fore); - else - foremat = 0; /* background */ copystruct(&br, r); - br.pert[0] = br.pert[1] = br.pert[2] = 0.0; - setcolor(br.pcol, 1.0, 1.0, 1.0); - setcolor(br.rcol, 0.0, 0.0, 0.0); - if (back != OVOID && coef < 1.0-FTINY) + if (coef < 1.0-FTINY) backmat = rayshade(&br, back); - else - backmat = foremat; - /* check */ - if (backmat != foremat) - objerror(USER, r->ro, "mixing material with non-material"); - /* sum perturbations */ + /* check for transparency */ + if (backmat ^ foremat) + if (backmat) + raytrans(&fr); + else + raytrans(&br); + /* mix perturbations */ for (i = 0; i < 3; i++) - r->pert[i] += coef*fr.pert[i] + (1.0-coef)*br.pert[i]; - /* multiply pattern colors */ + 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(ctmp, fr.pcol); - addcolor(ctmp, br.pcol); - multcolor(r->pcol, ctmp); - /* sum returned ray values */ + copycolor(r->pcol, fr.pcol); + addcolor(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); - addcolor(r->rcol, fr.rcol); + copycolor(r->rcol, fr.rcol); addcolor(r->rcol, br.rcol); - /* return value tells if material */ - return(foremat); + 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(sum); +} + + +double raynormal(norm, r) /* compute perturbed normal for ray */ FVECT norm; register RAY *r; @@ -353,6 +417,14 @@ register CUBE *scene; } if (sflags == 0) error(CONSISTENCY, "zero ray direction in localhit"); + /* 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]; + } + /* find global cube entrance point */ t = 0.0; if (!incube(scene, curpos)) { /* find distance to entry */ @@ -370,6 +442,8 @@ register CUBE *scene; t = dt; /* farthest face is the one */ } t += FTINY; /* fudge to get inside cube */ + if (t >= r->rot) /* clipped already */ + return(0); /* advance position */ for (i = 0; i < 3; i++) curpos[i] += r->rdir[i]*t; @@ -378,7 +452,8 @@ register CUBE *scene; return(0); } cxset[0] = 0; - return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); + raymove(curpos, cxset, sflags, r, scene); + return(r->ro != NULL & r->ro != &Aftplane); } @@ -433,7 +508,10 @@ register CUBE *cu; } /*NOTREACHED*/ } - if (isfull(cu->cutree) && checkhit(r, cu, cxs)) + if (isfull(cu->cutree)) { + if (checkhit(r, cu, cxs)) + return(RAYHIT); + } else if (r->ro == &Aftplane && incube(cu, r->rop)) return(RAYHIT); /* advance to next cube */ if (dirf&0x11) {