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.34 by greg, Thu Jun 20 09:03:57 1991 UTC vs.
Revision 2.64 by greg, Tue Feb 24 19:39:27 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines