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.20 by greg, Sat Dec 9 11:30:19 1995 UTC vs.
Revision 2.47 by greg, Thu Mar 10 22:37:00 2005 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1995 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  source.c - routines dealing with illumination sources.
6   *
7 < *     8/20/85
7 > *  External symbols declared in source.h
8   */
9  
10   #include  "ray.h"
14
15 #include  "octree.h"
16
11   #include  "otypes.h"
12 <
12 > #include  "rtotypes.h"
13   #include  "source.h"
20
14   #include  "random.h"
15  
16   extern double  ssampdist;               /* scatter sampling distance */
17  
18 + #ifndef MAXSSAMP
19 + #define MAXSSAMP        16              /* maximum samples per ray */
20 + #endif
21 +
22   /*
23   * Structures used by direct()
24   */
# Line 42 | 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 < 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 52 | 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))
65 <                        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 101 | 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 113 | 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 121 | Line 147 | memerr:
147   }
148  
149  
150 < srcray(sr, r, si)               /* send a ray to a source, return domega */
151 < register RAY  *sr;              /* returned source ray */
126 < RAY  *r;                        /* ray which hit object */
127 < SRCINDEX  *si;                  /* source sample index */
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;
161 +        }
162 +        if (maxcntr <= 0)
163 +                return;
164 +        free((void *)srccnt);
165 +        srccnt = NULL;
166 +        free((void *)cntord);
167 +        cntord = NULL;
168 +        maxcntr = 0;
169 + }
170 +
171 +
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;
181  
# Line 158 | Line 208 | SRCINDEX  *si;                 /* source sample index */
208   }
209  
210  
211 < srcvalue(r)                     /* punch ray to source and compute value */
212 < 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 198 | Line 250 | nomat:
250   }
251  
252  
253 < sourcehit(r)                    /* check to see if ray hit distant source */
254 < register RAY  *r;
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
271 + )
272 + {
273 +        int  glowsrc = -1;
274 +        int  transrc = -1;
275          int  first, last;
276          register int  i;
277  
# Line 209 | 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 <                for (i = 0; i < 3; i++)
298 <                        r->ron[i] = -r->rdir[i];
299 <                r->rod = 1.0;
300 <                r->rox = NULL;
301 <                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);
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 >        /*
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  
336   static int
337 < cntcmp(sc1, sc2)                        /* contribution compare (descending) */
338 < register CNTPTR  *sc1, *sc2;
337 > cntcmp(                         /* contribution compare (descending) */
338 >        const void *p1,
339 >        const void *p2
340 > )
341   {
342 +        register const CNTPTR  *sc1 = (const CNTPTR *)p1;
343 +        register const CNTPTR  *sc2 = (const CNTPTR *)p2;
344 +
345          if (sc1->brt > sc2->brt)
346                  return(-1);
347          if (sc1->brt < sc2->brt)
# Line 245 | Line 350 | register CNTPTR  *sc1, *sc2;
350   }
351  
352  
353 < direct(r, f, p)                         /* add direct component */
354 < RAY  *r;                        /* ray that hit surface */
355 < int  (*f)();                    /* direct component coefficient function */
356 < char  *p;                       /* data for f */
353 > extern void
354 > direct(                                 /* add direct component */
355 >        RAY  *r,                        /* ray that hit surface */
356 >        srcdirf_t *f,                   /* direct component coefficient function */
357 >        void  *p                        /* data for f */
358 > )
359   {
253        extern int  (*trace)();
360          register int  sn;
361          register CONTRIB  *scp;
362          SRCINDEX  si;
# Line 266 | Line 372 | char  *p;                      /* data for f */
372          for (sn = 0; srcray(&sr, r, &si); sn++) {
373                  if (sn >= maxcntr) {
374                          maxcntr = sn + MAXSPART;
375 <                        srccnt = (CONTRIB *)realloc((char *)srccnt,
375 >                        srccnt = (CONTRIB *)realloc((void *)srccnt,
376                                          maxcntr*sizeof(CONTRIB));
377 <                        cntord = (CNTPTR *)realloc((char *)cntord,
377 >                        cntord = (CNTPTR *)realloc((void *)cntord,
378                                          maxcntr*sizeof(CNTPTR));
379 <                        if (srccnt == NULL | cntord == NULL)
379 >                        if ((srccnt == NULL) | (cntord == NULL))
380                                  error(SYSTEM, "out of memory in direct");
381                  }
382                  cntord[sn].sndx = sn;
# Line 281 | Line 387 | char  *p;                      /* data for f */
387                  cntord[sn].brt = bright(scp->coef);
388                  if (cntord[sn].brt <= 0.0)
389                          continue;
390 + #if SHADCACHE
391 +                                                /* check shadow cache */
392 +                if (si.np == 1 && srcblocked(&sr)) {
393 +                        cntord[sn].brt = 0.0;
394 +                        continue;
395 +                }
396 + #endif
397                  VCOPY(scp->dir, sr.rdir);
398                                                  /* compute potential */
399                  sr.revf = srcvalue;
# Line 328 | Line 441 | char  *p;                      /* data for f */
441                  rayorigin(&sr, r, SHADOW, 1.0);
442                  VCOPY(sr.rdir, scp->dir);
443                  sr.rsrc = scp->sno;
444 <                source[scp->sno].ntests++;      /* keep statistics */
444 >                                                /* keep statistics */
445 >                if (source[scp->sno].ntests++ > 0xfffffff0) {
446 >                        source[scp->sno].ntests >>= 1;
447 >                        source[scp->sno].nhits >>= 1;
448 >                }
449                  if (localhit(&sr, &thescene) &&
450                                  ( sr.ro != source[scp->sno].so ||
451                                  source[scp->sno].sflags & SFOLLOW )) {
452                                                  /* follow entire path */
453 <                        if (!raycont(&sr))
337 <                                objerror(sr.ro, USER, "material not found");
453 >                        raycont(&sr);
454                          rayparticipate(&sr);
455                          if (trace != NULL)
456                                  (*trace)(&sr);  /* trace execution */
457 <                        if (bright(sr.rcol) <= FTINY)
457 >                        if (bright(sr.rcol) <= FTINY) {
458 > #if SHADCACHE
459 >                                if ((scp <= srccnt || scp[-1].sno != scp->sno)
460 >                                                && (scp >= srccnt+ncnts-1 ||
461 >                                                    scp[1].sno != scp->sno))
462 >                                        srcblocker(&sr);
463 > #endif
464                                  continue;       /* missed! */
465 +                        }
466                          copycolor(scp->val, sr.rcol);
467                          multcolor(scp->val, scp->coef);
468                  }
# Line 371 | Line 494 | char  *p;                      /* data for f */
494   }
495  
496  
497 < srcscatter(r)                   /* compute source scattering into ray */
498 < register RAY  *r;
497 > extern void
498 > srcscatter(                     /* compute source scattering into ray */
499 >        register RAY  *r
500 > )
501   {
502          int  oldsampndx;
503          int  nsamps;
504          RAY  sr;
505          SRCINDEX  si;
506 <        double  t, lastt, d;
507 <        COLOR  cumval, ctmp;
506 >        double  t, d;
507 >        double  re, ge, be;
508 >        COLOR  cvext;
509          int  i, j;
510  
511 <        if (r->slights == NULL || r->slights[0] == 0 || r->gecc >= 1.-FTINY)
511 >        if (r->slights == NULL || r->slights[0] == 0
512 >                        || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
513                  return;
514          if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
515                  nsamps = 1;
516 + #if MAXSSAMP
517 +        else if (nsamps > MAXSSAMP)
518 +                nsamps = MAXSSAMP;
519 + #endif
520          oldsampndx = samplendx;
521          samplendx = random()&0x7fff;            /* randomize */
391        initsrcindex(&si);
522          for (i = r->slights[0]; i > 0; i--) {   /* for each source */
523 <                setcolor(cumval, 0., 0., 0.);
394 <                lastt = r->rot;
395 <                for (j = nsamps; j-- > 0; ) {   /* for each sample position */
523 >                for (j = 0; j < nsamps; j++) {  /* for each sample position */
524                          samplendx++;
525                          t = r->rot * (j+frandom())/nsamps;
526 +                                                        /* extinction */
527 +                        re = t*colval(r->cext,RED);
528 +                        ge = t*colval(r->cext,GRN);
529 +                        be = t*colval(r->cext,BLU);
530 +                        setcolor(cvext, re > 92. ? 0. : exp(-re),
531 +                                        ge > 92. ? 0. : exp(-ge),
532 +                                        be > 92. ? 0. : exp(-be));
533 +                        if (intens(cvext) <= FTINY)
534 +                                break;                  /* too far away */
535                          sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
536                          sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
537                          sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
538                          sr.rmax = 0.;
539 <                                                /* sample ray to this source */
540 <                        if (si.sp >= si.np-1 || !srcray(&sr, NULL, &si) ||
541 <                                        sr.rsrc != r->slights[i]) {
542 <                                si.sn = r->slights[i]-1;        /* reset */
543 <                                si.np = 0;
544 <                                if (!srcray(&sr, NULL, &si) ||
545 <                                                sr.rsrc != r->slights[i])
546 <                                        continue;               /* no path */
547 <                        }
539 >                        initsrcindex(&si);      /* sample ray to this source */
540 >                        si.sn = r->slights[i];
541 >                        nopart(&si, &sr);
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 <                        sr.albedo = r->albedo;
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 <                                                        /* compute fall-off */
418 <                        d = lastt - t;
419 <                        setcolor(ctmp,  1.-d*colval(r->cext,RED),
420 <                                        1.-d*colval(r->cext,GRN),
421 <                                        1.-d*colval(r->cext,BLU));
422 <                        multcolor(cumval, ctmp);
423 <                        lastt = t;
559 >                        }
560                          if (r->gecc <= FTINY)           /* compute P(theta) */
561                                  d = 1.;
562                          else {
563                                  d = DOT(r->rdir, sr.rdir);
564 <                                d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d);
565 <                                d = (1. - r->gecc*r->gecc) / (d*d*d);
564 >                                d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
565 >                                d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
566                          }
567                                                          /* other factors */
568 <                        d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps);
568 >                        d *= si.dom * r->rot / (4.*PI*nsamps);
569                          multcolor(sr.rcol, r->cext);
570 +                        multcolor(sr.rcol, r->albedo);
571                          scalecolor(sr.rcol, d);
572 <                        addcolor(cumval, sr.rcol);
572 >                        multcolor(sr.rcol, cvext);
573 >                        addcolor(r->rcol, sr.rcol);     /* add it in */
574                  }
437                                                /* final fall-off */
438                setcolor(ctmp,  1.-lastt*colval(r->cext,RED),
439                                1.-lastt*colval(r->cext,GRN),
440                                1.-lastt*colval(r->cext,BLU));
441                multcolor(cumval, ctmp);
442                addcolor(r->rcol, cumval);      /* sum into ray result */
575          }
576          samplendx = oldsampndx;
577   }
# Line 461 | Line 593 | register RAY  *r;
593   * geometry behind (or inside) an effective radiator.
594   */
595  
596 < static int weaksrcmod(obj) int obj;     /* efficiency booster function */
597 < {register OBJREC *o = objptr(obj);
598 < return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
596 > static int
597 > weaksrcmat(OBJECT obj)          /* identify material */
598 > {
599 >        OBJREC *m = findmaterial(objptr(obj));
600 >        
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) && \
606                                  r->rod > 0.0 && \
607 <                                weaksrcmod(source[r->rsrc].so->omod))
607 >                                weaksrcmat(source[r->rsrc].so->omod))
608  
609   /* wrongsource *
610   *
# Line 523 | Line 660 | return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
660                                  distglow(m, r, raydist(r,PRIMARY)))
661  
662  
663 < m_light(m, r)                   /* ray hit a light source */
664 < register OBJREC  *m;
665 < register RAY  *r;
663 > extern int
664 > m_light(                                /* ray hit a light source */
665 >        register OBJREC  *m,
666 >        register RAY  *r
667 > )
668   {
669                                                  /* check for over-counting */
670          if (badcomponent(m, r))
# Line 535 | Line 674 | register RAY  *r;
674                                                  /* check for passed illum */
675          if (passillum(m, r)) {
676                  if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
677 <                        return(rayshade(r, modifier(m->oargs.sarg[0])));
677 >                        return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0])));
678                  raytrans(r);
679                  return(1);
680          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines