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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines