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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines