ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 2.66
Committed: Thu May 28 09:13:19 2015 UTC (8 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R0
Changes since 2.65: +3 -2 lines
Log Message:
Fixed bug in source obstructor cache for antimatter

File Contents

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