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

Comparing ray/src/rt/source.c (file contents):
Revision 1.34 by greg, Thu Jun 20 09:03:57 1991 UTC vs.
Revision 2.20 by greg, Sat Dec 9 11:30:19 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "octree.h"
16  
17 #include  "source.h"
18
17   #include  "otypes.h"
18  
19 < #include  "cone.h"
19 > #include  "source.h"
20  
23 #include  "face.h"
24
21   #include  "random.h"
22  
23 + extern double  ssampdist;               /* scatter sampling distance */
24  
25 < extern double  dstrsrc;                 /* source distribution amount */
26 < extern double  shadthresh;              /* relative shadow threshold */
27 < extern double  shadcert;                /* shadow testing certainty */
25 > /*
26 > * Structures used by direct()
27 > */
28  
29 < SRCREC  *source = NULL;                 /* our list of sources */
30 < int  nsources = 0;                      /* the number of sources */
29 > typedef struct {
30 >        int  sno;               /* source number */
31 >        FVECT  dir;             /* source direction */
32 >        COLOR  coef;            /* material coefficient */
33 >        COLOR  val;             /* contribution */
34 > }  CONTRIB;             /* direct contribution */
35  
36 + typedef struct {
37 +        int  sndx;              /* source index (to CONTRIB array) */
38 +        float  brt;             /* brightness (for comparison) */
39 + }  CNTPTR;              /* contribution pointer */
40 +
41   static CONTRIB  *srccnt;                /* source contributions in direct() */
42   static CNTPTR  *cntord;                 /* source ordering in direct() */
43 + static int  maxcntr = 0;                /* size of contribution arrays */
44  
45  
46   marksources()                   /* find and mark source objects */
47   {
48 +        int  foundsource = 0;
49          int  i;
50          register OBJREC  *o, *m;
51 <        register SRCREC  *ns;
52 <
51 >        register int  ns;
52 >                                        /* initialize dispatch table */
53 >        initstypes();
54 >                                        /* find direct sources */
55          for (i = 0; i < nobjects; i++) {
56          
57                  o = objptr(i);
58  
59 <                if (o->omod == OVOID)
59 >                if (!issurface(o->otype) || o->omod == OVOID)
60                          continue;
61  
62                  m = objptr(o->omod);
# Line 62 | Line 72 | marksources()                  /* find and mark source objects */
72                                  o->otype != OBJ_SOURCE &&
73                                  m->oargs.farg[3] <= FTINY)
74                          continue;                       /* don't bother */
75 +                if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY &&
76 +                                m->oargs.farg[2] <= FTINY)
77 +                        continue;                       /* don't bother */
78  
79 <                if ((ns = newsource()) == NULL)
79 >                if (sfun[o->otype].of == NULL ||
80 >                                sfun[o->otype].of->setsrc == NULL)
81 >                        objerror(o, USER, "illegal material");
82 >
83 >                if ((ns = newsource()) < 0)
84                          goto memerr;
85  
86 <                setsource(ns, o);
86 >                setsource(&source[ns], o);
87  
88                  if (m->otype == MAT_GLOW) {
89 <                        ns->sflags |= SPROX;
90 <                        ns->sl.prox = m->oargs.farg[3];
91 <                        if (o->otype == OBJ_SOURCE)
92 <                                ns->sflags |= SSKIP;
89 >                        source[ns].sflags |= SPROX;
90 >                        source[ns].sl.prox = m->oargs.farg[3];
91 >                        if (source[ns].sflags & SDISTANT)
92 >                                source[ns].sflags |= SSKIP;
93                  } else if (m->otype == MAT_SPOT) {
94 <                        ns->sflags |= SSPOT;
95 <                        if ((ns->sl.s = makespot(m)) == NULL)
94 >                        source[ns].sflags |= SSPOT;
95 >                        if ((source[ns].sl.s = makespot(m)) == NULL)
96                                  goto memerr;
97 +                        if (source[ns].sflags & SFLAT &&
98 +                                !checkspot(source[ns].sl.s,source[ns].snorm)) {
99 +                                objerror(o, WARNING,
100 +                                        "invalid spotlight direction");
101 +                                source[ns].sflags |= SSKIP;
102 +                        }
103                  }
104 +                if (!(source[ns].sflags & SSKIP))
105 +                        foundsource++;
106          }
107 <        if (nsources <= 0) {
107 >        if (!foundsource) {
108                  error(WARNING, "no light sources found");
109                  return;
110          }
111          markvirtuals();                 /* find and add virtual sources */
112 <        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
113 <        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
114 <        if (srccnt != NULL && cntord != NULL)
112 >                                /* allocate our contribution arrays */
113 >        maxcntr = nsources + MAXSPART;  /* start with this many */
114 >        srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
115 >        cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR));
116 >        if (srccnt == NULL | cntord == NULL)
117                  goto memerr;
118          return;
119   memerr:
# Line 94 | Line 121 | memerr:
121   }
122  
123  
124 < SRCREC *
125 < newsource()                     /* allocate new source in our array */
124 > srcray(sr, r, si)               /* send a ray to a source, return domega */
125 > register RAY  *sr;              /* returned source ray */
126 > RAY  *r;                        /* ray which hit object */
127 > SRCINDEX  *si;                  /* source sample index */
128   {
129 <        if (nsources == 0)
130 <                source = (SRCREC *)malloc(sizeof(SRCREC));
102 <        else
103 <                source = (SRCREC *)realloc((char *)source,
104 <                                (unsigned)(nsources+1)*sizeof(SRCREC));
105 <        if (source == NULL)
106 <                return(NULL);
107 <        source[nsources].sflags = 0;
108 <        source[nsources].nhits = 1;
109 <        source[nsources].ntests = 2;    /* initial hit probability = 1/2 */
110 <        return(&source[nsources++]);
111 < }
129 >    double  d;                          /* distance to source */
130 >    register SRCREC  *srcp;
131  
132 +    rayorigin(sr, r, SHADOW, 1.0);              /* ignore limits */
133  
134 < setsource(src, so)                      /* add a source to the array */
135 < register SRCREC  *src;
136 < register OBJREC  *so;
137 < {
138 <        double  cos(), tan(), sqrt();
139 <        double  theta;
140 <        FACE  *f;
121 <        CONE  *co;
122 <        int  j;
123 <        register int  i;
124 <        
125 <        src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
126 <        src->so = so;
127 <
128 <        switch (so->otype) {
129 <        case OBJ_SOURCE:
130 <                if (so->oargs.nfargs != 4)
131 <                        objerror(so, USER, "bad arguments");
132 <                src->sflags |= SDISTANT;
133 <                VCOPY(src->sloc, so->oargs.farg);
134 <                if (normalize(src->sloc) == 0.0)
135 <                        objerror(so, USER, "zero direction");
136 <                theta = PI/180.0/2.0 * so->oargs.farg[3];
137 <                if (theta <= FTINY)
138 <                        objerror(so, USER, "zero size");
139 <                src->ss = theta >= PI/4 ? 1.0 : tan(theta);
140 <                src->ss2 = 2.0*PI * (1.0 - cos(theta));
141 <                break;
142 <        case OBJ_SPHERE:
143 <                VCOPY(src->sloc, so->oargs.farg);
144 <                src->ss = so->oargs.farg[3];
145 <                src->ss2 = PI * src->ss * src->ss;
146 <                break;
147 <        case OBJ_FACE:
148 <                                                /* get the face */
149 <                f = getface(so);
150 <                                                /* find the center */
151 <                for (j = 0; j < 3; j++) {
152 <                        src->sloc[j] = 0.0;
153 <                        for (i = 0; i < f->nv; i++)
154 <                                src->sloc[j] += VERTEX(f,i)[j];
155 <                        src->sloc[j] /= (double)f->nv;
156 <                }
157 <                if (!inface(src->sloc, f))
158 <                        objerror(so, USER, "cannot hit center");
159 <                src->sflags |= SFLAT;
160 <                VCOPY(src->snorm, f->norm);
161 <                src->ss = sqrt(f->area / PI);
162 <                src->ss2 = f->area;
163 <                break;
164 <        case OBJ_RING:
165 <                                                /* get the ring */
166 <                co = getcone(so, 0);
167 <                VCOPY(src->sloc, CO_P0(co));
168 <                if (CO_R0(co) > 0.0)
169 <                        objerror(so, USER, "cannot hit center");
170 <                src->sflags |= SFLAT;
171 <                VCOPY(src->snorm, co->ad);
172 <                src->ss = CO_R1(co);
173 <                src->ss2 = PI * src->ss * src->ss;
174 <                break;
175 <        default:
176 <                objerror(so, USER, "illegal material");
134 >    while ((d = nextssamp(sr, si)) != 0.0) {
135 >        sr->rsrc = si->sn;                      /* remember source */
136 >        srcp = source + si->sn;
137 >        if (srcp->sflags & SDISTANT) {
138 >                if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
139 >                        continue;
140 >                return(1);              /* sample OK */
141          }
142 +                                /* local source */
143 +                                                /* check proximity */
144 +        if (srcp->sflags & SPROX && d > srcp->sl.prox)
145 +                continue;
146 +                                                /* check angle */
147 +        if (srcp->sflags & SSPOT) {
148 +                if (spotout(sr, srcp->sl.s))
149 +                        continue;
150 +                                        /* adjust solid angle */
151 +                si->dom *= d*d;
152 +                d += srcp->sl.s->flen;
153 +                si->dom /= d*d;
154 +        }
155 +        return(1);                      /* sample OK */
156 +    }
157 +    return(0);                  /* no more samples */
158   }
159  
160  
161 < SPOT *
162 < makespot(m)                     /* make a spotlight */
183 < register OBJREC  *m;
161 > srcvalue(r)                     /* punch ray to source and compute value */
162 > register RAY  *r;
163   {
164 <        extern double  cos();
186 <        register SPOT  *ns;
164 >        register SRCREC  *sp;
165  
166 <        if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
167 <                return(NULL);
168 <        ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
169 <        VCOPY(ns->aim, m->oargs.farg+4);
170 <        if ((ns->flen = normalize(ns->aim)) == 0.0)
171 <                objerror(m, USER, "zero focus vector");
172 <        return(ns);
173 < }
174 <
197 <
198 < double
199 < srcray(sr, r, sn)               /* send a ray to a source, return domega */
200 < register RAY  *sr;              /* returned source ray */
201 < RAY  *r;                        /* ray which hit object */
202 < register int  sn;               /* source number */
203 < {
204 <        double  ddot;                   /* (distance times) cosine */
205 <        FVECT  vd;
206 <        double  d;
207 <        register int  i;
208 <
209 <        if (source[sn].sflags & SSKIP)
210 <                return(0.0);                    /* skip this source */
211 <
212 <        rayorigin(sr, r, SHADOW, 1.0);          /* ignore limits */
213 <
214 <        sr->rsrc = sn;                          /* remember source */
215 <                                                /* get source direction */
216 <        if (source[sn].sflags & SDISTANT) {
217 <                if (source[sn].sflags & SSPOT) {        /* check location */
218 <                        for (i = 0; i < 3; i++)
219 <                                vd[i] = sr->rorg[i] - source[sn].sl.s->aim[i];
220 <                        d = DOT(source[sn].sloc,vd);
221 <                        d = DOT(vd,vd) - d*d;
222 <                        if (PI*d > source[sn].sl.s->siz)
223 <                                return(0.0);
224 <                }
225 <                                                /* constant direction */
226 <                VCOPY(sr->rdir, source[sn].sloc);
227 <        } else {                                /* compute direction */
228 <                for (i = 0; i < 3; i++)
229 <                        sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
230 <
231 <                if (source[sn].sflags & SFLAT &&
232 <                        (ddot = -DOT(sr->rdir, source[sn].snorm)) <= FTINY)
233 <                        return(0.0);            /* behind surface! */
166 >        sp = &source[r->rsrc];
167 >        if (sp->sflags & SVIRTUAL) {    /* virtual source */
168 >                                        /* check intersection */
169 >                if (!(*ofun[sp->so->otype].funp)(sp->so, r))
170 >                        return;
171 >                if (!rayshade(r, r->ro->omod))  /* compute contribution */
172 >                        goto nomat;
173 >                rayparticipate(r);
174 >                return;
175          }
176 <        if (dstrsrc > FTINY) {
177 <                                        /* distribute source direction */
178 <                dimlist[ndims++] = sn;
179 <                for (i = 0; i < 3; i++) {
180 <                        dimlist[ndims] = i + 8831;
181 <                        vd[i] = dstrsrc * source[sn].ss *
182 <                (1.0 - 2.0*urand(ilhash(dimlist,ndims+1)+samplendx));
183 <                }
184 <                ndims--;
244 <                if (source[sn].sflags & SFLAT) {        /* project offset */
245 <                        d = DOT(vd, source[sn].snorm);
246 <                        for (i = 0; i < 3; i++)
247 <                                vd[i] -= d * source[sn].snorm[i];
248 <                }
249 <                for (i = 0; i < 3; i++)         /* offset source direction */
250 <                        sr->rdir[i] += vd[i];
251 <
252 <        } else if (source[sn].sflags & SDISTANT)
253 <                                                /* already normalized */
254 <                return(source[sn].ss2);
255 <
256 <        if ((d = normalize(sr->rdir)) == 0.0)
257 <                                                /* at source! */
258 <                return(0.0);
259 <        
260 <        if (source[sn].sflags & SDISTANT)
261 <                                                /* domega constant */
262 <                return(source[sn].ss2);
263 <
264 <                                                /* check proximity */
265 <        if (source[sn].sflags & SPROX &&
266 <                        d > source[sn].sl.prox)
267 <                return(0.0);
268 <                                                /* compute dot product */
269 <        if (source[sn].sflags & SFLAT)
270 <                ddot /= d;
271 <        else
272 <                ddot = 1.0;
273 <                                                /* check angle */
274 <        if (source[sn].sflags & SSPOT) {
275 <                if (source[sn].sl.s->siz < 2.0*PI *
276 <                                (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
277 <                        return(0.0);
278 <                d += source[sn].sl.s->flen;     /* adjust length */
176 >                                        /* compute intersection */
177 >        if (sp->sflags & SDISTANT ? sourcehit(r) :
178 >                        (*ofun[sp->so->otype].funp)(sp->so, r)) {
179 >                if (sp->sa.success >= 0)
180 >                        sp->sa.success++;
181 >                if (!rayshade(r, r->ro->omod))  /* compute contribution */
182 >                        goto nomat;
183 >                rayparticipate(r);
184 >                return;
185          }
186 <                                                /* compute domega */
187 <        return(ddot*source[sn].ss2/(d*d));
186 >                                        /* we missed our mark! */
187 >        if (sp->sa.success < 0)
188 >                return;                 /* bitched already */
189 >        sp->sa.success -= AIMREQT;
190 >        if (sp->sa.success >= 0)
191 >                return;                 /* leniency */
192 >        sprintf(errmsg, "aiming failure for light source \"%s\"",
193 >                        sp->so->oname);
194 >        error(WARNING, errmsg);         /* issue warning */
195 >        return;
196 > nomat:
197 >        objerror(r->ro, USER, "material not found");
198   }
199  
200  
# Line 294 | Line 210 | register RAY  *r;
210                  first = 0; last = nsources-1;
211          }
212          for (i = first; i <= last; i++)
213 <                if (source[i].sflags & SDISTANT)
213 >                if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT)
214                          /*
215                           * Check to see if ray is within
216                           * solid angle of source.
# Line 334 | Line 250 | RAY  *r;                       /* ray that hit surface */
250   int  (*f)();                    /* direct component coefficient function */
251   char  *p;                       /* data for f */
252   {
253 <        extern double  pow();
253 >        extern int  (*trace)();
254          register int  sn;
255 +        register CONTRIB  *scp;
256 +        SRCINDEX  si;
257          int  nshadcheck, ncnts;
258          int  nhits;
259 <        double  dom, prob, ourthresh, hwt;
259 >        double  prob, ourthresh, hwt;
260          RAY  sr;
261                          /* NOTE: srccnt and cntord global so no recursion */
262          if (nsources <= 0)
263                  return;         /* no sources?! */
346                                                /* compute number to check */
347        nshadcheck = pow((double)nsources, shadcert) + .5;
348                                                /* modify threshold */
349        ourthresh = shadthresh / r->rweight;
264                                                  /* potential contributions */
265 <        for (sn = 0; sn < nsources; sn++) {
266 <                cntord[sn].sno = sn;
267 <                cntord[sn].brt = 0.0;
268 <                                                /* get source ray */
269 <                if ((dom = srcray(&sr, r, sn)) == 0.0)
270 <                        continue;
271 <                VCOPY(srccnt[sn].dir, sr.rdir);
265 >        initsrcindex(&si);
266 >        for (sn = 0; srcray(&sr, r, &si); sn++) {
267 >                if (sn >= maxcntr) {
268 >                        maxcntr = sn + MAXSPART;
269 >                        srccnt = (CONTRIB *)realloc((char *)srccnt,
270 >                                        maxcntr*sizeof(CONTRIB));
271 >                        cntord = (CNTPTR *)realloc((char *)cntord,
272 >                                        maxcntr*sizeof(CNTPTR));
273 >                        if (srccnt == NULL | cntord == NULL)
274 >                                error(SYSTEM, "out of memory in direct");
275 >                }
276 >                cntord[sn].sndx = sn;
277 >                scp = srccnt + sn;
278 >                scp->sno = sr.rsrc;
279                                                  /* compute coefficient */
280 <                (*f)(srccnt[sn].coef, p, srccnt[sn].dir, dom);
281 <                cntord[sn].brt = bright(srccnt[sn].coef);
280 >                (*f)(scp->coef, p, sr.rdir, si.dom);
281 >                cntord[sn].brt = bright(scp->coef);
282                  if (cntord[sn].brt <= 0.0)
283                          continue;
284 <                                                /* compute contribution */
285 <                srcvalue(&sr);
286 <                copycolor(srccnt[sn].val, sr.rcol);
287 <                multcolor(srccnt[sn].val, srccnt[sn].coef);
288 <                cntord[sn].brt = bright(srccnt[sn].val);
284 >                VCOPY(scp->dir, sr.rdir);
285 >                                                /* compute potential */
286 >                sr.revf = srcvalue;
287 >                rayvalue(&sr);
288 >                copycolor(scp->val, sr.rcol);
289 >                multcolor(scp->val, scp->coef);
290 >                cntord[sn].brt = bright(scp->val);
291          }
292                                                  /* sort contributions */
293 <        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
293 >        qsort(cntord, sn, sizeof(CNTPTR), cntcmp);
294          {                                       /* find last */
295                  register int  l, m;
296  
297 <                sn = 0; ncnts = l = nsources;
297 >                ncnts = l = sn;
298 >                sn = 0;
299                  while ((m = (sn + ncnts) >> 1) != l) {
300                          if (cntord[m].brt > 0.0)
301                                  sn = m;
# Line 380 | Line 304 | char  *p;                      /* data for f */
304                          l = m;
305                  }
306          }
307 +        if (ncnts == 0)
308 +                return;         /* no contributions! */
309                                                  /* accumulate tail */
310          for (sn = ncnts-1; sn > 0; sn--)
311                  cntord[sn-1].brt += cntord[sn].brt;
312 +                                                /* compute number to check */
313 +        nshadcheck = pow((double)ncnts, shadcert) + .5;
314 +                                                /* modify threshold */
315 +        ourthresh = shadthresh / r->rweight;
316                                                  /* test for shadows */
317 <        nhits = 0;
318 <        for (sn = 0; sn < ncnts; sn++) {
317 >        for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts;
318 >                        hwt += (double)source[scp->sno].nhits /
319 >                                (double)source[scp->sno].ntests,
320 >                        sn++) {
321                                                  /* check threshold */
322                  if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
323                                  cntord[sn].brt-cntord[sn+nshadcheck].brt)
324                                  < ourthresh*bright(r->rcol))
325                          break;
326 <                                                /* get statistics */
395 <                source[cntord[sn].sno].ntests++;
326 >                scp = srccnt + cntord[sn].sndx;
327                                                  /* test for hit */
328                  rayorigin(&sr, r, SHADOW, 1.0);
329 <                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
330 <                sr.rsrc = cntord[sn].sno;
329 >                VCOPY(sr.rdir, scp->dir);
330 >                sr.rsrc = scp->sno;
331 >                source[scp->sno].ntests++;      /* keep statistics */
332                  if (localhit(&sr, &thescene) &&
333 <                                ( sr.ro != source[cntord[sn].sno].so ||
334 <                                source[cntord[sn].sno].sflags & SFOLLOW )) {
333 >                                ( sr.ro != source[scp->sno].so ||
334 >                                source[scp->sno].sflags & SFOLLOW )) {
335                                                  /* follow entire path */
336 <                        raycont(&sr);
336 >                        if (!raycont(&sr))
337 >                                objerror(sr.ro, USER, "material not found");
338 >                        rayparticipate(&sr);
339 >                        if (trace != NULL)
340 >                                (*trace)(&sr);  /* trace execution */
341                          if (bright(sr.rcol) <= FTINY)
342                                  continue;       /* missed! */
343 <                        copycolor(srccnt[cntord[sn].sno].val, sr.rcol);
344 <                        multcolor(srccnt[cntord[sn].sno].val,
409 <                                        srccnt[cntord[sn].sno].coef);
343 >                        copycolor(scp->val, sr.rcol);
344 >                        multcolor(scp->val, scp->coef);
345                  }
346                                                  /* add contribution if hit */
347 <                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
347 >                addcolor(r->rcol, scp->val);
348                  nhits++;
349 <                source[cntord[sn].sno].nhits++;
349 >                source[scp->sno].nhits++;
350          }
351 <                                        /* surface hit rate */
352 <        if (sn > 0)
353 <                hwt = (double)nhits / (double)sn;
351 >                                        /* source hit rate */
352 >        if (hwt > FTINY)
353 >                hwt = (double)nhits / hwt;
354          else
355                  hwt = 0.5;
356   #ifdef DEBUG
357 <        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
357 >        sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n",
358                          sn, ncnts-sn, hwt);
359          eputs(errmsg);
360   #endif
361                                          /* add in untested sources */
362          for ( ; sn < ncnts; sn++) {
363 <                prob = hwt * (double)source[cntord[sn].sno].nhits /
364 <                                (double)source[cntord[sn].sno].ntests;
365 <                scalecolor(srccnt[cntord[sn].sno].val, prob);
366 <                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
363 >                scp = srccnt + cntord[sn].sndx;
364 >                prob = hwt * (double)source[scp->sno].nhits /
365 >                                (double)source[scp->sno].ntests;
366 >                if (prob > 1.0)
367 >                        prob = 1.0;
368 >                scalecolor(scp->val, prob);
369 >                addcolor(r->rcol, scp->val);
370          }
371   }
372  
373  
374 < srcvalue(r)                     /* punch ray to source and compute value */
375 < RAY  *r;
374 > srcscatter(r)                   /* compute source scattering into ray */
375 > register RAY  *r;
376   {
377 <        register SRCREC  *sp;
377 >        int  oldsampndx;
378 >        int  nsamps;
379 >        RAY  sr;
380 >        SRCINDEX  si;
381 >        double  t, lastt, d;
382 >        COLOR  cumval, ctmp;
383 >        int  i, j;
384  
385 <        sp = &source[r->rsrc];
442 <        if (sp->sflags & SVIRTUAL) {            /* virtual source */
443 <                RAY  nr;
444 <                                        /* check intersection */
445 <                if (!(*ofun[sp->so->otype].funp)(sp->so, r))
446 <                        return;
447 <                                        /* relay ray to source */
448 <                vsrcrelay(&nr, r);
449 <                srcvalue(&nr);
385 >        if (r->slights == NULL || r->slights[0] == 0 || r->gecc >= 1.-FTINY)
386                  return;
387 +        if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
388 +                nsamps = 1;
389 +        oldsampndx = samplendx;
390 +        samplendx = random()&0x7fff;            /* randomize */
391 +        initsrcindex(&si);
392 +        for (i = r->slights[0]; i > 0; i--) {   /* for each source */
393 +                setcolor(cumval, 0., 0., 0.);
394 +                lastt = r->rot;
395 +                for (j = nsamps; j-- > 0; ) {   /* for each sample position */
396 +                        samplendx++;
397 +                        t = r->rot * (j+frandom())/nsamps;
398 +                        sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
399 +                        sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
400 +                        sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
401 +                        sr.rmax = 0.;
402 +                                                /* sample ray to this source */
403 +                        if (si.sp >= si.np-1 || !srcray(&sr, NULL, &si) ||
404 +                                        sr.rsrc != r->slights[i]) {
405 +                                si.sn = r->slights[i]-1;        /* reset */
406 +                                si.np = 0;
407 +                                if (!srcray(&sr, NULL, &si) ||
408 +                                                sr.rsrc != r->slights[i])
409 +                                        continue;               /* no path */
410 +                        }
411 +                        copycolor(sr.cext, r->cext);
412 +                        sr.albedo = r->albedo;
413 +                        sr.gecc = r->gecc;
414 +                        rayvalue(&sr);                  /* eval. source ray */
415 +                        if (bright(sr.rcol) <= FTINY)
416 +                                continue;
417 +                                                        /* compute fall-off */
418 +                        d = lastt - t;
419 +                        setcolor(ctmp,  1.-d*colval(r->cext,RED),
420 +                                        1.-d*colval(r->cext,GRN),
421 +                                        1.-d*colval(r->cext,BLU));
422 +                        multcolor(cumval, ctmp);
423 +                        lastt = t;
424 +                        if (r->gecc <= FTINY)           /* compute P(theta) */
425 +                                d = 1.;
426 +                        else {
427 +                                d = DOT(r->rdir, sr.rdir);
428 +                                d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d);
429 +                                d = (1. - r->gecc*r->gecc) / (d*d*d);
430 +                        }
431 +                                                        /* other factors */
432 +                        d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps);
433 +                        multcolor(sr.rcol, r->cext);
434 +                        scalecolor(sr.rcol, d);
435 +                        addcolor(cumval, sr.rcol);
436 +                }
437 +                                                /* final fall-off */
438 +                setcolor(ctmp,  1.-lastt*colval(r->cext,RED),
439 +                                1.-lastt*colval(r->cext,GRN),
440 +                                1.-lastt*colval(r->cext,BLU));
441 +                multcolor(cumval, ctmp);
442 +                addcolor(r->rcol, cumval);      /* sum into ray result */
443          }
444 <                                        /* compute intersection */
453 <        if (sp->sflags & SDISTANT ? sourcehit(r) :
454 <                        (*ofun[sp->so->otype].funp)(sp->so, r)) {
455 <                if (sp->sa.success >= 0)
456 <                        sp->sa.success++;
457 <                raycont(r);             /* compute contribution */
458 <                return;
459 <        }
460 <        if (sp->sa.success < 0)
461 <                return;                 /* bitched already */
462 <        sp->sa.success -= AIMREQT;
463 <        if (sp->sa.success >= 0)
464 <                return;                 /* leniency */
465 <        sprintf(errmsg, "aiming failure for light source \"%s\"",
466 <                        sp->so->oname);
467 <        error(WARNING, errmsg);         /* issue warning */
444 >        samplendx = oldsampndx;
445   }
446  
447  
448 < #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
449 <                                r->rsrc>=0 && \
450 <                                source[r->rsrc].so!=r->ro)
448 > /****************************************************************
449 > * The following macros were separated from the m_light() routine
450 > * because they are very nasty and difficult to understand.
451 > */
452  
453 < #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
454 <                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
453 > /* illumblock *
454 > *
455 > * We cannot allow an illum to pass to another illum, because that
456 > * would almost certainly constitute overcounting.
457 > * However, we do allow an illum to pass to another illum
458 > * that is actually going to relay to a virtual light source.
459 > * We also prevent an illum from passing to a glow; this provides a
460 > * convenient mechanism for defining detailed light source
461 > * geometry behind (or inside) an effective radiator.
462 > */
463  
464 + static int weaksrcmod(obj) int obj;     /* efficiency booster function */
465 + {register OBJREC *o = objptr(obj);
466 + return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
467 +
468 + #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \
469 +                                r->rod > 0.0 && \
470 +                                weaksrcmod(source[r->rsrc].so->omod))
471 +
472 + /* wrongsource *
473 + *
474 + * This source is the wrong source (ie. overcounted) if we are
475 + * aimed to a different source than the one we hit and the one
476 + * we hit is not an illum that should be passed.
477 + */
478 +
479 + #define  wrongsource(m, r)      (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \
480 +                                (m->otype!=MAT_ILLUM || illumblock(m,r)))
481 +
482 + /* distglow *
483 + *
484 + * A distant glow is an object that sometimes acts as a light source,
485 + * but is too far away from the test point to be one in this case.
486 + * (Glows with negative radii should NEVER participate in illumination.)
487 + */
488 +
489 + #define  distglow(m, r, d)      (m->otype==MAT_GLOW && \
490 +                                m->oargs.farg[3] >= -FTINY && \
491 +                                d > m->oargs.farg[3])
492 +
493 + /* badcomponent *
494 + *
495 + * We must avoid counting light sources in the ambient calculation,
496 + * since the direct component is handled separately.  Therefore, any
497 + * ambient ray which hits an active light source must be discarded.
498 + * The same is true for stray specular samples, since the specular
499 + * contribution from light sources is calculated separately.
500 + */
501 +
502 + #define  badcomponent(m, r)     (r->crtype&(AMBIENT|SPECULAR) && \
503 +                                !(r->crtype&SHADOW || r->rod < 0.0 || \
504 +                /* not 100% correct */  distglow(m, r, r->rot)))
505 +
506 + /* passillum *
507 + *
508 + * An illum passes to another material type when we didn't hit it
509 + * on purpose (as part of a direct calculation), or it is relaying
510 + * a virtual light source.
511 + */
512 +
513   #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
514 <                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
514 >                                (r->rsrc<0 || source[r->rsrc].so!=r->ro || \
515 >                                source[r->rsrc].sflags&SVIRTUAL))
516  
517 + /* srcignore *
518 + *
519 + * The -dv flag is normally on for sources to be visible.
520 + */
521  
522 + #define  srcignore(m, r)        !(directvis || r->crtype&SHADOW || \
523 +                                distglow(m, r, raydist(r,PRIMARY)))
524 +
525 +
526   m_light(m, r)                   /* ray hit a light source */
527   register OBJREC  *m;
528   register RAY  *r;
529   {
530                                                  /* check for over-counting */
531 <        if (wrongsource(m, r) || badambient(m, r))
532 <                return;
531 >        if (badcomponent(m, r))
532 >                return(1);
533 >        if (wrongsource(m, r))
534 >                return(1);
535                                                  /* check for passed illum */
536          if (passillum(m, r)) {
537 <
538 <                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
539 <                        raytrans(r);
540 <                else
541 <                        rayshade(r, modifier(m->oargs.sarg[0]));
542 <
497 <                                                /* otherwise treat as source */
498 <        } else {
537 >                if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
538 >                        return(rayshade(r, modifier(m->oargs.sarg[0])));
539 >                raytrans(r);
540 >                return(1);
541 >        }
542 >                                        /* otherwise treat as source */
543                                                  /* check for behind */
544 <                if (r->rod < 0.0)
545 <                        return;
544 >        if (r->rod < 0.0)
545 >                return(1);
546 >                                                /* check for invisibility */
547 >        if (srcignore(m, r))
548 >                return(1);
549 >                                                /* check for outside spot */
550 >        if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
551 >                return(1);
552                                                  /* get distribution pattern */
553 <                raytexture(r, m->omod);
553 >        raytexture(r, m->omod);
554                                                  /* get source color */
555 <                setcolor(r->rcol, m->oargs.farg[0],
556 <                                  m->oargs.farg[1],
557 <                                  m->oargs.farg[2]);
555 >        setcolor(r->rcol, m->oargs.farg[0],
556 >                          m->oargs.farg[1],
557 >                          m->oargs.farg[2]);
558                                                  /* modify value */
559 <                multcolor(r->rcol, r->pcol);
560 <        }
559 >        multcolor(r->rcol, r->pcol);
560 >        return(1);
561   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines