ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 2.29
Committed: Sat Feb 22 02:07:29 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.28: +86 -9 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.29 static const char RCSid[] = "$Id$";
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     /* ====================================================================
11     * The Radiance Software License, Version 1.0
12     *
13     * Copyright (c) 1990 - 2002 The Regents of the University of California,
14     * through Lawrence Berkeley National Laboratory. All rights reserved.
15     *
16     * Redistribution and use in source and binary forms, with or without
17     * modification, are permitted provided that the following conditions
18     * are met:
19     *
20     * 1. Redistributions of source code must retain the above copyright
21     * notice, this list of conditions and the following disclaimer.
22     *
23     * 2. Redistributions in binary form must reproduce the above copyright
24     * notice, this list of conditions and the following disclaimer in
25     * the documentation and/or other materials provided with the
26     * distribution.
27     *
28     * 3. The end-user documentation included with the redistribution,
29     * if any, must include the following acknowledgment:
30     * "This product includes Radiance software
31     * (http://radsite.lbl.gov/)
32     * developed by the Lawrence Berkeley National Laboratory
33     * (http://www.lbl.gov/)."
34     * Alternately, this acknowledgment may appear in the software itself,
35     * if and wherever such third-party acknowledgments normally appear.
36     *
37     * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38     * and "The Regents of the University of California" must
39     * not be used to endorse or promote products derived from this
40     * software without prior written permission. For written
41     * permission, please contact [email protected].
42     *
43     * 5. Products derived from this software may not be called "Radiance",
44     * nor may "Radiance" appear in their name, without prior written
45     * permission of Lawrence Berkeley National Laboratory.
46     *
47     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50     * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58     * SUCH DAMAGE.
59     * ====================================================================
60     *
61     * This software consists of voluntary contributions made by many
62     * individuals on behalf of Lawrence Berkeley National Laboratory. For more
63     * information on Lawrence Berkeley National Laboratory, please see
64     * <http://www.lbl.gov/>.
65 greg 1.1 */
66    
67     #include "ray.h"
68    
69     #include "otypes.h"
70    
71 greg 1.35 #include "source.h"
72 greg 1.1
73 greg 2.19 #include "random.h"
74    
75     extern double ssampdist; /* scatter sampling distance */
76    
77 greg 2.22 #ifndef MAXSSAMP
78     #define MAXSSAMP 16 /* maximum samples per ray */
79     #endif
80    
81 greg 1.35 /*
82     * Structures used by direct()
83     */
84 greg 1.1
85 greg 1.35 typedef struct {
86 greg 1.45 int sno; /* source number */
87 greg 1.35 FVECT dir; /* source direction */
88     COLOR coef; /* material coefficient */
89     COLOR val; /* contribution */
90     } CONTRIB; /* direct contribution */
91 greg 1.1
92 greg 1.35 typedef struct {
93 greg 1.45 int sndx; /* source index (to CONTRIB array) */
94 greg 1.35 float brt; /* brightness (for comparison) */
95     } CNTPTR; /* contribution pointer */
96 greg 1.1
97 greg 1.25 static CONTRIB *srccnt; /* source contributions in direct() */
98     static CNTPTR *cntord; /* source ordering in direct() */
99 greg 1.45 static int maxcntr = 0; /* size of contribution arrays */
100 greg 1.1
101 greg 1.25
102 greg 2.29 void
103 greg 1.1 marksources() /* find and mark source objects */
104     {
105 greg 2.3 int foundsource = 0;
106 greg 1.33 int i;
107 greg 1.1 register OBJREC *o, *m;
108 greg 1.36 register int ns;
109 greg 1.35 /* initialize dispatch table */
110     initstypes();
111     /* find direct sources */
112 greg 1.1 for (i = 0; i < nobjects; i++) {
113    
114     o = objptr(i);
115    
116 greg 1.35 if (!issurface(o->otype) || o->omod == OVOID)
117 greg 1.1 continue;
118    
119     m = objptr(o->omod);
120    
121 greg 1.24 if (!islight(m->otype))
122 greg 1.1 continue;
123    
124 greg 1.6 if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
125     m->otype == MAT_SPOT ? 7 : 3))
126 greg 1.1 objerror(m, USER, "bad # arguments");
127    
128 greg 1.6 if (m->otype == MAT_GLOW &&
129     o->otype != OBJ_SOURCE &&
130     m->oargs.farg[3] <= FTINY)
131 greg 1.1 continue; /* don't bother */
132 greg 2.16 if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY &&
133     m->oargs.farg[2] <= FTINY)
134     continue; /* don't bother */
135 greg 1.1
136 greg 1.35 if (sfun[o->otype].of == NULL ||
137     sfun[o->otype].of->setsrc == NULL)
138     objerror(o, USER, "illegal material");
139    
140 greg 1.36 if ((ns = newsource()) < 0)
141 greg 1.25 goto memerr;
142 greg 1.1
143 greg 1.37 setsource(&source[ns], o);
144 greg 1.1
145 greg 1.6 if (m->otype == MAT_GLOW) {
146 greg 1.36 source[ns].sflags |= SPROX;
147     source[ns].sl.prox = m->oargs.farg[3];
148 greg 2.8 if (source[ns].sflags & SDISTANT)
149 greg 1.36 source[ns].sflags |= SSKIP;
150 greg 1.6 } else if (m->otype == MAT_SPOT) {
151 greg 1.36 source[ns].sflags |= SSPOT;
152     if ((source[ns].sl.s = makespot(m)) == NULL)
153 greg 1.33 goto memerr;
154 greg 1.38 if (source[ns].sflags & SFLAT &&
155     !checkspot(source[ns].sl.s,source[ns].snorm)) {
156     objerror(o, WARNING,
157     "invalid spotlight direction");
158     source[ns].sflags |= SSKIP;
159     }
160 greg 1.6 }
161 greg 2.3 if (!(source[ns].sflags & SSKIP))
162     foundsource++;
163 greg 1.1 }
164 greg 2.3 if (!foundsource) {
165 greg 1.25 error(WARNING, "no light sources found");
166     return;
167     }
168 greg 1.33 markvirtuals(); /* find and add virtual sources */
169 greg 1.45 /* allocate our contribution arrays */
170 greg 1.48 maxcntr = nsources + MAXSPART; /* start with this many */
171 greg 1.45 srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
172     cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR));
173 greg 1.48 if (srccnt == NULL | cntord == NULL)
174 greg 1.33 goto memerr;
175     return;
176 greg 1.25 memerr:
177     error(SYSTEM, "out of memory in marksources");
178 greg 1.1 }
179    
180    
181 greg 2.29 void
182     freesources() /* free all source structures */
183     {
184     if (nsources > 0) {
185     free((void *)source);
186     source = NULL;
187     nsources = 0;
188     }
189     if (maxcntr <= 0)
190     return;
191     free((void *)srccnt);
192     srccnt = NULL;
193     free((void *)cntord);
194     cntord = NULL;
195     maxcntr = 0;
196     }
197    
198    
199     int
200 greg 1.45 srcray(sr, r, si) /* send a ray to a source, return domega */
201 greg 1.1 register RAY *sr; /* returned source ray */
202     RAY *r; /* ray which hit object */
203 greg 1.45 SRCINDEX *si; /* source sample index */
204 greg 1.1 {
205 greg 1.45 double d; /* distance to source */
206     register SRCREC *srcp;
207 greg 1.1
208 greg 1.45 rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */
209 greg 1.1
210 greg 1.47 while ((d = nextssamp(sr, si)) != 0.0) {
211 greg 1.45 sr->rsrc = si->sn; /* remember source */
212     srcp = source + si->sn;
213     if (srcp->sflags & SDISTANT) {
214 greg 2.18 if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
215 greg 2.4 continue;
216 greg 1.45 return(1); /* sample OK */
217 greg 1.40 }
218 greg 1.45 /* local source */
219 greg 1.6 /* check proximity */
220 greg 1.45 if (srcp->sflags & SPROX && d > srcp->sl.prox)
221     continue;
222 greg 1.6 /* check angle */
223 greg 1.45 if (srcp->sflags & SSPOT) {
224 greg 2.18 if (spotout(sr, srcp->sl.s))
225 greg 1.45 continue;
226     /* adjust solid angle */
227     si->dom *= d*d;
228     d += srcp->sl.s->flen;
229     si->dom /= d*d;
230 greg 1.1 }
231 greg 1.45 return(1); /* sample OK */
232     }
233     return(0); /* no more samples */
234 greg 1.1 }
235    
236    
237 greg 2.29 void
238 greg 1.35 srcvalue(r) /* punch ray to source and compute value */
239 greg 2.15 register RAY *r;
240 greg 1.1 {
241 greg 1.35 register SRCREC *sp;
242 greg 1.1
243 greg 1.35 sp = &source[r->rsrc];
244     if (sp->sflags & SVIRTUAL) { /* virtual source */
245     /* check intersection */
246     if (!(*ofun[sp->so->otype].funp)(sp->so, r))
247     return;
248 greg 2.15 if (!rayshade(r, r->ro->omod)) /* compute contribution */
249     goto nomat;
250 greg 2.19 rayparticipate(r);
251 greg 1.35 return;
252 greg 1.1 }
253 greg 1.35 /* compute intersection */
254     if (sp->sflags & SDISTANT ? sourcehit(r) :
255     (*ofun[sp->so->otype].funp)(sp->so, r)) {
256     if (sp->sa.success >= 0)
257     sp->sa.success++;
258 greg 2.15 if (!rayshade(r, r->ro->omod)) /* compute contribution */
259     goto nomat;
260 greg 2.19 rayparticipate(r);
261 greg 1.35 return;
262 greg 1.1 }
263 greg 2.15 /* we missed our mark! */
264 greg 1.35 if (sp->sa.success < 0)
265     return; /* bitched already */
266     sp->sa.success -= AIMREQT;
267     if (sp->sa.success >= 0)
268     return; /* leniency */
269     sprintf(errmsg, "aiming failure for light source \"%s\"",
270     sp->so->oname);
271     error(WARNING, errmsg); /* issue warning */
272 greg 2.15 return;
273     nomat:
274     objerror(r->ro, USER, "material not found");
275 greg 1.1 }
276    
277    
278 greg 2.29 int
279 greg 2.5 sourcehit(r) /* check to see if ray hit distant source */
280     register RAY *r;
281     {
282     int first, last;
283     register int i;
284    
285     if (r->rsrc >= 0) { /* check only one if aimed */
286     first = last = r->rsrc;
287     } else { /* otherwise check all */
288     first = 0; last = nsources-1;
289     }
290     for (i = first; i <= last; i++)
291     if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT)
292     /*
293     * Check to see if ray is within
294     * solid angle of source.
295     */
296     if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
297     <= source[i].ss2) {
298     r->ro = source[i].so;
299     if (!(source[i].sflags & SSKIP))
300     break;
301     }
302    
303     if (r->ro != NULL) {
304 gregl 2.27 r->robj = objndx(r->ro);
305 greg 2.5 for (i = 0; i < 3; i++)
306     r->ron[i] = -r->rdir[i];
307     r->rod = 1.0;
308     r->rox = NULL;
309     return(1);
310     }
311     return(0);
312     }
313    
314    
315 greg 1.4 static int
316     cntcmp(sc1, sc2) /* contribution compare (descending) */
317 greg 1.9 register CNTPTR *sc1, *sc2;
318 greg 1.4 {
319     if (sc1->brt > sc2->brt)
320     return(-1);
321     if (sc1->brt < sc2->brt)
322     return(1);
323     return(0);
324     }
325    
326    
327 greg 2.29 void
328 greg 1.4 direct(r, f, p) /* add direct component */
329     RAY *r; /* ray that hit surface */
330 greg 2.29 void (*f)(); /* direct component coefficient function */
331 greg 1.4 char *p; /* data for f */
332     {
333 greg 2.29 extern void (*trace)();
334 greg 1.4 register int sn;
335 greg 2.7 register CONTRIB *scp;
336 greg 1.45 SRCINDEX si;
337 greg 1.12 int nshadcheck, ncnts;
338 greg 1.29 int nhits;
339 greg 1.45 double prob, ourthresh, hwt;
340 greg 1.4 RAY sr;
341 greg 1.25 /* NOTE: srccnt and cntord global so no recursion */
342 greg 1.22 if (nsources <= 0)
343 greg 1.25 return; /* no sources?! */
344 greg 1.4 /* potential contributions */
345 greg 1.45 initsrcindex(&si);
346     for (sn = 0; srcray(&sr, r, &si); sn++) {
347     if (sn >= maxcntr) {
348     maxcntr = sn + MAXSPART;
349     srccnt = (CONTRIB *)realloc((char *)srccnt,
350     maxcntr*sizeof(CONTRIB));
351     cntord = (CNTPTR *)realloc((char *)cntord,
352     maxcntr*sizeof(CNTPTR));
353 greg 1.48 if (srccnt == NULL | cntord == NULL)
354 greg 1.45 error(SYSTEM, "out of memory in direct");
355     }
356     cntord[sn].sndx = sn;
357 greg 2.7 scp = srccnt + sn;
358     scp->sno = sr.rsrc;
359 greg 1.4 /* compute coefficient */
360 greg 2.7 (*f)(scp->coef, p, sr.rdir, si.dom);
361     cntord[sn].brt = bright(scp->coef);
362 greg 1.15 if (cntord[sn].brt <= 0.0)
363 greg 1.4 continue;
364 greg 2.7 VCOPY(scp->dir, sr.rdir);
365 greg 1.35 /* compute potential */
366     sr.revf = srcvalue;
367     rayvalue(&sr);
368 greg 2.7 copycolor(scp->val, sr.rcol);
369     multcolor(scp->val, scp->coef);
370     cntord[sn].brt = bright(scp->val);
371 greg 1.4 }
372     /* sort contributions */
373 greg 1.45 qsort(cntord, sn, sizeof(CNTPTR), cntcmp);
374 greg 1.13 { /* find last */
375     register int l, m;
376    
377 greg 1.45 ncnts = l = sn;
378     sn = 0;
379 greg 1.13 while ((m = (sn + ncnts) >> 1) != l) {
380     if (cntord[m].brt > 0.0)
381     sn = m;
382     else
383     ncnts = m;
384     l = m;
385     }
386     }
387 greg 2.2 if (ncnts == 0)
388     return; /* no contributions! */
389 greg 1.12 /* accumulate tail */
390     for (sn = ncnts-1; sn > 0; sn--)
391     cntord[sn-1].brt += cntord[sn].brt;
392 greg 1.45 /* compute number to check */
393     nshadcheck = pow((double)ncnts, shadcert) + .5;
394     /* modify threshold */
395     ourthresh = shadthresh / r->rweight;
396 greg 1.10 /* test for shadows */
397 greg 2.13 for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts;
398     hwt += (double)source[scp->sno].nhits /
399     (double)source[scp->sno].ntests,
400     sn++) {
401 greg 1.10 /* check threshold */
402 greg 1.12 if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
403 greg 1.27 cntord[sn].brt-cntord[sn+nshadcheck].brt)
404     < ourthresh*bright(r->rcol))
405 greg 1.4 break;
406 greg 2.7 scp = srccnt + cntord[sn].sndx;
407 greg 1.4 /* test for hit */
408     rayorigin(&sr, r, SHADOW, 1.0);
409 greg 2.7 VCOPY(sr.rdir, scp->dir);
410     sr.rsrc = scp->sno;
411     source[scp->sno].ntests++; /* keep statistics */
412 greg 1.4 if (localhit(&sr, &thescene) &&
413 greg 2.7 ( sr.ro != source[scp->sno].so ||
414     source[scp->sno].sflags & SFOLLOW )) {
415 greg 1.33 /* follow entire path */
416 greg 2.23 raycont(&sr);
417 greg 2.19 rayparticipate(&sr);
418 greg 1.42 if (trace != NULL)
419     (*trace)(&sr); /* trace execution */
420 greg 1.5 if (bright(sr.rcol) <= FTINY)
421 greg 1.4 continue; /* missed! */
422 greg 2.7 copycolor(scp->val, sr.rcol);
423     multcolor(scp->val, scp->coef);
424 greg 1.4 }
425     /* add contribution if hit */
426 greg 2.7 addcolor(r->rcol, scp->val);
427 greg 1.29 nhits++;
428 greg 2.7 source[scp->sno].nhits++;
429 greg 1.4 }
430 greg 2.13 /* source hit rate */
431     if (hwt > FTINY)
432     hwt = (double)nhits / hwt;
433 greg 1.29 else
434     hwt = 0.5;
435 greg 1.20 #ifdef DEBUG
436 greg 2.13 sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n",
437 greg 1.12 sn, ncnts-sn, hwt);
438     eputs(errmsg);
439 greg 1.4 #endif
440     /* add in untested sources */
441 greg 1.12 for ( ; sn < ncnts; sn++) {
442 greg 2.7 scp = srccnt + cntord[sn].sndx;
443     prob = hwt * (double)source[scp->sno].nhits /
444     (double)source[scp->sno].ntests;
445 greg 2.13 if (prob > 1.0)
446     prob = 1.0;
447 greg 2.7 scalecolor(scp->val, prob);
448     addcolor(r->rcol, scp->val);
449 greg 2.19 }
450     }
451    
452    
453 greg 2.29 void
454 greg 2.19 srcscatter(r) /* compute source scattering into ray */
455     register RAY *r;
456     {
457 greg 2.20 int oldsampndx;
458 greg 2.19 int nsamps;
459     RAY sr;
460     SRCINDEX si;
461 greg 2.25 double t, d;
462     double re, ge, be;
463     COLOR cvext;
464 greg 2.19 int i, j;
465    
466 greg 2.22 if (r->slights == NULL || r->slights[0] == 0
467     || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
468 greg 2.19 return;
469     if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
470     nsamps = 1;
471 greg 2.22 #if MAXSSAMP
472     else if (nsamps > MAXSSAMP)
473     nsamps = MAXSSAMP;
474     #endif
475 greg 2.20 oldsampndx = samplendx;
476     samplendx = random()&0x7fff; /* randomize */
477 greg 2.19 for (i = r->slights[0]; i > 0; i--) { /* for each source */
478 greg 2.25 for (j = 0; j < nsamps; j++) { /* for each sample position */
479 greg 2.19 samplendx++;
480     t = r->rot * (j+frandom())/nsamps;
481 greg 2.25 /* extinction */
482     re = t*colval(r->cext,RED);
483     ge = t*colval(r->cext,GRN);
484     be = t*colval(r->cext,BLU);
485     setcolor(cvext, re > 92. ? 0. : exp(-re),
486     ge > 92. ? 0. : exp(-ge),
487     be > 92. ? 0. : exp(-be));
488     if (intens(cvext) <= FTINY)
489     break; /* too far away */
490 greg 2.19 sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
491     sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
492     sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
493     sr.rmax = 0.;
494 greg 2.21 initsrcindex(&si); /* sample ray to this source */
495     si.sn = r->slights[i];
496     nopart(&si, &sr);
497     if (!srcray(&sr, NULL, &si) ||
498     sr.rsrc != r->slights[i])
499     continue; /* no path */
500 greg 2.19 copycolor(sr.cext, r->cext);
501 greg 2.24 copycolor(sr.albedo, r->albedo);
502 greg 2.19 sr.gecc = r->gecc;
503 gregl 2.26 sr.slights = r->slights;
504 greg 2.19 rayvalue(&sr); /* eval. source ray */
505     if (bright(sr.rcol) <= FTINY)
506     continue;
507     if (r->gecc <= FTINY) /* compute P(theta) */
508     d = 1.;
509     else {
510     d = DOT(r->rdir, sr.rdir);
511 greg 2.25 d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
512     d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
513 greg 2.19 }
514     /* other factors */
515 greg 2.24 d *= si.dom * r->rot / (4.*PI*nsamps);
516 greg 2.19 multcolor(sr.rcol, r->cext);
517 greg 2.24 multcolor(sr.rcol, r->albedo);
518 greg 2.19 scalecolor(sr.rcol, d);
519 greg 2.25 multcolor(sr.rcol, cvext);
520     addcolor(r->rcol, sr.rcol); /* add it in */
521 greg 2.19 }
522 greg 1.1 }
523 greg 2.20 samplendx = oldsampndx;
524 greg 2.4 }
525    
526    
527     /****************************************************************
528     * The following macros were separated from the m_light() routine
529     * because they are very nasty and difficult to understand.
530     */
531    
532 greg 2.11 /* illumblock *
533 greg 2.4 *
534     * We cannot allow an illum to pass to another illum, because that
535     * would almost certainly constitute overcounting.
536     * However, we do allow an illum to pass to another illum
537     * that is actually going to relay to a virtual light source.
538 greg 2.11 * We also prevent an illum from passing to a glow; this provides a
539     * convenient mechanism for defining detailed light source
540     * geometry behind (or inside) an effective radiator.
541 greg 2.4 */
542    
543 greg 2.11 static int weaksrcmod(obj) int obj; /* efficiency booster function */
544     {register OBJREC *o = objptr(obj);
545     return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
546 greg 2.4
547 greg 2.11 #define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \
548 greg 2.12 r->rod > 0.0 && \
549 greg 2.11 weaksrcmod(source[r->rsrc].so->omod))
550    
551 greg 2.4 /* wrongsource *
552     *
553     * This source is the wrong source (ie. overcounted) if we are
554     * aimed to a different source than the one we hit and the one
555 greg 2.11 * we hit is not an illum that should be passed.
556 greg 2.4 */
557    
558     #define wrongsource(m, r) (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \
559 greg 2.11 (m->otype!=MAT_ILLUM || illumblock(m,r)))
560 greg 2.4
561     /* distglow *
562     *
563     * A distant glow is an object that sometimes acts as a light source,
564     * but is too far away from the test point to be one in this case.
565 greg 2.11 * (Glows with negative radii should NEVER participate in illumination.)
566 greg 2.4 */
567    
568 greg 2.17 #define distglow(m, r, d) (m->otype==MAT_GLOW && \
569 greg 2.10 m->oargs.farg[3] >= -FTINY && \
570 greg 2.17 d > m->oargs.farg[3])
571 greg 2.4
572     /* badcomponent *
573     *
574     * We must avoid counting light sources in the ambient calculation,
575     * since the direct component is handled separately. Therefore, any
576     * ambient ray which hits an active light source must be discarded.
577     * The same is true for stray specular samples, since the specular
578     * contribution from light sources is calculated separately.
579     */
580    
581     #define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \
582     !(r->crtype&SHADOW || r->rod < 0.0 || \
583 greg 2.17 /* not 100% correct */ distglow(m, r, r->rot)))
584 greg 2.4
585     /* passillum *
586     *
587     * An illum passes to another material type when we didn't hit it
588     * on purpose (as part of a direct calculation), or it is relaying
589     * a virtual light source.
590     */
591    
592     #define passillum(m, r) (m->otype==MAT_ILLUM && \
593     (r->rsrc<0 || source[r->rsrc].so!=r->ro || \
594     source[r->rsrc].sflags&SVIRTUAL))
595    
596     /* srcignore *
597     *
598 greg 2.10 * The -dv flag is normally on for sources to be visible.
599 greg 2.4 */
600    
601 greg 2.17 #define srcignore(m, r) !(directvis || r->crtype&SHADOW || \
602     distglow(m, r, raydist(r,PRIMARY)))
603 greg 2.4
604    
605 greg 2.29 int
606 greg 2.4 m_light(m, r) /* ray hit a light source */
607     register OBJREC *m;
608     register RAY *r;
609     {
610     /* check for over-counting */
611 greg 2.12 if (badcomponent(m, r))
612 greg 2.14 return(1);
613 greg 2.17 if (wrongsource(m, r))
614 greg 2.14 return(1);
615 greg 2.4 /* check for passed illum */
616     if (passillum(m, r)) {
617 greg 2.14 if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
618 gwlarson 2.28 return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0])));
619 greg 2.14 raytrans(r);
620     return(1);
621 greg 2.4 }
622     /* otherwise treat as source */
623     /* check for behind */
624     if (r->rod < 0.0)
625 greg 2.14 return(1);
626 greg 2.4 /* check for invisibility */
627     if (srcignore(m, r))
628 greg 2.14 return(1);
629 greg 2.4 /* check for outside spot */
630 greg 2.18 if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
631 greg 2.14 return(1);
632 greg 2.4 /* get distribution pattern */
633     raytexture(r, m->omod);
634     /* get source color */
635     setcolor(r->rcol, m->oargs.farg[0],
636     m->oargs.farg[1],
637     m->oargs.farg[2]);
638     /* modify value */
639     multcolor(r->rcol, r->pcol);
640 greg 2.14 return(1);
641 greg 1.1 }