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.38 by greg, Wed Dec 31 01:50:02 2003 UTC vs.
Revision 2.44 by schorsch, Tue Mar 30 16:13:01 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 <
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 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 > extern int
254   sourcehit(                      /* check to see if ray hit distant source */
255 < register RAY  *r
255 >        register RAY  *r
256   )
257   {
258          int  first, last;
# Line 294 | Line 290 | register RAY  *r
290   }
291  
292  
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        } else if (srcp->sflags & SFLAT) {
386                FVECT   sd;
387                RREAL   sd0m, sd1m;
388                sd[0] = -DOT(r->rdir, srcp->obscache->p.f.u);
389                sd[1] = -DOT(r->rdir, srcp->obscache->p.f.v);
390                sd[2] = -DOT(r->rdir, srcp->snorm);
391                if (sd[2] < 0)
392                        return &noobs;  /* shouldn't happen */
393                sd0m = ABS(sd[0]);
394                sd1m = ABS(sd[1]);
395                if (sd[2] >= sd0m && sd[2] >= sd1m) {
396                        ondx = SHADCACHE*(int)(SHADCACHE*(.5-FTINY) *
397                                        (1. + sd[0]/sd[2]));
398                        ondx += (int)(SHADCACHE*(.5-FTINY) *
399                                        (1. + sd[1]/sd[2]));
400                } else if (sd0m >= sd1m) {
401                        ondx = SHADCACHE*SHADCACHE;
402                        if (sd[0] < 0)
403                                ondx += ((SHADCACHE+1)>>1)*SHADCACHE;
404                        ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) *
405                                        sd[2]/sd0m);
406                        ondx += (int)(SHADCACHE*(.5-FTINY) *
407                                        (1. + sd[1]/sd0m));
408                } else /* sd1m > sd0m */ {
409                        ondx = SHADCACHE*SHADCACHE +
410                                        ((SHADCACHE+1)>>1)*SHADCACHE*2;
411                        if (sd[1] < 0)
412                                ondx += ((SHADCACHE+1)>>1)*SHADCACHE;
413                        ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) *
414                                        sd[2]/sd1m);
415                        ondx += (int)(SHADCACHE*(.5-FTINY) *
416                                        (1. + sd[0]/sd1m));
417                }
418        } else /* spherical distribution */ {
419                int     ax, ax1, ax2;
420                RREAL   amax = 0;
421                for (ax1 = 3; ax1--; )
422                        if (ABS(r->rdir[ax1]) > amax) {
423                                amax = ABS(r->rdir[ax1]);
424                                ax = ax1;
425                        }
426                if ((ax1 = ax+1) >= 3) ax1 -= 3;
427                if ((ax2 = ax+2) >= 3) ax2 -= 3;
428                ondx = 2*SHADCACHE*SHADCACHE * ax;
429                if (r->rdir[ax] < 0)
430                        ondx += SHADCACHE*SHADCACHE;
431                ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) *
432                                        (1. + r->rdir[ax1]/amax));
433                ondx += (int)(SHADCACHE*(.5-FTINY) *
434                                (1. + r->rdir[ax2]/amax));
435        }
436                                        /* return cache pointer */
437        return(&srcp->obscache->obs[ondx]);
438 }
439
440
441 void                            /* free obstruction cache */
442 freeobscache(SRCREC *srcp)
443 {
444        if (srcp->obscache == NULL)
445                return;
446        free((void *)srcp->obscache);
447        srcp->obscache = NULL;
448 }
449
450        
451 void                            /* record a source blocker */
452 srcblocker(register RAY *r)
453 {
454        OBJREC  *m;
455
456        if (r->robj == OVOID || objptr(r->robj) != r->ro ||
457                        isvolume(r->ro->otype))
458                return;                 /* don't record complex blockers */
459        m = findmaterial(r->ro);
460        if (m == NULL)
461                return;                 /* no material?! */
462        if (!(ofun[m->otype].flags & T_OPAQUE))
463                return;                 /* material not a reliable blocker */
464
465        *srcobstructp(r) = r->robj;     /* else record obstructor */
466 }
467
468
469 int                             /* check ray against cached blocker */
470 srcblocked(RAY *r)
471 {
472        OBJECT  obs = *srcobstructp(r);
473        OBJREC  *op;
474
475        if (obs == OVOID)
476                return(0);
477        op = objptr(obs);               /* check for intersection */
478        return ((*ofun[op->otype].funp)(op, r));
479 }
480
481 #endif
482
483
293   static int
294   cntcmp(                         /* contribution compare (descending) */
295 < const void *p1,
296 < const void *p2
295 >        const void *p1,
296 >        const void *p2
297   )
298   {
299          register const CNTPTR  *sc1 = (const CNTPTR *)p1;
# Line 498 | Line 307 | const void *p2
307   }
308  
309  
310 < void
310 > extern void
311   direct(                                 /* add direct component */
312 < RAY  *r,                        /* ray that hit surface */
313 < void  (*f)(),                   /* direct component coefficient function */
314 < char  *p                        /* data for f */
312 >        RAY  *r,                        /* ray that hit surface */
313 >        srcdirf_t *f,                   /* direct component coefficient function */
314 >        void  *p                        /* data for f */
315   )
316   {
508        extern void  (*trace)();
317          register int  sn;
318          register CONTRIB  *scp;
319          SRCINDEX  si;
# Line 606 | Line 414 | char  *p                       /* data for f */
414                          if (bright(sr.rcol) <= FTINY) {
415   #if SHADCACHE
416                                  if ((scp <= srccnt || scp[-1].sno != scp->sno)
417 <                                                && (scp >= srccnt+ncnts ||
417 >                                                && (scp >= srccnt+ncnts-1 ||
418                                                      scp[1].sno != scp->sno))
419                                          srcblocker(&sr);
420   #endif
# Line 643 | Line 451 | char  *p                       /* data for f */
451   }
452  
453  
454 < void
454 > extern void
455   srcscatter(                     /* compute source scattering into ray */
456 < register RAY  *r
456 >        register RAY  *r
457   )
458   {
459          int  oldsampndx;
# Line 735 | Line 543 | register RAY  *r
543   */
544  
545   static int
546 < weaksrcmat(int obj)             /* identify material */
546 > weaksrcmat(OBJECT obj)          /* identify material */
547   {
548 <        register OBJREC *o = objptr(obj);
548 >        OBJREC *o = findmaterial(objptr(obj));
549          
550 <        while (!ismaterial(o->otype))   /* find material */
743 <                o = objptr(o->omod);
550 >        if (o == NULL) return 0;
551          return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW));
552   }
553  
# Line 802 | Line 609 | weaksrcmat(int obj)            /* identify material */
609                                  distglow(m, r, raydist(r,PRIMARY)))
610  
611  
612 < int
612 > extern int
613   m_light(                                /* ray hit a light source */
614 < register OBJREC  *m,
615 < register RAY  *r
614 >        register OBJREC  *m,
615 >        register RAY  *r
616   )
617   {
618                                                  /* check for over-counting */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines