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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines