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.32 by greg, Wed Apr 23 00:52:34 2003 UTC vs.
Revision 2.39 by greg, Wed Dec 31 02:03:08 2003 UTC

# Line 13 | Line 13 | static const char RCSid[] = "$Id$";
13  
14   #include  "otypes.h"
15  
16 + #include  "otspecial.h"
17 +
18   #include  "source.h"
19  
20   #include  "random.h"
# Line 44 | Line 46 | static CNTPTR  *cntord;                        /* source ordering in direct
46   static int  maxcntr = 0;                /* size of contribution arrays */
47  
48  
49 + OBJREC *                        /* find an object's actual material */
50 + findmaterial(register OBJREC *o)
51 + {
52 +        while (!ismaterial(o->otype)) {
53 +                if (ismixture(o->otype))
54 +                        return(NULL);   /* reject mixed materials */
55 +                if (o->otype == MOD_ALIAS && o->oargs.nsargs) {
56 +                        OBJECT  aobj;
57 +                        OBJREC  *ao;
58 +                        aobj = lastmod(objndx(o), o->oargs.sarg[0]);
59 +                        if (aobj < 0)
60 +                                objerror(o, USER, "bad reference");
61 +                        ao = objptr(aobj);
62 +                        if (ismaterial(ao->otype))
63 +                                return(ao);
64 +                }
65 +                if (o->omod == OVOID)
66 +                        return(NULL);
67 +                o = objptr(o->omod);
68 +        }
69 +        return(o);
70 + }
71 +
72 +
73   void
74   marksources()                   /* find and mark source objects */
75   {
# Line 54 | Line 80 | marksources()                  /* find and mark source objects */
80                                          /* initialize dispatch table */
81          initstypes();
82                                          /* find direct sources */
83 <        for (i = 0; i < nobjects; i++) {
83 >        for (i = 0; i < nsceneobjs; i++) {
84          
85                  o = objptr(i);
86  
87                  if (!issurface(o->otype) || o->omod == OVOID)
88                          continue;
89 <
90 <                m = objptr(o->omod);
91 <
92 <                if (!islight(m->otype))
67 <                        continue;
89 >                                        /* find material */
90 >                m = findmaterial(o);
91 >                if (m == NULL || !islight(m->otype))
92 >                        continue;       /* not source modifier */
93          
94                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
95                                  m->otype == MAT_SPOT ? 7 : 3))
# Line 103 | Line 128 | marksources()                  /* find and mark source objects */
128                                  source[ns].sflags |= SSKIP;
129                          }
130                  }
131 + #if  SHADCACHE
132 +                source[ns].obscache = NULL;
133 + #endif
134                  if (!(source[ns].sflags & SSKIP))
135                          foundsource++;
136          }
# Line 115 | Line 143 | marksources()                  /* find and mark source objects */
143          maxcntr = nsources + MAXSPART;  /* start with this many */
144          srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
145          cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR));
146 <        if (srccnt == NULL | cntord == NULL)
146 >        if ((srccnt == NULL) | (cntord == NULL))
147                  goto memerr;
148          return;
149   memerr:
# Line 127 | Line 155 | void
155   freesources()                   /* free all source structures */
156   {
157          if (nsources > 0) {
158 + #if SHADCACHE
159 +                while (nsources--)
160 +                        freeobscache(&source[nsources]);
161 + #endif
162                  free((void *)source);
163                  source = NULL;
164                  nsources = 0;
# Line 142 | Line 174 | freesources()                  /* free all source structures */
174  
175  
176   int
177 < srcray(sr, r, si)               /* send a ray to a source, return domega */
178 < register RAY  *sr;              /* returned source ray */
179 < RAY  *r;                        /* ray which hit object */
180 < SRCINDEX  *si;                  /* source sample index */
177 > srcray(                         /* send a ray to a source, return domega */
178 > register RAY  *sr,              /* returned source ray */
179 > RAY  *r,                        /* ray which hit object */
180 > SRCINDEX  *si                   /* source sample index */
181 > )
182   {
183      double  d;                          /* distance to source */
184      register SRCREC  *srcp;
# Line 180 | Line 213 | SRCINDEX  *si;                 /* source sample index */
213  
214  
215   void
216 < srcvalue(r)                     /* punch ray to source and compute value */
217 < register RAY  *r;
216 > srcvalue(                       /* punch ray to source and compute value */
217 > register RAY  *r
218 > )
219   {
220          register SRCREC  *sp;
221  
# Line 221 | Line 255 | nomat:
255  
256  
257   int
258 < sourcehit(r)                    /* check to see if ray hit distant source */
259 < register RAY  *r;
258 > sourcehit(                      /* check to see if ray hit distant source */
259 > register RAY  *r
260 > )
261   {
262          int  first, last;
263          register int  i;
# Line 259 | Line 294 | register RAY  *r;
294   }
295  
296  
297 + #if  SHADCACHE                  /* preemptive shadow checking */
298 + #define ABS(x)  ((x)>0 ? (x) : -(x))
299 +
300 + static void                             /* find closest blockers to source */
301 + initobscache(SRCREC *srcp)
302 + {
303 +        int     i;
304 +        int     cachelen;
305 +
306 +        if (srcp->sflags & SDISTANT)
307 +                cachelen = 4*SHADCACHE*SHADCACHE;
308 +        else if (srcp->sflags & SFLAT)
309 +                cachelen = SHADCACHE*SHADCACHE*3 + (SHADCACHE&1)*SHADCACHE*4;
310 +        else /* spherical distribution */
311 +                cachelen = SHADCACHE*SHADCACHE*6;
312 +                                        /* allocate cache */
313 +        DCHECK(srcp->obscache != NULL,
314 +                        CONSISTENCY, "initobscache() called twice");
315 +        srcp->obscache = (OBSCACHE *)malloc(sizeof(OBSCACHE) +
316 +                                                sizeof(OBJECT)*(cachelen-1));
317 +        if (srcp->obscache == NULL)
318 +                error(SYSTEM, "out of memory in initobscache()");
319 +                                        /* set parameters */
320 +        if (srcp->sflags & SDISTANT) {
321 +                int     ax, ax1, ax2;
322 +                RREAL   amax = 0;
323 +                for (ax1 = 3; ax1--; )
324 +                        if (ABS(srcp->sloc[ax1]) > amax) {
325 +                                amax = ABS(srcp->sloc[ax1]);
326 +                                ax = ax1;
327 +                        }
328 +                srcp->obscache->p.d.ax = ax;
329 +                ax1 = (ax+1)%3;
330 +                ax2 = (ax+2)%3;
331 +                VCOPY(srcp->obscache->p.d.o, thescene.cuorg);
332 +                if (srcp->sloc[ax] > 0)
333 +                        srcp->obscache->p.d.o[ax] += thescene.cusize;
334 +                if (srcp->sloc[ax1] < 0)
335 +                        srcp->obscache->p.d.o[ax1] += thescene.cusize *
336 +                                        srcp->sloc[ax1] / ABS(srcp->sloc[ax]);
337 +                if (srcp->sloc[ax2] < 0)
338 +                        srcp->obscache->p.d.o[ax2] += thescene.cusize *
339 +                                        srcp->sloc[ax2] / ABS(srcp->sloc[ax]);
340 +                srcp->obscache->p.d.e1 = (1.-FTINY) / (thescene.cusize*(1. +
341 +                                fabs(srcp->sloc[ax1]/srcp->sloc[ax])));
342 +                srcp->obscache->p.d.e2 = (1.-FTINY) / (thescene.cusize*(1. +
343 +                                fabs(srcp->sloc[ax2]/srcp->sloc[ax])));
344 +        } else if (srcp->sflags & SFLAT) {
345 +                VCOPY(srcp->obscache->p.f.u, srcp->ss[SU]);
346 +                normalize(srcp->obscache->p.f.u);
347 +                fcross(srcp->obscache->p.f.v,
348 +                                srcp->snorm, srcp->obscache->p.f.u);
349 +        }
350 +                                        /* XXX Should cast rays from source */
351 +        for (i = cachelen; i--; )
352 +                srcp->obscache->obs[i] = OVOID;
353 + }
354 +
355 +
356 + static OBJECT *                 /* return occluder cache entry */
357 + srcobstructp(register RAY *r)
358 + {
359 +        static OBJECT   noobs;
360 +        SRCREC          *srcp;
361 +        int             ondx;
362 +
363 +        DCHECK(r->rsrc < 0, CONSISTENCY,
364 +                        "srcobstructp() called with unaimed ray");
365 +        noobs = OVOID;
366 +        srcp = &source[r->rsrc];
367 +        if (srcp->obscache == NULL)     /* initialize cache */
368 +                initobscache(srcp);
369 +                                        /* compute cache index */
370 +        if (srcp->sflags & SDISTANT) {
371 +                int     ax, ax1, ax2;
372 +                double  t;
373 +                ax = srcp->obscache->p.d.ax;
374 +                if ((ax1 = ax+1) >= 3) ax1 -= 3;
375 +                if ((ax2 = ax+2) >= 3) ax2 -= 3;
376 +                t = (srcp->obscache->p.d.o[ax] - r->rorg[ax]) / srcp->sloc[ax];
377 +                if (t <= FTINY)
378 +                        return &noobs;  /* could happen if ray is outside */
379 +                ondx = 2*SHADCACHE*(int)(2*SHADCACHE*srcp->obscache->p.d.e1 *
380 +                                (r->rorg[ax1] + t*srcp->sloc[ax1] -
381 +                                        srcp->obscache->p.d.o[ax1]));
382 +                ondx += (int)(2*SHADCACHE*srcp->obscache->p.d.e2 *
383 +                                (r->rorg[ax2] + t*srcp->sloc[ax2] -
384 +                                        srcp->obscache->p.d.o[ax2]));
385 +                if (ondx < 0 | ondx >= 4*SHADCACHE*SHADCACHE)
386 +                        return &nobs;   /* could happen if ray is outside */
387 +        } else if (srcp->sflags & SFLAT) {
388 +                FVECT   sd;
389 +                RREAL   sd0m, sd1m;
390 +                sd[0] = -DOT(r->rdir, srcp->obscache->p.f.u);
391 +                sd[1] = -DOT(r->rdir, srcp->obscache->p.f.v);
392 +                sd[2] = -DOT(r->rdir, srcp->snorm);
393 +                if (sd[2] < 0)
394 +                        return &noobs;  /* shouldn't happen */
395 +                sd0m = ABS(sd[0]);
396 +                sd1m = ABS(sd[1]);
397 +                if (sd[2] >= sd0m && sd[2] >= sd1m) {
398 +                        ondx = SHADCACHE*(int)(SHADCACHE*(.5-FTINY) *
399 +                                        (1. + sd[0]/sd[2]));
400 +                        ondx += (int)(SHADCACHE*(.5-FTINY) *
401 +                                        (1. + sd[1]/sd[2]));
402 +                } else if (sd0m >= sd1m) {
403 +                        ondx = SHADCACHE*SHADCACHE;
404 +                        if (sd[0] < 0)
405 +                                ondx += ((SHADCACHE+1)>>1)*SHADCACHE;
406 +                        ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) *
407 +                                        sd[2]/sd0m);
408 +                        ondx += (int)(SHADCACHE*(.5-FTINY) *
409 +                                        (1. + sd[1]/sd0m));
410 +                } else /* sd1m > sd0m */ {
411 +                        ondx = SHADCACHE*SHADCACHE +
412 +                                        ((SHADCACHE+1)>>1)*SHADCACHE*2;
413 +                        if (sd[1] < 0)
414 +                                ondx += ((SHADCACHE+1)>>1)*SHADCACHE;
415 +                        ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) *
416 +                                        sd[2]/sd1m);
417 +                        ondx += (int)(SHADCACHE*(.5-FTINY) *
418 +                                        (1. + sd[0]/sd1m));
419 +                }
420 +        } else /* spherical distribution */ {
421 +                int     ax, ax1, ax2;
422 +                RREAL   amax = 0;
423 +                for (ax1 = 3; ax1--; )
424 +                        if (ABS(r->rdir[ax1]) > amax) {
425 +                                amax = ABS(r->rdir[ax1]);
426 +                                ax = ax1;
427 +                        }
428 +                if ((ax1 = ax+1) >= 3) ax1 -= 3;
429 +                if ((ax2 = ax+2) >= 3) ax2 -= 3;
430 +                ondx = 2*SHADCACHE*SHADCACHE * ax;
431 +                if (r->rdir[ax] < 0)
432 +                        ondx += SHADCACHE*SHADCACHE;
433 +                ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) *
434 +                                        (1. + r->rdir[ax1]/amax));
435 +                ondx += (int)(SHADCACHE*(.5-FTINY) *
436 +                                (1. + r->rdir[ax2]/amax));
437 +        }
438 +                                        /* return cache pointer */
439 +        return(&srcp->obscache->obs[ondx]);
440 + }
441 +
442 +
443 + void                            /* free obstruction cache */
444 + freeobscache(SRCREC *srcp)
445 + {
446 +        if (srcp->obscache == NULL)
447 +                return;
448 +        free((void *)srcp->obscache);
449 +        srcp->obscache = NULL;
450 + }
451 +
452 +        
453 + void                            /* record a source blocker */
454 + srcblocker(register RAY *r)
455 + {
456 +        OBJREC  *m;
457 +
458 +        if (r->robj == OVOID || objptr(r->robj) != r->ro ||
459 +                        isvolume(r->ro->otype))
460 +                return;                 /* don't record complex blockers */
461 +        m = findmaterial(r->ro);
462 +        if (m == NULL)
463 +                return;                 /* no material?! */
464 +        if (!(ofun[m->otype].flags & T_OPAQUE))
465 +                return;                 /* material not a reliable blocker */
466 +
467 +        *srcobstructp(r) = r->robj;     /* else record obstructor */
468 + }
469 +
470 +
471 + int                             /* check ray against cached blocker */
472 + srcblocked(RAY *r)
473 + {
474 +        OBJECT  obs = *srcobstructp(r);
475 +        OBJREC  *op;
476 +
477 +        if (obs == OVOID)
478 +                return(0);
479 +        op = objptr(obs);               /* check for intersection */
480 +        return ((*ofun[op->otype].funp)(op, r));
481 + }
482 +
483 + #endif
484 +
485 +
486   static int
487 < cntcmp(sc1, sc2)                        /* contribution compare (descending) */
488 < register CNTPTR  *sc1, *sc2;
487 > cntcmp(                         /* contribution compare (descending) */
488 > const void *p1,
489 > const void *p2
490 > )
491   {
492 +        register const CNTPTR  *sc1 = (const CNTPTR *)p1;
493 +        register const CNTPTR  *sc2 = (const CNTPTR *)p2;
494 +
495          if (sc1->brt > sc2->brt)
496                  return(-1);
497          if (sc1->brt < sc2->brt)
# Line 272 | Line 501 | register CNTPTR  *sc1, *sc2;
501  
502  
503   void
504 < direct(r, f, p)                         /* add direct component */
505 < RAY  *r;                        /* ray that hit surface */
506 < void  (*f)();                   /* direct component coefficient function */
507 < char  *p;                       /* data for f */
504 > direct(                                 /* add direct component */
505 > RAY  *r,                        /* ray that hit surface */
506 > void  (*f)(),                   /* direct component coefficient function */
507 > char  *p                        /* data for f */
508 > )
509   {
510          extern void  (*trace)();
511          register int  sn;
# Line 297 | Line 527 | char  *p;                      /* data for f */
527                                          maxcntr*sizeof(CONTRIB));
528                          cntord = (CNTPTR *)realloc((void *)cntord,
529                                          maxcntr*sizeof(CNTPTR));
530 <                        if (srccnt == NULL | cntord == NULL)
530 >                        if ((srccnt == NULL) | (cntord == NULL))
531                                  error(SYSTEM, "out of memory in direct");
532                  }
533                  cntord[sn].sndx = sn;
# Line 308 | Line 538 | char  *p;                      /* data for f */
538                  cntord[sn].brt = bright(scp->coef);
539                  if (cntord[sn].brt <= 0.0)
540                          continue;
541 + #if SHADCACHE
542 +                                                /* check shadow cache */
543 +                if (si.np == 1 && srcblocked(&sr)) {
544 +                        cntord[sn].brt = 0.0;
545 +                        continue;
546 +                }
547 + #endif
548                  VCOPY(scp->dir, sr.rdir);
549                                                  /* compute potential */
550                  sr.revf = srcvalue;
# Line 355 | Line 592 | char  *p;                      /* data for f */
592                  rayorigin(&sr, r, SHADOW, 1.0);
593                  VCOPY(sr.rdir, scp->dir);
594                  sr.rsrc = scp->sno;
595 <                source[scp->sno].ntests++;      /* keep statistics */
595 >                                                /* keep statistics */
596 >                if (source[scp->sno].ntests++ > 0xfffffff0) {
597 >                        source[scp->sno].ntests >>= 1;
598 >                        source[scp->sno].nhits >>= 1;
599 >                }
600                  if (localhit(&sr, &thescene) &&
601                                  ( sr.ro != source[scp->sno].so ||
602                                  source[scp->sno].sflags & SFOLLOW )) {
# Line 364 | Line 605 | char  *p;                      /* data for f */
605                          rayparticipate(&sr);
606                          if (trace != NULL)
607                                  (*trace)(&sr);  /* trace execution */
608 <                        if (bright(sr.rcol) <= FTINY)
608 >                        if (bright(sr.rcol) <= FTINY) {
609 > #if SHADCACHE
610 >                                if ((scp <= srccnt || scp[-1].sno != scp->sno)
611 >                                                && (scp >= srccnt+ncnts ||
612 >                                                    scp[1].sno != scp->sno))
613 >                                        srcblocker(&sr);
614 > #endif
615                                  continue;       /* missed! */
616 +                        }
617                          copycolor(scp->val, sr.rcol);
618                          multcolor(scp->val, scp->coef);
619                  }
# Line 398 | Line 646 | char  *p;                      /* data for f */
646  
647  
648   void
649 < srcscatter(r)                   /* compute source scattering into ray */
650 < register RAY  *r;
649 > srcscatter(                     /* compute source scattering into ray */
650 > register RAY  *r
651 > )
652   {
653          int  oldsampndx;
654          int  nsamps;
# Line 487 | Line 736 | register RAY  *r;
736   * geometry behind (or inside) an effective radiator.
737   */
738  
739 < static int weaksrcmod(obj) int obj;     /* efficiency booster function */
740 < {register OBJREC *o = objptr(obj);
741 < return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
739 > static int
740 > weaksrcmat(int obj)             /* identify material */
741 > {
742 >        register OBJREC *o = objptr(obj);
743 >        
744 >        while (!ismaterial(o->otype))   /* find material */
745 >                o = objptr(o->omod);
746 >        return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW));
747 > }
748  
749   #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \
750                                  r->rod > 0.0 && \
751 <                                weaksrcmod(source[r->rsrc].so->omod))
751 >                                weaksrcmat(source[r->rsrc].so->omod))
752  
753   /* wrongsource *
754   *
# Line 550 | Line 805 | return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
805  
806  
807   int
808 < m_light(m, r)                   /* ray hit a light source */
809 < register OBJREC  *m;
810 < register RAY  *r;
808 > m_light(                                /* ray hit a light source */
809 > register OBJREC  *m,
810 > register RAY  *r
811 > )
812   {
813                                                  /* check for over-counting */
814          if (badcomponent(m, r))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines