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.17 by greg, Thu Nov 2 17:37:58 1995 UTC vs.
Revision 2.33 by greg, Wed May 14 20:43:46 2003 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 "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 117 | 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 131 | 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 141 | 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 154 | Line 179 | SRCINDEX  *si;                 /* source sample index */
179   }
180  
181  
182 + void
183   srcvalue(r)                     /* punch ray to source and compute value */
184   register RAY  *r;
185   {
# Line 166 | Line 192 | register RAY  *r;
192                          return;
193                  if (!rayshade(r, r->ro->omod))  /* compute contribution */
194                          goto nomat;
195 +                rayparticipate(r);
196                  return;
197          }
198                                          /* compute intersection */
# Line 175 | Line 202 | register RAY  *r;
202                          sp->sa.success++;
203                  if (!rayshade(r, r->ro->omod))  /* compute contribution */
204                          goto nomat;
205 +                rayparticipate(r);
206                  return;
207          }
208                                          /* we missed our mark! */
# Line 192 | Line 220 | nomat:
220   }
221  
222  
223 + int
224   sourcehit(r)                    /* check to see if ray hit distant source */
225   register RAY  *r;
226   {
# Line 217 | 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 239 | 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 260 | 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)
301                                  error(SYSTEM, "out of memory in direct");
# Line 327 | Line 360 | char  *p;                      /* data for f */
360                                  ( sr.ro != source[scp->sno].so ||
361                                  source[scp->sno].sflags & SFOLLOW )) {
362                                                  /* follow entire path */
363 <                        if (!raycont(&sr))
364 <                                objerror(sr.ro, USER, "material not found");
363 >                        raycont(&sr);
364 >                        rayparticipate(&sr);
365                          if (trace != NULL)
366                                  (*trace)(&sr);  /* trace execution */
367                          if (bright(sr.rcol) <= FTINY)
# Line 364 | Line 397 | char  *p;                      /* data for f */
397   }
398  
399  
400 + void
401 + srcscatter(r)                   /* compute source scattering into ray */
402 + register RAY  *r;
403 + {
404 +        int  oldsampndx;
405 +        int  nsamps;
406 +        RAY  sr;
407 +        SRCINDEX  si;
408 +        double  t, d;
409 +        double  re, ge, be;
410 +        COLOR  cvext;
411 +        int  i, j;
412 +
413 +        if (r->slights == NULL || r->slights[0] == 0
414 +                        || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
415 +                return;
416 +        if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
417 +                nsamps = 1;
418 + #if MAXSSAMP
419 +        else if (nsamps > MAXSSAMP)
420 +                nsamps = MAXSSAMP;
421 + #endif
422 +        oldsampndx = samplendx;
423 +        samplendx = random()&0x7fff;            /* randomize */
424 +        for (i = r->slights[0]; i > 0; i--) {   /* for each source */
425 +                for (j = 0; j < nsamps; j++) {  /* for each sample position */
426 +                        samplendx++;
427 +                        t = r->rot * (j+frandom())/nsamps;
428 +                                                        /* extinction */
429 +                        re = t*colval(r->cext,RED);
430 +                        ge = t*colval(r->cext,GRN);
431 +                        be = t*colval(r->cext,BLU);
432 +                        setcolor(cvext, re > 92. ? 0. : exp(-re),
433 +                                        ge > 92. ? 0. : exp(-ge),
434 +                                        be > 92. ? 0. : exp(-be));
435 +                        if (intens(cvext) <= FTINY)
436 +                                break;                  /* too far away */
437 +                        sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
438 +                        sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
439 +                        sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
440 +                        sr.rmax = 0.;
441 +                        initsrcindex(&si);      /* sample ray to this source */
442 +                        si.sn = r->slights[i];
443 +                        nopart(&si, &sr);
444 +                        if (!srcray(&sr, NULL, &si) ||
445 +                                        sr.rsrc != r->slights[i])
446 +                                continue;               /* no path */
447 +                        copycolor(sr.cext, r->cext);
448 +                        copycolor(sr.albedo, r->albedo);
449 +                        sr.gecc = r->gecc;
450 +                        sr.slights = r->slights;
451 +                        rayvalue(&sr);                  /* eval. source ray */
452 +                        if (bright(sr.rcol) <= FTINY)
453 +                                continue;
454 +                        if (r->gecc <= FTINY)           /* compute P(theta) */
455 +                                d = 1.;
456 +                        else {
457 +                                d = DOT(r->rdir, sr.rdir);
458 +                                d = 1. + r->gecc*r->gecc - 2.*r->gecc*d;
459 +                                d = (1. - r->gecc*r->gecc) / (d*sqrt(d));
460 +                        }
461 +                                                        /* other factors */
462 +                        d *= si.dom * r->rot / (4.*PI*nsamps);
463 +                        multcolor(sr.rcol, r->cext);
464 +                        multcolor(sr.rcol, r->albedo);
465 +                        scalecolor(sr.rcol, d);
466 +                        multcolor(sr.rcol, cvext);
467 +                        addcolor(r->rcol, sr.rcol);     /* add it in */
468 +                }
469 +        }
470 +        samplendx = oldsampndx;
471 + }
472 +
473 +
474   /****************************************************************
475   * The following macros were separated from the m_light() routine
476   * because they are very nasty and difficult to understand.
# Line 442 | Line 549 | return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
549                                  distglow(m, r, raydist(r,PRIMARY)))
550  
551  
552 + int
553   m_light(m, r)                   /* ray hit a light source */
554   register OBJREC  *m;
555   register RAY  *r;
# Line 454 | Line 562 | register RAY  *r;
562                                                  /* check for passed illum */
563          if (passillum(m, r)) {
564                  if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
565 <                        return(rayshade(r, modifier(m->oargs.sarg[0])));
565 >                        return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0])));
566                  raytrans(r);
567                  return(1);
568          }
# Line 466 | Line 574 | register RAY  *r;
574          if (srcignore(m, r))
575                  return(1);
576                                                  /* check for outside spot */
577 <        if (m->otype==MAT_SPOT && spotout(r, makespot(m), r->rot>=FHUGE))
577 >        if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
578                  return(1);
579                                                  /* get distribution pattern */
580          raytexture(r, m->omod);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines