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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines