--- ray/src/rt/m_clip.c 1991/11/12 17:08:41 2.1 +++ ray/src/rt/m_clip.c 2017/04/08 00:09:35 2.12 @@ -1,37 +1,49 @@ -/* Copyright (c) 1986 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: m_clip.c,v 2.12 2017/04/08 00:09:35 greg Exp $"; #endif - /* * m_clip.c - routine for clipped (cut) objects. - * - * 3/17/86 */ +#include "copyright.h" + #include "ray.h" +#include "rtotypes.h" /* * Clipping objects permit holes and sections to be taken out - * of other objects. The method is simple: + * of other objects. * * The argument is the clipped materials; * the first is used to shade upon exit. + * + * In the simple case of the first argument being "void", we + * just add or subtract (depending on whether we're coming or going) + * the list of modifiers to the ray's "newcset", which will then + * take over for "clipset" on penetration. Any surface modifier + * names found in "clipset" will be treated as invisible in raycont(). + * + * In the more complicated case of a non-void material as the + * first argument, we have to backtrack up the ray tree to count + * the number of times we've penetrated the front side of one of + * the surfaces we care about. This relies on outward-facing + * surface normals and closed objects, so is somewhat error-prone. */ -m_clip(m, r) /* clip objects from ray */ -register OBJREC *m; -register RAY *r; +int +m_clip( /* clip objects from ray */ + OBJREC *m, + RAY *r +) { OBJECT cset[MAXSET+1], *modset; + OBJECT obj, mod; int entering; - register int i; + int i; + obj = objndx(m); if ((modset = (OBJECT *)m->os) == NULL) { - register OBJECT mod; - if (m->oargs.nsargs < 1 || m->oargs.nsargs > MAXSET) objerror(m, USER, "bad # arguments"); modset = (OBJECT *)malloc((m->oargs.nsargs+1)*sizeof(OBJECT)); @@ -41,7 +53,7 @@ register RAY *r; for (i = 0; i < m->oargs.nsargs; i++) { if (!strcmp(m->oargs.sarg[i], VOIDID)) continue; - if ((mod = modifier(m->oargs.sarg[i])) == OVOID) { + if ((mod = lastmod(obj, m->oargs.sarg[i])) == OVOID) { sprintf(errmsg, "unknown modifier \"%s\"", m->oargs.sarg[i]); objerror(m, WARNING, errmsg); @@ -55,43 +67,44 @@ register RAY *r; } m->os = (char *)modset; } + if (r == NULL) + return(0); /* just initializing */ if (r->clipset != NULL) setcopy(cset, r->clipset); else cset[0] = 0; - entering = r->rod > 0.0; /* entering clipped region? */ + entering = (r->rod > 0.0); /* entering clipped region? */ - for (i = modset[0]; i > 0; i--) { + for (i = modset[0]; i > 0; i--) if (entering) { if (!inset(cset, modset[i])) { if (cset[0] >= MAXSET) error(INTERNAL, "set overflow in m_clip"); insertelem(cset, modset[i]); } - } else { - if (inset(cset, modset[i])) - deletelem(cset, modset[i]); - } - } + } else if (inset(cset, modset[i])) + deletelem(cset, modset[i]); + /* compute ray value */ r->newcset = cset; if (strcmp(m->oargs.sarg[0], VOIDID)) { int inside = 0; - register RAY *rp; + const RAY *rp; /* check for penetration */ for (rp = r; rp->parent != NULL; rp = rp->parent) if (!(rp->rtype & RAYREFL) && rp->parent->ro != NULL - && inset(modset, rp->parent->ro->omod)) + && inset(modset, rp->parent->ro->omod)) { if (rp->parent->rod > 0.0) inside++; else inside--; + } if (inside > 0) { /* we just hit the object */ flipsurface(r); - rayshade(r, modifier(m->oargs.sarg[0])); - return; + return(rayshade(r, lastmod(obj, m->oargs.sarg[0]))); } } raytrans(r); /* else transfer ray */ + return(1); }