| 1 | greg | 1.1 | /* Copyright (c) 1986 Regents of the University of California */ | 
| 2 |  |  |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | *  m_clip.c - routine for clipped (cut) objects. | 
| 9 |  |  | * | 
| 10 |  |  | *     3/17/86 | 
| 11 |  |  | */ | 
| 12 |  |  |  | 
| 13 |  |  | #include  "ray.h" | 
| 14 |  |  |  | 
| 15 |  |  | /* | 
| 16 |  |  | *  Clipping objects permit holes and sections to be taken out | 
| 17 |  |  | *  of other objects.  The method is simple: | 
| 18 |  |  | * | 
| 19 |  |  | *  The argument is the clipped materials; | 
| 20 |  |  | *  the first is used to shade upon exit. | 
| 21 |  |  | */ | 
| 22 |  |  |  | 
| 23 |  |  |  | 
| 24 |  |  | m_clip(m, r)                    /* clip objects from ray */ | 
| 25 |  |  | register OBJREC  *m; | 
| 26 |  |  | register RAY  *r; | 
| 27 |  |  | { | 
| 28 |  |  | OBJECT  cset[MAXSET+1], *modset; | 
| 29 |  |  | int  entering; | 
| 30 |  |  | register int  i; | 
| 31 |  |  |  | 
| 32 |  |  | if ((modset = (OBJECT *)m->os) == NULL) { | 
| 33 | greg | 2.3 | OBJECT  obj, mod; | 
| 34 | greg | 1.1 |  | 
| 35 |  |  | if (m->oargs.nsargs < 1 || m->oargs.nsargs > MAXSET) | 
| 36 |  |  | objerror(m, USER, "bad # arguments"); | 
| 37 | greg | 2.3 | obj = objndx(m); | 
| 38 | greg | 1.1 | modset = (OBJECT *)malloc((m->oargs.nsargs+1)*sizeof(OBJECT)); | 
| 39 |  |  | if (modset == NULL) | 
| 40 |  |  | error(SYSTEM, "out of memory in m_clip"); | 
| 41 |  |  | modset[0] = 0; | 
| 42 |  |  | for (i = 0; i < m->oargs.nsargs; i++) { | 
| 43 |  |  | if (!strcmp(m->oargs.sarg[i], VOIDID)) | 
| 44 |  |  | continue; | 
| 45 | greg | 2.3 | if ((mod = lastmod(obj, m->oargs.sarg[i])) == OVOID) { | 
| 46 | greg | 1.1 | sprintf(errmsg, "unknown modifier \"%s\"", | 
| 47 |  |  | m->oargs.sarg[i]); | 
| 48 |  |  | objerror(m, WARNING, errmsg); | 
| 49 |  |  | continue; | 
| 50 |  |  | } | 
| 51 |  |  | if (inset(modset, mod)) { | 
| 52 |  |  | objerror(m, WARNING, "duplicate modifier"); | 
| 53 |  |  | continue; | 
| 54 |  |  | } | 
| 55 |  |  | insertelem(modset, mod); | 
| 56 |  |  | } | 
| 57 | greg | 1.2 | m->os = (char *)modset; | 
| 58 | greg | 1.1 | } | 
| 59 |  |  | if (r->clipset != NULL) | 
| 60 |  |  | setcopy(cset, r->clipset); | 
| 61 |  |  | else | 
| 62 |  |  | cset[0] = 0; | 
| 63 |  |  |  | 
| 64 |  |  | entering = r->rod > 0.0;                /* entering clipped region? */ | 
| 65 |  |  |  | 
| 66 |  |  | for (i = modset[0]; i > 0; i--) { | 
| 67 |  |  | if (entering) { | 
| 68 |  |  | if (!inset(cset, modset[i])) { | 
| 69 |  |  | if (cset[0] >= MAXSET) | 
| 70 |  |  | error(INTERNAL, "set overflow in m_clip"); | 
| 71 |  |  | insertelem(cset, modset[i]); | 
| 72 |  |  | } | 
| 73 |  |  | } else { | 
| 74 |  |  | if (inset(cset, modset[i])) | 
| 75 |  |  | deletelem(cset, modset[i]); | 
| 76 |  |  | } | 
| 77 |  |  | } | 
| 78 |  |  | /* compute ray value */ | 
| 79 |  |  | r->newcset = cset; | 
| 80 |  |  | if (strcmp(m->oargs.sarg[0], VOIDID)) { | 
| 81 |  |  | int  inside = 0; | 
| 82 |  |  | register RAY  *rp; | 
| 83 |  |  | /* check for penetration */ | 
| 84 |  |  | for (rp = r; rp->parent != NULL; rp = rp->parent) | 
| 85 |  |  | if (!(rp->rtype & RAYREFL) && rp->parent->ro != NULL | 
| 86 |  |  | && inset(modset, rp->parent->ro->omod)) | 
| 87 |  |  | if (rp->parent->rod > 0.0) | 
| 88 |  |  | inside++; | 
| 89 |  |  | else | 
| 90 |  |  | inside--; | 
| 91 |  |  | if (inside > 0) {       /* we just hit the object */ | 
| 92 |  |  | flipsurface(r); | 
| 93 | greg | 2.2 | return(rayshade(r, modifier(m->oargs.sarg[0]))); | 
| 94 | greg | 1.1 | } | 
| 95 |  |  | } | 
| 96 |  |  | raytrans(r);                    /* else transfer ray */ | 
| 97 | greg | 2.2 | return(1); | 
| 98 | greg | 1.1 | } |