ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 2.65
Committed: Thu May 28 09:03:54 2015 UTC (8 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.64: +4 -5 lines
Log Message:
Moved source obstructor initialization out of source checking loop(!)

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.65 static const char RCSid[] = "$Id: source.c,v 2.64 2015/02/24 19:39:27 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 greg 2.64 #include "pmap.h"
16     #include "pmapsrc.h"
17 greg 2.19
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 greg 2.62 OBJREC * /* find an object's actual material */
46     findmaterial(OBJREC *o)
47 greg 2.38 {
48     while (!ismaterial(o->otype)) {
49     if (o->otype == MOD_ALIAS && o->oargs.nsargs) {
50     OBJECT aobj;
51     OBJREC *ao;
52     aobj = lastmod(objndx(o), o->oargs.sarg[0]);
53     if (aobj < 0)
54     objerror(o, USER, "bad reference");
55     ao = objptr(aobj);
56     if (ismaterial(ao->otype))
57     return(ao);
58 greg 2.54 if (ao->otype == MOD_ALIAS) {
59     o = ao;
60     continue;
61     }
62 greg 2.38 }
63     if (o->omod == OVOID)
64     return(NULL);
65     o = objptr(o->omod);
66     }
67 greg 2.48 return(o); /* mixtures will return NULL */
68 greg 2.38 }
69    
70    
71 greg 2.62 void
72 schorsch 2.44 marksources(void) /* find and mark source objects */
73 greg 1.1 {
74 greg 2.3 int foundsource = 0;
75 greg 1.33 int i;
76 greg 2.62 OBJREC *o, *m;
77     int ns;
78 greg 1.35 /* initialize dispatch table */
79     initstypes();
80     /* find direct sources */
81 greg 2.33 for (i = 0; i < nsceneobjs; i++) {
82 greg 1.1
83     o = objptr(i);
84    
85 greg 1.35 if (!issurface(o->otype) || o->omod == OVOID)
86 greg 1.1 continue;
87 greg 2.36 /* find material */
88 greg 2.43 m = findmaterial(objptr(o->omod));
89 greg 2.56 if (m == NULL)
90     continue;
91     if (m->otype == MAT_CLIP) {
92     markclip(m); /* special case for antimatter */
93     continue;
94     }
95     if (!islight(m->otype))
96 greg 2.36 continue; /* not source modifier */
97 greg 1.1
98 greg 1.6 if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
99     m->otype == MAT_SPOT ? 7 : 3))
100 greg 1.1 objerror(m, USER, "bad # arguments");
101    
102 greg 2.16 if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY &&
103     m->oargs.farg[2] <= FTINY)
104     continue; /* don't bother */
105 greg 2.61 if (m->otype == MAT_GLOW &&
106     o->otype != OBJ_SOURCE &&
107     m->oargs.farg[3] <= FTINY) {
108     foundsource += (ambounce > 0);
109     continue; /* don't track these */
110     }
111 greg 1.35 if (sfun[o->otype].of == NULL ||
112     sfun[o->otype].of->setsrc == NULL)
113     objerror(o, USER, "illegal material");
114    
115 greg 1.36 if ((ns = newsource()) < 0)
116 greg 1.25 goto memerr;
117 greg 1.1
118 greg 1.37 setsource(&source[ns], o);
119 greg 1.1
120 greg 1.6 if (m->otype == MAT_GLOW) {
121 greg 1.36 source[ns].sflags |= SPROX;
122     source[ns].sl.prox = m->oargs.farg[3];
123 greg 2.61 if (source[ns].sflags & SDISTANT) {
124 greg 1.36 source[ns].sflags |= SSKIP;
125 greg 2.61 foundsource += (ambounce > 0);
126     }
127 greg 1.6 } else if (m->otype == MAT_SPOT) {
128 greg 1.36 source[ns].sflags |= SSPOT;
129     if ((source[ns].sl.s = makespot(m)) == NULL)
130 greg 1.33 goto memerr;
131 greg 1.38 if (source[ns].sflags & SFLAT &&
132     !checkspot(source[ns].sl.s,source[ns].snorm)) {
133     objerror(o, WARNING,
134     "invalid spotlight direction");
135     source[ns].sflags |= SSKIP;
136     }
137 greg 1.6 }
138 greg 2.61 foundsource += !(source[ns].sflags & SSKIP);
139 greg 1.1 }
140 greg 2.3 if (!foundsource) {
141 greg 1.25 error(WARNING, "no light sources found");
142     return;
143     }
144 greg 2.65 #if SHADCACHE
145     initobscache(ns);
146     #endif
147 greg 2.64 /* PMAP: disable virtual sources */
148     if (!photonMapping)
149     markvirtuals(); /* find and add virtual sources */
150    
151 greg 1.45 /* allocate our contribution arrays */
152 greg 1.48 maxcntr = nsources + MAXSPART; /* start with this many */
153 greg 1.45 srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
154     cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR));
155 schorsch 2.35 if ((srccnt == NULL) | (cntord == NULL))
156 greg 1.33 goto memerr;
157     return;
158 greg 1.25 memerr:
159     error(SYSTEM, "out of memory in marksources");
160 greg 1.1 }
161    
162    
163 greg 2.62 void
164 schorsch 2.44 freesources(void) /* free all source structures */
165 greg 2.29 {
166     if (nsources > 0) {
167 greg 2.38 #if SHADCACHE
168     while (nsources--)
169     freeobscache(&source[nsources]);
170     #endif
171 greg 2.29 free((void *)source);
172     source = NULL;
173     nsources = 0;
174     }
175 greg 2.60 markclip(NULL);
176 greg 2.29 if (maxcntr <= 0)
177     return;
178     free((void *)srccnt);
179     srccnt = NULL;
180     free((void *)cntord);
181     cntord = NULL;
182     maxcntr = 0;
183     }
184    
185    
186 greg 2.62 int
187 greg 2.38 srcray( /* send a ray to a source, return domega */
188 greg 2.62 RAY *sr, /* returned source ray */
189 schorsch 2.44 RAY *r, /* ray which hit object */
190     SRCINDEX *si /* source sample index */
191 greg 2.38 )
192 greg 1.1 {
193 greg 2.59 double d; /* distance to source */
194 greg 2.62 SRCREC *srcp;
195 greg 1.1
196 greg 2.59 rayorigin(sr, SHADOW, r, NULL); /* ignore limits */
197 greg 1.1
198 greg 2.59 if (r == NULL)
199     sr->rmax = 0.0;
200    
201     while ((d = nextssamp(sr, si)) != 0.0) {
202     sr->rsrc = si->sn; /* remember source */
203     srcp = source + si->sn;
204     if (srcp->sflags & SDISTANT) {
205     if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
206     continue;
207     return(1); /* sample OK */
208     }
209 greg 1.45 /* local source */
210 greg 1.6 /* check proximity */
211 greg 2.59 if (srcp->sflags & SPROX && d > srcp->sl.prox)
212     continue;
213 greg 1.6 /* check angle */
214 greg 2.59 if (srcp->sflags & SSPOT) {
215     if (spotout(sr, srcp->sl.s))
216     continue;
217 greg 1.45 /* adjust solid angle */
218 greg 2.59 si->dom *= d*d;
219     d += srcp->sl.s->flen;
220     si->dom /= d*d;
221     }
222     return(1); /* sample OK */
223     }
224     return(0); /* no more samples */
225 greg 1.1 }
226    
227    
228 greg 2.62 void
229 greg 2.38 srcvalue( /* punch ray to source and compute value */
230 greg 2.62 RAY *r
231 greg 2.38 )
232 greg 1.1 {
233 greg 2.62 SRCREC *sp;
234 greg 1.1
235 greg 1.35 sp = &source[r->rsrc];
236     if (sp->sflags & SVIRTUAL) { /* virtual source */
237     /* check intersection */
238     if (!(*ofun[sp->so->otype].funp)(sp->so, r))
239     return;
240 greg 2.15 if (!rayshade(r, r->ro->omod)) /* compute contribution */
241     goto nomat;
242 greg 2.19 rayparticipate(r);
243 greg 1.35 return;
244 greg 1.1 }
245 greg 1.35 /* compute intersection */
246     if (sp->sflags & SDISTANT ? sourcehit(r) :
247     (*ofun[sp->so->otype].funp)(sp->so, r)) {
248     if (sp->sa.success >= 0)
249     sp->sa.success++;
250 greg 2.15 if (!rayshade(r, r->ro->omod)) /* compute contribution */
251     goto nomat;
252 greg 2.19 rayparticipate(r);
253 greg 1.35 return;
254 greg 1.1 }
255 greg 2.15 /* we missed our mark! */
256 greg 1.35 if (sp->sa.success < 0)
257     return; /* bitched already */
258     sp->sa.success -= AIMREQT;
259     if (sp->sa.success >= 0)
260     return; /* leniency */
261     sprintf(errmsg, "aiming failure for light source \"%s\"",
262     sp->so->oname);
263     error(WARNING, errmsg); /* issue warning */
264 greg 2.15 return;
265     nomat:
266     objerror(r->ro, USER, "material not found");
267 greg 1.1 }
268    
269    
270 greg 2.47 static int
271     transillum( /* check if material is transparent illum */
272     OBJECT obj
273     )
274     {
275     OBJREC *m = findmaterial(objptr(obj));
276    
277     if (m == NULL)
278     return(1);
279     if (m->otype != MAT_ILLUM)
280     return(0);
281     return(!m->oargs.nsargs || !strcmp(m->oargs.sarg[0], VOIDID));
282     }
283    
284    
285 greg 2.62 int
286 greg 2.38 sourcehit( /* check to see if ray hit distant source */
287 greg 2.62 RAY *r
288 greg 2.38 )
289 greg 2.5 {
290 greg 2.47 int glowsrc = -1;
291     int transrc = -1;
292 greg 2.5 int first, last;
293 greg 2.62 int i;
294 greg 2.5
295     if (r->rsrc >= 0) { /* check only one if aimed */
296     first = last = r->rsrc;
297     } else { /* otherwise check all */
298     first = 0; last = nsources-1;
299     }
300 greg 2.47 for (i = first; i <= last; i++) {
301     if ((source[i].sflags & (SDISTANT|SVIRTUAL)) != SDISTANT)
302     continue;
303     /*
304     * Check to see if ray is within
305     * solid angle of source.
306     */
307     if (2.*PI*(1. - DOT(source[i].sloc,r->rdir)) > source[i].ss2)
308     continue;
309     /* is it the only possibility? */
310     if (first == last) {
311     r->ro = source[i].so;
312     break;
313     }
314     /*
315     * If it's a glow or transparent illum, just remember it.
316     */
317     if (source[i].sflags & SSKIP) {
318 greg 2.58 if (glowsrc < 0)
319     glowsrc = i;
320 greg 2.47 continue;
321     }
322     if (transillum(source[i].so->omod)) {
323 greg 2.58 if (transrc < 0)
324     transrc = i;
325 greg 2.47 continue;
326     }
327     r->ro = source[i].so; /* otherwise, use first hit */
328     break;
329 greg 2.5 }
330 greg 2.47 /*
331     * Do we need fallback?
332     */
333     if (r->ro == NULL) {
334     if (transrc >= 0 && r->crtype & (AMBIENT|SPECULAR))
335     return(0); /* avoid overcounting */
336     if (glowsrc >= 0)
337     r->ro = source[glowsrc].so;
338     else
339     return(0); /* nothing usable */
340     }
341     /*
342     * Make assignments.
343     */
344     r->robj = objndx(r->ro);
345     for (i = 0; i < 3; i++)
346     r->ron[i] = -r->rdir[i];
347     r->rod = 1.0;
348     r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
349     r->uv[0] = r->uv[1] = 0.0;
350     r->rox = NULL;
351     return(1);
352 greg 2.5 }
353 greg 2.38
354    
355 greg 1.4 static int
356 greg 2.38 cntcmp( /* contribution compare (descending) */
357 schorsch 2.44 const void *p1,
358     const void *p2
359 greg 2.38 )
360 greg 1.4 {
361 greg 2.62 const CNTPTR *sc1 = (const CNTPTR *)p1;
362     const CNTPTR *sc2 = (const CNTPTR *)p2;
363 greg 2.38
364 greg 1.4 if (sc1->brt > sc2->brt)
365     return(-1);
366     if (sc1->brt < sc2->brt)
367     return(1);
368     return(0);
369     }
370    
371    
372 greg 2.62 void
373 greg 2.38 direct( /* add direct component */
374 schorsch 2.44 RAY *r, /* ray that hit surface */
375     srcdirf_t *f, /* direct component coefficient function */
376     void *p /* data for f */
377 greg 2.38 )
378 greg 1.4 {
379 greg 2.62 int sn;
380     CONTRIB *scp;
381 greg 1.45 SRCINDEX si;
382 greg 1.12 int nshadcheck, ncnts;
383 greg 1.29 int nhits;
384 greg 1.45 double prob, ourthresh, hwt;
385 greg 1.4 RAY sr;
386 greg 2.64
387     /* PMAP: Factor in direct photons (primarily for debugging/validation) */
388     if (directPhotonMapping) {
389     (*f)(r -> rcol, p, r -> ron, PI);
390     multDirectPmap(r);
391     return;
392     }
393    
394 greg 1.25 /* NOTE: srccnt and cntord global so no recursion */
395 greg 1.22 if (nsources <= 0)
396 greg 1.25 return; /* no sources?! */
397 greg 1.4 /* potential contributions */
398 greg 1.45 initsrcindex(&si);
399     for (sn = 0; srcray(&sr, r, &si); sn++) {
400     if (sn >= maxcntr) {
401     maxcntr = sn + MAXSPART;
402 greg 2.32 srccnt = (CONTRIB *)realloc((void *)srccnt,
403 greg 1.45 maxcntr*sizeof(CONTRIB));
404 greg 2.32 cntord = (CNTPTR *)realloc((void *)cntord,
405 greg 1.45 maxcntr*sizeof(CNTPTR));
406 schorsch 2.35 if ((srccnt == NULL) | (cntord == NULL))
407 greg 1.45 error(SYSTEM, "out of memory in direct");
408     }
409     cntord[sn].sndx = sn;
410 greg 2.7 scp = srccnt + sn;
411     scp->sno = sr.rsrc;
412 greg 1.4 /* compute coefficient */
413 greg 2.7 (*f)(scp->coef, p, sr.rdir, si.dom);
414 greg 2.53 cntord[sn].brt = intens(scp->coef);
415 greg 1.15 if (cntord[sn].brt <= 0.0)
416 greg 1.4 continue;
417 greg 2.38 #if SHADCACHE
418     /* check shadow cache */
419     if (si.np == 1 && srcblocked(&sr)) {
420     cntord[sn].brt = 0.0;
421     continue;
422     }
423     #endif
424 greg 2.7 VCOPY(scp->dir, sr.rdir);
425 greg 2.50 copycolor(sr.rcoef, scp->coef);
426 greg 1.35 /* compute potential */
427 greg 2.51 sr.revf = srcvalue;
428     rayvalue(&sr);
429 greg 2.50 multcolor(sr.rcol, sr.rcoef);
430 greg 2.7 copycolor(scp->val, sr.rcol);
431 greg 2.57 cntord[sn].brt = bright(sr.rcol);
432 greg 1.4 }
433     /* sort contributions */
434 greg 1.45 qsort(cntord, sn, sizeof(CNTPTR), cntcmp);
435 greg 1.13 { /* find last */
436 greg 2.62 int l, m;
437 greg 1.13
438 greg 1.45 ncnts = l = sn;
439     sn = 0;
440 greg 1.13 while ((m = (sn + ncnts) >> 1) != l) {
441     if (cntord[m].brt > 0.0)
442     sn = m;
443     else
444     ncnts = m;
445     l = m;
446     }
447     }
448 greg 2.2 if (ncnts == 0)
449     return; /* no contributions! */
450 greg 1.12 /* accumulate tail */
451     for (sn = ncnts-1; sn > 0; sn--)
452     cntord[sn-1].brt += cntord[sn].brt;
453 greg 1.45 /* compute number to check */
454     nshadcheck = pow((double)ncnts, shadcert) + .5;
455     /* modify threshold */
456 greg 2.63 if (ncnts > MINSHADCNT)
457     ourthresh = shadthresh / r->rweight;
458     else
459     ourthresh = 0;
460 greg 1.10 /* test for shadows */
461 greg 2.13 for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts;
462     hwt += (double)source[scp->sno].nhits /
463     (double)source[scp->sno].ntests,
464     sn++) {
465 greg 1.10 /* check threshold */
466 greg 1.12 if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
467 greg 1.27 cntord[sn].brt-cntord[sn+nshadcheck].brt)
468     < ourthresh*bright(r->rcol))
469 greg 1.4 break;
470 greg 2.7 scp = srccnt + cntord[sn].sndx;
471 greg 1.4 /* test for hit */
472 greg 2.52 rayorigin(&sr, SHADOW, r, NULL);
473     copycolor(sr.rcoef, scp->coef);
474 greg 2.7 VCOPY(sr.rdir, scp->dir);
475     sr.rsrc = scp->sno;
476 greg 2.34 /* keep statistics */
477     if (source[scp->sno].ntests++ > 0xfffffff0) {
478     source[scp->sno].ntests >>= 1;
479     source[scp->sno].nhits >>= 1;
480     }
481 greg 1.4 if (localhit(&sr, &thescene) &&
482 greg 2.7 ( sr.ro != source[scp->sno].so ||
483     source[scp->sno].sflags & SFOLLOW )) {
484 greg 1.33 /* follow entire path */
485 greg 2.23 raycont(&sr);
486 greg 1.42 if (trace != NULL)
487     (*trace)(&sr); /* trace execution */
488 greg 2.38 if (bright(sr.rcol) <= FTINY) {
489     #if SHADCACHE
490     if ((scp <= srccnt || scp[-1].sno != scp->sno)
491 greg 2.41 && (scp >= srccnt+ncnts-1 ||
492 greg 2.38 scp[1].sno != scp->sno))
493     srcblocker(&sr);
494     #endif
495 greg 1.4 continue; /* missed! */
496 greg 2.38 }
497 greg 2.55 rayparticipate(&sr);
498 greg 2.51 multcolor(sr.rcol, sr.rcoef);
499 greg 2.7 copycolor(scp->val, sr.rcol);
500 greg 2.51 } else if (trace != NULL &&
501     (source[scp->sno].sflags & (SDISTANT|SVIRTUAL|SFOLLOW))
502     == (SDISTANT|SFOLLOW) &&
503     sourcehit(&sr) && rayshade(&sr, sr.ro->omod)) {
504     (*trace)(&sr); /* trace execution */
505     /* skip call to rayparticipate() & scp->val update */
506 greg 1.4 }
507     /* add contribution if hit */
508 greg 2.7 addcolor(r->rcol, scp->val);
509 greg 1.29 nhits++;
510 greg 2.7 source[scp->sno].nhits++;
511 greg 1.4 }
512 greg 2.13 /* source hit rate */
513     if (hwt > FTINY)
514     hwt = (double)nhits / hwt;
515 greg 1.29 else
516     hwt = 0.5;
517 greg 1.20 #ifdef DEBUG
518 greg 2.13 sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n",
519 greg 1.12 sn, ncnts-sn, hwt);
520     eputs(errmsg);
521 greg 1.4 #endif
522     /* add in untested sources */
523 greg 1.12 for ( ; sn < ncnts; sn++) {
524 greg 2.7 scp = srccnt + cntord[sn].sndx;
525     prob = hwt * (double)source[scp->sno].nhits /
526     (double)source[scp->sno].ntests;
527 greg 2.51 if (prob < 1.0)
528     scalecolor(scp->val, prob);
529 greg 2.7 addcolor(r->rcol, scp->val);
530 greg 2.19 }
531     }
532    
533    
534 greg 2.62 void
535 greg 2.38 srcscatter( /* compute source scattering into ray */
536 greg 2.62 RAY *r
537 greg 2.38 )
538 greg 2.19 {
539 greg 2.20 int oldsampndx;
540 greg 2.19 int nsamps;
541     RAY sr;
542     SRCINDEX si;
543 greg 2.25 double t, d;
544     double re, ge, be;
545 greg 2.64 COLOR cvext, pmapInscatter;
546 greg 2.19 int i, j;
547    
548 greg 2.64 /* PMAP: do unconditional inscattering for volume photons ? */
549     /* if (!volumePhotonMapping) */
550 greg 2.22 if (r->slights == NULL || r->slights[0] == 0
551     || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
552 greg 2.19 return;
553 greg 2.64
554 greg 2.19 if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
555     nsamps = 1;
556 greg 2.22 #if MAXSSAMP
557     else if (nsamps > MAXSSAMP)
558     nsamps = MAXSSAMP;
559     #endif
560 greg 2.20 oldsampndx = samplendx;
561     samplendx = random()&0x7fff; /* randomize */
562 greg 2.19 for (i = r->slights[0]; i > 0; i--) { /* for each source */
563 greg 2.25 for (j = 0; j < nsamps; j++) { /* for each sample position */
564 greg 2.19 samplendx++;
565     t = r->rot * (j+frandom())/nsamps;
566 greg 2.25 /* extinction */
567     re = t*colval(r->cext,RED);
568     ge = t*colval(r->cext,GRN);
569     be = t*colval(r->cext,BLU);
570     setcolor(cvext, re > 92. ? 0. : exp(-re),
571     ge > 92. ? 0. : exp(-ge),
572     be > 92. ? 0. : exp(-be));
573     if (intens(cvext) <= FTINY)
574     break; /* too far away */
575 greg 2.19 sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
576     sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
577     sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
578 greg 2.21 initsrcindex(&si); /* sample ray to this source */
579     si.sn = r->slights[i];
580     nopart(&si, &sr);
581     if (!srcray(&sr, NULL, &si) ||
582     sr.rsrc != r->slights[i])
583     continue; /* no path */
584 greg 2.45 #if SHADCACHE
585     if (srcblocked(&sr)) /* check shadow cache */
586     continue;
587     #endif
588 greg 2.19 copycolor(sr.cext, r->cext);
589 greg 2.24 copycolor(sr.albedo, r->albedo);
590 greg 2.19 sr.gecc = r->gecc;
591 gregl 2.26 sr.slights = r->slights;
592 greg 2.19 rayvalue(&sr); /* eval. source ray */
593 greg 2.45 if (bright(sr.rcol) <= FTINY) {
594     #if SHADCACHE
595     srcblocker(&sr); /* add blocker to cache */
596     #endif
597 greg 2.19 continue;
598 greg 2.45 }
599 greg 2.19 if (r->gecc <= FTINY) /* compute P(theta) */
600     d = 1.;
601     else {
602     d = DOT(r->rdir, sr.rdir);
603 greg 2.25 d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
604     d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
605 greg 2.19 }
606     /* other factors */
607 greg 2.24 d *= si.dom * r->rot / (4.*PI*nsamps);
608 greg 2.64 scalecolor(sr.rcol, d);
609    
610     /* PMAP: Add ambient inscattering from volume photons once only */
611     if (volumePhotonMapping && i == 1) {
612     inscatterVolumePmap(&sr, pmapInscatter);
613     scalecolor(pmapInscatter, r -> rot / nsamps);
614     addcolor(sr.rcol, pmapInscatter);
615     }
616    
617 greg 2.19 multcolor(sr.rcol, r->cext);
618 greg 2.24 multcolor(sr.rcol, r->albedo);
619 greg 2.25 multcolor(sr.rcol, cvext);
620     addcolor(r->rcol, sr.rcol); /* add it in */
621 greg 2.19 }
622 greg 1.1 }
623 greg 2.20 samplendx = oldsampndx;
624 greg 2.4 }
625    
626    
627     /****************************************************************
628     * The following macros were separated from the m_light() routine
629     * because they are very nasty and difficult to understand.
630     */
631    
632 greg 2.11 /* illumblock *
633 greg 2.4 *
634     * We cannot allow an illum to pass to another illum, because that
635     * would almost certainly constitute overcounting.
636     * However, we do allow an illum to pass to another illum
637     * that is actually going to relay to a virtual light source.
638 greg 2.11 * We also prevent an illum from passing to a glow; this provides a
639     * convenient mechanism for defining detailed light source
640     * geometry behind (or inside) an effective radiator.
641 greg 2.4 */
642    
643 greg 2.37 static int
644 greg 2.42 weaksrcmat(OBJECT obj) /* identify material */
645 greg 2.37 {
646 greg 2.47 OBJREC *m = findmaterial(objptr(obj));
647 greg 2.37
648 greg 2.47 if (m == NULL) return(0);
649     return((m->otype==MAT_ILLUM) | (m->otype==MAT_GLOW));
650 greg 2.37 }
651 greg 2.4
652 greg 2.11 #define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \
653 greg 2.12 r->rod > 0.0 && \
654 greg 2.37 weaksrcmat(source[r->rsrc].so->omod))
655 greg 2.11
656 greg 2.4 /* wrongsource *
657     *
658     * This source is the wrong source (ie. overcounted) if we are
659     * aimed to a different source than the one we hit and the one
660 greg 2.11 * we hit is not an illum that should be passed.
661 greg 2.4 */
662    
663     #define wrongsource(m, r) (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \
664 greg 2.11 (m->otype!=MAT_ILLUM || illumblock(m,r)))
665 greg 2.4
666     /* distglow *
667     *
668     * A distant glow is an object that sometimes acts as a light source,
669     * but is too far away from the test point to be one in this case.
670 greg 2.11 * (Glows with negative radii should NEVER participate in illumination.)
671 greg 2.4 */
672    
673 greg 2.17 #define distglow(m, r, d) (m->otype==MAT_GLOW && \
674 greg 2.10 m->oargs.farg[3] >= -FTINY && \
675 greg 2.17 d > m->oargs.farg[3])
676 greg 2.4
677     /* badcomponent *
678     *
679     * We must avoid counting light sources in the ambient calculation,
680     * since the direct component is handled separately. Therefore, any
681     * ambient ray which hits an active light source must be discarded.
682     * The same is true for stray specular samples, since the specular
683     * contribution from light sources is calculated separately.
684     */
685    
686     #define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \
687     !(r->crtype&SHADOW || r->rod < 0.0 || \
688 greg 2.17 /* not 100% correct */ distglow(m, r, r->rot)))
689 greg 2.4
690     /* passillum *
691     *
692     * An illum passes to another material type when we didn't hit it
693     * on purpose (as part of a direct calculation), or it is relaying
694     * a virtual light source.
695     */
696    
697     #define passillum(m, r) (m->otype==MAT_ILLUM && \
698     (r->rsrc<0 || source[r->rsrc].so!=r->ro || \
699     source[r->rsrc].sflags&SVIRTUAL))
700    
701     /* srcignore *
702     *
703 greg 2.10 * The -dv flag is normally on for sources to be visible.
704 greg 2.4 */
705    
706 greg 2.17 #define srcignore(m, r) !(directvis || r->crtype&SHADOW || \
707     distglow(m, r, raydist(r,PRIMARY)))
708 greg 2.4
709    
710 greg 2.62 int
711 greg 2.38 m_light( /* ray hit a light source */
712 greg 2.62 OBJREC *m,
713     RAY *r
714 greg 2.38 )
715 greg 2.4 {
716     /* check for over-counting */
717 greg 2.50 if (badcomponent(m, r)) {
718     setcolor(r->rcoef, 0.0, 0.0, 0.0);
719 greg 2.14 return(1);
720 greg 2.50 }
721     if (wrongsource(m, r)) {
722     setcolor(r->rcoef, 0.0, 0.0, 0.0);
723 greg 2.14 return(1);
724 greg 2.50 }
725 greg 2.4 /* check for passed illum */
726     if (passillum(m, r)) {
727 greg 2.14 if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
728 gwlarson 2.28 return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0])));
729 greg 2.14 raytrans(r);
730     return(1);
731 greg 2.4 }
732 greg 2.50 /* check for invisibility */
733     if (srcignore(m, r)) {
734     setcolor(r->rcoef, 0.0, 0.0, 0.0);
735     return(1);
736     }
737 greg 2.4 /* otherwise treat as source */
738     /* check for behind */
739     if (r->rod < 0.0)
740 greg 2.14 return(1);
741 greg 2.4 /* check for outside spot */
742 greg 2.18 if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
743 greg 2.14 return(1);
744 greg 2.4 /* get distribution pattern */
745     raytexture(r, m->omod);
746     /* get source color */
747     setcolor(r->rcol, m->oargs.farg[0],
748     m->oargs.farg[1],
749     m->oargs.farg[2]);
750     /* modify value */
751     multcolor(r->rcol, r->pcol);
752 greg 2.14 return(1);
753 greg 1.1 }