--- ray/src/rt/raytrace.c 1994/01/12 20:42:34 2.10 +++ ray/src/rt/raytrace.c 1995/11/02 17:38:02 2.21 @@ -1,4 +1,4 @@ -/* Copyright (c) 1994 Regents of the University of California */ +/* Copyright (c) 1995 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -34,6 +34,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 */ @@ -67,6 +69,7 @@ double rw; 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); @@ -95,10 +98,13 @@ RAY *r; if (localhit(r, &thescene)) gotmat = raycont(r); - else if (sourcehit(r)) + else if (r->ro == &Aftplane) { + r->ro = NULL; + r->rot = FHUGE; + } else if (sourcehit(r)) gotmat = rayshade(r, r->ro->omod); - if (!gotmat) + if (r->ro != NULL && !gotmat) objerror(r->ro, USER, "material not found"); if (trace != NULL) @@ -125,6 +131,8 @@ 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; @@ -156,7 +164,7 @@ int mod; if (irr_ignore(m->otype)) { depth--; raytrans(r); - return; + return(1); } if (!islight(m->otype)) m = &Lamb; @@ -187,8 +195,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 */ } @@ -200,7 +211,6 @@ OBJECT fore, back; double coef; { RAY fr, br; - COLOR ctmp; int foremat, backmat; register int i; /* clip coefficient */ @@ -208,49 +218,63 @@ double coef; coef = 1.0; else if (coef < 0.0) coef = 0.0; + /* compute foreground and background */ + foremat = backmat = -1; /* 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) 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) backmat = rayshade(&br, back); - else - backmat = foremat; /* check */ - if (backmat != foremat) + if (foremat < 0) + if (backmat < 0) + foremat = backmat = 0; + else + foremat = backmat; + else if (backmat < 0) + backmat = foremat; + if ((foremat==0) != (backmat==0)) objerror(r->ro, USER, "mixing material with non-material"); - /* sum perturbations */ + /* 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 */ - scalecolor(fr.rcol, coef); - scalecolor(br.rcol, 1.0-coef); - addcolor(r->rcol, fr.rcol); - addcolor(r->rcol, br.rcol); - if (foremat) + copycolor(r->pcol, fr.pcol); + addcolor(r->pcol, br.pcol); + /* 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; + } /* return value tells if material */ return(foremat); } 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; @@ -355,6 +379,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 */ @@ -372,6 +404,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; @@ -380,7 +414,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); } @@ -435,7 +470,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) {