ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 2.46
Committed: Wed Sep 8 17:10:16 2004 UTC (19 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 2.45: +2 -2 lines
Log Message:
Made it so obstructor cache is only initialized once with parallel processes

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.46 static const char RCSid[] = "$Id: source.c,v 2.45 2004/06/23 12:58:02 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * source.c - routines dealing with illumination sources.
6     *
7 greg 2.29 * External symbols declared in source.h
8     */
9    
10 greg 1.1 #include "ray.h"
11     #include "otypes.h"
12 schorsch 2.44 #include "rtotypes.h"
13 greg 1.35 #include "source.h"
14 greg 2.19 #include "random.h"
15    
16     extern double ssampdist; /* scatter sampling distance */
17    
18 greg 2.22 #ifndef MAXSSAMP
19     #define MAXSSAMP 16 /* maximum samples per ray */
20     #endif
21    
22 greg 1.35 /*
23     * Structures used by direct()
24     */
25 greg 1.1
26 greg 1.35 typedef struct {
27 greg 1.45 int sno; /* source number */
28 greg 1.35 FVECT dir; /* source direction */
29     COLOR coef; /* material coefficient */
30     COLOR val; /* contribution */
31     } CONTRIB; /* direct contribution */
32 greg 1.1
33 greg 1.35 typedef struct {
34 greg 1.45 int sndx; /* source index (to CONTRIB array) */
35 greg 1.35 float brt; /* brightness (for comparison) */
36     } CNTPTR; /* contribution pointer */
37 greg 1.1
38 greg 1.25 static CONTRIB *srccnt; /* source contributions in direct() */
39     static CNTPTR *cntord; /* source ordering in direct() */
40 greg 1.45 static int maxcntr = 0; /* size of contribution arrays */
41 greg 1.1
42 schorsch 2.44 static int cntcmp(const void *p1, const void *p2);
43    
44 greg 1.25
45 schorsch 2.44 extern OBJREC * /* find an object's actual material */
46 greg 2.38 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 schorsch 2.44 extern void
70     marksources(void) /* find and mark source objects */
71 greg 1.1 {
72 greg 2.3 int foundsource = 0;
73 greg 1.33 int i;
74 greg 1.1 register OBJREC *o, *m;
75 greg 1.36 register int ns;
76 greg 1.35 /* initialize dispatch table */
77     initstypes();
78     /* find direct sources */
79 greg 2.33 for (i = 0; i < nsceneobjs; i++) {
80 greg 1.1
81     o = objptr(i);
82    
83 greg 1.35 if (!issurface(o->otype) || o->omod == OVOID)
84 greg 1.1 continue;
85 greg 2.36 /* find material */
86 greg 2.43 m = findmaterial(objptr(o->omod));
87 greg 2.36 if (m == NULL || !islight(m->otype))
88     continue; /* not source modifier */
89 greg 1.1
90 greg 1.6 if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
91     m->otype == MAT_SPOT ? 7 : 3))
92 greg 1.1 objerror(m, USER, "bad # arguments");
93    
94 greg 1.6 if (m->otype == MAT_GLOW &&
95     o->otype != OBJ_SOURCE &&
96     m->oargs.farg[3] <= FTINY)
97 greg 1.1 continue; /* don't bother */
98 greg 2.16 if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY &&
99     m->oargs.farg[2] <= FTINY)
100     continue; /* don't bother */
101 greg 1.1
102 greg 1.35 if (sfun[o->otype].of == NULL ||
103     sfun[o->otype].of->setsrc == NULL)
104     objerror(o, USER, "illegal material");
105    
106 greg 1.36 if ((ns = newsource()) < 0)
107 greg 1.25 goto memerr;
108 greg 1.1
109 greg 1.37 setsource(&source[ns], o);
110 greg 1.1
111 greg 1.6 if (m->otype == MAT_GLOW) {
112 greg 1.36 source[ns].sflags |= SPROX;
113     source[ns].sl.prox = m->oargs.farg[3];
114 greg 2.8 if (source[ns].sflags & SDISTANT)
115 greg 1.36 source[ns].sflags |= SSKIP;
116 greg 1.6 } else if (m->otype == MAT_SPOT) {
117 greg 1.36 source[ns].sflags |= SSPOT;
118     if ((source[ns].sl.s = makespot(m)) == NULL)
119 greg 1.33 goto memerr;
120 greg 1.38 if (source[ns].sflags & SFLAT &&
121     !checkspot(source[ns].sl.s,source[ns].snorm)) {
122     objerror(o, WARNING,
123     "invalid spotlight direction");
124     source[ns].sflags |= SSKIP;
125     }
126 greg 1.6 }
127 greg 2.38 #if SHADCACHE
128 greg 2.46 initobscache(ns);
129 greg 2.38 #endif
130 greg 2.3 if (!(source[ns].sflags & SSKIP))
131     foundsource++;
132 greg 1.1 }
133 greg 2.3 if (!foundsource) {
134 greg 1.25 error(WARNING, "no light sources found");
135     return;
136     }
137 greg 1.33 markvirtuals(); /* find and add virtual sources */
138 greg 1.45 /* allocate our contribution arrays */
139 greg 1.48 maxcntr = nsources + MAXSPART; /* start with this many */
140 greg 1.45 srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
141     cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR));
142 schorsch 2.35 if ((srccnt == NULL) | (cntord == NULL))
143 greg 1.33 goto memerr;
144     return;
145 greg 1.25 memerr:
146     error(SYSTEM, "out of memory in marksources");
147 greg 1.1 }
148    
149    
150 schorsch 2.44 extern void
151     freesources(void) /* free all source structures */
152 greg 2.29 {
153     if (nsources > 0) {
154 greg 2.38 #if SHADCACHE
155     while (nsources--)
156     freeobscache(&source[nsources]);
157     #endif
158 greg 2.29 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 schorsch 2.44 extern int
173 greg 2.38 srcray( /* send a ray to a source, return domega */
174 schorsch 2.44 register RAY *sr, /* returned source ray */
175     RAY *r, /* ray which hit object */
176     SRCINDEX *si /* source sample index */
177 greg 2.38 )
178 greg 1.1 {
179 greg 1.45 double d; /* distance to source */
180     register SRCREC *srcp;
181 greg 1.1
182 greg 1.45 rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */
183 greg 1.1
184 greg 1.47 while ((d = nextssamp(sr, si)) != 0.0) {
185 greg 1.45 sr->rsrc = si->sn; /* remember source */
186     srcp = source + si->sn;
187     if (srcp->sflags & SDISTANT) {
188 greg 2.18 if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
189 greg 2.4 continue;
190 greg 1.45 return(1); /* sample OK */
191 greg 1.40 }
192 greg 1.45 /* local source */
193 greg 1.6 /* check proximity */
194 greg 1.45 if (srcp->sflags & SPROX && d > srcp->sl.prox)
195     continue;
196 greg 1.6 /* check angle */
197 greg 1.45 if (srcp->sflags & SSPOT) {
198 greg 2.18 if (spotout(sr, srcp->sl.s))
199 greg 1.45 continue;
200     /* adjust solid angle */
201     si->dom *= d*d;
202     d += srcp->sl.s->flen;
203     si->dom /= d*d;
204 greg 1.1 }
205 greg 1.45 return(1); /* sample OK */
206     }
207     return(0); /* no more samples */
208 greg 1.1 }
209    
210    
211 schorsch 2.44 extern void
212 greg 2.38 srcvalue( /* punch ray to source and compute value */
213 schorsch 2.44 register RAY *r
214 greg 2.38 )
215 greg 1.1 {
216 greg 1.35 register SRCREC *sp;
217 greg 1.1
218 greg 1.35 sp = &source[r->rsrc];
219     if (sp->sflags & SVIRTUAL) { /* virtual source */
220     /* check intersection */
221     if (!(*ofun[sp->so->otype].funp)(sp->so, r))
222     return;
223 greg 2.15 if (!rayshade(r, r->ro->omod)) /* compute contribution */
224     goto nomat;
225 greg 2.19 rayparticipate(r);
226 greg 1.35 return;
227 greg 1.1 }
228 greg 1.35 /* compute intersection */
229     if (sp->sflags & SDISTANT ? sourcehit(r) :
230     (*ofun[sp->so->otype].funp)(sp->so, r)) {
231     if (sp->sa.success >= 0)
232     sp->sa.success++;
233 greg 2.15 if (!rayshade(r, r->ro->omod)) /* compute contribution */
234     goto nomat;
235 greg 2.19 rayparticipate(r);
236 greg 1.35 return;
237 greg 1.1 }
238 greg 2.15 /* we missed our mark! */
239 greg 1.35 if (sp->sa.success < 0)
240     return; /* bitched already */
241     sp->sa.success -= AIMREQT;
242     if (sp->sa.success >= 0)
243     return; /* leniency */
244     sprintf(errmsg, "aiming failure for light source \"%s\"",
245     sp->so->oname);
246     error(WARNING, errmsg); /* issue warning */
247 greg 2.15 return;
248     nomat:
249     objerror(r->ro, USER, "material not found");
250 greg 1.1 }
251    
252    
253 schorsch 2.44 extern int
254 greg 2.38 sourcehit( /* check to see if ray hit distant source */
255 schorsch 2.44 register RAY *r
256 greg 2.38 )
257 greg 2.5 {
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 gregl 2.27 r->robj = objndx(r->ro);
281 greg 2.5 for (i = 0; i < 3; i++)
282     r->ron[i] = -r->rdir[i];
283     r->rod = 1.0;
284 greg 2.31 r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
285     r->uv[0] = r->uv[1] = 0.0;
286 greg 2.5 r->rox = NULL;
287     return(1);
288     }
289     return(0);
290     }
291 greg 2.38
292    
293 greg 1.4 static int
294 greg 2.38 cntcmp( /* contribution compare (descending) */
295 schorsch 2.44 const void *p1,
296     const void *p2
297 greg 2.38 )
298 greg 1.4 {
299 greg 2.38 register const CNTPTR *sc1 = (const CNTPTR *)p1;
300     register const CNTPTR *sc2 = (const CNTPTR *)p2;
301    
302 greg 1.4 if (sc1->brt > sc2->brt)
303     return(-1);
304     if (sc1->brt < sc2->brt)
305     return(1);
306     return(0);
307     }
308    
309    
310 schorsch 2.44 extern void
311 greg 2.38 direct( /* add direct component */
312 schorsch 2.44 RAY *r, /* ray that hit surface */
313     srcdirf_t *f, /* direct component coefficient function */
314     void *p /* data for f */
315 greg 2.38 )
316 greg 1.4 {
317     register int sn;
318 greg 2.7 register CONTRIB *scp;
319 greg 1.45 SRCINDEX si;
320 greg 1.12 int nshadcheck, ncnts;
321 greg 1.29 int nhits;
322 greg 1.45 double prob, ourthresh, hwt;
323 greg 1.4 RAY sr;
324 greg 1.25 /* NOTE: srccnt and cntord global so no recursion */
325 greg 1.22 if (nsources <= 0)
326 greg 1.25 return; /* no sources?! */
327 greg 1.4 /* potential contributions */
328 greg 1.45 initsrcindex(&si);
329     for (sn = 0; srcray(&sr, r, &si); sn++) {
330     if (sn >= maxcntr) {
331     maxcntr = sn + MAXSPART;
332 greg 2.32 srccnt = (CONTRIB *)realloc((void *)srccnt,
333 greg 1.45 maxcntr*sizeof(CONTRIB));
334 greg 2.32 cntord = (CNTPTR *)realloc((void *)cntord,
335 greg 1.45 maxcntr*sizeof(CNTPTR));
336 schorsch 2.35 if ((srccnt == NULL) | (cntord == NULL))
337 greg 1.45 error(SYSTEM, "out of memory in direct");
338     }
339     cntord[sn].sndx = sn;
340 greg 2.7 scp = srccnt + sn;
341     scp->sno = sr.rsrc;
342 greg 1.4 /* compute coefficient */
343 greg 2.7 (*f)(scp->coef, p, sr.rdir, si.dom);
344     cntord[sn].brt = bright(scp->coef);
345 greg 1.15 if (cntord[sn].brt <= 0.0)
346 greg 1.4 continue;
347 greg 2.38 #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 greg 2.7 VCOPY(scp->dir, sr.rdir);
355 greg 1.35 /* compute potential */
356     sr.revf = srcvalue;
357     rayvalue(&sr);
358 greg 2.7 copycolor(scp->val, sr.rcol);
359     multcolor(scp->val, scp->coef);
360     cntord[sn].brt = bright(scp->val);
361 greg 1.4 }
362     /* sort contributions */
363 greg 1.45 qsort(cntord, sn, sizeof(CNTPTR), cntcmp);
364 greg 1.13 { /* find last */
365     register int l, m;
366    
367 greg 1.45 ncnts = l = sn;
368     sn = 0;
369 greg 1.13 while ((m = (sn + ncnts) >> 1) != l) {
370     if (cntord[m].brt > 0.0)
371     sn = m;
372     else
373     ncnts = m;
374     l = m;
375     }
376     }
377 greg 2.2 if (ncnts == 0)
378     return; /* no contributions! */
379 greg 1.12 /* accumulate tail */
380     for (sn = ncnts-1; sn > 0; sn--)
381     cntord[sn-1].brt += cntord[sn].brt;
382 greg 1.45 /* compute number to check */
383     nshadcheck = pow((double)ncnts, shadcert) + .5;
384     /* modify threshold */
385     ourthresh = shadthresh / r->rweight;
386 greg 1.10 /* test for shadows */
387 greg 2.13 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 greg 1.10 /* check threshold */
392 greg 1.12 if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
393 greg 1.27 cntord[sn].brt-cntord[sn+nshadcheck].brt)
394     < ourthresh*bright(r->rcol))
395 greg 1.4 break;
396 greg 2.7 scp = srccnt + cntord[sn].sndx;
397 greg 1.4 /* test for hit */
398     rayorigin(&sr, r, SHADOW, 1.0);
399 greg 2.7 VCOPY(sr.rdir, scp->dir);
400     sr.rsrc = scp->sno;
401 greg 2.34 /* keep statistics */
402     if (source[scp->sno].ntests++ > 0xfffffff0) {
403     source[scp->sno].ntests >>= 1;
404     source[scp->sno].nhits >>= 1;
405     }
406 greg 1.4 if (localhit(&sr, &thescene) &&
407 greg 2.7 ( sr.ro != source[scp->sno].so ||
408     source[scp->sno].sflags & SFOLLOW )) {
409 greg 1.33 /* follow entire path */
410 greg 2.23 raycont(&sr);
411 greg 2.19 rayparticipate(&sr);
412 greg 1.42 if (trace != NULL)
413     (*trace)(&sr); /* trace execution */
414 greg 2.38 if (bright(sr.rcol) <= FTINY) {
415     #if SHADCACHE
416     if ((scp <= srccnt || scp[-1].sno != scp->sno)
417 greg 2.41 && (scp >= srccnt+ncnts-1 ||
418 greg 2.38 scp[1].sno != scp->sno))
419     srcblocker(&sr);
420     #endif
421 greg 1.4 continue; /* missed! */
422 greg 2.38 }
423 greg 2.7 copycolor(scp->val, sr.rcol);
424     multcolor(scp->val, scp->coef);
425 greg 1.4 }
426     /* add contribution if hit */
427 greg 2.7 addcolor(r->rcol, scp->val);
428 greg 1.29 nhits++;
429 greg 2.7 source[scp->sno].nhits++;
430 greg 1.4 }
431 greg 2.13 /* source hit rate */
432     if (hwt > FTINY)
433     hwt = (double)nhits / hwt;
434 greg 1.29 else
435     hwt = 0.5;
436 greg 1.20 #ifdef DEBUG
437 greg 2.13 sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n",
438 greg 1.12 sn, ncnts-sn, hwt);
439     eputs(errmsg);
440 greg 1.4 #endif
441     /* add in untested sources */
442 greg 1.12 for ( ; sn < ncnts; sn++) {
443 greg 2.7 scp = srccnt + cntord[sn].sndx;
444     prob = hwt * (double)source[scp->sno].nhits /
445     (double)source[scp->sno].ntests;
446 greg 2.13 if (prob > 1.0)
447     prob = 1.0;
448 greg 2.7 scalecolor(scp->val, prob);
449     addcolor(r->rcol, scp->val);
450 greg 2.19 }
451     }
452    
453    
454 schorsch 2.44 extern void
455 greg 2.38 srcscatter( /* compute source scattering into ray */
456 schorsch 2.44 register RAY *r
457 greg 2.38 )
458 greg 2.19 {
459 greg 2.20 int oldsampndx;
460 greg 2.19 int nsamps;
461     RAY sr;
462     SRCINDEX si;
463 greg 2.25 double t, d;
464     double re, ge, be;
465     COLOR cvext;
466 greg 2.19 int i, j;
467    
468 greg 2.22 if (r->slights == NULL || r->slights[0] == 0
469     || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
470 greg 2.19 return;
471     if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
472     nsamps = 1;
473 greg 2.22 #if MAXSSAMP
474     else if (nsamps > MAXSSAMP)
475     nsamps = MAXSSAMP;
476     #endif
477 greg 2.20 oldsampndx = samplendx;
478     samplendx = random()&0x7fff; /* randomize */
479 greg 2.19 for (i = r->slights[0]; i > 0; i--) { /* for each source */
480 greg 2.25 for (j = 0; j < nsamps; j++) { /* for each sample position */
481 greg 2.19 samplendx++;
482     t = r->rot * (j+frandom())/nsamps;
483 greg 2.25 /* 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 greg 2.19 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 greg 2.21 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 greg 2.45 #if SHADCACHE
503     if (srcblocked(&sr)) /* check shadow cache */
504     continue;
505     #endif
506 greg 2.19 copycolor(sr.cext, r->cext);
507 greg 2.24 copycolor(sr.albedo, r->albedo);
508 greg 2.19 sr.gecc = r->gecc;
509 gregl 2.26 sr.slights = r->slights;
510 greg 2.19 rayvalue(&sr); /* eval. source ray */
511 greg 2.45 if (bright(sr.rcol) <= FTINY) {
512     #if SHADCACHE
513     srcblocker(&sr); /* add blocker to cache */
514     #endif
515 greg 2.19 continue;
516 greg 2.45 }
517 greg 2.19 if (r->gecc <= FTINY) /* compute P(theta) */
518     d = 1.;
519     else {
520     d = DOT(r->rdir, sr.rdir);
521 greg 2.25 d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
522     d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
523 greg 2.19 }
524     /* other factors */
525 greg 2.24 d *= si.dom * r->rot / (4.*PI*nsamps);
526 greg 2.19 multcolor(sr.rcol, r->cext);
527 greg 2.24 multcolor(sr.rcol, r->albedo);
528 greg 2.19 scalecolor(sr.rcol, d);
529 greg 2.25 multcolor(sr.rcol, cvext);
530     addcolor(r->rcol, sr.rcol); /* add it in */
531 greg 2.19 }
532 greg 1.1 }
533 greg 2.20 samplendx = oldsampndx;
534 greg 2.4 }
535    
536    
537     /****************************************************************
538     * The following macros were separated from the m_light() routine
539     * because they are very nasty and difficult to understand.
540     */
541    
542 greg 2.11 /* illumblock *
543 greg 2.4 *
544     * We cannot allow an illum to pass to another illum, because that
545     * would almost certainly constitute overcounting.
546     * However, we do allow an illum to pass to another illum
547     * that is actually going to relay to a virtual light source.
548 greg 2.11 * We also prevent an illum from passing to a glow; this provides a
549     * convenient mechanism for defining detailed light source
550     * geometry behind (or inside) an effective radiator.
551 greg 2.4 */
552    
553 greg 2.37 static int
554 greg 2.42 weaksrcmat(OBJECT obj) /* identify material */
555 greg 2.37 {
556 greg 2.42 OBJREC *o = findmaterial(objptr(obj));
557 greg 2.37
558 greg 2.42 if (o == NULL) return 0;
559 greg 2.37 return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW));
560     }
561 greg 2.4
562 greg 2.11 #define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \
563 greg 2.12 r->rod > 0.0 && \
564 greg 2.37 weaksrcmat(source[r->rsrc].so->omod))
565 greg 2.11
566 greg 2.4 /* wrongsource *
567     *
568     * This source is the wrong source (ie. overcounted) if we are
569     * aimed to a different source than the one we hit and the one
570 greg 2.11 * we hit is not an illum that should be passed.
571 greg 2.4 */
572    
573     #define wrongsource(m, r) (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \
574 greg 2.11 (m->otype!=MAT_ILLUM || illumblock(m,r)))
575 greg 2.4
576     /* distglow *
577     *
578     * A distant glow is an object that sometimes acts as a light source,
579     * but is too far away from the test point to be one in this case.
580 greg 2.11 * (Glows with negative radii should NEVER participate in illumination.)
581 greg 2.4 */
582    
583 greg 2.17 #define distglow(m, r, d) (m->otype==MAT_GLOW && \
584 greg 2.10 m->oargs.farg[3] >= -FTINY && \
585 greg 2.17 d > m->oargs.farg[3])
586 greg 2.4
587     /* badcomponent *
588     *
589     * We must avoid counting light sources in the ambient calculation,
590     * since the direct component is handled separately. Therefore, any
591     * ambient ray which hits an active light source must be discarded.
592     * The same is true for stray specular samples, since the specular
593     * contribution from light sources is calculated separately.
594     */
595    
596     #define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \
597     !(r->crtype&SHADOW || r->rod < 0.0 || \
598 greg 2.17 /* not 100% correct */ distglow(m, r, r->rot)))
599 greg 2.4
600     /* passillum *
601     *
602     * An illum passes to another material type when we didn't hit it
603     * on purpose (as part of a direct calculation), or it is relaying
604     * a virtual light source.
605     */
606    
607     #define passillum(m, r) (m->otype==MAT_ILLUM && \
608     (r->rsrc<0 || source[r->rsrc].so!=r->ro || \
609     source[r->rsrc].sflags&SVIRTUAL))
610    
611     /* srcignore *
612     *
613 greg 2.10 * The -dv flag is normally on for sources to be visible.
614 greg 2.4 */
615    
616 greg 2.17 #define srcignore(m, r) !(directvis || r->crtype&SHADOW || \
617     distglow(m, r, raydist(r,PRIMARY)))
618 greg 2.4
619    
620 schorsch 2.44 extern int
621 greg 2.38 m_light( /* ray hit a light source */
622 schorsch 2.44 register OBJREC *m,
623     register RAY *r
624 greg 2.38 )
625 greg 2.4 {
626     /* check for over-counting */
627 greg 2.12 if (badcomponent(m, r))
628 greg 2.14 return(1);
629 greg 2.17 if (wrongsource(m, r))
630 greg 2.14 return(1);
631 greg 2.4 /* check for passed illum */
632     if (passillum(m, r)) {
633 greg 2.14 if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
634 gwlarson 2.28 return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0])));
635 greg 2.14 raytrans(r);
636     return(1);
637 greg 2.4 }
638     /* otherwise treat as source */
639     /* check for behind */
640     if (r->rod < 0.0)
641 greg 2.14 return(1);
642 greg 2.4 /* check for invisibility */
643     if (srcignore(m, r))
644 greg 2.14 return(1);
645 greg 2.4 /* check for outside spot */
646 greg 2.18 if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
647 greg 2.14 return(1);
648 greg 2.4 /* get distribution pattern */
649     raytexture(r, m->omod);
650     /* get source color */
651     setcolor(r->rcol, m->oargs.farg[0],
652     m->oargs.farg[1],
653     m->oargs.farg[2]);
654     /* modify value */
655     multcolor(r->rcol, r->pcol);
656 greg 2.14 return(1);
657 greg 1.1 }