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.31 by greg, Tue Mar 11 17:08:55 2003 UTC vs.
Revision 2.46 by greg, Wed Sep 8 17:10:16 2004 UTC

# Line 7 | Line 7 | static const char RCSid[] = "$Id$";
7   *  External symbols declared in source.h
8   */
9  
10 #include "copyright.h"
11
10   #include  "ray.h"
13
11   #include  "otypes.h"
12 <
12 > #include  "rtotypes.h"
13   #include  "source.h"
17
14   #include  "random.h"
15  
16   extern double  ssampdist;               /* scatter sampling distance */
# Line 43 | Line 39 | static CONTRIB  *srccnt;               /* source contributions in d
39   static CNTPTR  *cntord;                 /* source ordering in direct() */
40   static int  maxcntr = 0;                /* size of contribution arrays */
41  
42 + static int cntcmp(const void *p1, const void *p2);
43  
44 < void
45 < marksources()                   /* find and mark source objects */
44 >
45 > extern OBJREC *                 /* find an object's actual material */
46 > findmaterial(register OBJREC *o)
47   {
48 +        while (!ismaterial(o->otype)) {
49 +                if (ismixture(o->otype))
50 +                        return(NULL);   /* reject mixed materials */
51 +                if (o->otype == MOD_ALIAS && o->oargs.nsargs) {
52 +                        OBJECT  aobj;
53 +                        OBJREC  *ao;
54 +                        aobj = lastmod(objndx(o), o->oargs.sarg[0]);
55 +                        if (aobj < 0)
56 +                                objerror(o, USER, "bad reference");
57 +                        ao = objptr(aobj);
58 +                        if (ismaterial(ao->otype))
59 +                                return(ao);
60 +                }
61 +                if (o->omod == OVOID)
62 +                        return(NULL);
63 +                o = objptr(o->omod);
64 +        }
65 +        return(o);
66 + }
67 +
68 +
69 + extern void
70 + marksources(void)                       /* find and mark source objects */
71 + {
72          int  foundsource = 0;
73          int  i;
74          register OBJREC  *o, *m;
# Line 54 | Line 76 | marksources()                  /* find and mark source objects */
76                                          /* initialize dispatch table */
77          initstypes();
78                                          /* find direct sources */
79 <        for (i = 0; i < nobjects; i++) {
79 >        for (i = 0; i < nsceneobjs; i++) {
80          
81                  o = objptr(i);
82  
83                  if (!issurface(o->otype) || o->omod == OVOID)
84                          continue;
85 <
86 <                m = objptr(o->omod);
87 <
88 <                if (!islight(m->otype))
67 <                        continue;
85 >                                        /* find material */
86 >                m = findmaterial(objptr(o->omod));
87 >                if (m == NULL || !islight(m->otype))
88 >                        continue;       /* not source modifier */
89          
90                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
91                                  m->otype == MAT_SPOT ? 7 : 3))
# Line 103 | Line 124 | marksources()                  /* find and mark source objects */
124                                  source[ns].sflags |= SSKIP;
125                          }
126                  }
127 + #if  SHADCACHE
128 +                initobscache(ns);
129 + #endif
130                  if (!(source[ns].sflags & SSKIP))
131                          foundsource++;
132          }
# Line 115 | Line 139 | marksources()                  /* find and mark source objects */
139          maxcntr = nsources + MAXSPART;  /* start with this many */
140          srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
141          cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR));
142 <        if (srccnt == NULL | cntord == NULL)
142 >        if ((srccnt == NULL) | (cntord == NULL))
143                  goto memerr;
144          return;
145   memerr:
# Line 123 | Line 147 | memerr:
147   }
148  
149  
150 < void
151 < freesources()                   /* free all source structures */
150 > extern void
151 > freesources(void)                       /* free all source structures */
152   {
153          if (nsources > 0) {
154 + #if SHADCACHE
155 +                while (nsources--)
156 +                        freeobscache(&source[nsources]);
157 + #endif
158                  free((void *)source);
159                  source = NULL;
160                  nsources = 0;
# Line 141 | Line 169 | freesources()                  /* free all source structures */
169   }
170  
171  
172 < int
173 < srcray(sr, r, si)               /* send a ray to a source, return domega */
174 < register RAY  *sr;              /* returned source ray */
175 < RAY  *r;                        /* ray which hit object */
176 < SRCINDEX  *si;                  /* source sample index */
172 > extern int
173 > srcray(                         /* send a ray to a source, return domega */
174 >        register RAY  *sr,              /* returned source ray */
175 >        RAY  *r,                        /* ray which hit object */
176 >        SRCINDEX  *si                   /* source sample index */
177 > )
178   {
179      double  d;                          /* distance to source */
180      register SRCREC  *srcp;
# Line 179 | Line 208 | SRCINDEX  *si;                 /* source sample index */
208   }
209  
210  
211 < void
212 < srcvalue(r)                     /* punch ray to source and compute value */
213 < register RAY  *r;
211 > extern void
212 > srcvalue(                       /* punch ray to source and compute value */
213 >        register RAY  *r
214 > )
215   {
216          register SRCREC  *sp;
217  
# Line 220 | Line 250 | nomat:
250   }
251  
252  
253 < int
254 < sourcehit(r)                    /* check to see if ray hit distant source */
255 < register RAY  *r;
253 > extern int
254 > sourcehit(                      /* check to see if ray hit distant source */
255 >        register RAY  *r
256 > )
257   {
258          int  first, last;
259          register int  i;
# Line 260 | Line 291 | register RAY  *r;
291  
292  
293   static int
294 < cntcmp(sc1, sc2)                        /* contribution compare (descending) */
295 < register CNTPTR  *sc1, *sc2;
294 > cntcmp(                         /* contribution compare (descending) */
295 >        const void *p1,
296 >        const void *p2
297 > )
298   {
299 +        register const CNTPTR  *sc1 = (const CNTPTR *)p1;
300 +        register const CNTPTR  *sc2 = (const CNTPTR *)p2;
301 +
302          if (sc1->brt > sc2->brt)
303                  return(-1);
304          if (sc1->brt < sc2->brt)
# Line 271 | Line 307 | register CNTPTR  *sc1, *sc2;
307   }
308  
309  
310 < void
311 < direct(r, f, p)                         /* add direct component */
312 < RAY  *r;                        /* ray that hit surface */
313 < void  (*f)();                   /* direct component coefficient function */
314 < char  *p;                       /* data for f */
310 > extern void
311 > direct(                                 /* add direct component */
312 >        RAY  *r,                        /* ray that hit surface */
313 >        srcdirf_t *f,                   /* direct component coefficient function */
314 >        void  *p                        /* data for f */
315 > )
316   {
280        extern void  (*trace)();
317          register int  sn;
318          register CONTRIB  *scp;
319          SRCINDEX  si;
# Line 293 | Line 329 | char  *p;                      /* data for f */
329          for (sn = 0; srcray(&sr, r, &si); sn++) {
330                  if (sn >= maxcntr) {
331                          maxcntr = sn + MAXSPART;
332 <                        srccnt = (CONTRIB *)realloc((char *)srccnt,
332 >                        srccnt = (CONTRIB *)realloc((void *)srccnt,
333                                          maxcntr*sizeof(CONTRIB));
334 <                        cntord = (CNTPTR *)realloc((char *)cntord,
334 >                        cntord = (CNTPTR *)realloc((void *)cntord,
335                                          maxcntr*sizeof(CNTPTR));
336 <                        if (srccnt == NULL | cntord == NULL)
336 >                        if ((srccnt == NULL) | (cntord == NULL))
337                                  error(SYSTEM, "out of memory in direct");
338                  }
339                  cntord[sn].sndx = sn;
# Line 308 | Line 344 | char  *p;                      /* data for f */
344                  cntord[sn].brt = bright(scp->coef);
345                  if (cntord[sn].brt <= 0.0)
346                          continue;
347 + #if SHADCACHE
348 +                                                /* check shadow cache */
349 +                if (si.np == 1 && srcblocked(&sr)) {
350 +                        cntord[sn].brt = 0.0;
351 +                        continue;
352 +                }
353 + #endif
354                  VCOPY(scp->dir, sr.rdir);
355                                                  /* compute potential */
356                  sr.revf = srcvalue;
# Line 355 | Line 398 | char  *p;                      /* data for f */
398                  rayorigin(&sr, r, SHADOW, 1.0);
399                  VCOPY(sr.rdir, scp->dir);
400                  sr.rsrc = scp->sno;
401 <                source[scp->sno].ntests++;      /* keep statistics */
401 >                                                /* keep statistics */
402 >                if (source[scp->sno].ntests++ > 0xfffffff0) {
403 >                        source[scp->sno].ntests >>= 1;
404 >                        source[scp->sno].nhits >>= 1;
405 >                }
406                  if (localhit(&sr, &thescene) &&
407                                  ( sr.ro != source[scp->sno].so ||
408                                  source[scp->sno].sflags & SFOLLOW )) {
# Line 364 | Line 411 | char  *p;                      /* data for f */
411                          rayparticipate(&sr);
412                          if (trace != NULL)
413                                  (*trace)(&sr);  /* trace execution */
414 <                        if (bright(sr.rcol) <= FTINY)
414 >                        if (bright(sr.rcol) <= FTINY) {
415 > #if SHADCACHE
416 >                                if ((scp <= srccnt || scp[-1].sno != scp->sno)
417 >                                                && (scp >= srccnt+ncnts-1 ||
418 >                                                    scp[1].sno != scp->sno))
419 >                                        srcblocker(&sr);
420 > #endif
421                                  continue;       /* missed! */
422 +                        }
423                          copycolor(scp->val, sr.rcol);
424                          multcolor(scp->val, scp->coef);
425                  }
# Line 397 | Line 451 | char  *p;                      /* data for f */
451   }
452  
453  
454 < void
455 < srcscatter(r)                   /* compute source scattering into ray */
456 < register RAY  *r;
454 > extern void
455 > srcscatter(                     /* compute source scattering into ray */
456 >        register RAY  *r
457 > )
458   {
459          int  oldsampndx;
460          int  nsamps;
# Line 444 | Line 499 | register RAY  *r;
499                          if (!srcray(&sr, NULL, &si) ||
500                                          sr.rsrc != r->slights[i])
501                                  continue;               /* no path */
502 + #if SHADCACHE
503 +                        if (srcblocked(&sr))            /* check shadow cache */
504 +                                continue;
505 + #endif
506                          copycolor(sr.cext, r->cext);
507                          copycolor(sr.albedo, r->albedo);
508                          sr.gecc = r->gecc;
509                          sr.slights = r->slights;
510                          rayvalue(&sr);                  /* eval. source ray */
511 <                        if (bright(sr.rcol) <= FTINY)
511 >                        if (bright(sr.rcol) <= FTINY) {
512 > #if SHADCACHE
513 >                                srcblocker(&sr);        /* add blocker to cache */
514 > #endif
515                                  continue;
516 +                        }
517                          if (r->gecc <= FTINY)           /* compute P(theta) */
518                                  d = 1.;
519                          else {
# Line 487 | Line 550 | register RAY  *r;
550   * geometry behind (or inside) an effective radiator.
551   */
552  
553 < static int weaksrcmod(obj) int obj;     /* efficiency booster function */
554 < {register OBJREC *o = objptr(obj);
555 < return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
553 > static int
554 > weaksrcmat(OBJECT obj)          /* identify material */
555 > {
556 >        OBJREC *o = findmaterial(objptr(obj));
557 >        
558 >        if (o == NULL) return 0;
559 >        return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW));
560 > }
561  
562   #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \
563                                  r->rod > 0.0 && \
564 <                                weaksrcmod(source[r->rsrc].so->omod))
564 >                                weaksrcmat(source[r->rsrc].so->omod))
565  
566   /* wrongsource *
567   *
# Line 549 | Line 617 | return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
617                                  distglow(m, r, raydist(r,PRIMARY)))
618  
619  
620 < int
621 < m_light(m, r)                   /* ray hit a light source */
622 < register OBJREC  *m;
623 < register RAY  *r;
620 > extern int
621 > m_light(                                /* ray hit a light source */
622 >        register OBJREC  *m,
623 >        register RAY  *r
624 > )
625   {
626                                                  /* check for over-counting */
627          if (badcomponent(m, r))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines