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 1.9 by greg, Thu Jun 8 09:35:43 1989 UTC vs.
Revision 1.26 by greg, Sat Dec 15 15:03:34 1990 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1990 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 27 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27  
28   extern double  dstrsrc;                 /* source distribution amount */
29   extern double  shadthresh;              /* relative shadow threshold */
30 + extern double  shadcert;                /* shadow testing certainty */
31  
32   SRCREC  *source = NULL;                 /* our list of sources */
33   int  nsources = 0;                      /* the number of sources */
34  
35 + static CONTRIB  *srccnt;                /* source contributions in direct() */
36 + static CNTPTR  *cntord;                 /* source ordering in direct() */
37  
38 +
39   marksources()                   /* find and mark source objects */
40   {
41          register OBJREC  *o, *m;
# Line 46 | Line 50 | marksources()                  /* find and mark source objects */
50  
51                  m = objptr(o->omod);
52  
53 <                if (m->otype != MAT_LIGHT &&
50 <                                m->otype != MAT_ILLUM &&
51 <                                m->otype != MAT_GLOW &&
52 <                                m->otype != MAT_SPOT)
53 >                if (!islight(m->otype))
54                          continue;
55          
56                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
# Line 67 | Line 68 | marksources()                  /* find and mark source objects */
68                          source = (SRCREC *)realloc((char *)source,
69                                          (unsigned)(nsources+1)*sizeof(SRCREC));
70                  if (source == NULL)
71 <                        error(SYSTEM, "out of memory in marksources");
71 >                        goto memerr;
72  
73                  newsource(&source[nsources], o);
74  
# Line 82 | Line 83 | marksources()                  /* find and mark source objects */
83                  }
84                  nsources++;
85          }
86 +        if (nsources <= 0) {
87 +                error(WARNING, "no light sources found");
88 +                return;
89 +        }
90 +        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
91 +        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
92 +        if (srccnt != NULL && cntord != NULL)
93 +                return;
94 +        /* fall through */
95 + memerr:
96 +        error(SYSTEM, "out of memory in marksources");
97   }
98  
99  
# Line 276 | Line 288 | register RAY  *r;
288                  for (i = 0; i < 3; i++)
289                          r->ron[i] = -r->rdir[i];
290                  r->rod = 1.0;
291 <                r->rofs = 1.0; setident4(r->rofx);
280 <                r->robs = 1.0; setident4(r->robx);
291 >                r->rox = NULL;
292                  return(1);
293          }
294          return(0);
# Line 301 | Line 312 | RAY  *r;                       /* ray that hit surface */
312   int  (*f)();                    /* direct component coefficient function */
313   char  *p;                       /* data for f */
314   {
315 +        extern double  pow();
316          register int  sn;
317 <        register CONTRIB  *srccnt;
318 <        register CNTPTR  *cntord;
307 <        int  ncnts;
308 <        double  ourthresh, prob, hwt, test2, hit2;
317 >        int  nshadcheck, ncnts;
318 >        double  prob, ourthresh, hwt, test2, hit2;
319          RAY  sr;
320 <
321 <        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
322 <        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
323 <        if (srccnt == NULL || cntord == NULL)
324 <                error(SYSTEM, "out of memory in direct");
320 >                        /* NOTE: srccnt and cntord global so no recursion */
321 >        if (nsources <= 0)
322 >                return;         /* no sources?! */
323 >                                                /* compute number to check */
324 >        nshadcheck = pow((double)nsources, shadcert) + .5;
325                                                  /* modify threshold */
326          ourthresh = shadthresh / r->rweight;
327                                                  /* potential contributions */
# Line 325 | Line 335 | char  *p;                      /* data for f */
335                                                  /* compute coefficient */
336                  (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
337                  cntord[sn].brt = bright(srccnt[sn].val);
338 <                if (cntord[sn].brt <= FTINY)
338 >                if (cntord[sn].brt <= 0.0)
339                          continue;
340                                                  /* compute intersection */
341                  if (!( source[sn].sflags & SDISTANT ?
342                                  sourcehit(&sr) :
343                                  (*ofun[source[sn].so->otype].funp)
344 <                                (source[sn].so, &sr) ))
344 >                                (source[sn].so, &sr) )) {
345 >                        sprintf(errmsg,
346 >                                "aiming failure for light source \"%s\"",
347 >                                        source[sn].so->oname);
348 >                        error(WARNING, errmsg);
349                          continue;
350 +                }
351                                                  /* compute contribution */
352 <                rayshade(&sr, sr.ro->omod);
352 >                raycont(&sr);
353                  multcolor(srccnt[sn].val, sr.rcol);
354                  cntord[sn].brt = bright(srccnt[sn].val);
355          }
356                                                  /* sort contributions */
357          qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
358 <        hit2 = 0.0; test2 = FTINY;
359 <                                                /* find last */
360 <        sn = 0; ncnts = nsources;
361 <        while (sn < ncnts-1) {
362 <                register int  m;
363 <                m = (sn + ncnts) >> 1;
364 <                if (cntord[m].brt > 0.0)
365 <                        sn = m;
366 <                else
367 <                        ncnts = m;
358 >        {                                       /* find last */
359 >                register int  l, m;
360 >
361 >                sn = 0; ncnts = l = nsources;
362 >                while ((m = (sn + ncnts) >> 1) != l) {
363 >                        if (cntord[m].brt > 0.0)
364 >                                sn = m;
365 >                        else
366 >                                ncnts = m;
367 >                        l = m;
368 >                }
369          }
370 <                                                /* accumulate tail */
371 <        for (sn = ncnts-1; sn > 0; sn--)
372 <                cntord[sn-1].brt += cntord[sn].brt;
373 <                                                /* shadow testing */
370 >                                                /* accumulate tail */
371 >        for (sn = ncnts-1; sn > 0; sn--)
372 >                cntord[sn-1].brt += cntord[sn].brt;
373 >                                                /* start with prob=.5 */
374 >        hit2 = 0.5; test2 = 1.0;
375 >                                                /* test for shadows */
376          for (sn = 0; sn < ncnts; sn++) {
377 <                                                /* tail below threshold? */
378 <                if (cntord[sn].brt < ourthresh*bright(r->rcol))
377 >                                                /* check threshold */
378 >                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
379 >                                cntord[sn].brt-cntord[sn+nshadcheck].brt) <
380 >                                ourthresh*bright(r->rcol))
381                          break;
382                                                  /* get statistics */
383                  hwt = (double)source[cntord[sn].sno].nhits /
# Line 367 | Line 387 | char  *p;                      /* data for f */
387                                                  /* test for hit */
388                  rayorigin(&sr, r, SHADOW, 1.0);
389                  VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
390 +                sr.rsrc = cntord[sn].sno;
391                  if (localhit(&sr, &thescene) &&
392                                  sr.ro != source[cntord[sn].sno].so) {
393                                                  /* check for transmission */
394 <                        if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
374 <                                raytrans(&sr);          /* object is clipped */
375 <                        else
376 <                                rayshade(&sr, sr.ro->omod);
394 >                        raycont(&sr);
395                          if (bright(sr.rcol) <= FTINY)
396                                  continue;       /* missed! */
397                          (*f)(srccnt[cntord[sn].sno].val, p,
# Line 389 | Line 407 | char  *p;                      /* data for f */
407                                          /* weighted hit rate */
408          hwt = hit2 / test2;
409   #ifdef DEBUG
410 <        fprintf(stderr, "%d tested, %d untested, %f hit rate\n",
410 >        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
411                          sn, ncnts-sn, hwt);
412 +        eputs(errmsg);
413   #endif
414                                          /* add in untested sources */
415          for ( ; sn < ncnts; sn++) {
# Line 399 | Line 418 | char  *p;                      /* data for f */
418                  scalecolor(srccnt[cntord[sn].sno].val, prob);
419                  addcolor(r->rcol, srccnt[cntord[sn].sno].val);
420          }
402        free(srccnt);
403        free(cntord);
421   }
422  
423  
# Line 420 | Line 437 | m_light(m, r)                  /* ray hit a light source */
437   register OBJREC  *m;
438   register RAY  *r;
439   {
423                                                /* check for behind */
424        if (r->rod < 0.0)
425                return;
440                                                  /* check for over-counting */
441          if (wrongsource(m, r) || badambient(m, r))
442                  return;
# Line 436 | Line 450 | register RAY  *r;
450  
451                                                  /* otherwise treat as source */
452          } else {
453 +                                                /* check for behind */
454 +                if (r->rod < 0.0)
455 +                        return;
456                                                  /* get distribution pattern */
457                  raytexture(r, m->omod);
458                                                  /* get source color */
# Line 444 | Line 461 | register RAY  *r;
461                                    m->oargs.farg[2]);
462                                                  /* modify value */
463                  multcolor(r->rcol, r->pcol);
464 +                                                /* assign distance */
465 +                r->rt = r->rot;
466          }
467   }
449
450
451 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines