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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines