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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines