--- ray/src/rt/raytrace.c 1991/05/29 11:09:23 1.18 +++ ray/src/rt/raytrace.c 1996/03/21 15:33:05 2.24 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1995 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -18,19 +18,32 @@ static char SCCSid[] = "$SunId$ LBL"; #include "otspecial.h" +#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ + 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 */ -long nrays = 0L; /* number of rays traced */ +extern COLOR cextinction; /* global extinction coefficient */ +extern double salbedo; /* global scattering albedo */ +extern double seccg; /* global scattering eccentricity */ +extern double ssampdist; /* scatter sampling distance */ -static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; +unsigned long raynum = 0; /* next unique ray number */ +unsigned long nrays = 0; /* number of calls to localhit */ + +static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; OBJREC Lamb = { OVOID, MAT_PLASTIC, "Lambertian", - {0, 5, NULL, Lambfa}, NULL, -1, + {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 */ #define RAYHIT (-1) /* return value for intercepted ray */ @@ -47,21 +60,41 @@ double rw; r->crtype = r->rtype = rt; r->rsrc = -1; r->clipset = NULL; + r->revf = raytrace; + copycolor(r->cext, cextinction); + 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); + 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->rno = nrays; + rayclear(r); + return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); +} + + +rayclear(r) /* clear a ray for (re)evaluation */ +register RAY *r; +{ + r->rno = raynum++; r->newcset = r->clipset; r->ro = NULL; r->rot = FHUGE; @@ -69,20 +102,24 @@ double rw; setcolor(r->pcol, 1.0, 1.0, 1.0); setcolor(r->rcol, 0.0, 0.0, 0.0); r->rt = 0.0; - return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); } -rayvalue(r) /* compute a ray's value */ +raytrace(r) /* trace a ray and compute its value */ RAY *r; { extern int (*trace)(); if (localhit(r, &thescene)) - raycont(r); - else if (sourcehit(r)) - 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 */ + rayparticipate(r); /* for participating medium */ + if (trace != NULL) (*trace)(r); /* trace execution */ } @@ -91,10 +128,9 @@ RAY *r; raycont(r) /* check for clipped object and continue */ register RAY *r; { - if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) + if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || + !rayshade(r, r->ro->omod)) raytrans(r); - else - rayshade(r, r->ro->omod); } @@ -117,11 +153,13 @@ register RAY *r; int mod; { static int depth = 0; + int gotmat; register OBJREC *m; /* check for infinite loop */ if (depth++ >= MAXLOOP) objerror(r->ro, USER, "possible modifier loop"); - for ( ; mod != OVOID; mod = m->omod) { + r->rt = r->rot; /* set effective ray length */ + for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { m = objptr(mod); /****** unnecessary test since modifier() is always called if (!ismodifier(m->otype)) { @@ -134,22 +172,51 @@ int mod; if (irr_ignore(m->otype)) { depth--; raytrans(r); - return; + return(1); } if (!islight(m->otype)) m = &Lamb; } - (*ofun[m->otype].funp)(m, r); /* execute function */ - m->lastrno = r->rno; - if (ismaterial(m->otype)) { /* materials call raytexture */ - depth--; - return; /* we're done */ - } + /* materials call raytexture */ + gotmat = (*ofun[m->otype].funp)(m, r); } - objerror(r->ro, USER, "material not found"); + depth--; + return(gotmat); } +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? */ + if (r->crtype & SHADOW) + dist *= 1. - salbedo; /* no scattering for sources */ + if (dist <= FTINY) + return; /* no effective ray travel */ + re = dist*colval(r->cext,RED); + ge = dist*colval(r->cext,GRN); + be = dist*colval(r->cext,BLU); + setcolor(ce, re>92. ? 0. : exp(-re), + ge>92. ? 0. : exp(-ge), + be>92. ? 0. : exp(-be)); + multcolor(r->rcol, ce); /* path absorption */ + if (r->albedo <= FTINY || r->crtype & SHADOW) + return; /* no scattering */ + setcolor(ca, salbedo*colval(ambval,RED)*(1.-colval(ce,RED)), + salbedo*colval(ambval,GRN)*(1.-colval(ce,GRN)), + salbedo*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; @@ -162,12 +229,17 @@ int mod; /* execute textures and patterns */ for ( ; mod != OVOID; mod = m->omod) { m = objptr(mod); - if (!istexture(m->otype)) { + /****** unnecessary test since modifier() is always called + if (!ismodifier(m->otype)) { sprintf(errmsg, "illegal modifier \"%s\"", m->oname); error(USER, errmsg); } - (*ofun[m->otype].funp)(m, r); - m->lastrno = r->rno; + ******/ + if ((*ofun[m->otype].funp)(m, r)) { + sprintf(errmsg, "conflicting material \"%s\"", + m->oname); + objerror(r->ro, USER, errmsg); + } } depth--; /* end here */ } @@ -178,48 +250,67 @@ register RAY *r; OBJECT fore, back; double coef; { - FVECT curpert, forepert, backpert; - COLOR curpcol, forepcol, backpcol; + 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; - /* save current mods */ - VCOPY(curpert, r->pert); - copycolor(curpcol, r->pcol); - /* compute new mods */ - /* foreground */ - r->pert[0] = r->pert[1] = r->pert[2] = 0.0; - setcolor(r->pcol, 1.0, 1.0, 1.0); - if (fore != OVOID && coef > FTINY) - raytexture(r, fore); - VCOPY(forepert, r->pert); - copycolor(forepcol, r->pcol); - /* background */ - r->pert[0] = r->pert[1] = r->pert[2] = 0.0; - setcolor(r->pcol, 1.0, 1.0, 1.0); - if (back != OVOID && coef < 1.0-FTINY) - raytexture(r, back); - VCOPY(backpert, r->pert); - copycolor(backpcol, r->pcol); - /* sum perturbations */ + /* compute foreground and background */ + foremat = backmat = 0; + /* foreground */ + copystruct(&fr, r); + if (coef > FTINY) + foremat = rayshade(&fr, fore); + /* background */ + copystruct(&br, r); + if (coef < 1.0-FTINY) + backmat = rayshade(&br, back); + /* check for transparency */ + if (backmat ^ foremat) + if (backmat) + raytrans(&fr); + else + raytrans(&br); + /* mix perturbations */ for (i = 0; i < 3; i++) - r->pert[i] = curpert[i] + coef*forepert[i] + - (1.0-coef)*backpert[i]; - /* multiply colors */ - setcolor(r->pcol, coef*colval(forepcol,RED) + - (1.0-coef)*colval(backpcol,RED), - coef*colval(forepcol,GRN) + - (1.0-coef)*colval(backpcol,GRN), - coef*colval(forepcol,BLU) + - (1.0-coef)*colval(backpcol,BLU)); - multcolor(r->pcol, curpcol); + 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); + /* 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; + 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; @@ -307,23 +398,31 @@ localhit(r, scene) /* check for hit in the octree */ register RAY *r; register CUBE *scene; { + OBJECT cxset[MAXCSET+1]; /* set of checked objects */ FVECT curpos; /* current cube position */ int sflags; /* sign flags */ double t, dt; register int i; nrays++; /* increment trace counter */ - sflags = 0; for (i = 0; i < 3; i++) { curpos[i] = r->rorg[i]; - if (r->rdir[i] > FTINY) + if (r->rdir[i] > 1e-7) sflags |= 1 << i; - else if (r->rdir[i] < -FTINY) + else if (r->rdir[i] < -1e-7) sflags |= 0x10 << i; } 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 */ @@ -341,6 +440,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; @@ -348,13 +449,16 @@ register CUBE *scene; if (!incube(scene, curpos)) /* non-intersecting ray */ return(0); } - return(raymove(curpos, sflags, r, scene) == RAYHIT); + cxset[0] = 0; + raymove(curpos, cxset, sflags, r, scene); + return(r->ro != NULL & r->ro != &Aftplane); } static int -raymove(pos, dirf, r, cu) /* check for hit as we move */ -FVECT pos; /* modified */ +raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ +FVECT pos; /* current position, modified herein */ +OBJECT *cxs; /* checked objects, modified by checkhit */ int dirf; /* direction indicators to speed tests */ register RAY *r; register CUBE *cu; @@ -383,7 +487,7 @@ register CUBE *cu; } for ( ; ; ) { cukid.cutree = octkid(cu->cutree, br); - if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) + if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) return(RAYHIT); sgn = 1 << ax; if (sgn & dirf) /* positive axis? */ @@ -402,7 +506,10 @@ register CUBE *cu; } /*NOTREACHED*/ } - if (isfull(cu->cutree) && checkhit(r, cu)) + 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) { @@ -435,24 +542,55 @@ register CUBE *cu; static -checkhit(r, cu) /* check for hit in full cube */ +checkhit(r, cu, cxs) /* check for hit in full cube */ register RAY *r; CUBE *cu; +OBJECT *cxs; { OBJECT oset[MAXSET+1]; register OBJREC *o; register int i; objset(oset, cu->cutree); + checkset(oset, cxs); /* eliminate double-checking */ for (i = oset[0]; i > 0; i--) { o = objptr(oset[i]); - if (o->lastrno == r->rno) /* checked already? */ - continue; (*ofun[o->otype].funp)(o, r); - o->lastrno = r->rno; } if (r->ro == NULL) return(0); /* no scores yet */ return(incube(cu, r->rop)); /* hit OK if in current cube */ +} + + +static +checkset(os, cs) /* modify checked set and set to check */ +register OBJECT *os; /* os' = os - cs */ +register OBJECT *cs; /* cs' = cs + os */ +{ + OBJECT cset[MAXCSET+MAXSET+1]; + register int i, j; + int k; + /* copy os in place, cset <- cs */ + cset[0] = 0; + k = 0; + for (i = j = 1; i <= os[0]; i++) { + while (j <= cs[0] && cs[j] < os[i]) + cset[++cset[0]] = cs[j++]; + if (j > cs[0] || os[i] != cs[j]) { /* object to check */ + os[++k] = os[i]; + cset[++cset[0]] = os[i]; + } + } + if (!(os[0] = k)) /* new "to check" set size */ + return; /* special case */ + while (j <= cs[0]) /* get the rest of cs */ + cset[++cset[0]] = cs[j++]; + if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */ + cset[0] = MAXCSET; + /* setcopy(cs, cset); */ /* copy cset back to cs */ + os = cset; + for (i = os[0]; i-- >= 0; ) + *cs++ = *os++; }