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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines