--- ray/src/rt/raytrace.c 1993/01/25 15:16:59 2.2 +++ ray/src/rt/raytrace.c 1994/01/12 20:42:34 2.10 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1994 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -18,13 +18,15 @@ 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? */ -long raynum = 0L; /* next unique ray number */ -long nrays = 0L; /* number of calls to localhit */ +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 = { @@ -32,6 +34,8 @@ OBJREC Lamb = { {0, 5, NULL, Lambfa}, NULL, }; /* a Lambertian surface */ +static int raymove(), checkset(), checkhit(); + #define MAXLOOP 128 /* modifier loop detection */ #define RAYHIT (-1) /* return value for intercepted ray */ @@ -73,7 +77,6 @@ rayclear(r) /* clear a ray for (re)evaluation */ register RAY *r; { r->rno = raynum++; - r->cxs[0] = 0; r->newcset = r->clipset; r->ro = NULL; r->rot = FHUGE; @@ -88,12 +91,16 @@ raytrace(r) /* trace a ray and compute its value */ RAY *r; { extern int (*trace)(); + int gotmat; if (localhit(r, &thescene)) - raycont(r); + gotmat = raycont(r); else if (sourcehit(r)) - rayshade(r, r->ro->omod); + gotmat = rayshade(r, r->ro->omod); + if (!gotmat) + objerror(r->ro, USER, "material not found"); + if (trace != NULL) (*trace)(r); /* trace execution */ } @@ -102,10 +109,12 @@ 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)) || + r->ro->omod == OVOID) { raytrans(r); - else - rayshade(r, r->ro->omod); + return(1); + } + return(rayshade(r, r->ro->omod)); } @@ -128,12 +137,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"); r->rt = r->rot; /* set effective ray length */ - for ( ; mod != OVOID; mod = m->omod) { + for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { m = objptr(mod); /****** unnecessary test since modifier() is always called if (!ismodifier(m->otype)) { @@ -151,13 +161,11 @@ int mod; if (!islight(m->otype)) m = &Lamb; } - (*ofun[m->otype].funp)(m, r); /* execute function */ - 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); } @@ -173,11 +181,14 @@ 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); + ******/ + if ((*ofun[m->otype].funp)(m, r)) + objerror(r->ro, USER, "conflicting materials"); } depth--; /* end here */ } @@ -188,44 +199,54 @@ register RAY *r; OBJECT fore, back; double coef; { - FVECT curpert, forepert, backpert; - COLOR curpcol, forepcol, backpcol; + RAY fr, br; + COLOR ctmp; + int foremat, backmat; register int i; /* clip 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); + /* 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) - 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); + 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) - raytexture(r, back); - VCOPY(backpert, r->pert); - copycolor(backpcol, r->pcol); + backmat = rayshade(&br, back); + else + backmat = foremat; + /* check */ + if (backmat != foremat) + objerror(r->ro, USER, "mixing material with non-material"); /* sum 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]; + /* multiply 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 */ + scalecolor(fr.rcol, coef); + scalecolor(br.rcol, 1.0-coef); + addcolor(r->rcol, fr.rcol); + addcolor(r->rcol, br.rcol); + if (foremat) + r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; + /* return value tells if material */ + return(foremat); } @@ -317,6 +338,7 @@ 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; @@ -326,9 +348,9 @@ register CUBE *scene; 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) @@ -357,13 +379,15 @@ register CUBE *scene; if (!incube(scene, curpos)) /* non-intersecting ray */ return(0); } - return(raymove(curpos, sflags, r, scene) == RAYHIT); + cxset[0] = 0; + return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); } 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; @@ -392,7 +416,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? */ @@ -411,7 +435,7 @@ register CUBE *cu; } /*NOTREACHED*/ } - if (isfull(cu->cutree) && checkhit(r, cu)) + if (isfull(cu->cutree) && checkhit(r, cu, cxs)) return(RAYHIT); /* advance to next cube */ if (dirf&0x11) { @@ -444,16 +468,17 @@ 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, r->cxs); /* eliminate double-checking */ + checkset(oset, cxs); /* eliminate double-checking */ for (i = oset[0]; i > 0; i--) { o = objptr(oset[i]); (*ofun[o->otype].funp)(o, r); @@ -467,11 +492,12 @@ CUBE *cu; static checkset(os, cs) /* modify checked set and set to check */ -register OBJECT os[MAXSET+1]; /* os' = os - cs */ -register OBJECT cs[MAXCSET+1]; /* cs' = cs + os */ +register OBJECT *os; /* os' = os - cs */ +register OBJECT *cs; /* cs' = cs + os */ { OBJECT cset[MAXCSET+MAXSET+1]; - register int i, j, k; + register int i, j; + int k; /* copy os in place, cset <- cs */ cset[0] = 0; k = 0; @@ -483,10 +509,14 @@ register OBJECT cs[MAXCSET+1]; /* cs' = cs + os */ 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 if necessary */ + if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */ cset[0] = MAXCSET; - setcopy(cs, cset); /* copy new "checked" set back */ - os[0] = k; /* new "to check" set size */ + /* setcopy(cs, cset); */ /* copy cset back to cs */ + os = cset; + for (i = os[0]; i-- >= 0; ) + *cs++ = *os++; }