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.12 by greg, Wed Aug 18 00:19:05 1993 UTC vs.
Revision 2.35 by schorsch, Sun Jul 27 22:12:03 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 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 "copyright.h"
11 +
12   #include  "ray.h"
13  
15 #include  "octree.h"
16
14   #include  "otypes.h"
15  
16   #include  "source.h"
17  
18 + #include  "random.h"
19 +
20 + extern double  ssampdist;               /* scatter sampling distance */
21 +
22 + #ifndef MAXSSAMP
23 + #define MAXSSAMP        16              /* maximum samples per ray */
24 + #endif
25 +
26   /*
27   * Structures used by direct()
28   */
# Line 39 | Line 44 | static CNTPTR  *cntord;                        /* source ordering in direct
44   static int  maxcntr = 0;                /* size of contribution arrays */
45  
46  
47 + void
48   marksources()                   /* find and mark source objects */
49   {
50          int  foundsource = 0;
# Line 48 | Line 54 | marksources()                  /* find and mark source objects */
54                                          /* initialize dispatch table */
55          initstypes();
56                                          /* find direct sources */
57 <        for (i = 0; i < nobjects; i++) {
57 >        for (i = 0; i < nsceneobjs; i++) {
58          
59                  o = objptr(i);
60  
# Line 68 | Line 74 | marksources()                  /* find and mark source objects */
74                                  o->otype != OBJ_SOURCE &&
75                                  m->oargs.farg[3] <= FTINY)
76                          continue;                       /* don't bother */
77 +                if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY &&
78 +                                m->oargs.farg[2] <= FTINY)
79 +                        continue;                       /* don't bother */
80  
81                  if (sfun[o->otype].of == NULL ||
82                                  sfun[o->otype].of->setsrc == NULL)
# Line 106 | Line 115 | marksources()                  /* find and mark source objects */
115          maxcntr = nsources + MAXSPART;  /* start with this many */
116          srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
117          cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR));
118 <        if (srccnt == NULL | cntord == NULL)
118 >        if ((srccnt == NULL) | (cntord == NULL))
119                  goto memerr;
120          return;
121   memerr:
# Line 114 | Line 123 | memerr:
123   }
124  
125  
126 + void
127 + freesources()                   /* free all source structures */
128 + {
129 +        if (nsources > 0) {
130 +                free((void *)source);
131 +                source = NULL;
132 +                nsources = 0;
133 +        }
134 +        if (maxcntr <= 0)
135 +                return;
136 +        free((void *)srccnt);
137 +        srccnt = NULL;
138 +        free((void *)cntord);
139 +        cntord = NULL;
140 +        maxcntr = 0;
141 + }
142 +
143 +
144 + int
145   srcray(sr, r, si)               /* send a ray to a source, return domega */
146   register RAY  *sr;              /* returned source ray */
147   RAY  *r;                        /* ray which hit object */
# Line 128 | Line 156 | SRCINDEX  *si;                 /* source sample index */
156          sr->rsrc = si->sn;                      /* remember source */
157          srcp = source + si->sn;
158          if (srcp->sflags & SDISTANT) {
159 <                if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s, 1))
159 >                if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
160                          continue;
161                  return(1);              /* sample OK */
162          }
# Line 138 | Line 166 | SRCINDEX  *si;                 /* source sample index */
166                  continue;
167                                                  /* check angle */
168          if (srcp->sflags & SSPOT) {
169 <                if (spotout(sr, srcp->sl.s, 0))
169 >                if (spotout(sr, srcp->sl.s))
170                          continue;
171                                          /* adjust solid angle */
172                  si->dom *= d*d;
# Line 151 | Line 179 | SRCINDEX  *si;                 /* source sample index */
179   }
180  
181  
182 + void
183   srcvalue(r)                     /* punch ray to source and compute value */
184 < RAY  *r;
184 > register RAY  *r;
185   {
186          register SRCREC  *sp;
187  
# Line 161 | Line 190 | RAY  *r;
190                                          /* check intersection */
191                  if (!(*ofun[sp->so->otype].funp)(sp->so, r))
192                          return;
193 <                raycont(r);             /* compute contribution */
193 >                if (!rayshade(r, r->ro->omod))  /* compute contribution */
194 >                        goto nomat;
195 >                rayparticipate(r);
196                  return;
197          }
198                                          /* compute intersection */
# Line 169 | Line 200 | RAY  *r;
200                          (*ofun[sp->so->otype].funp)(sp->so, r)) {
201                  if (sp->sa.success >= 0)
202                          sp->sa.success++;
203 <                raycont(r);             /* compute contribution */
203 >                if (!rayshade(r, r->ro->omod))  /* compute contribution */
204 >                        goto nomat;
205 >                rayparticipate(r);
206                  return;
207          }
208 +                                        /* we missed our mark! */
209          if (sp->sa.success < 0)
210                  return;                 /* bitched already */
211          sp->sa.success -= AIMREQT;
# Line 180 | Line 214 | RAY  *r;
214          sprintf(errmsg, "aiming failure for light source \"%s\"",
215                          sp->so->oname);
216          error(WARNING, errmsg);         /* issue warning */
217 +        return;
218 + nomat:
219 +        objerror(r->ro, USER, "material not found");
220   }
221  
222  
223 + int
224   sourcehit(r)                    /* check to see if ray hit distant source */
225   register RAY  *r;
226   {
# Line 208 | Line 246 | register RAY  *r;
246                          }
247  
248          if (r->ro != NULL) {
249 +                r->robj = objndx(r->ro);
250                  for (i = 0; i < 3; i++)
251                          r->ron[i] = -r->rdir[i];
252                  r->rod = 1.0;
253 +                r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
254 +                r->uv[0] = r->uv[1] = 0.0;
255                  r->rox = NULL;
256                  return(1);
257          }
# Line 230 | Line 271 | register CNTPTR  *sc1, *sc2;
271   }
272  
273  
274 + void
275   direct(r, f, p)                         /* add direct component */
276   RAY  *r;                        /* ray that hit surface */
277 < int  (*f)();                    /* direct component coefficient function */
277 > void  (*f)();                   /* direct component coefficient function */
278   char  *p;                       /* data for f */
279   {
280 <        extern int  (*trace)();
280 >        extern void  (*trace)();
281          register int  sn;
282          register CONTRIB  *scp;
283          SRCINDEX  si;
# Line 251 | Line 293 | char  *p;                      /* data for f */
293          for (sn = 0; srcray(&sr, r, &si); sn++) {
294                  if (sn >= maxcntr) {
295                          maxcntr = sn + MAXSPART;
296 <                        srccnt = (CONTRIB *)realloc((char *)srccnt,
296 >                        srccnt = (CONTRIB *)realloc((void *)srccnt,
297                                          maxcntr*sizeof(CONTRIB));
298 <                        cntord = (CNTPTR *)realloc((char *)cntord,
298 >                        cntord = (CNTPTR *)realloc((void *)cntord,
299                                          maxcntr*sizeof(CNTPTR));
300 <                        if (srccnt == NULL | cntord == NULL)
300 >                        if ((srccnt == NULL) | (cntord == NULL))
301                                  error(SYSTEM, "out of memory in direct");
302                  }
303                  cntord[sn].sndx = sn;
# Line 299 | Line 341 | char  *p;                      /* data for f */
341                                                  /* modify threshold */
342          ourthresh = shadthresh / r->rweight;
343                                                  /* test for shadows */
344 <        nhits = 0;
345 <        for (sn = 0; sn < ncnts; sn++) {
344 >        for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts;
345 >                        hwt += (double)source[scp->sno].nhits /
346 >                                (double)source[scp->sno].ntests,
347 >                        sn++) {
348                                                  /* check threshold */
349                  if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
350                                  cntord[sn].brt-cntord[sn+nshadcheck].brt)
# Line 311 | Line 355 | char  *p;                      /* data for f */
355                  rayorigin(&sr, r, SHADOW, 1.0);
356                  VCOPY(sr.rdir, scp->dir);
357                  sr.rsrc = scp->sno;
358 <                source[scp->sno].ntests++;      /* keep statistics */
358 >                                                /* keep statistics */
359 >                if (source[scp->sno].ntests++ > 0xfffffff0) {
360 >                        source[scp->sno].ntests >>= 1;
361 >                        source[scp->sno].nhits >>= 1;
362 >                }
363                  if (localhit(&sr, &thescene) &&
364                                  ( sr.ro != source[scp->sno].so ||
365                                  source[scp->sno].sflags & SFOLLOW )) {
366                                                  /* follow entire path */
367                          raycont(&sr);
368 +                        rayparticipate(&sr);
369                          if (trace != NULL)
370                                  (*trace)(&sr);  /* trace execution */
371                          if (bright(sr.rcol) <= FTINY)
# Line 329 | Line 378 | char  *p;                      /* data for f */
378                  nhits++;
379                  source[scp->sno].nhits++;
380          }
381 <                                        /* surface hit rate */
382 <        if (sn > 0)
383 <                hwt = (double)nhits / (double)sn;
381 >                                        /* source hit rate */
382 >        if (hwt > FTINY)
383 >                hwt = (double)nhits / hwt;
384          else
385                  hwt = 0.5;
386   #ifdef DEBUG
387 <        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
387 >        sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n",
388                          sn, ncnts-sn, hwt);
389          eputs(errmsg);
390   #endif
# Line 344 | Line 393 | char  *p;                      /* data for f */
393                  scp = srccnt + cntord[sn].sndx;
394                  prob = hwt * (double)source[scp->sno].nhits /
395                                  (double)source[scp->sno].ntests;
396 +                if (prob > 1.0)
397 +                        prob = 1.0;
398                  scalecolor(scp->val, prob);
399                  addcolor(r->rcol, scp->val);
400          }
401   }
402  
403  
404 + void
405 + srcscatter(r)                   /* compute source scattering into ray */
406 + register RAY  *r;
407 + {
408 +        int  oldsampndx;
409 +        int  nsamps;
410 +        RAY  sr;
411 +        SRCINDEX  si;
412 +        double  t, d;
413 +        double  re, ge, be;
414 +        COLOR  cvext;
415 +        int  i, j;
416 +
417 +        if (r->slights == NULL || r->slights[0] == 0
418 +                        || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
419 +                return;
420 +        if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
421 +                nsamps = 1;
422 + #if MAXSSAMP
423 +        else if (nsamps > MAXSSAMP)
424 +                nsamps = MAXSSAMP;
425 + #endif
426 +        oldsampndx = samplendx;
427 +        samplendx = random()&0x7fff;            /* randomize */
428 +        for (i = r->slights[0]; i > 0; i--) {   /* for each source */
429 +                for (j = 0; j < nsamps; j++) {  /* for each sample position */
430 +                        samplendx++;
431 +                        t = r->rot * (j+frandom())/nsamps;
432 +                                                        /* extinction */
433 +                        re = t*colval(r->cext,RED);
434 +                        ge = t*colval(r->cext,GRN);
435 +                        be = t*colval(r->cext,BLU);
436 +                        setcolor(cvext, re > 92. ? 0. : exp(-re),
437 +                                        ge > 92. ? 0. : exp(-ge),
438 +                                        be > 92. ? 0. : exp(-be));
439 +                        if (intens(cvext) <= FTINY)
440 +                                break;                  /* too far away */
441 +                        sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
442 +                        sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
443 +                        sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
444 +                        sr.rmax = 0.;
445 +                        initsrcindex(&si);      /* sample ray to this source */
446 +                        si.sn = r->slights[i];
447 +                        nopart(&si, &sr);
448 +                        if (!srcray(&sr, NULL, &si) ||
449 +                                        sr.rsrc != r->slights[i])
450 +                                continue;               /* no path */
451 +                        copycolor(sr.cext, r->cext);
452 +                        copycolor(sr.albedo, r->albedo);
453 +                        sr.gecc = r->gecc;
454 +                        sr.slights = r->slights;
455 +                        rayvalue(&sr);                  /* eval. source ray */
456 +                        if (bright(sr.rcol) <= FTINY)
457 +                                continue;
458 +                        if (r->gecc <= FTINY)           /* compute P(theta) */
459 +                                d = 1.;
460 +                        else {
461 +                                d = DOT(r->rdir, sr.rdir);
462 +                                d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
463 +                                d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
464 +                        }
465 +                                                        /* other factors */
466 +                        d *= si.dom * r->rot / (4.*PI*nsamps);
467 +                        multcolor(sr.rcol, r->cext);
468 +                        multcolor(sr.rcol, r->albedo);
469 +                        scalecolor(sr.rcol, d);
470 +                        multcolor(sr.rcol, cvext);
471 +                        addcolor(r->rcol, sr.rcol);     /* add it in */
472 +                }
473 +        }
474 +        samplendx = oldsampndx;
475 + }
476 +
477 +
478   /****************************************************************
479   * The following macros were separated from the m_light() routine
480   * because they are very nasty and difficult to understand.
# Line 368 | Line 493 | char  *p;                      /* data for f */
493  
494   static int weaksrcmod(obj) int obj;     /* efficiency booster function */
495   {register OBJREC *o = objptr(obj);
496 < return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
496 > return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW));}
497  
498   #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \
499                                  r->rod > 0.0 && \
# Line 391 | Line 516 | return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
516   * (Glows with negative radii should NEVER participate in illumination.)
517   */
518  
519 < #define  distglow(m, r)         (m->otype==MAT_GLOW && \
519 > #define  distglow(m, r, d)      (m->otype==MAT_GLOW && \
520                                  m->oargs.farg[3] >= -FTINY && \
521 <                                r->rot > m->oargs.farg[3])
521 >                                d > m->oargs.farg[3])
522  
523   /* badcomponent *
524   *
# Line 406 | Line 531 | return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
531  
532   #define  badcomponent(m, r)     (r->crtype&(AMBIENT|SPECULAR) && \
533                                  !(r->crtype&SHADOW || r->rod < 0.0 || \
534 <                                        distglow(m, r)))
534 >                /* not 100% correct */  distglow(m, r, r->rot)))
535  
536   /* passillum *
537   *
# Line 424 | Line 549 | return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
549   * The -dv flag is normally on for sources to be visible.
550   */
551  
552 < #define  srcignore(m, r)        (!directvis && !(r->crtype&SHADOW) && \
553 <                                !distglow(m, r))
552 > #define  srcignore(m, r)        !(directvis || r->crtype&SHADOW || \
553 >                                distglow(m, r, raydist(r,PRIMARY)))
554  
555  
556 + int
557   m_light(m, r)                   /* ray hit a light source */
558   register OBJREC  *m;
559   register RAY  *r;
560   {
561                                                  /* check for over-counting */
562          if (badcomponent(m, r))
563 <                return;
564 <        if (wrongsource(m,r))
565 <                return;
563 >                return(1);
564 >        if (wrongsource(m, r))
565 >                return(1);
566                                                  /* check for passed illum */
567          if (passillum(m, r)) {
568 <                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
569 <                        raytrans(r);
570 <                else
571 <                        rayshade(r, modifier(m->oargs.sarg[0]));
446 <                return;
568 >                if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
569 >                        return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0])));
570 >                raytrans(r);
571 >                return(1);
572          }
573                                          /* otherwise treat as source */
574                                                  /* check for behind */
575          if (r->rod < 0.0)
576 <                return;
576 >                return(1);
577                                                  /* check for invisibility */
578          if (srcignore(m, r))
579 <                return;
579 >                return(1);
580                                                  /* check for outside spot */
581 <        if (m->otype==MAT_SPOT && spotout(r, makespot(m), r->rot>=FHUGE))
582 <                return;
581 >        if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
582 >                return(1);
583                                                  /* get distribution pattern */
584          raytexture(r, m->omod);
585                                                  /* get source color */
# Line 463 | Line 588 | register RAY  *r;
588                            m->oargs.farg[2]);
589                                                  /* modify value */
590          multcolor(r->rcol, r->pcol);
591 +        return(1);
592   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines