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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines