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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines