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.1 by greg, Tue Nov 12 17:08:57 1991 UTC vs.
Revision 2.24 by greg, Wed Apr 17 14:02:18 1996 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "source.h"
20  
21 + #include  "random.h"
22 +
23 + extern double  ssampdist;               /* scatter sampling distance */
24 +
25 + #ifndef MAXSSAMP
26 + #define MAXSSAMP        16              /* maximum samples per ray */
27 + #endif
28 +
29   /*
30   * Structures used by direct()
31   */
# Line 41 | Line 49 | static int  maxcntr = 0;               /* size of contribution arra
49  
50   marksources()                   /* find and mark source objects */
51   {
52 +        int  foundsource = 0;
53          int  i;
54          register OBJREC  *o, *m;
55          register int  ns;
# Line 67 | Line 76 | marksources()                  /* find and mark source objects */
76                                  o->otype != OBJ_SOURCE &&
77                                  m->oargs.farg[3] <= FTINY)
78                          continue;                       /* don't bother */
79 +                if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY &&
80 +                                m->oargs.farg[2] <= FTINY)
81 +                        continue;                       /* don't bother */
82  
83                  if (sfun[o->otype].of == NULL ||
84                                  sfun[o->otype].of->setsrc == NULL)
# Line 80 | Line 92 | marksources()                  /* find and mark source objects */
92                  if (m->otype == MAT_GLOW) {
93                          source[ns].sflags |= SPROX;
94                          source[ns].sl.prox = m->oargs.farg[3];
95 <                        if (o->otype == OBJ_SOURCE)
95 >                        if (source[ns].sflags & SDISTANT)
96                                  source[ns].sflags |= SSKIP;
97                  } else if (m->otype == MAT_SPOT) {
98                          source[ns].sflags |= SSPOT;
# Line 93 | Line 105 | marksources()                  /* find and mark source objects */
105                                  source[ns].sflags |= SSKIP;
106                          }
107                  }
108 +                if (!(source[ns].sflags & SSKIP))
109 +                        foundsource++;
110          }
111 <        if (nsources <= 0) {
111 >        if (!foundsource) {
112                  error(WARNING, "no light sources found");
113                  return;
114          }
# Line 117 | Line 131 | RAY  *r;                       /* ray which hit object */
131   SRCINDEX  *si;                  /* source sample index */
132   {
133      double  d;                          /* distance to source */
120    FVECT  vd;
134      register SRCREC  *srcp;
122    register int  i;
135  
136      rayorigin(sr, r, SHADOW, 1.0);              /* ignore limits */
137  
# Line 127 | Line 139 | SRCINDEX  *si;                 /* source sample index */
139          sr->rsrc = si->sn;                      /* remember source */
140          srcp = source + si->sn;
141          if (srcp->sflags & SDISTANT) {
142 <                if (srcp->sflags & SSPOT) {     /* check location */
143 <                        for (i = 0; i < 3; i++)
132 <                            vd[i] = srcp->sl.s->aim[i] - sr->rorg[i];
133 <                        d = DOT(sr->rdir,vd);
134 <                        if (d <= FTINY)
135 <                                continue;
136 <                        d = DOT(vd,vd) - d*d;
137 <                        if (PI*d > srcp->sl.s->siz)
138 <                                continue;
139 <                }
142 >                if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
143 >                        continue;
144                  return(1);              /* sample OK */
145          }
146                                  /* local source */
# Line 145 | Line 149 | SRCINDEX  *si;                 /* source sample index */
149                  continue;
150                                                  /* check angle */
151          if (srcp->sflags & SSPOT) {
152 <                if (srcp->sl.s->siz < 2.0*PI *
149 <                                (1.0 + DOT(srcp->sl.s->aim,sr->rdir)))
152 >                if (spotout(sr, srcp->sl.s))
153                          continue;
154                                          /* adjust solid angle */
155                  si->dom *= d*d;
# Line 160 | Line 163 | SRCINDEX  *si;                 /* source sample index */
163  
164  
165   srcvalue(r)                     /* punch ray to source and compute value */
166 < RAY  *r;
166 > register RAY  *r;
167   {
168          register SRCREC  *sp;
169  
# Line 169 | Line 172 | RAY  *r;
172                                          /* check intersection */
173                  if (!(*ofun[sp->so->otype].funp)(sp->so, r))
174                          return;
175 <                raycont(r);             /* compute contribution */
175 >                if (!rayshade(r, r->ro->omod))  /* compute contribution */
176 >                        goto nomat;
177 >                rayparticipate(r);
178                  return;
179          }
180                                          /* compute intersection */
# Line 177 | Line 182 | RAY  *r;
182                          (*ofun[sp->so->otype].funp)(sp->so, r)) {
183                  if (sp->sa.success >= 0)
184                          sp->sa.success++;
185 <                raycont(r);             /* compute contribution */
185 >                if (!rayshade(r, r->ro->omod))  /* compute contribution */
186 >                        goto nomat;
187 >                rayparticipate(r);
188                  return;
189          }
190 +                                        /* we missed our mark! */
191          if (sp->sa.success < 0)
192                  return;                 /* bitched already */
193          sp->sa.success -= AIMREQT;
# Line 188 | Line 196 | RAY  *r;
196          sprintf(errmsg, "aiming failure for light source \"%s\"",
197                          sp->so->oname);
198          error(WARNING, errmsg);         /* issue warning */
199 +        return;
200 + nomat:
201 +        objerror(r->ro, USER, "material not found");
202   }
203  
204  
205 + sourcehit(r)                    /* check to see if ray hit distant source */
206 + register RAY  *r;
207 + {
208 +        int  first, last;
209 +        register int  i;
210 +
211 +        if (r->rsrc >= 0) {             /* check only one if aimed */
212 +                first = last = r->rsrc;
213 +        } else {                        /* otherwise check all */
214 +                first = 0; last = nsources-1;
215 +        }
216 +        for (i = first; i <= last; i++)
217 +                if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT)
218 +                        /*
219 +                         * Check to see if ray is within
220 +                         * solid angle of source.
221 +                         */
222 +                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
223 +                                        <= source[i].ss2) {
224 +                                r->ro = source[i].so;
225 +                                if (!(source[i].sflags & SSKIP))
226 +                                        break;
227 +                        }
228 +
229 +        if (r->ro != NULL) {
230 +                for (i = 0; i < 3; i++)
231 +                        r->ron[i] = -r->rdir[i];
232 +                r->rod = 1.0;
233 +                r->rox = NULL;
234 +                return(1);
235 +        }
236 +        return(0);
237 + }
238 +
239 +
240   static int
241   cntcmp(sc1, sc2)                        /* contribution compare (descending) */
242   register CNTPTR  *sc1, *sc2;
# Line 209 | Line 255 | int  (*f)();                   /* direct component coefficient functio
255   char  *p;                       /* data for f */
256   {
257          extern int  (*trace)();
212        extern double  pow();
258          register int  sn;
259 +        register CONTRIB  *scp;
260          SRCINDEX  si;
261          int  nshadcheck, ncnts;
262          int  nhits;
# Line 232 | Line 278 | char  *p;                      /* data for f */
278                                  error(SYSTEM, "out of memory in direct");
279                  }
280                  cntord[sn].sndx = sn;
281 <                srccnt[sn].sno = sr.rsrc;
281 >                scp = srccnt + sn;
282 >                scp->sno = sr.rsrc;
283                                                  /* compute coefficient */
284 <                (*f)(srccnt[sn].coef, p, sr.rdir, si.dom);
285 <                cntord[sn].brt = bright(srccnt[sn].coef);
284 >                (*f)(scp->coef, p, sr.rdir, si.dom);
285 >                cntord[sn].brt = bright(scp->coef);
286                  if (cntord[sn].brt <= 0.0)
287                          continue;
288 <                VCOPY(srccnt[sn].dir, sr.rdir);
288 >                VCOPY(scp->dir, sr.rdir);
289                                                  /* compute potential */
290                  sr.revf = srcvalue;
291                  rayvalue(&sr);
292 <                copycolor(srccnt[sn].val, sr.rcol);
293 <                multcolor(srccnt[sn].val, srccnt[sn].coef);
294 <                cntord[sn].brt = bright(srccnt[sn].val);
292 >                copycolor(scp->val, sr.rcol);
293 >                multcolor(scp->val, scp->coef);
294 >                cntord[sn].brt = bright(scp->val);
295          }
296                                                  /* sort contributions */
297          qsort(cntord, sn, sizeof(CNTPTR), cntcmp);
# Line 261 | Line 308 | char  *p;                      /* data for f */
308                          l = m;
309                  }
310          }
311 +        if (ncnts == 0)
312 +                return;         /* no contributions! */
313                                                  /* accumulate tail */
314          for (sn = ncnts-1; sn > 0; sn--)
315                  cntord[sn-1].brt += cntord[sn].brt;
# Line 269 | Line 318 | char  *p;                      /* data for f */
318                                                  /* modify threshold */
319          ourthresh = shadthresh / r->rweight;
320                                                  /* test for shadows */
321 <        nhits = 0;
322 <        for (sn = 0; sn < ncnts; sn++) {
321 >        for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts;
322 >                        hwt += (double)source[scp->sno].nhits /
323 >                                (double)source[scp->sno].ntests,
324 >                        sn++) {
325                                                  /* check threshold */
326                  if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
327                                  cntord[sn].brt-cntord[sn+nshadcheck].brt)
328                                  < ourthresh*bright(r->rcol))
329                          break;
330 +                scp = srccnt + cntord[sn].sndx;
331                                                  /* test for hit */
332                  rayorigin(&sr, r, SHADOW, 1.0);
333 <                VCOPY(sr.rdir, srccnt[cntord[sn].sndx].dir);
334 <                sr.rsrc = srccnt[cntord[sn].sndx].sno;
335 <                source[sr.rsrc].ntests++;       /* keep statistics */
333 >                VCOPY(sr.rdir, scp->dir);
334 >                sr.rsrc = scp->sno;
335 >                source[scp->sno].ntests++;      /* keep statistics */
336                  if (localhit(&sr, &thescene) &&
337 <                                ( sr.ro != source[sr.rsrc].so ||
338 <                                source[sr.rsrc].sflags & SFOLLOW )) {
337 >                                ( sr.ro != source[scp->sno].so ||
338 >                                source[scp->sno].sflags & SFOLLOW )) {
339                                                  /* follow entire path */
340                          raycont(&sr);
341 +                        rayparticipate(&sr);
342                          if (trace != NULL)
343                                  (*trace)(&sr);  /* trace execution */
344                          if (bright(sr.rcol) <= FTINY)
345                                  continue;       /* missed! */
346 <                        copycolor(srccnt[cntord[sn].sndx].val, sr.rcol);
347 <                        multcolor(srccnt[cntord[sn].sndx].val,
295 <                                        srccnt[cntord[sn].sndx].coef);
346 >                        copycolor(scp->val, sr.rcol);
347 >                        multcolor(scp->val, scp->coef);
348                  }
349                                                  /* add contribution if hit */
350 <                addcolor(r->rcol, srccnt[cntord[sn].sndx].val);
350 >                addcolor(r->rcol, scp->val);
351                  nhits++;
352 <                source[sr.rsrc].nhits++;
352 >                source[scp->sno].nhits++;
353          }
354 <                                        /* surface hit rate */
355 <        if (sn > 0)
356 <                hwt = (double)nhits / (double)sn;
354 >                                        /* source hit rate */
355 >        if (hwt > FTINY)
356 >                hwt = (double)nhits / hwt;
357          else
358                  hwt = 0.5;
359   #ifdef DEBUG
360 <        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
360 >        sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n",
361                          sn, ncnts-sn, hwt);
362          eputs(errmsg);
363   #endif
364                                          /* add in untested sources */
365          for ( ; sn < ncnts; sn++) {
366 <                sr.rsrc = srccnt[cntord[sn].sndx].sno;
367 <                prob = hwt * (double)source[sr.rsrc].nhits /
368 <                                (double)source[sr.rsrc].ntests;
369 <                scalecolor(srccnt[cntord[sn].sndx].val, prob);
370 <                addcolor(r->rcol, srccnt[cntord[sn].sndx].val);
366 >                scp = srccnt + cntord[sn].sndx;
367 >                prob = hwt * (double)source[scp->sno].nhits /
368 >                                (double)source[scp->sno].ntests;
369 >                if (prob > 1.0)
370 >                        prob = 1.0;
371 >                scalecolor(scp->val, prob);
372 >                addcolor(r->rcol, scp->val);
373          }
374 + }
375 +
376 +
377 + srcscatter(r)                   /* compute source scattering into ray */
378 + register RAY  *r;
379 + {
380 +        int  oldsampndx;
381 +        int  nsamps;
382 +        RAY  sr;
383 +        SRCINDEX  si;
384 +        double  t, lastt, d;
385 +        COLOR  cumval, ctmp;
386 +        int  i, j;
387 +
388 +        if (r->slights == NULL || r->slights[0] == 0
389 +                        || r->gecc >= 1.-FTINY || r->rot >= FHUGE)
390 +                return;
391 +        if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
392 +                nsamps = 1;
393 + #if MAXSSAMP
394 +        else if (nsamps > MAXSSAMP)
395 +                nsamps = MAXSSAMP;
396 + #endif
397 +        oldsampndx = samplendx;
398 +        samplendx = random()&0x7fff;            /* randomize */
399 +        for (i = r->slights[0]; i > 0; i--) {   /* for each source */
400 +                setcolor(cumval, 0., 0., 0.);
401 +                lastt = r->rot;
402 +                for (j = nsamps; j-- > 0; ) {   /* for each sample position */
403 +                        samplendx++;
404 +                        t = r->rot * (j+frandom())/nsamps;
405 +                        sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
406 +                        sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
407 +                        sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
408 +                        sr.rmax = 0.;
409 +                        initsrcindex(&si);      /* sample ray to this source */
410 +                        si.sn = r->slights[i];
411 +                        nopart(&si, &sr);
412 +                        if (!srcray(&sr, NULL, &si) ||
413 +                                        sr.rsrc != r->slights[i])
414 +                                continue;               /* no path */
415 +                        copycolor(sr.cext, r->cext);
416 +                        copycolor(sr.albedo, r->albedo);
417 +                        sr.gecc = r->gecc;
418 +                        rayvalue(&sr);                  /* eval. source ray */
419 +                        if (bright(sr.rcol) <= FTINY)
420 +                                continue;
421 +                                                        /* compute fall-off */
422 +                        d = lastt - t;
423 +                        setcolor(ctmp,  1.-d*colval(r->cext,RED),
424 +                                        1.-d*colval(r->cext,GRN),
425 +                                        1.-d*colval(r->cext,BLU));
426 +                        multcolor(cumval, ctmp);
427 +                        lastt = t;
428 +                        if (r->gecc <= FTINY)           /* compute P(theta) */
429 +                                d = 1.;
430 +                        else {
431 +                                d = DOT(r->rdir, sr.rdir);
432 +                                d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d);
433 +                                d = (1. - r->gecc*r->gecc) / (d*d*d);
434 +                        }
435 +                                                        /* other factors */
436 +                        d *= si.dom * r->rot / (4.*PI*nsamps);
437 +                        multcolor(sr.rcol, r->cext);
438 +                        multcolor(sr.rcol, r->albedo);
439 +                        scalecolor(sr.rcol, d);
440 +                        addcolor(cumval, sr.rcol);
441 +                }
442 +                                                /* final fall-off */
443 +                setcolor(ctmp,  1.-lastt*colval(r->cext,RED),
444 +                                1.-lastt*colval(r->cext,GRN),
445 +                                1.-lastt*colval(r->cext,BLU));
446 +                multcolor(cumval, ctmp);
447 +                addcolor(r->rcol, cumval);      /* sum into ray result */
448 +        }
449 +        samplendx = oldsampndx;
450 + }
451 +
452 +
453 + /****************************************************************
454 + * The following macros were separated from the m_light() routine
455 + * because they are very nasty and difficult to understand.
456 + */
457 +
458 + /* illumblock *
459 + *
460 + * We cannot allow an illum to pass to another illum, because that
461 + * would almost certainly constitute overcounting.
462 + * However, we do allow an illum to pass to another illum
463 + * that is actually going to relay to a virtual light source.
464 + * We also prevent an illum from passing to a glow; this provides a
465 + * convenient mechanism for defining detailed light source
466 + * geometry behind (or inside) an effective radiator.
467 + */
468 +
469 + static int weaksrcmod(obj) int obj;     /* efficiency booster function */
470 + {register OBJREC *o = objptr(obj);
471 + return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
472 +
473 + #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \
474 +                                r->rod > 0.0 && \
475 +                                weaksrcmod(source[r->rsrc].so->omod))
476 +
477 + /* wrongsource *
478 + *
479 + * This source is the wrong source (ie. overcounted) if we are
480 + * aimed to a different source than the one we hit and the one
481 + * we hit is not an illum that should be passed.
482 + */
483 +
484 + #define  wrongsource(m, r)      (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \
485 +                                (m->otype!=MAT_ILLUM || illumblock(m,r)))
486 +
487 + /* distglow *
488 + *
489 + * A distant glow is an object that sometimes acts as a light source,
490 + * but is too far away from the test point to be one in this case.
491 + * (Glows with negative radii should NEVER participate in illumination.)
492 + */
493 +
494 + #define  distglow(m, r, d)      (m->otype==MAT_GLOW && \
495 +                                m->oargs.farg[3] >= -FTINY && \
496 +                                d > m->oargs.farg[3])
497 +
498 + /* badcomponent *
499 + *
500 + * We must avoid counting light sources in the ambient calculation,
501 + * since the direct component is handled separately.  Therefore, any
502 + * ambient ray which hits an active light source must be discarded.
503 + * The same is true for stray specular samples, since the specular
504 + * contribution from light sources is calculated separately.
505 + */
506 +
507 + #define  badcomponent(m, r)     (r->crtype&(AMBIENT|SPECULAR) && \
508 +                                !(r->crtype&SHADOW || r->rod < 0.0 || \
509 +                /* not 100% correct */  distglow(m, r, r->rot)))
510 +
511 + /* passillum *
512 + *
513 + * An illum passes to another material type when we didn't hit it
514 + * on purpose (as part of a direct calculation), or it is relaying
515 + * a virtual light source.
516 + */
517 +
518 + #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
519 +                                (r->rsrc<0 || source[r->rsrc].so!=r->ro || \
520 +                                source[r->rsrc].sflags&SVIRTUAL))
521 +
522 + /* srcignore *
523 + *
524 + * The -dv flag is normally on for sources to be visible.
525 + */
526 +
527 + #define  srcignore(m, r)        !(directvis || r->crtype&SHADOW || \
528 +                                distglow(m, r, raydist(r,PRIMARY)))
529 +
530 +
531 + m_light(m, r)                   /* ray hit a light source */
532 + register OBJREC  *m;
533 + register RAY  *r;
534 + {
535 +                                                /* check for over-counting */
536 +        if (badcomponent(m, r))
537 +                return(1);
538 +        if (wrongsource(m, r))
539 +                return(1);
540 +                                                /* check for passed illum */
541 +        if (passillum(m, r)) {
542 +                if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
543 +                        return(rayshade(r, modifier(m->oargs.sarg[0])));
544 +                raytrans(r);
545 +                return(1);
546 +        }
547 +                                        /* otherwise treat as source */
548 +                                                /* check for behind */
549 +        if (r->rod < 0.0)
550 +                return(1);
551 +                                                /* check for invisibility */
552 +        if (srcignore(m, r))
553 +                return(1);
554 +                                                /* check for outside spot */
555 +        if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
556 +                return(1);
557 +                                                /* get distribution pattern */
558 +        raytexture(r, m->omod);
559 +                                                /* get source color */
560 +        setcolor(r->rcol, m->oargs.farg[0],
561 +                          m->oargs.farg[1],
562 +                          m->oargs.farg[2]);
563 +                                                /* modify value */
564 +        multcolor(r->rcol, r->pcol);
565 +        return(1);
566   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines