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.2 by greg, Thu Jun 20 09:37:38 1991 UTC vs.
Revision 2.20 by greg, Fri Sep 14 13:34:02 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines