ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
(Generate patch)

Comparing ray/src/rt/source.c (file contents):
Revision 2.50 by greg, Tue Apr 19 01:15:07 2005 UTC vs.
Revision 2.71 by greg, Thu Mar 12 17:19:18 2020 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9  
10   #include  "ray.h"
11   #include  "otypes.h"
12 + #include  "otspecial.h"
13   #include  "rtotypes.h"
14   #include  "source.h"
15   #include  "random.h"
16 + #include  "pmapsrc.h"
17 + #include  "pmapmat.h"
18  
16 extern double  ssampdist;               /* scatter sampling distance */
17
19   #ifndef MAXSSAMP
20   #define MAXSSAMP        16              /* maximum samples per ray */
21   #endif
# Line 42 | Line 43 | static int  maxcntr = 0;               /* size of contribution arra
43   static int cntcmp(const void *p1, const void *p2);
44  
45  
46 < extern OBJREC *                 /* find an object's actual material */
46 < findmaterial(register OBJREC *o)
47 < {
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 <                }
59 <                if (o->omod == OVOID)
60 <                        return(NULL);
61 <                o = objptr(o->omod);
62 <        }
63 <        return(o);              /* mixtures will return NULL */
64 < }
65 <
66 <
67 < extern void
46 > void
47   marksources(void)                       /* find and mark source objects */
48   {
49          int  foundsource = 0;
50          int  i;
51 <        register OBJREC  *o, *m;
52 <        register int  ns;
51 >        OBJREC  *o, *m;
52 >        int  ns;
53                                          /* initialize dispatch table */
54          initstypes();
55                                          /* find direct sources */
# Line 82 | Line 61 | marksources(void)                      /* find and mark source objects */
61                          continue;
62                                          /* find material */
63                  m = findmaterial(objptr(o->omod));
64 <                if (m == NULL || !islight(m->otype))
64 >                if (m == NULL)
65 >                        continue;
66 >                if (m->otype == MAT_CLIP) {
67 >                        markclip(m);    /* special case for antimatter */
68 >                        continue;
69 >                }
70 >                if (!islight(m->otype))
71                          continue;       /* not source modifier */
72          
73                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
74                                  m->otype == MAT_SPOT ? 7 : 3))
75                          objerror(m, USER, "bad # arguments");
76  
92                if (m->otype == MAT_GLOW &&
93                                o->otype != OBJ_SOURCE &&
94                                m->oargs.farg[3] <= FTINY)
95                        continue;                       /* don't bother */
77                  if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY &&
78                                  m->oargs.farg[2] <= FTINY)
79                          continue;                       /* don't bother */
80 <
80 >                if (m->otype == MAT_GLOW &&
81 >                                o->otype != OBJ_SOURCE &&
82 >                                m->oargs.farg[3] <= FTINY) {
83 >                        foundsource += (ambounce > 0);
84 >                        continue;                       /* don't track these */
85 >                }
86                  if (sfun[o->otype].of == NULL ||
87                                  sfun[o->otype].of->setsrc == NULL)
88                          objerror(o, USER, "illegal material");
# Line 109 | Line 95 | marksources(void)                      /* find and mark source objects */
95                  if (m->otype == MAT_GLOW) {
96                          source[ns].sflags |= SPROX;
97                          source[ns].sl.prox = m->oargs.farg[3];
98 <                        if (source[ns].sflags & SDISTANT)
98 >                        if (source[ns].sflags & SDISTANT) {
99                                  source[ns].sflags |= SSKIP;
100 +                                foundsource += (ambounce > 0);
101 +                        }
102                  } else if (m->otype == MAT_SPOT) {
103                          source[ns].sflags |= SSPOT;
104                          if ((source[ns].sl.s = makespot(m)) == NULL)
# Line 122 | Line 110 | marksources(void)                      /* find and mark source objects */
110                                  source[ns].sflags |= SSKIP;
111                          }
112                  }
113 < #if  SHADCACHE
126 <                initobscache(ns);
127 < #endif
128 <                if (!(source[ns].sflags & SSKIP))
129 <                        foundsource++;
113 >                foundsource += !(source[ns].sflags & SSKIP);
114          }
115          if (!foundsource) {
116                  error(WARNING, "no light sources found");
117                  return;
118          }
119 <        markvirtuals();                 /* find and add virtual sources */
119 > #if  SHADCACHE
120 >        for (ns = 0; ns < nsources; ns++)       /* initialize obstructor cache */
121 >                initobscache(ns);
122 > #endif
123 >        /* PMAP: disable virtual sources */
124 >        if (!photonMapping)
125 >                markvirtuals();                 /* find and add virtual sources */
126 >                
127                                  /* allocate our contribution arrays */
128          maxcntr = nsources + MAXSPART;  /* start with this many */
129          srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
# Line 145 | Line 136 | memerr:
136   }
137  
138  
139 < extern void
139 > void
140   freesources(void)                       /* free all source structures */
141   {
142          if (nsources > 0) {
# Line 157 | Line 148 | freesources(void)                      /* free all source structures */
148                  source = NULL;
149                  nsources = 0;
150          }
151 +        markclip(NULL);
152          if (maxcntr <= 0)
153                  return;
154          free((void *)srccnt);
# Line 167 | Line 159 | freesources(void)                      /* free all source structures */
159   }
160  
161  
162 < extern int
162 > int
163   srcray(                         /* send a ray to a source, return domega */
164 <        register RAY  *sr,              /* returned source ray */
164 >        RAY  *sr,               /* returned source ray */
165          RAY  *r,                        /* ray which hit object */
166          SRCINDEX  *si                   /* source sample index */
167   )
168   {
169 <    double  d;                          /* distance to source */
170 <    register SRCREC  *srcp;
169 >        double  d;                              /* distance to source */
170 >        SRCREC  *srcp;
171  
172 <    rayorigin(sr, SHADOW, r, NULL);             /* ignore limits */
172 >        rayorigin(sr, SHADOW, r, NULL);         /* ignore limits */
173  
174 <    while ((d = nextssamp(sr, si)) != 0.0) {
175 <        sr->rsrc = si->sn;                      /* remember source */
176 <        srcp = source + si->sn;
177 <        if (srcp->sflags & SDISTANT) {
178 <                if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
179 <                        continue;
180 <                return(1);              /* sample OK */
181 <        }
174 >        if (r == NULL)
175 >                sr->rmax = 0.0;
176 >
177 >        while ((d = nextssamp(sr, si)) != 0.0) {
178 >                sr->rsrc = si->sn;                      /* remember source */
179 >                srcp = source + si->sn;
180 >                if (srcp->sflags & SDISTANT) {
181 >                        if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
182 >                                continue;
183 >                        return(1);              /* sample OK */
184 >                }
185                                  /* local source */
186                                                  /* check proximity */
187 <        if (srcp->sflags & SPROX && d > srcp->sl.prox)
193 <                continue;
194 <                                                /* check angle */
195 <        if (srcp->sflags & SSPOT) {
196 <                if (spotout(sr, srcp->sl.s))
187 >                if (srcp->sflags & SPROX && d > srcp->sl.prox)
188                          continue;
189 +                                                /* check angle */
190 +                if (srcp->sflags & SSPOT) {
191 +                        if (spotout(sr, srcp->sl.s))
192 +                                continue;
193                                          /* adjust solid angle */
194 <                si->dom *= d*d;
195 <                d += srcp->sl.s->flen;
196 <                si->dom /= d*d;
194 >                        si->dom *= d*d;
195 >                        d += srcp->sl.s->flen;
196 >                        si->dom /= d*d;
197 >                }
198 >                return(1);                      /* sample OK */
199          }
200 <        return(1);                      /* sample OK */
204 <    }
205 <    return(0);                  /* no more samples */
200 >        return(0);                      /* no more samples */
201   }
202  
203  
204 < extern void
204 > void
205   srcvalue(                       /* punch ray to source and compute value */
206 <        register RAY  *r
206 >        RAY  *r
207   )
208   {
209 <        register SRCREC  *sp;
209 >        SRCREC  *sp;
210  
211          sp = &source[r->rsrc];
212          if (sp->sflags & SVIRTUAL) {    /* virtual source */
# Line 263 | Line 258 | transillum(                    /* check if material is transparent illu
258   }
259  
260  
261 < extern int
261 > int
262   sourcehit(                      /* check to see if ray hit distant source */
263 <        register RAY  *r
263 >        RAY  *r
264   )
265   {
266          int  glowsrc = -1;
267          int  transrc = -1;
268          int  first, last;
269 <        register int  i;
269 >        int  i;
270  
271          if (r->rsrc >= 0) {             /* check only one if aimed */
272                  first = last = r->rsrc;
# Line 296 | Line 291 | sourcehit(                     /* check to see if ray hit distant source
291                   * If it's a glow or transparent illum, just remember it.
292                   */
293                  if (source[i].sflags & SSKIP) {
294 <                        glowsrc = i;
294 >                        if (glowsrc < 0)
295 >                                glowsrc = i;
296                          continue;
297                  }
298                  if (transillum(source[i].so->omod)) {
299 <                        transrc = i;
299 >                        if (transrc < 0)
300 >                                transrc = i;
301                          continue;
302                  }
303                  r->ro = source[i].so;   /* otherwise, use first hit */
# Line 337 | Line 334 | cntcmp(                                /* contribution compare (descending) */
334          const void *p2
335   )
336   {
337 <        register const CNTPTR  *sc1 = (const CNTPTR *)p1;
338 <        register const CNTPTR  *sc2 = (const CNTPTR *)p2;
337 >        const CNTPTR  *sc1 = (const CNTPTR *)p1;
338 >        const CNTPTR  *sc2 = (const CNTPTR *)p2;
339  
340          if (sc1->brt > sc2->brt)
341                  return(-1);
# Line 348 | Line 345 | cntcmp(                                /* contribution compare (descending) */
345   }
346  
347  
348 < extern void
348 > void
349   direct(                                 /* add direct component */
350          RAY  *r,                        /* ray that hit surface */
351          srcdirf_t *f,                   /* direct component coefficient function */
352          void  *p                        /* data for f */
353   )
354   {
355 <        register int  sn;
356 <        register CONTRIB  *scp;
355 >        int  sn;
356 >        CONTRIB  *scp;
357          SRCINDEX  si;
358          int  nshadcheck, ncnts;
359          int  nhits;
360          double  prob, ourthresh, hwt;
361          RAY  sr;
362 +        
363 +        /* PMAP: Factor in direct photons (primarily for debugging/validation) */
364 +        if (directPhotonMapping) {
365 +                (*f)(r -> rcol, p, r -> ron, PI);              
366 +                multDirectPmap(r);
367 +                return;
368 +        }
369 +        
370                          /* NOTE: srccnt and cntord global so no recursion */
371          if (nsources <= 0)
372                  return;         /* no sources?! */
# Line 382 | Line 387 | direct(                                        /* add direct component */
387                  scp->sno = sr.rsrc;
388                                                  /* compute coefficient */
389                  (*f)(scp->coef, p, sr.rdir, si.dom);
390 <                cntord[sn].brt = bright(scp->coef);
390 >                cntord[sn].brt = intens(scp->coef);
391                  if (cntord[sn].brt <= 0.0)
392                          continue;
393   #if SHADCACHE
# Line 395 | Line 400 | direct(                                        /* add direct component */
400                  VCOPY(scp->dir, sr.rdir);
401                  copycolor(sr.rcoef, scp->coef);
402                                                  /* compute potential */
403 <                srcvalue(&sr);
403 >                sr.revf = srcvalue;
404 >                rayvalue(&sr);
405                  multcolor(sr.rcol, sr.rcoef);
406                  copycolor(scp->val, sr.rcol);
407 <                cntord[sn].brt = bright(scp->val);
407 >                cntord[sn].brt = bright(sr.rcol);
408          }
409                                                  /* sort contributions */
410          qsort(cntord, sn, sizeof(CNTPTR), cntcmp);
411          {                                       /* find last */
412 <                register int  l, m;
412 >                int  l, m;
413  
414                  ncnts = l = sn;
415                  sn = 0;
# Line 423 | Line 429 | direct(                                        /* add direct component */
429                                                  /* compute number to check */
430          nshadcheck = pow((double)ncnts, shadcert) + .5;
431                                                  /* modify threshold */
432 <        ourthresh = shadthresh / r->rweight;
432 >        if (ncnts > MINSHADCNT)
433 >                ourthresh = shadthresh / r->rweight;
434 >        else
435 >                ourthresh = 0;
436                                                  /* test for shadows */
437          for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts;
438                          hwt += (double)source[scp->sno].nhits /
# Line 437 | Line 446 | direct(                                        /* add direct component */
446                  scp = srccnt + cntord[sn].sndx;
447                                                  /* test for hit */
448                  rayorigin(&sr, SHADOW, r, NULL);
449 +                copycolor(sr.rcoef, scp->coef);
450                  VCOPY(sr.rdir, scp->dir);
451                  sr.rsrc = scp->sno;
452                                                  /* keep statistics */
# Line 451 | Line 461 | direct(                                        /* add direct component */
461                          raycont(&sr);
462                          if (trace != NULL)
463                                  (*trace)(&sr);  /* trace execution */
454                        rayparticipate(&sr);
464                          if (bright(sr.rcol) <= FTINY) {
465   #if SHADCACHE
466                                  if ((scp <= srccnt || scp[-1].sno != scp->sno)
# Line 461 | Line 470 | direct(                                        /* add direct component */
470   #endif
471                                  continue;       /* missed! */
472                          }
473 +                        rayparticipate(&sr);
474 +                        multcolor(sr.rcol, sr.rcoef);
475                          copycolor(scp->val, sr.rcol);
476 <                        multcolor(scp->val, scp->coef);
476 >                } else if (trace != NULL &&
477 >                        (source[scp->sno].sflags & (SDISTANT|SVIRTUAL|SFOLLOW))
478 >                                                == (SDISTANT|SFOLLOW) &&
479 >                                sourcehit(&sr) && rayshade(&sr, sr.ro->omod)) {
480 >                        (*trace)(&sr);          /* trace execution */
481 >                        /* skip call to rayparticipate() & scp->val update */
482                  }
483                                                  /* add contribution if hit */
484                  addcolor(r->rcol, scp->val);
# Line 484 | Line 500 | direct(                                        /* add direct component */
500                  scp = srccnt + cntord[sn].sndx;
501                  prob = hwt * (double)source[scp->sno].nhits /
502                                  (double)source[scp->sno].ntests;
503 <                if (prob > 1.0)
504 <                        prob = 1.0;
489 <                scalecolor(scp->val, prob);
503 >                if (prob < 1.0)
504 >                        scalecolor(scp->val, prob);
505                  addcolor(r->rcol, scp->val);
506          }
507   }
508  
509  
510 < extern void
510 > void
511   srcscatter(                     /* compute source scattering into ray */
512 <        register RAY  *r
512 >        RAY  *r
513   )
514   {
515          int  oldsampndx;
# Line 506 | Line 521 | srcscatter(                    /* compute source scattering into ray */
521          COLOR  cvext;
522          int  i, j;
523  
524 <        if (r->slights == NULL || r->slights[0] == 0
525 <                        || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
524 >        if (r->rot >= FHUGE*.99 || r->gecc >= 1.-FTINY)
525 >                return;         /* this can never work */
526 >        /* PMAP: do unconditional inscattering for volume photons */
527 >        if (!volumePhotonMapping && (r->slights == NULL || r->slights[0] == 0))
528                  return;
529 +                
530          if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
531                  nsamps = 1;
532   #if MAXSSAMP
# Line 517 | Line 535 | srcscatter(                    /* compute source scattering into ray */
535   #endif
536          oldsampndx = samplendx;
537          samplendx = random()&0x7fff;            /* randomize */
538 <        for (i = r->slights[0]; i > 0; i--) {   /* for each source */
538 >        for (i = volumePhotonMapping ? 1 : r->slights[0]; i > 0; i--) {
539 >                /* for each source OR once if volume photon map enabled */
540                  for (j = 0; j < nsamps; j++) {  /* for each sample position */
541                          samplendx++;
542                          t = r->rot * (j+frandom())/nsamps;
# Line 533 | Line 552 | srcscatter(                    /* compute source scattering into ray */
552                          sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
553                          sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
554                          sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
555 <                        sr.rmax = 0.;
556 <                        initsrcindex(&si);      /* sample ray to this source */
557 <                        si.sn = r->slights[i];
558 <                        nopart(&si, &sr);
559 <                        if (!srcray(&sr, NULL, &si) ||
560 <                                        sr.rsrc != r->slights[i])
561 <                                continue;               /* no path */
555 >                        
556 >                        if (!volumePhotonMapping) {
557 >                                initsrcindex(&si);      /* sample ray to this source */
558 >                                si.sn = r->slights[i];
559 >                                nopart(&si, &sr);
560 >                                if (!srcray(&sr, NULL, &si) ||
561 >                                                sr.rsrc != r->slights[i])
562 >                                        continue;       /* no path */
563   #if SHADCACHE
564 <                        if (srcblocked(&sr))            /* check shadow cache */
565 <                                continue;
564 >                                if (srcblocked(&sr))    /* check shadow cache */
565 >                                        continue;
566   #endif
567 <                        copycolor(sr.cext, r->cext);
568 <                        copycolor(sr.albedo, r->albedo);
569 <                        sr.gecc = r->gecc;
570 <                        sr.slights = r->slights;
571 <                        rayvalue(&sr);                  /* eval. source ray */
572 <                        if (bright(sr.rcol) <= FTINY) {
567 >                                copycolor(sr.cext, r->cext);
568 >                                copycolor(sr.albedo, r->albedo);
569 >                                sr.gecc = r->gecc;
570 >                                sr.slights = r->slights;
571 >                                rayvalue(&sr);          /* eval. source ray */
572 >                                if (bright(sr.rcol) <= FTINY) {
573   #if SHADCACHE
574 <                                srcblocker(&sr);        /* add blocker to cache */
574 >                                        srcblocker(&sr); /* add blocker to cache */
575   #endif
576 <                                continue;
577 <                        }
578 <                        if (r->gecc <= FTINY)           /* compute P(theta) */
579 <                                d = 1.;
580 <                        else {
581 <                                d = DOT(r->rdir, sr.rdir);
582 <                                d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
583 <                                d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
584 <                        }
576 >                                        continue;
577 >                                }
578 >                                if (r->gecc <= FTINY)   /* compute P(theta) */
579 >                                        d = 1.;
580 >                                else {
581 >                                        d = DOT(r->rdir, sr.rdir);
582 >                                        d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
583 >                                        d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
584 >                                }
585                                                          /* other factors */
586 <                        d *= si.dom * r->rot / (4.*PI*nsamps);
586 >                                d *= si.dom * r->rot / (4.*PI*nsamps);
587 >                                scalecolor(sr.rcol, d);
588 >                        } else {
589 >                                /* PMAP: Add ambient inscattering from
590 >                                 * volume photons; note we reverse the
591 >                                 * incident ray direction since we're
592 >                                 * now in *backward* raytracing mode! */
593 >                                sr.rdir [0] = -r -> rdir [0];
594 >                                sr.rdir [1] = -r -> rdir [1];
595 >                                sr.rdir [2] = -r -> rdir [2];
596 >                                sr.gecc = r -> gecc;
597 >                                inscatterVolumePmap(&sr, sr.rcol);
598 >                                scalecolor(sr.rcol, r -> rot / nsamps);
599 >                        }
600                          multcolor(sr.rcol, r->cext);
601                          multcolor(sr.rcol, r->albedo);
569                        scalecolor(sr.rcol, d);
602                          multcolor(sr.rcol, cvext);
603                          addcolor(r->rcol, sr.rcol);     /* add it in */
604                  }
# Line 633 | Line 665 | weaksrcmat(OBJECT obj)         /* identify material */
665   * The same is true for stray specular samples, since the specular
666   * contribution from light sources is calculated separately.
667   */
668 <
669 < #define  badcomponent(m, r)     (r->crtype&(AMBIENT|SPECULAR) && \
668 > /* PMAP: Also avoid counting sources via transferred ambient rays (e.g.
669 > * through glass) when photon mapping is enabled, as these indirect
670 > * components are already accounted for.
671 > */
672 > #define  badcomponent(m, r)   (srcRayInPmap(r) || \
673 >                                (r->crtype&(AMBIENT|SPECULAR) && \
674                                  !(r->crtype&SHADOW || r->rod < 0.0 || \
675 <                /* not 100% correct */  distglow(m, r, r->rot)))
675 >                /* not 100% correct */  distglow(m, r, r->rot))))
676  
677   /* passillum *
678   *
# Line 658 | Line 694 | weaksrcmat(OBJECT obj)         /* identify material */
694                                  distglow(m, r, raydist(r,PRIMARY)))
695  
696  
697 < extern int
697 > int
698   m_light(                                /* ray hit a light source */
699 <        register OBJREC  *m,
700 <        register RAY  *r
699 >        OBJREC  *m,
700 >        RAY  *r
701   )
702   {
703                                                  /* check for over-counting */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines