ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
(Generate patch)

Comparing ray/src/rt/source.c (file contents):
Revision 1.9 by greg, Thu Jun 8 09:35:43 1989 UTC vs.
Revision 2.46 by greg, Wed Sep 8 17:10:16 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 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   *  source.c - routines dealing with illumination sources.
6   *
7 < *     8/20/85
7 > *  External symbols declared in source.h
8   */
9  
10   #include  "ray.h"
11 + #include  "otypes.h"
12 + #include  "rtotypes.h"
13 + #include  "source.h"
14 + #include  "random.h"
15  
16 < #include  "octree.h"
16 > extern double  ssampdist;               /* scatter sampling distance */
17  
18 < #include  "source.h"
18 > #ifndef MAXSSAMP
19 > #define MAXSSAMP        16              /* maximum samples per ray */
20 > #endif
21  
22 < #include  "otypes.h"
22 > /*
23 > * Structures used by direct()
24 > */
25  
26 < #include  "cone.h"
26 > typedef struct {
27 >        int  sno;               /* source number */
28 >        FVECT  dir;             /* source direction */
29 >        COLOR  coef;            /* material coefficient */
30 >        COLOR  val;             /* contribution */
31 > }  CONTRIB;             /* direct contribution */
32  
33 < #include  "face.h"
33 > typedef struct {
34 >        int  sndx;              /* source index (to CONTRIB array) */
35 >        float  brt;             /* brightness (for comparison) */
36 > }  CNTPTR;              /* contribution pointer */
37  
38 < #include  "random.h"
38 > static CONTRIB  *srccnt;                /* source contributions in direct() */
39 > static CNTPTR  *cntord;                 /* source ordering in direct() */
40 > static int  maxcntr = 0;                /* size of contribution arrays */
41  
42 + static int cntcmp(const void *p1, const void *p2);
43  
28 extern double  dstrsrc;                 /* source distribution amount */
29 extern double  shadthresh;              /* relative shadow threshold */
44  
45 < SRCREC  *source = NULL;                 /* our list of sources */
46 < int  nsources = 0;                      /* the number of sources */
45 > extern OBJREC *                 /* find an object's actual material */
46 > findmaterial(register OBJREC *o)
47 > {
48 >        while (!ismaterial(o->otype)) {
49 >                if (ismixture(o->otype))
50 >                        return(NULL);   /* reject mixed materials */
51 >                if (o->otype == MOD_ALIAS && o->oargs.nsargs) {
52 >                        OBJECT  aobj;
53 >                        OBJREC  *ao;
54 >                        aobj = lastmod(objndx(o), o->oargs.sarg[0]);
55 >                        if (aobj < 0)
56 >                                objerror(o, USER, "bad reference");
57 >                        ao = objptr(aobj);
58 >                        if (ismaterial(ao->otype))
59 >                                return(ao);
60 >                }
61 >                if (o->omod == OVOID)
62 >                        return(NULL);
63 >                o = objptr(o->omod);
64 >        }
65 >        return(o);
66 > }
67  
68  
69 < marksources()                   /* find and mark source objects */
69 > extern void
70 > marksources(void)                       /* find and mark source objects */
71   {
72 +        int  foundsource = 0;
73 +        int  i;
74          register OBJREC  *o, *m;
75 <        register int  i;
76 <
77 <        for (i = 0; i < nobjects; i++) {
75 >        register int  ns;
76 >                                        /* initialize dispatch table */
77 >        initstypes();
78 >                                        /* find direct sources */
79 >        for (i = 0; i < nsceneobjs; i++) {
80          
81                  o = objptr(i);
82  
83 <                if (o->omod == OVOID)
83 >                if (!issurface(o->otype) || o->omod == OVOID)
84                          continue;
85 <
86 <                m = objptr(o->omod);
87 <
88 <                if (m->otype != MAT_LIGHT &&
50 <                                m->otype != MAT_ILLUM &&
51 <                                m->otype != MAT_GLOW &&
52 <                                m->otype != MAT_SPOT)
53 <                        continue;
85 >                                        /* find material */
86 >                m = findmaterial(objptr(o->omod));
87 >                if (m == NULL || !islight(m->otype))
88 >                        continue;       /* not source modifier */
89          
90                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
91                                  m->otype == MAT_SPOT ? 7 : 3))
# Line 60 | Line 95 | marksources()                  /* find and mark source objects */
95                                  o->otype != OBJ_SOURCE &&
96                                  m->oargs.farg[3] <= FTINY)
97                          continue;                       /* don't bother */
98 +                if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY &&
99 +                                m->oargs.farg[2] <= FTINY)
100 +                        continue;                       /* don't bother */
101  
102 <                if (source == NULL)
103 <                        source = (SRCREC *)malloc(sizeof(SRCREC));
104 <                else
67 <                        source = (SRCREC *)realloc((char *)source,
68 <                                        (unsigned)(nsources+1)*sizeof(SRCREC));
69 <                if (source == NULL)
70 <                        error(SYSTEM, "out of memory in marksources");
102 >                if (sfun[o->otype].of == NULL ||
103 >                                sfun[o->otype].of->setsrc == NULL)
104 >                        objerror(o, USER, "illegal material");
105  
106 <                newsource(&source[nsources], o);
106 >                if ((ns = newsource()) < 0)
107 >                        goto memerr;
108  
109 +                setsource(&source[ns], o);
110 +
111                  if (m->otype == MAT_GLOW) {
112 <                        source[nsources].sflags |= SPROX;
113 <                        source[nsources].sl.prox = m->oargs.farg[3];
114 <                        if (o->otype == OBJ_SOURCE)
115 <                                source[nsources].sflags |= SSKIP;
112 >                        source[ns].sflags |= SPROX;
113 >                        source[ns].sl.prox = m->oargs.farg[3];
114 >                        if (source[ns].sflags & SDISTANT)
115 >                                source[ns].sflags |= SSKIP;
116                  } else if (m->otype == MAT_SPOT) {
117 <                        source[nsources].sflags |= SSPOT;
118 <                        source[nsources].sl.s = makespot(m);
117 >                        source[ns].sflags |= SSPOT;
118 >                        if ((source[ns].sl.s = makespot(m)) == NULL)
119 >                                goto memerr;
120 >                        if (source[ns].sflags & SFLAT &&
121 >                                !checkspot(source[ns].sl.s,source[ns].snorm)) {
122 >                                objerror(o, WARNING,
123 >                                        "invalid spotlight direction");
124 >                                source[ns].sflags |= SSKIP;
125 >                        }
126                  }
127 <                nsources++;
127 > #if  SHADCACHE
128 >                initobscache(ns);
129 > #endif
130 >                if (!(source[ns].sflags & SSKIP))
131 >                        foundsource++;
132          }
133 +        if (!foundsource) {
134 +                error(WARNING, "no light sources found");
135 +                return;
136 +        }
137 +        markvirtuals();                 /* find and add virtual sources */
138 +                                /* allocate our contribution arrays */
139 +        maxcntr = nsources + MAXSPART;  /* start with this many */
140 +        srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
141 +        cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR));
142 +        if ((srccnt == NULL) | (cntord == NULL))
143 +                goto memerr;
144 +        return;
145 + memerr:
146 +        error(SYSTEM, "out of memory in marksources");
147   }
148  
149  
150 < newsource(src, so)                      /* add a source to the array */
151 < register SRCREC  *src;
90 < register OBJREC  *so;
150 > extern void
151 > freesources(void)                       /* free all source structures */
152   {
153 <        double  cos(), tan(), sqrt();
154 <        double  theta;
155 <        FACE  *f;
156 <        CONE  *co;
157 <        int  j;
158 <        register int  i;
159 <        
160 <        src->sflags = 0;
100 <        src->nhits = 1; src->ntests = 2;        /* start probability = 1/2 */
101 <        src->so = so;
102 <
103 <        switch (so->otype) {
104 <        case OBJ_SOURCE:
105 <                if (so->oargs.nfargs != 4)
106 <                        objerror(so, USER, "bad arguments");
107 <                src->sflags |= SDISTANT;
108 <                VCOPY(src->sloc, so->oargs.farg);
109 <                if (normalize(src->sloc) == 0.0)
110 <                        objerror(so, USER, "zero direction");
111 <                theta = PI/180.0/2.0 * so->oargs.farg[3];
112 <                if (theta <= FTINY)
113 <                        objerror(so, USER, "zero size");
114 <                src->ss = theta >= PI/4 ? 1.0 : tan(theta);
115 <                src->ss2 = 2.0*PI * (1.0 - cos(theta));
116 <                break;
117 <        case OBJ_SPHERE:
118 <                VCOPY(src->sloc, so->oargs.farg);
119 <                src->ss = so->oargs.farg[3];
120 <                src->ss2 = PI * src->ss * src->ss;
121 <                break;
122 <        case OBJ_FACE:
123 <                                                /* get the face */
124 <                f = getface(so);
125 <                                                /* find the center */
126 <                for (j = 0; j < 3; j++) {
127 <                        src->sloc[j] = 0.0;
128 <                        for (i = 0; i < f->nv; i++)
129 <                                src->sloc[j] += VERTEX(f,i)[j];
130 <                        src->sloc[j] /= f->nv;
131 <                }
132 <                if (!inface(src->sloc, f))
133 <                        objerror(so, USER, "cannot hit center");
134 <                src->ss = sqrt(f->area / PI);
135 <                src->ss2 = f->area;
136 <                break;
137 <        case OBJ_RING:
138 <                                                /* get the ring */
139 <                co = getcone(so, 0);
140 <                VCOPY(src->sloc, CO_P0(co));
141 <                if (CO_R0(co) > 0.0)
142 <                        objerror(so, USER, "cannot hit center");
143 <                src->ss = CO_R1(co);
144 <                src->ss2 = PI * src->ss * src->ss;
145 <                break;
146 <        default:
147 <                objerror(so, USER, "illegal material");
153 >        if (nsources > 0) {
154 > #if SHADCACHE
155 >                while (nsources--)
156 >                        freeobscache(&source[nsources]);
157 > #endif
158 >                free((void *)source);
159 >                source = NULL;
160 >                nsources = 0;
161          }
162 +        if (maxcntr <= 0)
163 +                return;
164 +        free((void *)srccnt);
165 +        srccnt = NULL;
166 +        free((void *)cntord);
167 +        cntord = NULL;
168 +        maxcntr = 0;
169   }
170  
171  
172 < SPOT *
173 < makespot(m)                     /* make a spotlight */
174 < register OBJREC  *m;
172 > extern int
173 > srcray(                         /* send a ray to a source, return domega */
174 >        register RAY  *sr,              /* returned source ray */
175 >        RAY  *r,                        /* ray which hit object */
176 >        SRCINDEX  *si                   /* source sample index */
177 > )
178   {
179 <        extern double  cos();
180 <        register SPOT  *ns;
179 >    double  d;                          /* distance to source */
180 >    register SRCREC  *srcp;
181  
182 <        if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
183 <                error(SYSTEM, "out of memory in makespot");
184 <        ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
185 <        VCOPY(ns->aim, m->oargs.farg+4);
186 <        if ((ns->flen = normalize(ns->aim)) == 0.0)
187 <                objerror(m, USER, "zero focus vector");
188 <        return(ns);
182 >    rayorigin(sr, r, SHADOW, 1.0);              /* ignore limits */
183 >
184 >    while ((d = nextssamp(sr, si)) != 0.0) {
185 >        sr->rsrc = si->sn;                      /* remember source */
186 >        srcp = source + si->sn;
187 >        if (srcp->sflags & SDISTANT) {
188 >                if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
189 >                        continue;
190 >                return(1);              /* sample OK */
191 >        }
192 >                                /* local source */
193 >                                                /* check proximity */
194 >        if (srcp->sflags & SPROX && d > srcp->sl.prox)
195 >                continue;
196 >                                                /* check angle */
197 >        if (srcp->sflags & SSPOT) {
198 >                if (spotout(sr, srcp->sl.s))
199 >                        continue;
200 >                                        /* adjust solid angle */
201 >                si->dom *= d*d;
202 >                d += srcp->sl.s->flen;
203 >                si->dom /= d*d;
204 >        }
205 >        return(1);                      /* sample OK */
206 >    }
207 >    return(0);                  /* no more samples */
208   }
209  
210  
211 < double
212 < srcray(sr, r, sn)               /* send a ray to a source, return domega */
213 < register RAY  *sr;              /* returned source ray */
214 < RAY  *r;                        /* ray which hit object */
173 < register int  sn;               /* source number */
211 > extern void
212 > srcvalue(                       /* punch ray to source and compute value */
213 >        register RAY  *r
214 > )
215   {
216 <        register double  *norm = NULL;  /* plane normal */
176 <        double  ddot;                   /* (distance times) cosine */
177 <        FVECT  vd;
178 <        double  d;
179 <        register int  i;
216 >        register SRCREC  *sp;
217  
218 <        if (source[sn].sflags & SSKIP)
219 <                return(0.0);                    /* skip this source */
220 <
221 <        rayorigin(sr, r, SHADOW, 1.0);          /* ignore limits */
222 <
223 <        sr->rsrc = sn;                          /* remember source */
224 <                                                /* get source direction */
225 <        if (source[sn].sflags & SDISTANT)
226 <                                                /* constant direction */
190 <                VCOPY(sr->rdir, source[sn].sloc);
191 <        else {                                  /* compute direction */
192 <                for (i = 0; i < 3; i++)
193 <                        sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
194 <
195 <                if (source[sn].so->otype == OBJ_FACE)
196 <                        norm = getface(source[sn].so)->norm;
197 <                else if (source[sn].so->otype == OBJ_RING)
198 <                        norm = getcone(source[sn].so,0)->ad;
199 <
200 <                if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY)
201 <                        return(0.0);            /* behind surface! */
218 >        sp = &source[r->rsrc];
219 >        if (sp->sflags & SVIRTUAL) {    /* virtual source */
220 >                                        /* check intersection */
221 >                if (!(*ofun[sp->so->otype].funp)(sp->so, r))
222 >                        return;
223 >                if (!rayshade(r, r->ro->omod))  /* compute contribution */
224 >                        goto nomat;
225 >                rayparticipate(r);
226 >                return;
227          }
228 <        if (dstrsrc > FTINY) {
229 <                                        /* distribute source direction */
230 <                for (i = 0; i < 3; i++)
231 <                        vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom());
232 <
233 <                if (norm != NULL) {             /* project offset */
234 <                        d = DOT(vd, norm);
235 <                        for (i = 0; i < 3; i++)
236 <                                vd[i] -= d * norm[i];
212 <                }
213 <                for (i = 0; i < 3; i++)         /* offset source direction */
214 <                        sr->rdir[i] += vd[i];
215 <
216 <        } else if (source[sn].sflags & SDISTANT)
217 <                                                /* already normalized */
218 <                return(source[sn].ss2);
219 <
220 <        if ((d = normalize(sr->rdir)) == 0.0)
221 <                                                /* at source! */
222 <                return(0.0);
223 <        
224 <        if (source[sn].sflags & SDISTANT)
225 <                                                /* domega constant */
226 <                return(source[sn].ss2);
227 <
228 <        else {
229 <                                                /* check proximity */
230 <                if (source[sn].sflags & SPROX &&
231 <                                d > source[sn].sl.prox)
232 <                        return(0.0);
233 <
234 <                if (norm != NULL)
235 <                        ddot /= d;
236 <                else
237 <                        ddot = 1.0;
238 <                                                /* check angle */
239 <                if (source[sn].sflags & SSPOT) {
240 <                        if (source[sn].sl.s->siz < 2.0*PI *
241 <                                (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
242 <                                return(0.0);
243 <                        d += source[sn].sl.s->flen;
244 <                }
245 <                                                /* return domega */
246 <                return(ddot*source[sn].ss2/(d*d));
228 >                                        /* compute intersection */
229 >        if (sp->sflags & SDISTANT ? sourcehit(r) :
230 >                        (*ofun[sp->so->otype].funp)(sp->so, r)) {
231 >                if (sp->sa.success >= 0)
232 >                        sp->sa.success++;
233 >                if (!rayshade(r, r->ro->omod))  /* compute contribution */
234 >                        goto nomat;
235 >                rayparticipate(r);
236 >                return;
237          }
238 +                                        /* we missed our mark! */
239 +        if (sp->sa.success < 0)
240 +                return;                 /* bitched already */
241 +        sp->sa.success -= AIMREQT;
242 +        if (sp->sa.success >= 0)
243 +                return;                 /* leniency */
244 +        sprintf(errmsg, "aiming failure for light source \"%s\"",
245 +                        sp->so->oname);
246 +        error(WARNING, errmsg);         /* issue warning */
247 +        return;
248 + nomat:
249 +        objerror(r->ro, USER, "material not found");
250   }
251  
252  
253 < sourcehit(r)                    /* check to see if ray hit distant source */
254 < register RAY  *r;
253 > extern int
254 > sourcehit(                      /* check to see if ray hit distant source */
255 >        register RAY  *r
256 > )
257   {
258          int  first, last;
259          register int  i;
# Line 260 | Line 264 | register RAY  *r;
264                  first = 0; last = nsources-1;
265          }
266          for (i = first; i <= last; i++)
267 <                if (source[i].sflags & SDISTANT)
267 >                if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT)
268                          /*
269                           * Check to see if ray is within
270                           * solid angle of source.
# Line 273 | Line 277 | register RAY  *r;
277                          }
278  
279          if (r->ro != NULL) {
280 +                r->robj = objndx(r->ro);
281                  for (i = 0; i < 3; i++)
282                          r->ron[i] = -r->rdir[i];
283                  r->rod = 1.0;
284 <                r->rofs = 1.0; setident4(r->rofx);
285 <                r->robs = 1.0; setident4(r->robx);
284 >                r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
285 >                r->uv[0] = r->uv[1] = 0.0;
286 >                r->rox = NULL;
287                  return(1);
288          }
289          return(0);
# Line 285 | Line 291 | register RAY  *r;
291  
292  
293   static int
294 < cntcmp(sc1, sc2)                        /* contribution compare (descending) */
295 < register CNTPTR  *sc1, *sc2;
294 > cntcmp(                         /* contribution compare (descending) */
295 >        const void *p1,
296 >        const void *p2
297 > )
298   {
299 +        register const CNTPTR  *sc1 = (const CNTPTR *)p1;
300 +        register const CNTPTR  *sc2 = (const CNTPTR *)p2;
301 +
302          if (sc1->brt > sc2->brt)
303                  return(-1);
304          if (sc1->brt < sc2->brt)
# Line 296 | Line 307 | register CNTPTR  *sc1, *sc2;
307   }
308  
309  
310 < direct(r, f, p)                         /* add direct component */
311 < RAY  *r;                        /* ray that hit surface */
312 < int  (*f)();                    /* direct component coefficient function */
313 < char  *p;                       /* data for f */
310 > extern void
311 > direct(                                 /* add direct component */
312 >        RAY  *r,                        /* ray that hit surface */
313 >        srcdirf_t *f,                   /* direct component coefficient function */
314 >        void  *p                        /* data for f */
315 > )
316   {
317          register int  sn;
318 <        register CONTRIB  *srccnt;
319 <        register CNTPTR  *cntord;
320 <        int  ncnts;
321 <        double  ourthresh, prob, hwt, test2, hit2;
318 >        register CONTRIB  *scp;
319 >        SRCINDEX  si;
320 >        int  nshadcheck, ncnts;
321 >        int  nhits;
322 >        double  prob, ourthresh, hwt;
323          RAY  sr;
324 <
325 <        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
326 <        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
313 <        if (srccnt == NULL || cntord == NULL)
314 <                error(SYSTEM, "out of memory in direct");
315 <                                                /* modify threshold */
316 <        ourthresh = shadthresh / r->rweight;
324 >                        /* NOTE: srccnt and cntord global so no recursion */
325 >        if (nsources <= 0)
326 >                return;         /* no sources?! */
327                                                  /* potential contributions */
328 <        for (sn = 0; sn < nsources; sn++) {
329 <                cntord[sn].sno = sn;
330 <                cntord[sn].brt = 0.0;
331 <                                                /* get source ray */
332 <                if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
333 <                        continue;
334 <                VCOPY(srccnt[sn].dir, sr.rdir);
328 >        initsrcindex(&si);
329 >        for (sn = 0; srcray(&sr, r, &si); sn++) {
330 >                if (sn >= maxcntr) {
331 >                        maxcntr = sn + MAXSPART;
332 >                        srccnt = (CONTRIB *)realloc((void *)srccnt,
333 >                                        maxcntr*sizeof(CONTRIB));
334 >                        cntord = (CNTPTR *)realloc((void *)cntord,
335 >                                        maxcntr*sizeof(CNTPTR));
336 >                        if ((srccnt == NULL) | (cntord == NULL))
337 >                                error(SYSTEM, "out of memory in direct");
338 >                }
339 >                cntord[sn].sndx = sn;
340 >                scp = srccnt + sn;
341 >                scp->sno = sr.rsrc;
342                                                  /* compute coefficient */
343 <                (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
344 <                cntord[sn].brt = bright(srccnt[sn].val);
345 <                if (cntord[sn].brt <= FTINY)
343 >                (*f)(scp->coef, p, sr.rdir, si.dom);
344 >                cntord[sn].brt = bright(scp->coef);
345 >                if (cntord[sn].brt <= 0.0)
346                          continue;
347 <                                                /* compute intersection */
348 <                if (!( source[sn].sflags & SDISTANT ?
349 <                                sourcehit(&sr) :
350 <                                (*ofun[source[sn].so->otype].funp)
334 <                                (source[sn].so, &sr) ))
347 > #if SHADCACHE
348 >                                                /* check shadow cache */
349 >                if (si.np == 1 && srcblocked(&sr)) {
350 >                        cntord[sn].brt = 0.0;
351                          continue;
352 <                                                /* compute contribution */
353 <                rayshade(&sr, sr.ro->omod);
354 <                multcolor(srccnt[sn].val, sr.rcol);
355 <                cntord[sn].brt = bright(srccnt[sn].val);
352 >                }
353 > #endif
354 >                VCOPY(scp->dir, sr.rdir);
355 >                                                /* compute potential */
356 >                sr.revf = srcvalue;
357 >                rayvalue(&sr);
358 >                copycolor(scp->val, sr.rcol);
359 >                multcolor(scp->val, scp->coef);
360 >                cntord[sn].brt = bright(scp->val);
361          }
362                                                  /* sort contributions */
363 <        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
364 <        hit2 = 0.0; test2 = FTINY;
365 <                                                /* find last */
366 <        sn = 0; ncnts = nsources;
367 <        while (sn < ncnts-1) {
368 <                register int  m;
369 <                m = (sn + ncnts) >> 1;
370 <                if (cntord[m].brt > 0.0)
371 <                        sn = m;
372 <                else
373 <                        ncnts = m;
363 >        qsort(cntord, sn, sizeof(CNTPTR), cntcmp);
364 >        {                                       /* find last */
365 >                register int  l, m;
366 >
367 >                ncnts = l = sn;
368 >                sn = 0;
369 >                while ((m = (sn + ncnts) >> 1) != l) {
370 >                        if (cntord[m].brt > 0.0)
371 >                                sn = m;
372 >                        else
373 >                                ncnts = m;
374 >                        l = m;
375 >                }
376          }
377 <                                                /* accumulate tail */
378 <        for (sn = ncnts-1; sn > 0; sn--)
379 <                cntord[sn-1].brt += cntord[sn].brt;
380 <                                                /* shadow testing */
381 <        for (sn = 0; sn < ncnts; sn++) {
382 <                                                /* tail below threshold? */
383 <                if (cntord[sn].brt < ourthresh*bright(r->rcol))
377 >        if (ncnts == 0)
378 >                return;         /* no contributions! */
379 >                                                /* accumulate tail */
380 >        for (sn = ncnts-1; sn > 0; sn--)
381 >                cntord[sn-1].brt += cntord[sn].brt;
382 >                                                /* compute number to check */
383 >        nshadcheck = pow((double)ncnts, shadcert) + .5;
384 >                                                /* modify threshold */
385 >        ourthresh = shadthresh / r->rweight;
386 >                                                /* test for shadows */
387 >        for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts;
388 >                        hwt += (double)source[scp->sno].nhits /
389 >                                (double)source[scp->sno].ntests,
390 >                        sn++) {
391 >                                                /* check threshold */
392 >                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
393 >                                cntord[sn].brt-cntord[sn+nshadcheck].brt)
394 >                                < ourthresh*bright(r->rcol))
395                          break;
396 <                                                /* get statistics */
363 <                hwt = (double)source[cntord[sn].sno].nhits /
364 <                                (double)source[cntord[sn].sno].ntests;
365 <                test2 += hwt;
366 <                source[cntord[sn].sno].ntests++;
396 >                scp = srccnt + cntord[sn].sndx;
397                                                  /* test for hit */
398                  rayorigin(&sr, r, SHADOW, 1.0);
399 <                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
399 >                VCOPY(sr.rdir, scp->dir);
400 >                sr.rsrc = scp->sno;
401 >                                                /* keep statistics */
402 >                if (source[scp->sno].ntests++ > 0xfffffff0) {
403 >                        source[scp->sno].ntests >>= 1;
404 >                        source[scp->sno].nhits >>= 1;
405 >                }
406                  if (localhit(&sr, &thescene) &&
407 <                                sr.ro != source[cntord[sn].sno].so) {
408 <                                                /* check for transmission */
409 <                        if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
410 <                                raytrans(&sr);          /* object is clipped */
411 <                        else
412 <                                rayshade(&sr, sr.ro->omod);
413 <                        if (bright(sr.rcol) <= FTINY)
407 >                                ( sr.ro != source[scp->sno].so ||
408 >                                source[scp->sno].sflags & SFOLLOW )) {
409 >                                                /* follow entire path */
410 >                        raycont(&sr);
411 >                        rayparticipate(&sr);
412 >                        if (trace != NULL)
413 >                                (*trace)(&sr);  /* trace execution */
414 >                        if (bright(sr.rcol) <= FTINY) {
415 > #if SHADCACHE
416 >                                if ((scp <= srccnt || scp[-1].sno != scp->sno)
417 >                                                && (scp >= srccnt+ncnts-1 ||
418 >                                                    scp[1].sno != scp->sno))
419 >                                        srcblocker(&sr);
420 > #endif
421                                  continue;       /* missed! */
422 <                        (*f)(srccnt[cntord[sn].sno].val, p,
423 <                                        srccnt[cntord[sn].sno].dir,
424 <                                        srccnt[cntord[sn].sno].dom);
382 <                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
422 >                        }
423 >                        copycolor(scp->val, sr.rcol);
424 >                        multcolor(scp->val, scp->coef);
425                  }
426                                                  /* add contribution if hit */
427 <                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
428 <                hit2 += hwt;
429 <                source[cntord[sn].sno].nhits++;
427 >                addcolor(r->rcol, scp->val);
428 >                nhits++;
429 >                source[scp->sno].nhits++;
430          }
431 <                                        /* weighted hit rate */
432 <        hwt = hit2 / test2;
431 >                                        /* source hit rate */
432 >        if (hwt > FTINY)
433 >                hwt = (double)nhits / hwt;
434 >        else
435 >                hwt = 0.5;
436   #ifdef DEBUG
437 <        fprintf(stderr, "%d tested, %d untested, %f hit rate\n",
437 >        sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n",
438                          sn, ncnts-sn, hwt);
439 +        eputs(errmsg);
440   #endif
441                                          /* add in untested sources */
442          for ( ; sn < ncnts; sn++) {
443 <                prob = hwt * (double)source[cntord[sn].sno].nhits /
444 <                                (double)source[cntord[sn].sno].ntests;
445 <                scalecolor(srccnt[cntord[sn].sno].val, prob);
446 <                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
443 >                scp = srccnt + cntord[sn].sndx;
444 >                prob = hwt * (double)source[scp->sno].nhits /
445 >                                (double)source[scp->sno].ntests;
446 >                if (prob > 1.0)
447 >                        prob = 1.0;
448 >                scalecolor(scp->val, prob);
449 >                addcolor(r->rcol, scp->val);
450          }
402        free(srccnt);
403        free(cntord);
451   }
452  
453  
454 < #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
455 <                                r->rsrc>=0 && \
456 <                                source[r->rsrc].so!=r->ro)
454 > extern void
455 > srcscatter(                     /* compute source scattering into ray */
456 >        register RAY  *r
457 > )
458 > {
459 >        int  oldsampndx;
460 >        int  nsamps;
461 >        RAY  sr;
462 >        SRCINDEX  si;
463 >        double  t, d;
464 >        double  re, ge, be;
465 >        COLOR  cvext;
466 >        int  i, j;
467  
468 < #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
469 <                                !(r->rtype&REFLECTED) &&        /* hack! */\
470 <                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
468 >        if (r->slights == NULL || r->slights[0] == 0
469 >                        || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
470 >                return;
471 >        if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
472 >                nsamps = 1;
473 > #if MAXSSAMP
474 >        else if (nsamps > MAXSSAMP)
475 >                nsamps = MAXSSAMP;
476 > #endif
477 >        oldsampndx = samplendx;
478 >        samplendx = random()&0x7fff;            /* randomize */
479 >        for (i = r->slights[0]; i > 0; i--) {   /* for each source */
480 >                for (j = 0; j < nsamps; j++) {  /* for each sample position */
481 >                        samplendx++;
482 >                        t = r->rot * (j+frandom())/nsamps;
483 >                                                        /* extinction */
484 >                        re = t*colval(r->cext,RED);
485 >                        ge = t*colval(r->cext,GRN);
486 >                        be = t*colval(r->cext,BLU);
487 >                        setcolor(cvext, re > 92. ? 0. : exp(-re),
488 >                                        ge > 92. ? 0. : exp(-ge),
489 >                                        be > 92. ? 0. : exp(-be));
490 >                        if (intens(cvext) <= FTINY)
491 >                                break;                  /* too far away */
492 >                        sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
493 >                        sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
494 >                        sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
495 >                        sr.rmax = 0.;
496 >                        initsrcindex(&si);      /* sample ray to this source */
497 >                        si.sn = r->slights[i];
498 >                        nopart(&si, &sr);
499 >                        if (!srcray(&sr, NULL, &si) ||
500 >                                        sr.rsrc != r->slights[i])
501 >                                continue;               /* no path */
502 > #if SHADCACHE
503 >                        if (srcblocked(&sr))            /* check shadow cache */
504 >                                continue;
505 > #endif
506 >                        copycolor(sr.cext, r->cext);
507 >                        copycolor(sr.albedo, r->albedo);
508 >                        sr.gecc = r->gecc;
509 >                        sr.slights = r->slights;
510 >                        rayvalue(&sr);                  /* eval. source ray */
511 >                        if (bright(sr.rcol) <= FTINY) {
512 > #if SHADCACHE
513 >                                srcblocker(&sr);        /* add blocker to cache */
514 > #endif
515 >                                continue;
516 >                        }
517 >                        if (r->gecc <= FTINY)           /* compute P(theta) */
518 >                                d = 1.;
519 >                        else {
520 >                                d = DOT(r->rdir, sr.rdir);
521 >                                d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
522 >                                d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
523 >                        }
524 >                                                        /* other factors */
525 >                        d *= si.dom * r->rot / (4.*PI*nsamps);
526 >                        multcolor(sr.rcol, r->cext);
527 >                        multcolor(sr.rcol, r->albedo);
528 >                        scalecolor(sr.rcol, d);
529 >                        multcolor(sr.rcol, cvext);
530 >                        addcolor(r->rcol, sr.rcol);     /* add it in */
531 >                }
532 >        }
533 >        samplendx = oldsampndx;
534 > }
535  
536 +
537 + /****************************************************************
538 + * The following macros were separated from the m_light() routine
539 + * because they are very nasty and difficult to understand.
540 + */
541 +
542 + /* illumblock *
543 + *
544 + * We cannot allow an illum to pass to another illum, because that
545 + * would almost certainly constitute overcounting.
546 + * However, we do allow an illum to pass to another illum
547 + * that is actually going to relay to a virtual light source.
548 + * We also prevent an illum from passing to a glow; this provides a
549 + * convenient mechanism for defining detailed light source
550 + * geometry behind (or inside) an effective radiator.
551 + */
552 +
553 + static int
554 + weaksrcmat(OBJECT obj)          /* identify material */
555 + {
556 +        OBJREC *o = findmaterial(objptr(obj));
557 +        
558 +        if (o == NULL) return 0;
559 +        return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW));
560 + }
561 +
562 + #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \
563 +                                r->rod > 0.0 && \
564 +                                weaksrcmat(source[r->rsrc].so->omod))
565 +
566 + /* wrongsource *
567 + *
568 + * This source is the wrong source (ie. overcounted) if we are
569 + * aimed to a different source than the one we hit and the one
570 + * we hit is not an illum that should be passed.
571 + */
572 +
573 + #define  wrongsource(m, r)      (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \
574 +                                (m->otype!=MAT_ILLUM || illumblock(m,r)))
575 +
576 + /* distglow *
577 + *
578 + * A distant glow is an object that sometimes acts as a light source,
579 + * but is too far away from the test point to be one in this case.
580 + * (Glows with negative radii should NEVER participate in illumination.)
581 + */
582 +
583 + #define  distglow(m, r, d)      (m->otype==MAT_GLOW && \
584 +                                m->oargs.farg[3] >= -FTINY && \
585 +                                d > m->oargs.farg[3])
586 +
587 + /* badcomponent *
588 + *
589 + * We must avoid counting light sources in the ambient calculation,
590 + * since the direct component is handled separately.  Therefore, any
591 + * ambient ray which hits an active light source must be discarded.
592 + * The same is true for stray specular samples, since the specular
593 + * contribution from light sources is calculated separately.
594 + */
595 +
596 + #define  badcomponent(m, r)     (r->crtype&(AMBIENT|SPECULAR) && \
597 +                                !(r->crtype&SHADOW || r->rod < 0.0 || \
598 +                /* not 100% correct */  distglow(m, r, r->rot)))
599 +
600 + /* passillum *
601 + *
602 + * An illum passes to another material type when we didn't hit it
603 + * on purpose (as part of a direct calculation), or it is relaying
604 + * a virtual light source.
605 + */
606 +
607   #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
608 <                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
608 >                                (r->rsrc<0 || source[r->rsrc].so!=r->ro || \
609 >                                source[r->rsrc].sflags&SVIRTUAL))
610  
611 + /* srcignore *
612 + *
613 + * The -dv flag is normally on for sources to be visible.
614 + */
615  
616 < m_light(m, r)                   /* ray hit a light source */
617 < register OBJREC  *m;
618 < register RAY  *r;
616 > #define  srcignore(m, r)        !(directvis || r->crtype&SHADOW || \
617 >                                distglow(m, r, raydist(r,PRIMARY)))
618 >
619 >
620 > extern int
621 > m_light(                                /* ray hit a light source */
622 >        register OBJREC  *m,
623 >        register RAY  *r
624 > )
625   {
423                                                /* check for behind */
424        if (r->rod < 0.0)
425                return;
626                                                  /* check for over-counting */
627 <        if (wrongsource(m, r) || badambient(m, r))
628 <                return;
627 >        if (badcomponent(m, r))
628 >                return(1);
629 >        if (wrongsource(m, r))
630 >                return(1);
631                                                  /* check for passed illum */
632          if (passillum(m, r)) {
633 <
634 <                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
635 <                        raytrans(r);
636 <                else
637 <                        rayshade(r, modifier(m->oargs.sarg[0]));
638 <
639 <                                                /* otherwise treat as source */
640 <        } else {
633 >                if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
634 >                        return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0])));
635 >                raytrans(r);
636 >                return(1);
637 >        }
638 >                                        /* otherwise treat as source */
639 >                                                /* check for behind */
640 >        if (r->rod < 0.0)
641 >                return(1);
642 >                                                /* check for invisibility */
643 >        if (srcignore(m, r))
644 >                return(1);
645 >                                                /* check for outside spot */
646 >        if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
647 >                return(1);
648                                                  /* get distribution pattern */
649 <                raytexture(r, m->omod);
649 >        raytexture(r, m->omod);
650                                                  /* get source color */
651 <                setcolor(r->rcol, m->oargs.farg[0],
652 <                                  m->oargs.farg[1],
653 <                                  m->oargs.farg[2]);
651 >        setcolor(r->rcol, m->oargs.farg[0],
652 >                          m->oargs.farg[1],
653 >                          m->oargs.farg[2]);
654                                                  /* modify value */
655 <                multcolor(r->rcol, r->pcol);
656 <        }
655 >        multcolor(r->rcol, r->pcol);
656 >        return(1);
657   }
449
450
451 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines