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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines