--- ray/src/rt/raytrace.c 1993/07/21 15:36:13 2.7 +++ ray/src/rt/raytrace.c 1994/12/20 20:18:22 2.16 @@ -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"; @@ -34,6 +34,8 @@ OBJREC Lamb = { {0, 5, NULL, Lambfa}, NULL, }; /* a Lambertian surface */ +static 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); @@ -78,8 +81,16 @@ register RAY *r; { r->rno = raynum++; r->newcset = r->clipset; - r->ro = NULL; - r->rot = FHUGE; + if (r->rmax > FTINY) { + r->ro = &Aftplane; + r->rot = r->rmax; + r->rop[0] = r->rorg[0] + r->rot*r->rdir[0]; + r->rop[1] = r->rorg[1] + r->rot*r->rdir[1]; + r->rop[2] = r->rorg[2] + r->rot*r->rdir[2]; + } else { + r->ro = NULL; + r->rot = FHUGE; + } r->pert[0] = r->pert[1] = r->pert[2] = 0.0; setcolor(r->pcol, 1.0, 1.0, 1.0); setcolor(r->rcol, 0.0, 0.0, 0.0); @@ -91,12 +102,19 @@ raytrace(r) /* trace a ray and compute its value */ RAY *r; { extern int (*trace)(); + int gotmat; if (localhit(r, &thescene)) - raycont(r); - else if (sourcehit(r)) - rayshade(r, r->ro->omod); + gotmat = raycont(r); + else if (r->ro == &Aftplane) { + r->ro = NULL; + r->rot = FHUGE; + } else if (sourcehit(r)) + gotmat = rayshade(r, r->ro->omod); + if (r->ro != NULL && !gotmat) + objerror(r->ro, USER, "material not found"); + if (trace != NULL) (*trace)(r); /* trace execution */ } @@ -106,10 +124,11 @@ 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) + r->ro->omod == OVOID) { raytrans(r); - else - rayshade(r, r->ro->omod); + return(1); + } + return(rayshade(r, r->ro->omod)); } @@ -132,12 +151,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)) { @@ -150,18 +170,16 @@ 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 */ - 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); } @@ -177,11 +195,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 */ } @@ -192,44 +213,52 @@ 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 */ 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); + /* compute foreground and background */ + foremat = backmat = -1; + /* foreground */ + copystruct(&fr, r); 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); + /* background */ + copystruct(&br, r); if (back != OVOID && coef < 1.0-FTINY) - raytexture(r, back); - VCOPY(backpert, r->pert); - copycolor(backpcol, r->pcol); - /* sum perturbations */ + backmat = rayshade(&br, back); + /* check */ + 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"); + /* 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); + /* 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); } @@ -331,9 +360,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) @@ -363,7 +392,8 @@ register CUBE *scene; return(0); } cxset[0] = 0; - return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); + return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT && + r->ro != &Aftplane); }