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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines