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.39 by greg, Wed Dec 31 02:03:08 2003 UTC vs.
Revision 2.47 by greg, Thu Mar 10 22:37:00 2005 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 <
16 < #include  "otspecial.h"
17 <
12 > #include  "rtotypes.h"
13   #include  "source.h"
19
14   #include  "random.h"
15  
16   extern double  ssampdist;               /* scatter sampling distance */
# Line 45 | 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 < OBJREC *                        /* find an object's actual material */
44 >
45 > extern OBJREC *                 /* find an object's actual material */
46   findmaterial(register OBJREC *o)
47   {
48          while (!ismaterial(o->otype)) {
# Line 70 | Line 66 | findmaterial(register OBJREC *o)
66   }
67  
68  
69 < void
70 < marksources()                   /* find and mark source objects */
69 > extern void
70 > marksources(void)                       /* find and mark source objects */
71   {
72          int  foundsource = 0;
73          int  i;
# Line 87 | Line 83 | marksources()                  /* find and mark source objects */
83                  if (!issurface(o->otype) || o->omod == OVOID)
84                          continue;
85                                          /* find material */
86 <                m = findmaterial(o);
86 >                m = findmaterial(objptr(o->omod));
87                  if (m == NULL || !islight(m->otype))
88                          continue;       /* not source modifier */
89          
# Line 129 | Line 125 | marksources()                  /* find and mark source objects */
125                          }
126                  }
127   #if  SHADCACHE
128 <                source[ns].obscache = NULL;
128 >                initobscache(ns);
129   #endif
130                  if (!(source[ns].sflags & SSKIP))
131                          foundsource++;
# Line 151 | 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
# Line 173 | Line 169 | freesources()                  /* free all source structures */
169   }
170  
171  
172 < int
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 */
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 */
# Line 212 | Line 208 | SRCINDEX  *si                  /* source sample index */
208   }
209  
210  
211 < void
211 > extern void
212   srcvalue(                       /* punch ray to source and compute value */
213 < register RAY  *r
213 >        register RAY  *r
214   )
215   {
216          register SRCREC  *sp;
# Line 254 | Line 250 | nomat:
250   }
251  
252  
253 < int
253 > static int
254 > transillum(                     /* check if material is transparent illum */
255 >        OBJECT  obj
256 > )
257 > {
258 >        OBJREC *m = findmaterial(objptr(obj));
259 >        
260 >        if (m == NULL)
261 >                return(1);
262 >        if (m->otype != MAT_ILLUM)
263 >                return(0);
264 >        return(!m->oargs.nsargs || !strcmp(m->oargs.sarg[0], VOIDID));
265 > }
266 >
267 >
268 > extern int
269   sourcehit(                      /* check to see if ray hit distant source */
270 < register RAY  *r
270 >        register RAY  *r
271   )
272   {
273 +        int  glowsrc = -1;
274 +        int  transrc = -1;
275          int  first, last;
276          register int  i;
277  
# Line 267 | Line 280 | register RAY  *r
280          } else {                        /* otherwise check all */
281                  first = 0; last = nsources-1;
282          }
283 <        for (i = first; i <= last; i++)
284 <                if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT)
285 <                        /*
286 <                         * Check to see if ray is within
287 <                         * solid angle of source.
288 <                         */
289 <                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
290 <                                        <= source[i].ss2) {
291 <                                r->ro = source[i].so;
292 <                                if (!(source[i].sflags & SSKIP))
293 <                                        break;
294 <                        }
295 <
296 <        if (r->ro != NULL) {
297 <                r->robj = objndx(r->ro);
298 <                for (i = 0; i < 3; i++)
299 <                        r->ron[i] = -r->rdir[i];
300 <                r->rod = 1.0;
301 <                r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
302 <                r->uv[0] = r->uv[1] = 0.0;
303 <                r->rox = NULL;
304 <                return(1);
283 >        for (i = first; i <= last; i++) {
284 >                if ((source[i].sflags & (SDISTANT|SVIRTUAL)) != SDISTANT)
285 >                        continue;
286 >                /*
287 >                 * Check to see if ray is within
288 >                 * solid angle of source.
289 >                 */
290 >                if (2.*PI*(1. - DOT(source[i].sloc,r->rdir)) > source[i].ss2)
291 >                        continue;
292 >                                        /* is it the only possibility? */
293 >                if (first == last) {
294 >                        r->ro = source[i].so;
295 >                        break;
296 >                }
297 >                /*
298 >                 * If it's a glow or transparent illum, just remember it.
299 >                 */
300 >                if (source[i].sflags & SSKIP) {
301 >                        glowsrc = i;
302 >                        continue;
303 >                }
304 >                if (transillum(source[i].so->omod)) {
305 >                        transrc = i;
306 >                        continue;
307 >                }
308 >                r->ro = source[i].so;   /* otherwise, use first hit */
309 >                break;
310          }
311 <        return(0);
312 < }
313 <
314 <
315 < #if  SHADCACHE                  /* preemptive shadow checking */
316 < #define ABS(x)  ((x)>0 ? (x) : -(x))
317 <
318 < static void                             /* find closest blockers to source */
319 < initobscache(SRCREC *srcp)
320 < {
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);
311 >        /*
312 >         * Do we need fallback?
313 >         */
314 >        if (r->ro == NULL) {
315 >                if (transrc >= 0 && r->crtype & (AMBIENT|SPECULAR))
316 >                        return(0);      /* avoid overcounting */
317 >                if (glowsrc >= 0)
318 >                        r->ro = source[glowsrc].so;
319 >                else
320 >                        return(0);      /* nothing usable */
321          }
322 <                                        /* XXX Should cast rays from source */
323 <        for (i = cachelen; i--; )
324 <                srcp->obscache->obs[i] = OVOID;
322 >        /*
323 >         * Make assignments.
324 >         */
325 >        r->robj = objndx(r->ro);
326 >        for (i = 0; i < 3; i++)
327 >                r->ron[i] = -r->rdir[i];
328 >        r->rod = 1.0;
329 >        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
330 >        r->uv[0] = r->uv[1] = 0.0;
331 >        r->rox = NULL;
332 >        return(1);
333   }
334  
335  
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
336   static int
337   cntcmp(                         /* contribution compare (descending) */
338 < const void *p1,
339 < const void *p2
338 >        const void *p1,
339 >        const void *p2
340   )
341   {
342          register const CNTPTR  *sc1 = (const CNTPTR *)p1;
# Line 500 | Line 350 | const void *p2
350   }
351  
352  
353 < void
353 > extern void
354   direct(                                 /* add direct component */
355 < RAY  *r,                        /* ray that hit surface */
356 < void  (*f)(),                   /* direct component coefficient function */
357 < char  *p                        /* data for f */
355 >        RAY  *r,                        /* ray that hit surface */
356 >        srcdirf_t *f,                   /* direct component coefficient function */
357 >        void  *p                        /* data for f */
358   )
359   {
510        extern void  (*trace)();
360          register int  sn;
361          register CONTRIB  *scp;
362          SRCINDEX  si;
# Line 608 | Line 457 | char  *p                       /* data for f */
457                          if (bright(sr.rcol) <= FTINY) {
458   #if SHADCACHE
459                                  if ((scp <= srccnt || scp[-1].sno != scp->sno)
460 <                                                && (scp >= srccnt+ncnts ||
460 >                                                && (scp >= srccnt+ncnts-1 ||
461                                                      scp[1].sno != scp->sno))
462                                          srcblocker(&sr);
463   #endif
# Line 645 | Line 494 | char  *p                       /* data for f */
494   }
495  
496  
497 < void
497 > extern void
498   srcscatter(                     /* compute source scattering into ray */
499 < register RAY  *r
499 >        register RAY  *r
500   )
501   {
502          int  oldsampndx;
# Line 693 | Line 542 | register RAY  *r
542                          if (!srcray(&sr, NULL, &si) ||
543                                          sr.rsrc != r->slights[i])
544                                  continue;               /* no path */
545 + #if SHADCACHE
546 +                        if (srcblocked(&sr))            /* check shadow cache */
547 +                                continue;
548 + #endif
549                          copycolor(sr.cext, r->cext);
550                          copycolor(sr.albedo, r->albedo);
551                          sr.gecc = r->gecc;
552                          sr.slights = r->slights;
553                          rayvalue(&sr);                  /* eval. source ray */
554 <                        if (bright(sr.rcol) <= FTINY)
554 >                        if (bright(sr.rcol) <= FTINY) {
555 > #if SHADCACHE
556 >                                srcblocker(&sr);        /* add blocker to cache */
557 > #endif
558                                  continue;
559 +                        }
560                          if (r->gecc <= FTINY)           /* compute P(theta) */
561                                  d = 1.;
562                          else {
# Line 737 | Line 594 | register RAY  *r
594   */
595  
596   static int
597 < weaksrcmat(int obj)             /* identify material */
597 > weaksrcmat(OBJECT obj)          /* identify material */
598   {
599 <        register OBJREC *o = objptr(obj);
599 >        OBJREC *m = findmaterial(objptr(obj));
600          
601 <        while (!ismaterial(o->otype))   /* find material */
602 <                o = objptr(o->omod);
746 <        return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW));
601 >        if (m == NULL) return(0);
602 >        return((m->otype==MAT_ILLUM) | (m->otype==MAT_GLOW));
603   }
604  
605   #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \
# Line 804 | Line 660 | weaksrcmat(int obj)            /* identify material */
660                                  distglow(m, r, raydist(r,PRIMARY)))
661  
662  
663 < int
663 > extern int
664   m_light(                                /* ray hit a light source */
665 < register OBJREC  *m,
666 < register RAY  *r
665 >        register OBJREC  *m,
666 >        register RAY  *r
667   )
668   {
669                                                  /* check for over-counting */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines