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.3 by greg, Thu Apr 27 12:44:13 1989 UTC vs.
Revision 2.23 by greg, Thu Mar 21 15:33:09 1996 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 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 12 | Line 12 | static char SCCSid[] = "$SunId$ LBL";
12  
13   #include  "ray.h"
14  
15 < #include  "source.h"
15 > #include  "octree.h"
16  
17   #include  "otypes.h"
18  
19 < #include  "cone.h"
19 > #include  "source.h"
20  
21 #include  "face.h"
22
21   #include  "random.h"
22  
23 + extern double  ssampdist;               /* scatter sampling distance */
24  
25 < extern double  dstrsrc;                 /* source distribution amount */
25 > #ifndef MAXSSAMP
26 > #define MAXSSAMP        16              /* maximum samples per ray */
27 > #endif
28  
29 < SOURCE  srcval[MAXSOURCE];              /* our array 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);
67  
68 <                if (m->otype != MAT_LIGHT &&
47 <                                m->otype != MAT_ILLUM &&
48 <                                m->otype != MAT_GLOW &&
49 <                                m->otype != MAT_SPOT)
68 >                if (!islight(m->otype))
69                          continue;
70          
71                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
# Line 57 | 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 (nsources >= MAXSOURCE)
84 <                        error(INTERNAL, "too many sources in marksources");
83 >                if (sfun[o->otype].of == NULL ||
84 >                                sfun[o->otype].of->setsrc == NULL)
85 >                        objerror(o, USER, "illegal material");
86  
87 <                newsource(&srcval[nsources], o);
87 >                if ((ns = newsource()) < 0)
88 >                        goto memerr;
89  
90 +                setsource(&source[ns], o);
91 +
92                  if (m->otype == MAT_GLOW) {
93 <                        srcval[nsources].sflags |= SPROX;
94 <                        srcval[nsources].sl.prox = m->oargs.farg[3];
95 <                        if (o->otype == OBJ_SOURCE)
96 <                                srcval[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 <                        srcval[nsources].sflags |= SSPOT;
99 <                        srcval[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 < }
112 <
113 <
80 < newsource(src, so)                      /* add a source to the array */
81 < register SOURCE  *src;
82 < register OBJREC  *so;
83 < {
84 <        double  cos(), tan(), sqrt();
85 <        double  theta;
86 <        FACE  *f;
87 <        CONE  *co;
88 <        int  j;
89 <        register int  i;
90 <        
91 <        src->sflags = 0;
92 <        src->so = so;
93 <
94 <        switch (so->otype) {
95 <        case OBJ_SOURCE:
96 <                if (so->oargs.nfargs != 4)
97 <                        objerror(so, USER, "bad arguments");
98 <                src->sflags |= SDISTANT;
99 <                VCOPY(src->sloc, so->oargs.farg);
100 <                if (normalize(src->sloc) == 0.0)
101 <                        objerror(so, USER, "zero direction");
102 <                theta = PI/180.0/2.0 * so->oargs.farg[3];
103 <                if (theta <= FTINY)
104 <                        objerror(so, USER, "zero size");
105 <                src->ss = theta >= PI/4 ? 1.0 : tan(theta);
106 <                src->ss2 = 2.0*PI * (1.0 - cos(theta));
107 <                break;
108 <        case OBJ_SPHERE:
109 <                VCOPY(src->sloc, so->oargs.farg);
110 <                src->ss = so->oargs.farg[3];
111 <                src->ss2 = PI * src->ss * src->ss;
112 <                break;
113 <        case OBJ_FACE:
114 <                                                /* get the face */
115 <                f = getface(so);
116 <                                                /* find the center */
117 <                for (j = 0; j < 3; j++) {
118 <                        src->sloc[j] = 0.0;
119 <                        for (i = 0; i < f->nv; i++)
120 <                                src->sloc[j] += VERTEX(f,i)[j];
121 <                        src->sloc[j] /= f->nv;
122 <                }
123 <                if (!inface(src->sloc, f))
124 <                        objerror(so, USER, "cannot hit center");
125 <                src->ss = sqrt(f->area / PI);
126 <                src->ss2 = f->area;
127 <                break;
128 <        case OBJ_RING:
129 <                                                /* get the ring */
130 <                co = getcone(so, 0);
131 <                VCOPY(src->sloc, CO_P0(co));
132 <                if (CO_R0(co) > 0.0)
133 <                        objerror(so, USER, "cannot hit center");
134 <                src->ss = CO_R1(co);
135 <                src->ss2 = PI * src->ss * src->ss;
136 <                break;
137 <        default:
138 <                objerror(so, USER, "illegal material");
111 >        if (!foundsource) {
112 >                error(WARNING, "no light sources found");
113 >                return;
114          }
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 < SPOT *
144 < makespot(m)                     /* make a spotlight */
145 < register OBJREC  *m;
146 < {
147 <        extern double  cos();
148 <        register SPOT  *ns;
149 <
150 <        if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
151 <                error(SYSTEM, "out of memory in makespot");
152 <        ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
153 <        VCOPY(ns->aim, m->oargs.farg+4);
154 <        if ((ns->flen = normalize(ns->aim)) == 0.0)
155 <                objerror(m, USER, "zero focus vector");
156 <        return(ns);
157 < }
158 <
159 <
160 < double
161 < 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 */
168 <        FVECT  vd;
169 <        double  d;
170 <        register int  i;
133 >    double  d;                          /* distance to source */
134 >    register SRCREC  *srcp;
135  
136 <        if (srcval[sn].sflags & SSKIP)
173 <                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 (srcval[sn].sflags & SDISTANT)
143 <                                                /* constant direction */
144 <                VCOPY(sr->rdir, srcval[sn].sloc);
182 <        else {                                  /* compute direction */
183 <                for (i = 0; i < 3; i++)
184 <                        sr->rdir[i] = srcval[sn].sloc[i] - sr->rorg[i];
185 <
186 <                if (srcval[sn].so->otype == OBJ_FACE)
187 <                        norm = getface(srcval[sn].so)->norm;
188 <                else if (srcval[sn].so->otype == OBJ_RING)
189 <                        norm = getcone(srcval[sn].so,0)->ad;
190 <
191 <                if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY)
192 <                        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 <                for (i = 0; i < 3; i++)
149 <                        vd[i] = dstrsrc * srcval[sn].ss * (1.0 - 2.0*frandom());
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  
199                if (norm != NULL) {             /* project offset */
200                        d = DOT(vd, norm);
201                        for (i = 0; i < 3; i++)
202                                vd[i] -= d * norm[i];
203                }
204                for (i = 0; i < 3; i++)         /* offset source direction */
205                        sr->rdir[i] += vd[i];
164  
165 <        } else if (srcval[sn].sflags & SDISTANT)
166 <                                                /* already normalized */
167 <                return(srcval[sn].ss2);
165 > srcvalue(r)                     /* punch ray to source and compute value */
166 > register RAY  *r;
167 > {
168 >        register SRCREC  *sp;
169  
170 <        if ((d = normalize(sr->rdir)) == 0.0)
171 <                                                /* at source! */
172 <                return(0.0);
173 <        
174 <        if (srcval[sn].sflags & SDISTANT)
175 <                                                /* domega constant */
176 <                return(srcval[sn].ss2);
177 <
178 <        else {
220 <                                                /* check proximity */
221 <                if (srcval[sn].sflags & SPROX &&
222 <                                d > srcval[sn].sl.prox)
223 <                        return(0.0);
224 <
225 <                if (norm != NULL)
226 <                        ddot /= d;
227 <                else
228 <                        ddot = 1.0;
229 <                                                /* check angle */
230 <                if (srcval[sn].sflags & SSPOT) {
231 <                        if (srcval[sn].sl.s->siz < 2.0*PI *
232 <                                (1.0 + DOT(srcval[sn].sl.s->aim,sr->rdir)))
233 <                                return(0.0);
234 <                        d += srcval[sn].sl.s->flen;
235 <                }
236 <                                                /* return domega */
237 <                return(ddot*srcval[sn].ss2/(d*d));
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 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 251 | Line 214 | register RAY  *r;
214                  first = 0; last = nsources-1;
215          }
216          for (i = first; i <= last; i++)
217 <                if (srcval[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.
221                           */
222 <                        if (2.0*PI * (1.0 - DOT(srcval[i].sloc,r->rdir))
223 <                                        <= srcval[i].ss2) {
224 <                                r->ro = srcval[i].so;
225 <                                if (!(srcval[i].sflags & SSKIP))
222 >                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
223 >                                        <= source[i].ss2) {
224 >                                r->ro = source[i].so;
225 >                                if (!(source[i].sflags & SSKIP))
226                                          break;
227                          }
228  
# Line 267 | Line 230 | register RAY  *r;
230                  for (i = 0; i < 3; i++)
231                          r->ron[i] = -r->rdir[i];
232                  r->rod = 1.0;
233 <                r->rofs = 1.0; setident4(r->rofx);
271 <                r->robs = 1.0; setident4(r->robx);
233 >                r->rox = NULL;
234                  return(1);
235          }
236          return(0);
237   }
238  
239  
240 < #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
241 <                                r->rsrc>=0 && \
242 <                                srcval[r->rsrc].so!=r->ro)
240 > static int
241 > cntcmp(sc1, sc2)                        /* contribution compare (descending) */
242 > register CNTPTR  *sc1, *sc2;
243 > {
244 >        if (sc1->brt > sc2->brt)
245 >                return(-1);
246 >        if (sc1->brt < sc2->brt)
247 >                return(1);
248 >        return(0);
249 > }
250  
282 #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
283                                !(r->rtype&REFLECTED) &&        /* hack! */\
284                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
251  
252 + direct(r, f, p)                         /* add direct component */
253 + RAY  *r;                        /* ray that hit surface */
254 + int  (*f)();                    /* direct component coefficient function */
255 + char  *p;                       /* data for f */
256 + {
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;
264 +        RAY  sr;
265 +                        /* NOTE: srccnt and cntord global so no recursion */
266 +        if (nsources <= 0)
267 +                return;         /* no sources?! */
268 +                                                /* potential contributions */
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)(scp->coef, p, sr.rdir, si.dom);
285 +                cntord[sn].brt = bright(scp->coef);
286 +                if (cntord[sn].brt <= 0.0)
287 +                        continue;
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, sn, sizeof(CNTPTR), cntcmp);
298 +        {                                       /* find last */
299 +                register int  l, m;
300 +
301 +                ncnts = l = sn;
302 +                sn = 0;
303 +                while ((m = (sn + ncnts) >> 1) != l) {
304 +                        if (cntord[m].brt > 0.0)
305 +                                sn = m;
306 +                        else
307 +                                ncnts = m;
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 +        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 +                scp = srccnt + cntord[sn].sndx;
331 +                                                /* test for hit */
332 +                rayorigin(&sr, r, SHADOW, 1.0);
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[scp->sno].so ||
338 +                                source[scp->sno].sflags & SFOLLOW )) {
339 +                                                /* follow entire path */
340 +                        raycont(&sr);
341 +                        rayparticipate(&sr);
342 +                        if (trace != NULL)
343 +                                (*trace)(&sr);  /* trace execution */
344 +                        if (bright(sr.rcol) <= FTINY)
345 +                                continue;       /* missed! */
346 +                        copycolor(scp->val, sr.rcol);
347 +                        multcolor(scp->val, scp->coef);
348 +                }
349 +                                                /* add contribution if hit */
350 +                addcolor(r->rcol, scp->val);
351 +                nhits++;
352 +                source[scp->sno].nhits++;
353 +        }
354 +                                        /* source hit rate */
355 +        if (hwt > FTINY)
356 +                hwt = (double)nhits / hwt;
357 +        else
358 +                hwt = 0.5;
359 + #ifdef DEBUG
360 +        sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n",
361 +                        sn, ncnts-sn, hwt);
362 +        eputs(errmsg);
363 + #endif
364 +                                        /* add in untested sources */
365 +        for ( ; sn < ncnts; sn++) {
366 +                scp = srccnt + cntord[sn].sndx;
367 +                prob = hwt * (double)source[scp->sno].nhits /
368 +                                (double)source[scp->sno].ntests;
369 +                if (prob > 1.0)
370 +                        prob = 1.0;
371 +                scalecolor(scp->val, prob);
372 +                addcolor(r->rcol, scp->val);
373 +        }
374 + }
375 +
376 +
377 + srcscatter(r)                   /* compute source scattering into ray */
378 + register RAY  *r;
379 + {
380 +        int  oldsampndx;
381 +        int  nsamps;
382 +        RAY  sr;
383 +        SRCINDEX  si;
384 +        double  t, lastt, d;
385 +        COLOR  cumval, ctmp;
386 +        int  i, j;
387 +
388 +        if (r->slights == NULL || r->slights[0] == 0
389 +                        || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
390 +                return;
391 +        if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
392 +                nsamps = 1;
393 + #if MAXSSAMP
394 +        else if (nsamps > MAXSSAMP)
395 +                nsamps = MAXSSAMP;
396 + #endif
397 +        oldsampndx = samplendx;
398 +        samplendx = random()&0x7fff;            /* randomize */
399 +        for (i = r->slights[0]; i > 0; i--) {   /* for each source */
400 +                setcolor(cumval, 0., 0., 0.);
401 +                lastt = r->rot;
402 +                for (j = nsamps; j-- > 0; ) {   /* for each sample position */
403 +                        samplendx++;
404 +                        t = r->rot * (j+frandom())/nsamps;
405 +                        sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
406 +                        sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
407 +                        sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
408 +                        sr.rmax = 0.;
409 +                        initsrcindex(&si);      /* sample ray to this source */
410 +                        si.sn = r->slights[i];
411 +                        nopart(&si, &sr);
412 +                        if (!srcray(&sr, NULL, &si) ||
413 +                                        sr.rsrc != r->slights[i])
414 +                                continue;               /* no path */
415 +                        copycolor(sr.cext, r->cext);
416 +                        sr.albedo = r->albedo;
417 +                        sr.gecc = r->gecc;
418 +                        rayvalue(&sr);                  /* eval. source ray */
419 +                        if (bright(sr.rcol) <= FTINY)
420 +                                continue;
421 +                                                        /* compute fall-off */
422 +                        d = lastt - t;
423 +                        setcolor(ctmp,  1.-d*colval(r->cext,RED),
424 +                                        1.-d*colval(r->cext,GRN),
425 +                                        1.-d*colval(r->cext,BLU));
426 +                        multcolor(cumval, ctmp);
427 +                        lastt = t;
428 +                        if (r->gecc <= FTINY)           /* compute P(theta) */
429 +                                d = 1.;
430 +                        else {
431 +                                d = DOT(r->rdir, sr.rdir);
432 +                                d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d);
433 +                                d = (1. - r->gecc*r->gecc) / (d*d*d);
434 +                        }
435 +                                                        /* other factors */
436 +                        d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps);
437 +                        multcolor(sr.rcol, r->cext);
438 +                        scalecolor(sr.rcol, d);
439 +                        addcolor(cumval, sr.rcol);
440 +                }
441 +                                                /* final fall-off */
442 +                setcolor(ctmp,  1.-lastt*colval(r->cext,RED),
443 +                                1.-lastt*colval(r->cext,GRN),
444 +                                1.-lastt*colval(r->cext,BLU));
445 +                multcolor(cumval, ctmp);
446 +                addcolor(r->rcol, cumval);      /* sum into ray result */
447 +        }
448 +        samplendx = oldsampndx;
449 + }
450 +
451 +
452 + /****************************************************************
453 + * The following macros were separated from the m_light() routine
454 + * because they are very nasty and difficult to understand.
455 + */
456 +
457 + /* illumblock *
458 + *
459 + * We cannot allow an illum to pass to another illum, because that
460 + * would almost certainly constitute overcounting.
461 + * However, we do allow an illum to pass to another illum
462 + * that is actually going to relay to a virtual light source.
463 + * We also prevent an illum from passing to a glow; this provides a
464 + * convenient mechanism for defining detailed light source
465 + * geometry behind (or inside) an effective radiator.
466 + */
467 +
468 + static int weaksrcmod(obj) int obj;     /* efficiency booster function */
469 + {register OBJREC *o = objptr(obj);
470 + return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
471 +
472 + #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \
473 +                                r->rod > 0.0 && \
474 +                                weaksrcmod(source[r->rsrc].so->omod))
475 +
476 + /* wrongsource *
477 + *
478 + * This source is the wrong source (ie. overcounted) if we are
479 + * aimed to a different source than the one we hit and the one
480 + * we hit is not an illum that should be passed.
481 + */
482 +
483 + #define  wrongsource(m, r)      (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \
484 +                                (m->otype!=MAT_ILLUM || illumblock(m,r)))
485 +
486 + /* distglow *
487 + *
488 + * A distant glow is an object that sometimes acts as a light source,
489 + * but is too far away from the test point to be one in this case.
490 + * (Glows with negative radii should NEVER participate in illumination.)
491 + */
492 +
493 + #define  distglow(m, r, d)      (m->otype==MAT_GLOW && \
494 +                                m->oargs.farg[3] >= -FTINY && \
495 +                                d > m->oargs.farg[3])
496 +
497 + /* badcomponent *
498 + *
499 + * We must avoid counting light sources in the ambient calculation,
500 + * since the direct component is handled separately.  Therefore, any
501 + * ambient ray which hits an active light source must be discarded.
502 + * The same is true for stray specular samples, since the specular
503 + * contribution from light sources is calculated separately.
504 + */
505 +
506 + #define  badcomponent(m, r)     (r->crtype&(AMBIENT|SPECULAR) && \
507 +                                !(r->crtype&SHADOW || r->rod < 0.0 || \
508 +                /* not 100% correct */  distglow(m, r, r->rot)))
509 +
510 + /* passillum *
511 + *
512 + * An illum passes to another material type when we didn't hit it
513 + * on purpose (as part of a direct calculation), or it is relaying
514 + * a virtual light source.
515 + */
516 +
517   #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
518 <                                !(r->rsrc>=0&&srcval[r->rsrc].so==r->ro))
518 >                                (r->rsrc<0 || source[r->rsrc].so!=r->ro || \
519 >                                source[r->rsrc].sflags&SVIRTUAL))
520  
521 + /* srcignore *
522 + *
523 + * The -dv flag is normally on for sources to be visible.
524 + */
525  
526 + #define  srcignore(m, r)        !(directvis || r->crtype&SHADOW || \
527 +                                distglow(m, r, raydist(r,PRIMARY)))
528 +
529 +
530   m_light(m, r)                   /* ray hit a light source */
531   register OBJREC  *m;
532   register RAY  *r;
533   {
294                                                /* check for behind */
295        if (r->rod < 0.0)
296                return;
534                                                  /* check for over-counting */
535 <        if (wrongsource(m, r) || badambient(m, r))
536 <                return;
535 >        if (badcomponent(m, r))
536 >                return(1);
537 >        if (wrongsource(m, r))
538 >                return(1);
539                                                  /* check for passed illum */
540          if (passillum(m, r)) {
541 <
542 <                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
543 <                        raytrans(r);
544 <                else
545 <                        rayshade(r, modifier(m->oargs.sarg[0]));
546 <
547 <                                                /* otherwise treat as source */
548 <        } else {
541 >                if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
542 >                        return(rayshade(r, modifier(m->oargs.sarg[0])));
543 >                raytrans(r);
544 >                return(1);
545 >        }
546 >                                        /* otherwise treat as source */
547 >                                                /* check for behind */
548 >        if (r->rod < 0.0)
549 >                return(1);
550 >                                                /* check for invisibility */
551 >        if (srcignore(m, r))
552 >                return(1);
553 >                                                /* check for outside spot */
554 >        if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
555 >                return(1);
556                                                  /* get distribution pattern */
557 <                raytexture(r, m->omod);
557 >        raytexture(r, m->omod);
558                                                  /* get source color */
559 <                setcolor(r->rcol, m->oargs.farg[0],
560 <                                  m->oargs.farg[1],
561 <                                  m->oargs.farg[2]);
559 >        setcolor(r->rcol, m->oargs.farg[0],
560 >                          m->oargs.farg[1],
561 >                          m->oargs.farg[2]);
562                                                  /* modify value */
563 <                multcolor(r->rcol, r->pcol);
564 <        }
563 >        multcolor(r->rcol, r->pcol);
564 >        return(1);
565   }
320
321
322 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines