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 2.1 by greg, Tue Nov 12 17:08:57 1991 UTC vs.
Revision 2.44 by schorsch, Tue Mar 30 16:13:01 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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  "ray.h"
14
15 #include  "octree.h"
16
11   #include  "otypes.h"
12 <
12 > #include  "rtotypes.h"
13   #include  "source.h"
14 + #include  "random.h"
15  
16 + extern double  ssampdist;               /* scatter sampling distance */
17 +
18 + #ifndef MAXSSAMP
19 + #define MAXSSAMP        16              /* maximum samples per ray */
20 + #endif
21 +
22   /*
23   * Structures used by direct()
24   */
# Line 38 | Line 39 | static CONTRIB  *srccnt;               /* source contributions in d
39   static CNTPTR  *cntord;                 /* source ordering in direct() */
40   static int  maxcntr = 0;                /* size of contribution arrays */
41  
42 + static int cntcmp(const void *p1, const void *p2);
43  
44 < marksources()                   /* find and mark source objects */
44 >
45 > extern OBJREC *                 /* find an object's actual material */
46 > findmaterial(register OBJREC *o)
47   {
48 +        while (!ismaterial(o->otype)) {
49 +                if (ismixture(o->otype))
50 +                        return(NULL);   /* reject mixed materials */
51 +                if (o->otype == MOD_ALIAS && o->oargs.nsargs) {
52 +                        OBJECT  aobj;
53 +                        OBJREC  *ao;
54 +                        aobj = lastmod(objndx(o), o->oargs.sarg[0]);
55 +                        if (aobj < 0)
56 +                                objerror(o, USER, "bad reference");
57 +                        ao = objptr(aobj);
58 +                        if (ismaterial(ao->otype))
59 +                                return(ao);
60 +                }
61 +                if (o->omod == OVOID)
62 +                        return(NULL);
63 +                o = objptr(o->omod);
64 +        }
65 +        return(o);
66 + }
67 +
68 +
69 + extern void
70 + marksources(void)                       /* find and mark source objects */
71 + {
72 +        int  foundsource = 0;
73          int  i;
74          register OBJREC  *o, *m;
75          register int  ns;
76                                          /* initialize dispatch table */
77          initstypes();
78                                          /* find direct sources */
79 <        for (i = 0; i < nobjects; i++) {
79 >        for (i = 0; i < nsceneobjs; i++) {
80          
81                  o = objptr(i);
82  
83                  if (!issurface(o->otype) || o->omod == OVOID)
84                          continue;
85 <
86 <                m = objptr(o->omod);
87 <
88 <                if (!islight(m->otype))
60 <                        continue;
85 >                                        /* find material */
86 >                m = findmaterial(objptr(o->omod));
87 >                if (m == NULL || !islight(m->otype))
88 >                        continue;       /* not source modifier */
89          
90                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
91                                  m->otype == MAT_SPOT ? 7 : 3))
# Line 67 | Line 95 | marksources()                  /* find and mark source objects */
95                                  o->otype != OBJ_SOURCE &&
96                                  m->oargs.farg[3] <= FTINY)
97                          continue;                       /* don't bother */
98 +                if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY &&
99 +                                m->oargs.farg[2] <= FTINY)
100 +                        continue;                       /* don't bother */
101  
102                  if (sfun[o->otype].of == NULL ||
103                                  sfun[o->otype].of->setsrc == NULL)
# Line 80 | Line 111 | marksources()                  /* find and mark source objects */
111                  if (m->otype == MAT_GLOW) {
112                          source[ns].sflags |= SPROX;
113                          source[ns].sl.prox = m->oargs.farg[3];
114 <                        if (o->otype == OBJ_SOURCE)
114 >                        if (source[ns].sflags & SDISTANT)
115                                  source[ns].sflags |= SSKIP;
116                  } else if (m->otype == MAT_SPOT) {
117                          source[ns].sflags |= SSPOT;
# Line 93 | Line 124 | marksources()                  /* find and mark source objects */
124                                  source[ns].sflags |= SSKIP;
125                          }
126                  }
127 + #if  SHADCACHE
128 +                source[ns].obscache = NULL;
129 + #endif
130 +                if (!(source[ns].sflags & SSKIP))
131 +                        foundsource++;
132          }
133 <        if (nsources <= 0) {
133 >        if (!foundsource) {
134                  error(WARNING, "no light sources found");
135                  return;
136          }
# Line 103 | Line 139 | marksources()                  /* find and mark source objects */
139          maxcntr = nsources + MAXSPART;  /* start with this many */
140          srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
141          cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR));
142 <        if (srccnt == NULL | cntord == NULL)
142 >        if ((srccnt == NULL) | (cntord == NULL))
143                  goto memerr;
144          return;
145   memerr:
# Line 111 | Line 147 | memerr:
147   }
148  
149  
150 < srcray(sr, r, si)               /* send a ray to a source, return domega */
151 < register RAY  *sr;              /* returned source ray */
116 < RAY  *r;                        /* ray which hit object */
117 < SRCINDEX  *si;                  /* source sample index */
150 > extern void
151 > freesources(void)                       /* free all source structures */
152   {
153 +        if (nsources > 0) {
154 + #if SHADCACHE
155 +                while (nsources--)
156 +                        freeobscache(&source[nsources]);
157 + #endif
158 +                free((void *)source);
159 +                source = NULL;
160 +                nsources = 0;
161 +        }
162 +        if (maxcntr <= 0)
163 +                return;
164 +        free((void *)srccnt);
165 +        srccnt = NULL;
166 +        free((void *)cntord);
167 +        cntord = NULL;
168 +        maxcntr = 0;
169 + }
170 +
171 +
172 + extern int
173 + srcray(                         /* send a ray to a source, return domega */
174 +        register RAY  *sr,              /* returned source ray */
175 +        RAY  *r,                        /* ray which hit object */
176 +        SRCINDEX  *si                   /* source sample index */
177 + )
178 + {
179      double  d;                          /* distance to source */
120    FVECT  vd;
180      register SRCREC  *srcp;
122    register int  i;
181  
182      rayorigin(sr, r, SHADOW, 1.0);              /* ignore limits */
183  
# Line 127 | Line 185 | SRCINDEX  *si;                 /* source sample index */
185          sr->rsrc = si->sn;                      /* remember source */
186          srcp = source + si->sn;
187          if (srcp->sflags & SDISTANT) {
188 <                if (srcp->sflags & SSPOT) {     /* check location */
189 <                        for (i = 0; i < 3; i++)
132 <                            vd[i] = srcp->sl.s->aim[i] - sr->rorg[i];
133 <                        d = DOT(sr->rdir,vd);
134 <                        if (d <= FTINY)
135 <                                continue;
136 <                        d = DOT(vd,vd) - d*d;
137 <                        if (PI*d > srcp->sl.s->siz)
138 <                                continue;
139 <                }
188 >                if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
189 >                        continue;
190                  return(1);              /* sample OK */
191          }
192                                  /* local source */
# Line 145 | Line 195 | SRCINDEX  *si;                 /* source sample index */
195                  continue;
196                                                  /* check angle */
197          if (srcp->sflags & SSPOT) {
198 <                if (srcp->sl.s->siz < 2.0*PI *
149 <                                (1.0 + DOT(srcp->sl.s->aim,sr->rdir)))
198 >                if (spotout(sr, srcp->sl.s))
199                          continue;
200                                          /* adjust solid angle */
201                  si->dom *= d*d;
# Line 159 | Line 208 | SRCINDEX  *si;                 /* source sample index */
208   }
209  
210  
211 < srcvalue(r)                     /* punch ray to source and compute value */
212 < RAY  *r;
211 > extern void
212 > srcvalue(                       /* punch ray to source and compute value */
213 >        register RAY  *r
214 > )
215   {
216          register SRCREC  *sp;
217  
# Line 169 | Line 220 | RAY  *r;
220                                          /* check intersection */
221                  if (!(*ofun[sp->so->otype].funp)(sp->so, r))
222                          return;
223 <                raycont(r);             /* compute contribution */
223 >                if (!rayshade(r, r->ro->omod))  /* compute contribution */
224 >                        goto nomat;
225 >                rayparticipate(r);
226                  return;
227          }
228                                          /* compute intersection */
# Line 177 | Line 230 | RAY  *r;
230                          (*ofun[sp->so->otype].funp)(sp->so, r)) {
231                  if (sp->sa.success >= 0)
232                          sp->sa.success++;
233 <                raycont(r);             /* compute contribution */
233 >                if (!rayshade(r, r->ro->omod))  /* compute contribution */
234 >                        goto nomat;
235 >                rayparticipate(r);
236                  return;
237          }
238 +                                        /* we missed our mark! */
239          if (sp->sa.success < 0)
240                  return;                 /* bitched already */
241          sp->sa.success -= AIMREQT;
# Line 188 | Line 244 | RAY  *r;
244          sprintf(errmsg, "aiming failure for light source \"%s\"",
245                          sp->so->oname);
246          error(WARNING, errmsg);         /* issue warning */
247 +        return;
248 + nomat:
249 +        objerror(r->ro, USER, "material not found");
250   }
251  
252  
253 + extern int
254 + sourcehit(                      /* check to see if ray hit distant source */
255 +        register RAY  *r
256 + )
257 + {
258 +        int  first, last;
259 +        register int  i;
260 +
261 +        if (r->rsrc >= 0) {             /* check only one if aimed */
262 +                first = last = r->rsrc;
263 +        } else {                        /* otherwise check all */
264 +                first = 0; last = nsources-1;
265 +        }
266 +        for (i = first; i <= last; i++)
267 +                if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT)
268 +                        /*
269 +                         * Check to see if ray is within
270 +                         * solid angle of source.
271 +                         */
272 +                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
273 +                                        <= source[i].ss2) {
274 +                                r->ro = source[i].so;
275 +                                if (!(source[i].sflags & SSKIP))
276 +                                        break;
277 +                        }
278 +
279 +        if (r->ro != NULL) {
280 +                r->robj = objndx(r->ro);
281 +                for (i = 0; i < 3; i++)
282 +                        r->ron[i] = -r->rdir[i];
283 +                r->rod = 1.0;
284 +                r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
285 +                r->uv[0] = r->uv[1] = 0.0;
286 +                r->rox = NULL;
287 +                return(1);
288 +        }
289 +        return(0);
290 + }
291 +
292 +
293   static int
294 < cntcmp(sc1, sc2)                        /* contribution compare (descending) */
295 < register CNTPTR  *sc1, *sc2;
294 > cntcmp(                         /* contribution compare (descending) */
295 >        const void *p1,
296 >        const void *p2
297 > )
298   {
299 +        register const CNTPTR  *sc1 = (const CNTPTR *)p1;
300 +        register const CNTPTR  *sc2 = (const CNTPTR *)p2;
301 +
302          if (sc1->brt > sc2->brt)
303                  return(-1);
304          if (sc1->brt < sc2->brt)
# Line 203 | Line 307 | register CNTPTR  *sc1, *sc2;
307   }
308  
309  
310 < direct(r, f, p)                         /* add direct component */
311 < RAY  *r;                        /* ray that hit surface */
312 < int  (*f)();                    /* direct component coefficient function */
313 < char  *p;                       /* data for f */
310 > extern void
311 > direct(                                 /* add direct component */
312 >        RAY  *r,                        /* ray that hit surface */
313 >        srcdirf_t *f,                   /* direct component coefficient function */
314 >        void  *p                        /* data for f */
315 > )
316   {
211        extern int  (*trace)();
212        extern double  pow();
317          register int  sn;
318 +        register CONTRIB  *scp;
319          SRCINDEX  si;
320          int  nshadcheck, ncnts;
321          int  nhits;
# Line 224 | Line 329 | char  *p;                      /* data for f */
329          for (sn = 0; srcray(&sr, r, &si); sn++) {
330                  if (sn >= maxcntr) {
331                          maxcntr = sn + MAXSPART;
332 <                        srccnt = (CONTRIB *)realloc((char *)srccnt,
332 >                        srccnt = (CONTRIB *)realloc((void *)srccnt,
333                                          maxcntr*sizeof(CONTRIB));
334 <                        cntord = (CNTPTR *)realloc((char *)cntord,
334 >                        cntord = (CNTPTR *)realloc((void *)cntord,
335                                          maxcntr*sizeof(CNTPTR));
336 <                        if (srccnt == NULL | cntord == NULL)
336 >                        if ((srccnt == NULL) | (cntord == NULL))
337                                  error(SYSTEM, "out of memory in direct");
338                  }
339                  cntord[sn].sndx = sn;
340 <                srccnt[sn].sno = sr.rsrc;
340 >                scp = srccnt + sn;
341 >                scp->sno = sr.rsrc;
342                                                  /* compute coefficient */
343 <                (*f)(srccnt[sn].coef, p, sr.rdir, si.dom);
344 <                cntord[sn].brt = bright(srccnt[sn].coef);
343 >                (*f)(scp->coef, p, sr.rdir, si.dom);
344 >                cntord[sn].brt = bright(scp->coef);
345                  if (cntord[sn].brt <= 0.0)
346                          continue;
347 <                VCOPY(srccnt[sn].dir, sr.rdir);
347 > #if SHADCACHE
348 >                                                /* check shadow cache */
349 >                if (si.np == 1 && srcblocked(&sr)) {
350 >                        cntord[sn].brt = 0.0;
351 >                        continue;
352 >                }
353 > #endif
354 >                VCOPY(scp->dir, sr.rdir);
355                                                  /* compute potential */
356                  sr.revf = srcvalue;
357                  rayvalue(&sr);
358 <                copycolor(srccnt[sn].val, sr.rcol);
359 <                multcolor(srccnt[sn].val, srccnt[sn].coef);
360 <                cntord[sn].brt = bright(srccnt[sn].val);
358 >                copycolor(scp->val, sr.rcol);
359 >                multcolor(scp->val, scp->coef);
360 >                cntord[sn].brt = bright(scp->val);
361          }
362                                                  /* sort contributions */
363          qsort(cntord, sn, sizeof(CNTPTR), cntcmp);
# Line 261 | Line 374 | char  *p;                      /* data for f */
374                          l = m;
375                  }
376          }
377 +        if (ncnts == 0)
378 +                return;         /* no contributions! */
379                                                  /* accumulate tail */
380          for (sn = ncnts-1; sn > 0; sn--)
381                  cntord[sn-1].brt += cntord[sn].brt;
# Line 269 | Line 384 | char  *p;                      /* data for f */
384                                                  /* modify threshold */
385          ourthresh = shadthresh / r->rweight;
386                                                  /* test for shadows */
387 <        nhits = 0;
388 <        for (sn = 0; sn < ncnts; sn++) {
387 >        for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts;
388 >                        hwt += (double)source[scp->sno].nhits /
389 >                                (double)source[scp->sno].ntests,
390 >                        sn++) {
391                                                  /* check threshold */
392                  if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
393                                  cntord[sn].brt-cntord[sn+nshadcheck].brt)
394                                  < ourthresh*bright(r->rcol))
395                          break;
396 +                scp = srccnt + cntord[sn].sndx;
397                                                  /* test for hit */
398                  rayorigin(&sr, r, SHADOW, 1.0);
399 <                VCOPY(sr.rdir, srccnt[cntord[sn].sndx].dir);
400 <                sr.rsrc = srccnt[cntord[sn].sndx].sno;
401 <                source[sr.rsrc].ntests++;       /* keep statistics */
399 >                VCOPY(sr.rdir, scp->dir);
400 >                sr.rsrc = scp->sno;
401 >                                                /* keep statistics */
402 >                if (source[scp->sno].ntests++ > 0xfffffff0) {
403 >                        source[scp->sno].ntests >>= 1;
404 >                        source[scp->sno].nhits >>= 1;
405 >                }
406                  if (localhit(&sr, &thescene) &&
407 <                                ( sr.ro != source[sr.rsrc].so ||
408 <                                source[sr.rsrc].sflags & SFOLLOW )) {
407 >                                ( sr.ro != source[scp->sno].so ||
408 >                                source[scp->sno].sflags & SFOLLOW )) {
409                                                  /* follow entire path */
410                          raycont(&sr);
411 +                        rayparticipate(&sr);
412                          if (trace != NULL)
413                                  (*trace)(&sr);  /* trace execution */
414 <                        if (bright(sr.rcol) <= FTINY)
414 >                        if (bright(sr.rcol) <= FTINY) {
415 > #if SHADCACHE
416 >                                if ((scp <= srccnt || scp[-1].sno != scp->sno)
417 >                                                && (scp >= srccnt+ncnts-1 ||
418 >                                                    scp[1].sno != scp->sno))
419 >                                        srcblocker(&sr);
420 > #endif
421                                  continue;       /* missed! */
422 <                        copycolor(srccnt[cntord[sn].sndx].val, sr.rcol);
423 <                        multcolor(srccnt[cntord[sn].sndx].val,
424 <                                        srccnt[cntord[sn].sndx].coef);
422 >                        }
423 >                        copycolor(scp->val, sr.rcol);
424 >                        multcolor(scp->val, scp->coef);
425                  }
426                                                  /* add contribution if hit */
427 <                addcolor(r->rcol, srccnt[cntord[sn].sndx].val);
427 >                addcolor(r->rcol, scp->val);
428                  nhits++;
429 <                source[sr.rsrc].nhits++;
429 >                source[scp->sno].nhits++;
430          }
431 <                                        /* surface hit rate */
432 <        if (sn > 0)
433 <                hwt = (double)nhits / (double)sn;
431 >                                        /* source hit rate */
432 >        if (hwt > FTINY)
433 >                hwt = (double)nhits / hwt;
434          else
435                  hwt = 0.5;
436   #ifdef DEBUG
437 <        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
437 >        sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n",
438                          sn, ncnts-sn, hwt);
439          eputs(errmsg);
440   #endif
441                                          /* add in untested sources */
442          for ( ; sn < ncnts; sn++) {
443 <                sr.rsrc = srccnt[cntord[sn].sndx].sno;
444 <                prob = hwt * (double)source[sr.rsrc].nhits /
445 <                                (double)source[sr.rsrc].ntests;
446 <                scalecolor(srccnt[cntord[sn].sndx].val, prob);
447 <                addcolor(r->rcol, srccnt[cntord[sn].sndx].val);
443 >                scp = srccnt + cntord[sn].sndx;
444 >                prob = hwt * (double)source[scp->sno].nhits /
445 >                                (double)source[scp->sno].ntests;
446 >                if (prob > 1.0)
447 >                        prob = 1.0;
448 >                scalecolor(scp->val, prob);
449 >                addcolor(r->rcol, scp->val);
450          }
451 + }
452 +
453 +
454 + extern void
455 + srcscatter(                     /* compute source scattering into ray */
456 +        register RAY  *r
457 + )
458 + {
459 +        int  oldsampndx;
460 +        int  nsamps;
461 +        RAY  sr;
462 +        SRCINDEX  si;
463 +        double  t, d;
464 +        double  re, ge, be;
465 +        COLOR  cvext;
466 +        int  i, j;
467 +
468 +        if (r->slights == NULL || r->slights[0] == 0
469 +                        || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
470 +                return;
471 +        if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
472 +                nsamps = 1;
473 + #if MAXSSAMP
474 +        else if (nsamps > MAXSSAMP)
475 +                nsamps = MAXSSAMP;
476 + #endif
477 +        oldsampndx = samplendx;
478 +        samplendx = random()&0x7fff;            /* randomize */
479 +        for (i = r->slights[0]; i > 0; i--) {   /* for each source */
480 +                for (j = 0; j < nsamps; j++) {  /* for each sample position */
481 +                        samplendx++;
482 +                        t = r->rot * (j+frandom())/nsamps;
483 +                                                        /* extinction */
484 +                        re = t*colval(r->cext,RED);
485 +                        ge = t*colval(r->cext,GRN);
486 +                        be = t*colval(r->cext,BLU);
487 +                        setcolor(cvext, re > 92. ? 0. : exp(-re),
488 +                                        ge > 92. ? 0. : exp(-ge),
489 +                                        be > 92. ? 0. : exp(-be));
490 +                        if (intens(cvext) <= FTINY)
491 +                                break;                  /* too far away */
492 +                        sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
493 +                        sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
494 +                        sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
495 +                        sr.rmax = 0.;
496 +                        initsrcindex(&si);      /* sample ray to this source */
497 +                        si.sn = r->slights[i];
498 +                        nopart(&si, &sr);
499 +                        if (!srcray(&sr, NULL, &si) ||
500 +                                        sr.rsrc != r->slights[i])
501 +                                continue;               /* no path */
502 +                        copycolor(sr.cext, r->cext);
503 +                        copycolor(sr.albedo, r->albedo);
504 +                        sr.gecc = r->gecc;
505 +                        sr.slights = r->slights;
506 +                        rayvalue(&sr);                  /* eval. source ray */
507 +                        if (bright(sr.rcol) <= FTINY)
508 +                                continue;
509 +                        if (r->gecc <= FTINY)           /* compute P(theta) */
510 +                                d = 1.;
511 +                        else {
512 +                                d = DOT(r->rdir, sr.rdir);
513 +                                d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
514 +                                d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
515 +                        }
516 +                                                        /* other factors */
517 +                        d *= si.dom * r->rot / (4.*PI*nsamps);
518 +                        multcolor(sr.rcol, r->cext);
519 +                        multcolor(sr.rcol, r->albedo);
520 +                        scalecolor(sr.rcol, d);
521 +                        multcolor(sr.rcol, cvext);
522 +                        addcolor(r->rcol, sr.rcol);     /* add it in */
523 +                }
524 +        }
525 +        samplendx = oldsampndx;
526 + }
527 +
528 +
529 + /****************************************************************
530 + * The following macros were separated from the m_light() routine
531 + * because they are very nasty and difficult to understand.
532 + */
533 +
534 + /* illumblock *
535 + *
536 + * We cannot allow an illum to pass to another illum, because that
537 + * would almost certainly constitute overcounting.
538 + * However, we do allow an illum to pass to another illum
539 + * that is actually going to relay to a virtual light source.
540 + * We also prevent an illum from passing to a glow; this provides a
541 + * convenient mechanism for defining detailed light source
542 + * geometry behind (or inside) an effective radiator.
543 + */
544 +
545 + static int
546 + weaksrcmat(OBJECT obj)          /* identify material */
547 + {
548 +        OBJREC *o = findmaterial(objptr(obj));
549 +        
550 +        if (o == NULL) return 0;
551 +        return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW));
552 + }
553 +
554 + #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \
555 +                                r->rod > 0.0 && \
556 +                                weaksrcmat(source[r->rsrc].so->omod))
557 +
558 + /* wrongsource *
559 + *
560 + * This source is the wrong source (ie. overcounted) if we are
561 + * aimed to a different source than the one we hit and the one
562 + * we hit is not an illum that should be passed.
563 + */
564 +
565 + #define  wrongsource(m, r)      (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \
566 +                                (m->otype!=MAT_ILLUM || illumblock(m,r)))
567 +
568 + /* distglow *
569 + *
570 + * A distant glow is an object that sometimes acts as a light source,
571 + * but is too far away from the test point to be one in this case.
572 + * (Glows with negative radii should NEVER participate in illumination.)
573 + */
574 +
575 + #define  distglow(m, r, d)      (m->otype==MAT_GLOW && \
576 +                                m->oargs.farg[3] >= -FTINY && \
577 +                                d > m->oargs.farg[3])
578 +
579 + /* badcomponent *
580 + *
581 + * We must avoid counting light sources in the ambient calculation,
582 + * since the direct component is handled separately.  Therefore, any
583 + * ambient ray which hits an active light source must be discarded.
584 + * The same is true for stray specular samples, since the specular
585 + * contribution from light sources is calculated separately.
586 + */
587 +
588 + #define  badcomponent(m, r)     (r->crtype&(AMBIENT|SPECULAR) && \
589 +                                !(r->crtype&SHADOW || r->rod < 0.0 || \
590 +                /* not 100% correct */  distglow(m, r, r->rot)))
591 +
592 + /* passillum *
593 + *
594 + * An illum passes to another material type when we didn't hit it
595 + * on purpose (as part of a direct calculation), or it is relaying
596 + * a virtual light source.
597 + */
598 +
599 + #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
600 +                                (r->rsrc<0 || source[r->rsrc].so!=r->ro || \
601 +                                source[r->rsrc].sflags&SVIRTUAL))
602 +
603 + /* srcignore *
604 + *
605 + * The -dv flag is normally on for sources to be visible.
606 + */
607 +
608 + #define  srcignore(m, r)        !(directvis || r->crtype&SHADOW || \
609 +                                distglow(m, r, raydist(r,PRIMARY)))
610 +
611 +
612 + extern int
613 + m_light(                                /* ray hit a light source */
614 +        register OBJREC  *m,
615 +        register RAY  *r
616 + )
617 + {
618 +                                                /* check for over-counting */
619 +        if (badcomponent(m, r))
620 +                return(1);
621 +        if (wrongsource(m, r))
622 +                return(1);
623 +                                                /* check for passed illum */
624 +        if (passillum(m, r)) {
625 +                if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
626 +                        return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0])));
627 +                raytrans(r);
628 +                return(1);
629 +        }
630 +                                        /* otherwise treat as source */
631 +                                                /* check for behind */
632 +        if (r->rod < 0.0)
633 +                return(1);
634 +                                                /* check for invisibility */
635 +        if (srcignore(m, r))
636 +                return(1);
637 +                                                /* check for outside spot */
638 +        if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
639 +                return(1);
640 +                                                /* get distribution pattern */
641 +        raytexture(r, m->omod);
642 +                                                /* get source color */
643 +        setcolor(r->rcol, m->oargs.farg[0],
644 +                          m->oargs.farg[1],
645 +                          m->oargs.farg[2]);
646 +                                                /* modify value */
647 +        multcolor(r->rcol, r->pcol);
648 +        return(1);
649   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines