ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/virtuals.c
(Generate patch)

Comparing ray/src/rt/virtuals.c (file contents):
Revision 1.1 by greg, Wed Jun 19 16:36:14 1991 UTC vs.
Revision 1.20 by greg, Fri Aug 2 10:30:27 1991 UTC

# Line 11 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11  
12   #include  "ray.h"
13  
14 < #include  "source.h"
14 > #include  "octree.h"
15  
16   #include  "otypes.h"
17  
18 < #include  "cone.h"
18 > #include  "source.h"
19  
20 < #include  "face.h"
20 > #include  "random.h"
21  
22 < extern int  directrelay;                /* maximum number of source relays */
22 > #define  MINSAMPLES     5               /* minimum number of pretest samples */
23 > #define  STESTMAX       30              /* maximum seeks per sample */
24  
24 double  getplaneq();
25 double  getmaxdisk();
26 double  intercircle();
27 SRCREC  *makevsrc();
25  
26 + double  getdisk();
27 +
28   static OBJECT  *vobject;                /* virtual source objects */
29   static int  nvobjects = 0;              /* number of virtual source objects */
30  
# Line 40 | Line 39 | markvirtuals()                 /* find and mark virtual sources */
39                                          /* find virtual source objects */
40          for (i = 0; i < nobjects; i++) {
41                  o = objptr(i);
42 <                if (o->omod == OVOID)
42 >                if (!issurface(o->otype) || o->omod == OVOID)
43                          continue;
44                  if (!isvlight(objptr(o->omod)->otype))
45                          continue;
46 +                if (sfun[o->otype].of == NULL ||
47 +                                sfun[o->otype].of->getpleq == NULL)
48 +                        objerror(o, USER, "illegal material");
49                  if (nvobjects == 0)
50                          vobject = (OBJECT *)malloc(sizeof(OBJECT));
51                  else
# Line 55 | Line 57 | markvirtuals()                 /* find and mark virtual sources */
57          }
58          if (nvobjects == 0)
59                  return;
60 + #ifdef DEBUG
61 +        fprintf(stderr, "found %d virtual source objects\n", nvobjects);
62 + #endif
63                                          /* append virtual sources */
64          for (i = nsources; i-- > 0; )
65 <                if (!(source[i].sflags & SSKIP))
61 <                        addvirtuals(&source[i], directrelay);
65 >                addvirtuals(i, directrelay);
66                                          /* done with our object list */
67          free((char *)vobject);
68          nvobjects = 0;
69   }
70  
71  
72 < addvirtuals(sr, nr)             /* add virtual sources associated with sr */
73 < SRCREC  *sr;
72 > addvirtuals(sn, nr)             /* add virtuals associated with source */
73 > int  sn;
74   int  nr;
75   {
76          register int  i;
77                                  /* check relay limit first */
78          if (nr <= 0)
79                  return;
80 +        if (source[sn].sflags & SSKIP)
81 +                return;
82                                  /* check each virtual object for projection */
83          for (i = 0; i < nvobjects; i++)
84 <                vproject(objptr(i), sr, nr-1);  /* calls us recursively */
84 >                                        /* vproject() calls us recursively */
85 >                vproject(objptr(vobject[i]), sn, nr-1);
86   }
87  
88  
89 < SRCREC *
90 < makevsrc(op, sp, pm)            /* make virtual source if reasonable */
89 > vproject(o, sn, n)              /* create projected source(s) if they exist */
90 > OBJREC  *o;
91 > int  sn;
92 > int  n;
93 > {
94 >        register int  i;
95 >        register VSMATERIAL  *vsmat;
96 >        MAT4  proj;
97 >        int  ns;
98 >
99 >        if (o == source[sn].so) /* objects cannot project themselves */
100 >                return;
101 >                                /* get virtual source material */
102 >        vsmat = sfun[objptr(o->omod)->otype].mf;
103 >                                /* project virtual sources */
104 >        for (i = 0; i < vsmat->nproj; i++)
105 >                if ((*vsmat->vproj)(proj, o, &source[sn], i))
106 >                        if ((ns = makevsrc(o, sn, proj)) >= 0) {
107 >                                source[ns].sa.sv.pn = i;
108 > #ifdef DEBUG
109 >                                virtverb(ns, stderr);
110 > #endif
111 >                                addvirtuals(ns, n);
112 >                        }
113 > }
114 >
115 >
116 > int
117 > makevsrc(op, sn, pm)            /* make virtual source if reasonable */
118   OBJREC  *op;
119 < register SRCREC  *sp;
119 > register int  sn;
120   MAT4  pm;
121   {
122 <        register SRCREC  *newsrc;
123 <        FVECT  nsloc, ocent, nsnorm;
124 <        double  maxrad2;
91 <        double  d1, d2;
122 >        FVECT  nsloc, nsnorm, ocent, v;
123 >        double  maxrad2, d;
124 >        int  nsflags;
125          SPOT  theirspot, ourspot;
126          register int  i;
127 +
128 +        nsflags = source[sn].sflags | (SVIRTUAL|SSPOT|SFOLLOW);
129                                          /* get object center and max. radius */
130 <        maxrad2 = getmaxdisk(ocent, op);
130 >        maxrad2 = getdisk(ocent, op, sn);
131          if (maxrad2 <= FTINY)                   /* too small? */
132 <                return(NULL);
132 >                return(-1);
133                                          /* get location and spot */
134 <        if (sp->sflags & SDISTANT) {            /* distant source */
135 <                if (sp->sflags & SPROX)
136 <                        return(NULL);           /* should never get here! */
137 <                multv3(nsloc, sp->sloc, pm);
134 >        if (source[sn].sflags & SDISTANT) {             /* distant source */
135 >                if (source[sn].sflags & SPROX)
136 >                        return(-1);             /* should never get here! */
137 >                multv3(nsloc, source[sn].sloc, pm);
138 >                normalize(nsloc);
139                  VCOPY(ourspot.aim, ocent);
140                  ourspot.siz = PI*maxrad2;
141                  ourspot.flen = 0.;
142 <                if (sp->sflags & SSPOT) {
143 <                        copystruct(&theirspot, sp->sl.s);
144 <                        multp3(theirspot.aim, sp->sl.s->aim, pm);
142 >                if (source[sn].sflags & SSPOT) {
143 >                        multp3(theirspot.aim, source[sn].sl.s->aim, pm);
144 >                        d = sqrt(dist2(ourspot.aim, theirspot.aim));
145 >                        d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].ss;
146 >                        theirspot.siz = PI*d*d;
147 >                        ourspot.flen = theirspot.flen = source[sn].sl.s->flen;
148 >                        d = ourspot.siz;
149                          if (!commonbeam(&ourspot, &theirspot, nsloc))
150 <                                return(NULL);           /* no overlap */
151 <                }
152 <        } else {                                /* local source */
153 <                multp3(nsloc, sp->sloc, pm);
154 <                if (sp->sflags & SPROX) {
155 <                        d2 = 0.;
156 <                        for (i = 0; i < 3; i++) {
157 <                                d1 = ocent[i] - nsloc[i];
158 <                                d2 += d1*d1;
150 >                                return(-1);     /* no overlap */
151 >                        if (ourspot.siz < d-FTINY) {    /* it shrunk */
152 >                                d = beamdisk(v, op, &ourspot, nsloc);
153 >                                if (d <= FTINY)
154 >                                        return(-1);
155 >                                if (d < maxrad2) {
156 >                                        maxrad2 = d;
157 >                                        VCOPY(ocent, v);
158 >                                }
159                          }
120                        if (d2 > sp->sl.prox*sp->sl.prox)
121                                return(NULL);   /* too far away */
160                  }
161 +        } else {                                /* local source */
162 +                multp3(nsloc, source[sn].sloc, pm);
163                  for (i = 0; i < 3; i++)
164                          ourspot.aim[i] = ocent[i] - nsloc[i];
165 <                if ((d1 = normalize(ourspot.aim)) == 0.)
166 <                        return(NULL);           /* at source!! */
167 <                ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2));
165 >                if ((d = normalize(ourspot.aim)) == 0.)
166 >                        return(-1);             /* at source!! */
167 >                if (source[sn].sflags & SPROX && d > source[sn].sl.prox)
168 >                        return(-1);             /* too far away */
169                  ourspot.flen = 0.;
170 <                if (sp->sflags & SSPOT) {
171 <                        copystruct(&theirspot, sp->sl.s);
172 <                        multv3(theirspot.aim, sp->sl.s->aim, pm);
173 <                        if (!commonspot(&ourspot, &theirspot, nsloc))
174 <                                return(NULL);           /* no overlap */
175 <                        ourspot.flen = theirspot.flen;
170 >                d = (sqrt(maxrad2) + source[sn].ss) / d;
171 >                if (d < 1.-FTINY)
172 >                        ourspot.siz = 2.*PI*(1. - sqrt(1.-d*d));
173 >                else
174 >                        nsflags &= ~SSPOT;
175 >                if (source[sn].sflags & SSPOT) {
176 >                        copystruct(&theirspot, source[sn].sl.s);
177 >                        multv3(theirspot.aim, source[sn].sl.s->aim, pm);
178 >                        normalize(theirspot.aim);
179 >                        if (nsflags & SSPOT) {
180 >                                ourspot.flen = theirspot.flen;
181 >                                d = ourspot.siz;
182 >                                if (!commonspot(&ourspot, &theirspot, nsloc))
183 >                                        return(-1);     /* no overlap */
184 >                        } else {
185 >                                nsflags |= SSPOT;
186 >                                copystruct(&ourspot, &theirspot);
187 >                                d = 2.*ourspot.siz;
188 >                        }
189 >                        if (ourspot.siz < d-FTINY) {    /* it shrunk */
190 >                                d = spotdisk(v, op, &ourspot, nsloc);
191 >                                if (d <= FTINY)
192 >                                        return(-1);
193 >                                if (d < maxrad2) {
194 >                                        maxrad2 = d;
195 >                                        VCOPY(ocent, v);
196 >                                }
197 >                        }
198                  }
199 <                if (sp->sflags & SFLAT) {       /* check for behind source */
200 <                        multv3(nsnorm, sp->snorm, pm);
201 <                        if (checkspot(&ourspot, nsnorm) < 0)
202 <                                return(NULL);
199 >                if (source[sn].sflags & SFLAT) {        /* behind source? */
200 >                        multv3(nsnorm, source[sn].snorm, pm);
201 >                        normalize(nsnorm);
202 >                        if (nsflags & SSPOT && !checkspot(&ourspot, nsnorm))
203 >                                return(-1);
204                  }
205          }
206 <                                        /* everything is OK, make source */
207 <        if ((newsrc = newsource()) == NULL)
206 >                                        /* pretest visibility */
207 >        nsflags = vstestvis(nsflags, op, ocent, maxrad2, sn);
208 >        if (nsflags & SSKIP)
209 >                return(-1);     /* obstructed */
210 >                                        /* it all checks out, so make it */
211 >        if ((i = newsource()) < 0)
212                  goto memerr;
213 <        newsrc->sflags = sp->sflags | (SVIRTUAL|SSPOT|SFOLLOW);
214 <        VCOPY(newsrc->sloc, nsloc);
215 <        if (newsrc->sflags & SFLAT)
216 <                VCOPY(newsrc->snorm, nsnorm);
217 <        newsrc->ss = sp->ss; newsrc->ss2 = sp->ss2;
218 <        if ((newsrc->sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
219 <                goto memerr;
220 <        copystruct(newsrc->sl.s, &ourspot);
221 <        if (newsrc->sflags & SPROX)
222 <                newsrc->sl.prox = sp->sl.prox;
223 <        newsrc->sa.svnext = sp - source;
224 <        return(newsrc);
213 >        source[i].sflags = nsflags;
214 >        VCOPY(source[i].sloc, nsloc);
215 >        if (nsflags & SFLAT)
216 >                VCOPY(source[i].snorm, nsnorm);
217 >        source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2;
218 >        if (nsflags & SSPOT) {
219 >                if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
220 >                        goto memerr;
221 >                copystruct(source[i].sl.s, &ourspot);
222 >        }
223 >        if (nsflags & SPROX)
224 >                source[i].sl.prox = source[sn].sl.prox;
225 >        source[i].sa.sv.sn = sn;
226 >        source[i].so = op;
227 >        return(i);
228   memerr:
229          error(SYSTEM, "out of memory in makevsrc");
230   }
231  
232  
233 < commonspot(sp1, sp2, org)       /* set sp1 to intersection of sp1 and sp2 */
234 < register SPOT  *sp1, *sp2;
235 < FVECT  org;
233 > double
234 > getdisk(oc, op, sn)             /* get visible object disk */
235 > FVECT  oc;
236 > OBJREC  *op;
237 > register int  sn;
238   {
239 <        FVECT  cent;
240 <        double  rad2, d1r2, d2r2;
241 <
242 <        d1r2 = 1. - sp1->siz/(2.*PI);
243 <        d2r2 = 1. - sp2->siz/(2.*PI);
244 <        if (sp2->siz >= 2.*PI-FTINY)            /* BIG, just check overlap */
245 <                return(DOT(sp1->aim,sp2->aim) >= d1r2*d2r2 -
246 <                                        sqrt((1.-d1r2*d1r2)*(1.-d2r2*d2r2)));
247 <                                /* compute and check disks */
248 <        d1r2 = 1./(d1r2*d1r2) - 1.;
249 <        d2r2 = 1./(d2r2*d2r2) - 1.;
250 <        rad2 = intercircle(cent, sp1->aim, sp2->aim, d1r2, d2r2);
251 <        if (rad2 <= FTINY || normalize(cent) == 0.)
252 <                return(0);
253 <        VCOPY(sp1->aim, cent);
254 <        sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2));
255 <        return(1);
239 >        double  rad2, roffs, offs, d, rd, rdoto;
240 >        FVECT  rnrm, nrm;
241 >                                /* first, use object getdisk function */
242 >        rad2 = getmaxdisk(oc, op);
243 >        if (!(source[sn].sflags & SVIRTUAL))
244 >                return(rad2);           /* all done for normal source */
245 >                                /* check for correct side of relay surface */
246 >        roffs = getplaneq(rnrm, source[sn].so);
247 >        rd = DOT(rnrm, source[sn].sloc);        /* source projection */
248 >        if (!(source[sn].sflags & SDISTANT))
249 >                rd -= roffs;
250 >        d = DOT(rnrm, oc) - roffs;      /* disk distance to relay plane */
251 >        if ((d > 0.) ^ (rd > 0.))
252 >                return(rad2);           /* OK if opposite sides */
253 >        if (d*d >= rad2)
254 >                return(0.);             /* no relay is possible */
255 >                                /* we need a closer look */
256 >        offs = getplaneq(nrm, op);
257 >        rdoto = DOT(rnrm, nrm);
258 >        if (d*d >= rad2*(1.-rdoto*rdoto))
259 >                return(0.);             /* disk entirely on projection side */
260 >                                /* should shrink disk but I'm lazy */
261 >        return(rad2);
262   }
263  
264  
265 < commonbeam(sp1, sp2, dir)       /* set sp1 to intersection of sp1 and sp2 */
266 < register SPOT  *sp1, *sp2;
267 < FVECT  dir;
265 > int
266 > vstestvis(f, o, oc, or2, sn)            /* pretest source visibility */
267 > int  f;                 /* virtual source flags */
268 > OBJREC  *o;             /* relay object */
269 > FVECT  oc;              /* relay object center */
270 > double  or2;            /* relay object radius squared */
271 > register int  sn;       /* target source number */
272   {
273 <        FVECT  cent, c1, c2;
274 <        double  rad2, d;
275 <        register int  i;
276 <                                        /* move centers to common plane */
277 <        d = DOT(sp1->aim, dir);
278 <        for (i = 0; i < 3; i++)
279 <                c1[i] = sp2->aim[i] - d*dir[i];
280 <        d = DOT(sp2->aim, dir);
281 <        for (i = 0; i < 3; i++)
282 <                c2[i] = sp2->aim[i] - d*dir[i];
283 <                                        /* compute overlap */
284 <        rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
285 <        if (rad2 <= FTINY)
286 <                return(0);
287 <        VCOPY(sp1->aim, cent);
288 <        sp1->siz = PI*rad2;
289 <        return(1);
290 < }
208 <
209 <
210 < checkspot(sp, nrm)              /* check spotlight for behind source */
211 < register SPOT  *sp;
212 < FVECT  nrm;
213 < {
214 <        double  d, d1;
215 <
216 <        d = DOT(sp->aim, nrm);
217 <        if (d > FTINY)                  /* center in front? */
218 <                return(0);
219 <                                        /* else check horizon */
220 <        d1 = 1. - sp->siz/(2.*PI);
221 <        return(1.-FTINY-d*d > d1*d1);
222 < }
223 <
224 <
225 < mirrorproj(m, nv, offs)         /* get mirror projection for surface */
226 < register MAT4  m;
227 < FVECT  nv;
228 < double  offs;
229 < {
230 <        register int  i, j;
231 <                                        /* assign matrix */
232 <        setident4(m);
233 <        for (i = 0; i < 3; i++)
234 <                for (j = 0; j < 3; j++)
235 <                        m[i][j] -= 2.*nv[i]*nv[j];
236 <        for (j = 0; j < 3; j++)
237 <                m[3][j] = 2.*offs*nv[j];
238 < }
239 <
240 <
241 < double
242 < intercircle(cc, c1, c2, r1s, r2s)       /* intersect two circles */
243 < FVECT  cc;                      /* midpoint (return value) */
244 < FVECT  c1, c2;                  /* circle centers */
245 < double  r1s, r2s;               /* radii squared */
246 < {
247 <        double  a2, d2, l;
248 <        FVECT  disp;
249 <        register int  i;
250 <
251 <        for (i = 0; i < 3; i++)
252 <                disp[i] = c2[i] - c1[i];
253 <        d2 = DOT(disp,disp);
254 <                                        /* circle within overlap? */
255 <        if (r1s < r2s) {
256 <                if (r2s >= r1s + d2) {
257 <                        VCOPY(cc, c1);
258 <                        return(r1s);
259 <                }
273 >        RAY  sr;
274 >        FVECT  onorm;
275 >        FVECT  offsdir;
276 >        double  or, d;
277 >        int  infront;
278 >        int  stestlim, ssn;
279 >        int  nhit, nok;
280 >        register int  i, n;
281 >                                /* return if pretesting disabled */
282 >        if (vspretest <= 0)
283 >                return(f);
284 >                                /* get surface normal */
285 >        getplaneq(onorm, o);
286 >                                /* set number of rays to sample */
287 >        if (source[sn].sflags & SDISTANT) {
288 >                n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)*
289 >                                vspretest + .5;
290 >                infront = DOT(onorm, source[sn].sloc) > 0.;
291          } else {
292 <                if (r1s >= r2s + d2) {
293 <                        VCOPY(cc, c2);
294 <                        return(r2s);
295 <                }
292 >                for (i = 0; i < 3; i++)
293 >                        offsdir[i] = source[sn].sloc[i] - oc[i];
294 >                d = DOT(offsdir,offsdir);
295 >                if (d <= FTINY)
296 >                        n = 2.*PI * vspretest + .5;
297 >                else
298 >                        n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5;
299 >                infront = DOT(onorm, offsdir) > 0.;
300          }
301 <        a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
302 <                                        /* no overlap? */
303 <        if (a2 <= 0.)
304 <                return(0.);
305 <        l = sqrt((r1s - a2)/d2);
306 <        for (i = 0; i < 3; i++)
307 <                cc[i] = c1[i] + l*disp[i];
308 <        return(a2);
309 < }
310 <
311 <
312 < /*
313 < * The following routines depend on the supported OBJECTS:
314 < */
315 <
316 <
317 < double
318 < getmaxdisk(ocent, op)           /* get object center and squared radius */
284 < FVECT  ocent;
285 < register OBJREC  *op;
286 < {
287 <        double  maxrad2;
288 <
289 <        switch (op->otype) {
290 <        case OBJ_FACE:
291 <                {
292 <                        double  d1, d2;
293 <                        register int  i, j;
294 <                        register FACE  *f = getface(op);
295 <
296 <                        for (i = 0; i < 3; i++) {
297 <                                ocent[i] = 0.;
298 <                                for (j = 0; j < f->nv; j++)
299 <                                        ocent[i] += VERTEX(f,j)[i];
300 <                                ocent[i] /= (double)f->nv;
301 >        if (n < MINSAMPLES) n = MINSAMPLES;
302 > #ifdef DEBUG
303 >        fprintf(stderr, "pretesting source %d in object %s with %d rays\n",
304 >                        sn, o->oname, n);
305 > #endif
306 >                                /* sample */
307 >        or = sqrt(or2);
308 >        stestlim = n*STESTMAX;
309 >        ssn = 0;
310 >        nhit = nok = 0;
311 >        while (n-- > 0) {
312 >                                        /* get sample point */
313 >                do {
314 >                        if (ssn >= stestlim) {
315 > #ifdef DEBUG
316 >                                fprintf(stderr, "\ttoo hard to hit\n");
317 > #endif
318 >                                return(f);      /* too small a target! */
319                          }
320 <                        maxrad2 = 0.;
321 <                        for (j = 0; j < f->nv; j++) {
322 <                                d2 = 0.;
320 >                        for (i = 0; i < 3; i++)
321 >                                offsdir[i] = or*(1. -
322 >                                        2.*urand(urind(931*i+5827,ssn)));
323 >                        ssn++;
324 >                        for (i = 0; i < 3; i++)
325 >                                sr.rorg[i] = oc[i] + offsdir[i];
326 >                        d = DOT(offsdir,onorm);
327 >                        if (infront)
328                                  for (i = 0; i < 3; i++) {
329 <                                        d1 = VERTEX(f,j)[i] - ocent[i];
330 <                                        d2 += d1*d1;
329 >                                        sr.rorg[i] -= (d-.0001)*onorm[i];
330 >                                        sr.rdir[i] = -onorm[i];
331                                  }
332 <                                if (d2 > maxrad2)
333 <                                        maxrad2 = d2;
334 <                        }
332 >                        else
333 >                                for (i = 0; i < 3; i++) {
334 >                                        sr.rorg[i] -= (d+.0001)*onorm[i];
335 >                                        sr.rdir[i] = onorm[i];
336 >                                }
337 >                        rayorigin(&sr, NULL, PRIMARY, 1.0);
338 >                } while (!(*ofun[o->otype].funp)(o, &sr));
339 >                                        /* check against source */
340 >                samplendx++;
341 >                if (srcray(&sr, NULL, sn) == 0.)
342 >                        continue;
343 >                sr.revf = srcvalue;
344 >                rayvalue(&sr);
345 >                if (bright(sr.rcol) <= FTINY)
346 >                        continue;
347 >                nok++;
348 >                                        /* check against obstructions */
349 >                rayclear(&sr);
350 >                sr.revf = raytrace;
351 >                rayvalue(&sr);
352 >                if (bright(sr.rcol) > FTINY)
353 >                        nhit++;
354 >                if (nhit > 0 && nhit < nok) {
355 > #ifdef DEBUG
356 >                        fprintf(stderr, "\tpartially occluded\n");
357 > #endif
358 >                        return(f);              /* need to shadow test */
359                  }
313                return(maxrad2);
314        case OBJ_RING:
315                {
316                        register CONE  *co = getcone(op, 0);
317
318                        VCOPY(ocent, CO_P0(co));
319                        maxrad2 = CO_R1(co);
320                        maxrad2 *= maxrad2;
321                }
322                return(maxrad2);
360          }
361 <        objerror(op, USER, "illegal material");
325 < }
326 <
327 <
328 < double
329 < getplaneq(nvec, op)                     /* get plane equation for object */
330 < FVECT  nvec;
331 < OBJREC  *op;
332 < {
333 <        register FACE  *fo;
334 <        register CONE  *co;
335 <
336 <        switch (op->otype) {
337 <        case OBJ_FACE:
338 <                fo = getface(op);
339 <                VCOPY(nvec, fo->norm);
340 <                return(fo->offset);
341 <        case OBJ_RING:
342 <                co = getcone(op, 0);
343 <                VCOPY(nvec, co->ad);
344 <                return(DOT(nvec, CO_P0(co)));
345 <        }
346 <        objerror(op, USER, "illegal material");
347 < }
348 <
349 <
350 < /*
351 < * The following routines depend on the supported MATERIALS:
352 < */
353 <
354 <
355 < vproject(o, s, n)               /* create projected source(s) if they exist */
356 < OBJREC  *o;
357 < SRCREC  *s;
358 < int  n;
359 < {
360 <        SRCREC  *ns;
361 <        FVECT  norm;
362 <        double  offset;
363 <        MAT4  proj;
364 <                                /* get surface normal and offset */
365 <        offset = getplaneq(norm, o);
366 <        switch (objptr(o->omod)->otype) {
367 <        case MAT_MIRROR:                        /* mirror source */
368 <                if (DOT(s->sloc, norm) <= (s->sflags & SDISTANT ?
369 <                                        FTINY : offset+FTINY))
370 <                        return;                 /* behind mirror */
371 <                mirrorproj(proj, norm, offset);
372 <                if ((ns = makevsrc(o, s, proj)) != NULL)
373 <                        addvirtuals(ns, n);
374 <                break;
375 <        }
376 < }
377 <
378 <
379 < vsrcrelay(rn, rv)               /* relay virtual source ray */
380 < register RAY  *rn, *rv;
381 < {
382 <        int  snext;
383 <        register int  i;
384 <                                        /* source we're aiming for here */
385 <        snext = source[rv->rsrc].sa.svnext;
386 <                                        /* compute relayed ray direction */
387 <        switch (objptr(rv->ro->omod)->otype) {
388 <        case MAT_MIRROR:                /* mirror: singular reflection */
389 <                rayorigin(rn, rv, REFLECTED, 1.);
390 <                                        /* ignore textures */
391 <                for (i = 0; i < 3; i++)
392 <                        rn->rdir[i] = rv->rdir[i] + 2.*rv->rod*rv->ron[i];
393 <                break;
361 >        if (nhit == 0) {
362   #ifdef DEBUG
363 <        default:
396 <                error(CONSISTENCY, "inappropriate material in vsrcrelay");
363 >                fprintf(stderr, "\t0%% hit rate\n");
364   #endif
365 +                return(f | SSKIP);      /* 0% hit rate:  totally occluded */
366          }
367 <        rn->rsrc = snext;
367 > #ifdef DEBUG
368 >        fprintf(stderr, "\t100%% hit rate\n");
369 > #endif
370 >        return(f & ~SFOLLOW);           /* 100% hit rate:  no occlusion */
371   }
372 +        
373  
374 <
375 < m_mirror(m, r)                  /* shade mirrored ray */
376 < register OBJREC  *m;
377 < register RAY  *r;
374 > #ifdef DEBUG
375 > virtverb(sn, fp)        /* print verbose description of virtual source */
376 > register int  sn;
377 > FILE  *fp;
378   {
407        COLOR  mcolor;
408        RAY  nr;
379          register int  i;
380  
381 <        if (m->oargs.nfargs != 3 || m->oargs.nsargs > 1)
382 <                objerror(m, USER, "bad number of arguments");
383 <        if (r->rsrc >= 0) {                     /* aiming for somebody */
384 <                if (source[r->rsrc].so != r->ro)
385 <                        return;                         /* but not us */
386 <        } else if (m->oargs.nsargs > 0) {       /* else call substitute? */
387 <                rayshade(r, modifier(m->oargs.sarg[0]));
381 >        fprintf(fp, "%s virtual source %d in %s %s\n",
382 >                        source[sn].sflags & SDISTANT ? "distant" : "local",
383 >                        sn, ofun[source[sn].so->otype].funame,
384 >                        source[sn].so->oname);
385 >        fprintf(fp, "\tat (%f,%f,%f)\n",
386 >                source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
387 >        fprintf(fp, "\tlinked to source %d (%s)\n",
388 >                source[sn].sa.sv.sn, source[source[sn].sa.sv.sn].so->oname);
389 >        if (source[sn].sflags & SFOLLOW)
390 >                fprintf(fp, "\talways followed\n");
391 >        else
392 >                fprintf(fp, "\tnever followed\n");
393 >        if (!(source[sn].sflags & SSPOT))
394                  return;
395 <        }
396 <        if (r->rod < 0.)                        /* back is black */
397 <                return;
422 <                                        /* get modifiers */
423 <        raytexture(r, m->omod);
424 <                                        /* assign material color */
425 <        setcolor(mcolor, m->oargs.farg[0],
426 <                        m->oargs.farg[1],
427 <                        m->oargs.farg[2]);
428 <        multcolor(mcolor, r->pcol);
429 <                                        /* compute reflected ray */
430 <        if (r->rsrc >= 0)                       /* relayed light source */
431 <                vsrcrelay(&nr, r);
432 <        else {                                  /* ordinary reflection */
433 <                FVECT  pnorm;
434 <                double  pdot;
435 <
436 <                if (rayorigin(&nr, r, REFLECTED, bright(mcolor)) < 0)
437 <                        return;
438 <                pdot = raynormal(pnorm, r);     /* use textures */
439 <                for (i = 0; i < 3; i++)
440 <                        nr.rdir[i] = r->rdir[i] + 2.*pdot*pnorm[i];
441 <        }
442 <        rayvalue(&nr);
443 <        multcolor(nr.rcol, mcolor);
444 <        addcolor(r->rcol, nr.rcol);
395 >        fprintf(fp, "\twith spot aim (%f,%f,%f) and size %f\n",
396 >                        source[sn].sl.s->aim[0], source[sn].sl.s->aim[1],
397 >                        source[sn].sl.s->aim[2], source[sn].sl.s->siz);
398   }
399 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines