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.43 by greg, Mon Mar 1 18:11:20 2004 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"
11  
15 #include  "octree.h"
16
12   #include  "otypes.h"
13  
14   #include  "source.h"
# Line 22 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17  
18   extern double  ssampdist;               /* scatter sampling distance */
19  
20 + #ifndef MAXSSAMP
21 + #define MAXSSAMP        16              /* maximum samples per ray */
22 + #endif
23 +
24   /*
25   * Structures used by direct()
26   */
# Line 43 | Line 42 | static CNTPTR  *cntord;                        /* source ordering in direct
42   static int  maxcntr = 0;                /* size of contribution arrays */
43  
44  
45 + 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 + void
70   marksources()                   /* find and mark source objects */
71   {
72          int  foundsource = 0;
# 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 +                source[ns].obscache = NULL;
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 > void
151 > freesources()                   /* 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 + 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 > 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 > 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 223 | Line 277 | register RAY  *r;
277                          }
278  
279          if (r->ro != NULL) {
280 +                r->robj = objndx(r->ro);
281                  for (i = 0; i < 3; i++)
282                          r->ron[i] = -r->rdir[i];
283                  r->rod = 1.0;
284 +                r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
285 +                r->uv[0] = r->uv[1] = 0.0;
286                  r->rox = NULL;
287                  return(1);
288          }
# Line 234 | 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 245 | Line 307 | register CNTPTR  *sc1, *sc2;
307   }
308  
309  
310 < direct(r, f, p)                         /* add direct component */
311 < RAY  *r;                        /* ray that hit surface */
312 < int  (*f)();                    /* direct component coefficient function */
313 < char  *p;                       /* data for f */
310 > 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 */
315 > )
316   {
317 <        extern int  (*trace)();
317 >        extern void  (*trace)();
318          register int  sn;
319          register CONTRIB  *scp;
320          SRCINDEX  si;
# Line 266 | Line 330 | char  *p;                      /* data for f */
330          for (sn = 0; srcray(&sr, r, &si); sn++) {
331                  if (sn >= maxcntr) {
332                          maxcntr = sn + MAXSPART;
333 <                        srccnt = (CONTRIB *)realloc((char *)srccnt,
333 >                        srccnt = (CONTRIB *)realloc((void *)srccnt,
334                                          maxcntr*sizeof(CONTRIB));
335 <                        cntord = (CNTPTR *)realloc((char *)cntord,
335 >                        cntord = (CNTPTR *)realloc((void *)cntord,
336                                          maxcntr*sizeof(CNTPTR));
337 <                        if (srccnt == NULL | cntord == NULL)
337 >                        if ((srccnt == NULL) | (cntord == NULL))
338                                  error(SYSTEM, "out of memory in direct");
339                  }
340                  cntord[sn].sndx = sn;
# Line 281 | Line 345 | char  *p;                      /* data for f */
345                  cntord[sn].brt = bright(scp->coef);
346                  if (cntord[sn].brt <= 0.0)
347                          continue;
348 + #if SHADCACHE
349 +                                                /* check shadow cache */
350 +                if (si.np == 1 && srcblocked(&sr)) {
351 +                        cntord[sn].brt = 0.0;
352 +                        continue;
353 +                }
354 + #endif
355                  VCOPY(scp->dir, sr.rdir);
356                                                  /* compute potential */
357                  sr.revf = srcvalue;
# Line 328 | Line 399 | char  *p;                      /* data for f */
399                  rayorigin(&sr, r, SHADOW, 1.0);
400                  VCOPY(sr.rdir, scp->dir);
401                  sr.rsrc = scp->sno;
402 <                source[scp->sno].ntests++;      /* keep statistics */
402 >                                                /* keep statistics */
403 >                if (source[scp->sno].ntests++ > 0xfffffff0) {
404 >                        source[scp->sno].ntests >>= 1;
405 >                        source[scp->sno].nhits >>= 1;
406 >                }
407                  if (localhit(&sr, &thescene) &&
408                                  ( sr.ro != source[scp->sno].so ||
409                                  source[scp->sno].sflags & SFOLLOW )) {
410                                                  /* follow entire path */
411 <                        if (!raycont(&sr))
337 <                                objerror(sr.ro, USER, "material not found");
411 >                        raycont(&sr);
412                          rayparticipate(&sr);
413                          if (trace != NULL)
414                                  (*trace)(&sr);  /* trace execution */
415 <                        if (bright(sr.rcol) <= FTINY)
415 >                        if (bright(sr.rcol) <= FTINY) {
416 > #if SHADCACHE
417 >                                if ((scp <= srccnt || scp[-1].sno != scp->sno)
418 >                                                && (scp >= srccnt+ncnts-1 ||
419 >                                                    scp[1].sno != scp->sno))
420 >                                        srcblocker(&sr);
421 > #endif
422                                  continue;       /* missed! */
423 +                        }
424                          copycolor(scp->val, sr.rcol);
425                          multcolor(scp->val, scp->coef);
426                  }
# Line 371 | Line 452 | char  *p;                      /* data for f */
452   }
453  
454  
455 < srcscatter(r)                   /* compute source scattering into ray */
456 < register RAY  *r;
455 > void
456 > srcscatter(                     /* compute source scattering into ray */
457 > register RAY  *r
458 > )
459   {
460          int  oldsampndx;
461          int  nsamps;
462          RAY  sr;
463          SRCINDEX  si;
464 <        double  t, lastt, d;
465 <        COLOR  cumval, ctmp;
464 >        double  t, d;
465 >        double  re, ge, be;
466 >        COLOR  cvext;
467          int  i, j;
468  
469 <        if (r->slights == NULL || r->slights[0] == 0 || r->gecc >= 1.-FTINY)
469 >        if (r->slights == NULL || r->slights[0] == 0
470 >                        || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
471                  return;
472          if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
473                  nsamps = 1;
474 + #if MAXSSAMP
475 +        else if (nsamps > MAXSSAMP)
476 +                nsamps = MAXSSAMP;
477 + #endif
478          oldsampndx = samplendx;
479          samplendx = random()&0x7fff;            /* randomize */
391        initsrcindex(&si);
480          for (i = r->slights[0]; i > 0; i--) {   /* for each source */
481 <                setcolor(cumval, 0., 0., 0.);
394 <                lastt = r->rot;
395 <                for (j = nsamps; j-- > 0; ) {   /* for each sample position */
481 >                for (j = 0; j < nsamps; j++) {  /* for each sample position */
482                          samplendx++;
483                          t = r->rot * (j+frandom())/nsamps;
484 +                                                        /* extinction */
485 +                        re = t*colval(r->cext,RED);
486 +                        ge = t*colval(r->cext,GRN);
487 +                        be = t*colval(r->cext,BLU);
488 +                        setcolor(cvext, re > 92. ? 0. : exp(-re),
489 +                                        ge > 92. ? 0. : exp(-ge),
490 +                                        be > 92. ? 0. : exp(-be));
491 +                        if (intens(cvext) <= FTINY)
492 +                                break;                  /* too far away */
493                          sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
494                          sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
495                          sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
496                          sr.rmax = 0.;
497 <                                                /* sample ray to this source */
498 <                        if (si.sp >= si.np-1 || !srcray(&sr, NULL, &si) ||
499 <                                        sr.rsrc != r->slights[i]) {
500 <                                si.sn = r->slights[i]-1;        /* reset */
501 <                                si.np = 0;
502 <                                if (!srcray(&sr, NULL, &si) ||
408 <                                                sr.rsrc != r->slights[i])
409 <                                        continue;               /* no path */
410 <                        }
497 >                        initsrcindex(&si);      /* sample ray to this source */
498 >                        si.sn = r->slights[i];
499 >                        nopart(&si, &sr);
500 >                        if (!srcray(&sr, NULL, &si) ||
501 >                                        sr.rsrc != r->slights[i])
502 >                                continue;               /* no path */
503                          copycolor(sr.cext, r->cext);
504 <                        sr.albedo = r->albedo;
504 >                        copycolor(sr.albedo, r->albedo);
505                          sr.gecc = r->gecc;
506 +                        sr.slights = r->slights;
507                          rayvalue(&sr);                  /* eval. source ray */
508                          if (bright(sr.rcol) <= FTINY)
509                                  continue;
417                                                        /* 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;
510                          if (r->gecc <= FTINY)           /* compute P(theta) */
511                                  d = 1.;
512                          else {
513                                  d = DOT(r->rdir, sr.rdir);
514 <                                d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d);
515 <                                d = (1. - r->gecc*r->gecc) / (d*d*d);
514 >                                d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
515 >                                d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
516                          }
517                                                          /* other factors */
518 <                        d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps);
518 >                        d *= si.dom * r->rot / (4.*PI*nsamps);
519                          multcolor(sr.rcol, r->cext);
520 +                        multcolor(sr.rcol, r->albedo);
521                          scalecolor(sr.rcol, d);
522 <                        addcolor(cumval, sr.rcol);
522 >                        multcolor(sr.rcol, cvext);
523 >                        addcolor(r->rcol, sr.rcol);     /* add it in */
524                  }
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 */
525          }
526          samplendx = oldsampndx;
527   }
# Line 461 | Line 543 | register RAY  *r;
543   * geometry behind (or inside) an effective radiator.
544   */
545  
546 < static int weaksrcmod(obj) int obj;     /* efficiency booster function */
547 < {register OBJREC *o = objptr(obj);
548 < return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
546 > static int
547 > weaksrcmat(OBJECT obj)          /* identify material */
548 > {
549 >        OBJREC *o = findmaterial(objptr(obj));
550 >        
551 >        if (o == NULL) return 0;
552 >        return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW));
553 > }
554  
555   #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \
556                                  r->rod > 0.0 && \
557 <                                weaksrcmod(source[r->rsrc].so->omod))
557 >                                weaksrcmat(source[r->rsrc].so->omod))
558  
559   /* wrongsource *
560   *
# Line 523 | Line 610 | return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
610                                  distglow(m, r, raydist(r,PRIMARY)))
611  
612  
613 < m_light(m, r)                   /* ray hit a light source */
614 < register OBJREC  *m;
615 < register RAY  *r;
613 > int
614 > m_light(                                /* ray hit a light source */
615 > register OBJREC  *m,
616 > register RAY  *r
617 > )
618   {
619                                                  /* check for over-counting */
620          if (badcomponent(m, r))
# Line 535 | Line 624 | register RAY  *r;
624                                                  /* check for passed illum */
625          if (passillum(m, r)) {
626                  if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
627 <                        return(rayshade(r, modifier(m->oargs.sarg[0])));
627 >                        return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0])));
628                  raytrans(r);
629                  return(1);
630          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines