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.5 by greg, Wed Jun 7 08:38:38 1989 UTC vs.
Revision 1.23 by greg, Wed Dec 12 22:43:03 1990 UTC

# 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 */
# Line 48 | Line 49 | marksources()                  /* find and mark source objects */
49  
50                  if (m->otype != MAT_LIGHT &&
51                                  m->otype != MAT_ILLUM &&
52 <                                m->otype != MAT_GLOW)
52 >                                m->otype != MAT_GLOW &&
53 >                                m->otype != MAT_SPOT)
54                          continue;
55          
56 <                if (m->oargs.nfargs != 3)
56 >                if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
57 >                                m->otype == MAT_SPOT ? 7 : 3))
58                          objerror(m, USER, "bad # arguments");
59  
60 <                if (m->otype == MAT_GLOW && o->otype != OBJ_SOURCE)
60 >                if (m->otype == MAT_GLOW &&
61 >                                o->otype != OBJ_SOURCE &&
62 >                                m->oargs.farg[3] <= FTINY)
63                          continue;                       /* don't bother */
64  
65                  if (source == NULL)
# Line 67 | Line 72 | marksources()                  /* find and mark source objects */
72  
73                  newsource(&source[nsources], o);
74  
75 <                if (m->otype == MAT_GLOW)
76 <                        source[nsources].sflags |= SSKIP;
77 <
75 >                if (m->otype == MAT_GLOW) {
76 >                        source[nsources].sflags |= SPROX;
77 >                        source[nsources].sl.prox = m->oargs.farg[3];
78 >                        if (o->otype == OBJ_SOURCE)
79 >                                source[nsources].sflags |= SSKIP;
80 >                } else if (m->otype == MAT_SPOT) {
81 >                        source[nsources].sflags |= SSPOT;
82 >                        source[nsources].sl.s = makespot(m);
83 >                }
84                  nsources++;
85          }
86   }
# Line 87 | Line 98 | register OBJREC  *so;
98          register int  i;
99          
100          src->sflags = 0;
101 <        src->nhits = src->ntests = 1;   /* start with hit probability = 1 */
101 >        src->nhits = 1; src->ntests = 2;        /* start probability = 1/2 */
102          src->so = so;
103  
104          switch (so->otype) {
# Line 139 | Line 150 | register OBJREC  *so;
150   }
151  
152  
153 + SPOT *
154 + makespot(m)                     /* make a spotlight */
155 + register OBJREC  *m;
156 + {
157 +        extern double  cos();
158 +        register SPOT  *ns;
159 +
160 +        if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
161 +                error(SYSTEM, "out of memory in makespot");
162 +        ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
163 +        VCOPY(ns->aim, m->oargs.farg+4);
164 +        if ((ns->flen = normalize(ns->aim)) == 0.0)
165 +                objerror(m, USER, "zero focus vector");
166 +        return(ns);
167 + }
168 +
169 +
170   double
171   srcray(sr, r, sn)               /* send a ray to a source, return domega */
172   register RAY  *sr;              /* returned source ray */
# Line 199 | Line 227 | register int  sn;              /* source number */
227                  return(source[sn].ss2);
228  
229          else {
230 +                                                /* check proximity */
231 +                if (source[sn].sflags & SPROX &&
232 +                                d > source[sn].sl.prox)
233 +                        return(0.0);
234  
235                  if (norm != NULL)
236                          ddot /= d;
237                  else
238                          ddot = 1.0;
239 +                                                /* check angle */
240 +                if (source[sn].sflags & SSPOT) {
241 +                        if (source[sn].sl.s->siz < 2.0*PI *
242 +                                (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
243 +                                return(0.0);
244 +                        d += source[sn].sl.s->flen;
245 +                }
246                                                  /* return domega */
247                  return(ddot*source[sn].ss2/(d*d));
248          }
# Line 248 | Line 287 | register RAY  *r;
287  
288   static int
289   cntcmp(sc1, sc2)                        /* contribution compare (descending) */
290 < register CONTRIB  *sc1, *sc2;
290 > register CNTPTR  *sc1, *sc2;
291   {
292          if (sc1->brt > sc2->brt)
293                  return(-1);
# Line 263 | Line 302 | RAY  *r;                       /* ray that hit surface */
302   int  (*f)();                    /* direct component coefficient function */
303   char  *p;                       /* data for f */
304   {
305 +        extern double  pow();
306          register int  sn;
307          register CONTRIB  *srccnt;
308 <        double  dtmp, hwt, test2, hit2;
308 >        register CNTPTR  *cntord;
309 >        int  nshadcheck, ncnts;
310 >        double  prob, ourthresh, hwt, test2, hit2;
311          RAY  sr;
312  
313 <        if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL)
313 >        if (nsources <= 0)
314 >                return;
315 >        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
316 >        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
317 >        if (srccnt == NULL || cntord == NULL)
318                  error(SYSTEM, "out of memory in direct");
319 +                                                /* compute number to check */
320 +        nshadcheck = pow((double)nsources, shadcert) + .5;
321 +                                                /* modify threshold */
322 +        ourthresh = shadthresh / r->rweight;
323                                                  /* potential contributions */
324          for (sn = 0; sn < nsources; sn++) {
325 <                srccnt[sn].sno = sn;
326 <                setcolor(srccnt[sn].val, 0.0, 0.0, 0.0);
325 >                cntord[sn].sno = sn;
326 >                cntord[sn].brt = 0.0;
327                                                  /* get source ray */
328                  if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
329                          continue;
330                  VCOPY(srccnt[sn].dir, sr.rdir);
331                                                  /* compute coefficient */
332                  (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
333 <                srccnt[sn].brt = bright(srccnt[sn].val);
334 <                if (srccnt[sn].brt <= FTINY)
333 >                cntord[sn].brt = bright(srccnt[sn].val);
334 >                if (cntord[sn].brt <= 0.0)
335                          continue;
336                                                  /* compute intersection */
337                  if (!( source[sn].sflags & SDISTANT ?
# Line 290 | Line 340 | char  *p;                      /* data for f */
340                                  (source[sn].so, &sr) ))
341                          continue;
342                                                  /* compute contribution */
343 <                rayshade(&sr, sr.ro->omod);
343 >                raycont(&sr);
344                  multcolor(srccnt[sn].val, sr.rcol);
345 <                srccnt[sn].brt = bright(srccnt[sn].val);
345 >                cntord[sn].brt = bright(srccnt[sn].val);
346          }
347                                                  /* sort contributions */
348 <        qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp);
349 <        hit2 = test2 = 0.0;
348 >        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
349 >        {                                       /* find last */
350 >                register int  l, m;
351 >
352 >                sn = 0; ncnts = l = nsources;
353 >                while ((m = (sn + ncnts) >> 1) != l) {
354 >                        if (cntord[m].brt > 0.0)
355 >                                sn = m;
356 >                        else
357 >                                ncnts = m;
358 >                        l = m;
359 >                }
360 >        }
361 >                                                /* accumulate tail */
362 >        for (sn = ncnts-1; sn > 0; sn--)
363 >                cntord[sn-1].brt += cntord[sn].brt;
364 >                                                /* start with prob=.5 */
365 >        hit2 = 0.5; test2 = 1.0;
366                                                  /* test for shadows */
367 <        for (sn = 0; sn < nsources; sn++) {
367 >        for (sn = 0; sn < ncnts; sn++) {
368                                                  /* check threshold */
369 <                if (srccnt[sn].brt <= shadthresh*bright(r->rcol)/r->rweight)
369 >                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
370 >                                cntord[sn].brt-cntord[sn+nshadcheck].brt) <
371 >                                ourthresh*bright(r->rcol))
372                          break;
373                                                  /* get statistics */
374 <                hwt = (double)source[srccnt[sn].sno].nhits /
375 <                                (double)source[srccnt[sn].sno].ntests;
374 >                hwt = (double)source[cntord[sn].sno].nhits /
375 >                                (double)source[cntord[sn].sno].ntests;
376                  test2 += hwt;
377 <                source[srccnt[sn].sno].ntests++;
377 >                source[cntord[sn].sno].ntests++;
378                                                  /* test for hit */
379                  rayorigin(&sr, r, SHADOW, 1.0);
380 <                VCOPY(sr.rdir, srccnt[sn].dir);
380 >                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
381 >                sr.rsrc = cntord[sn].sno;
382                  if (localhit(&sr, &thescene) &&
383 <                                sr.ro != source[srccnt[sn].sno].so) {
383 >                                sr.ro != source[cntord[sn].sno].so) {
384                                                  /* check for transmission */
385 <                        if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
317 <                                raytrans(&sr);          /* object is clipped */
318 <                        else
319 <                                rayshade(&sr, sr.ro->omod);
385 >                        raycont(&sr);
386                          if (bright(sr.rcol) <= FTINY)
387                                  continue;       /* missed! */
388 <                        (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
389 <                        multcolor(srccnt[sn].val, sr.rcol);
388 >                        (*f)(srccnt[cntord[sn].sno].val, p,
389 >                                        srccnt[cntord[sn].sno].dir,
390 >                                        srccnt[cntord[sn].sno].dom);
391 >                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
392                  }
393                                                  /* add contribution if hit */
394 <                addcolor(r->rcol, srccnt[sn].val);
394 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
395                  hit2 += hwt;
396 <                source[srccnt[sn].sno].nhits++;
396 >                source[cntord[sn].sno].nhits++;
397          }
398 <        if (test2 > FTINY)              /* weighted hit rate */
399 <                hwt = hit2 / test2;
332 <        else
333 <                hwt = 0.0;
398 >                                        /* weighted hit rate */
399 >        hwt = hit2 / test2;
400   #ifdef DEBUG
401 <        fprintf(stderr, "%d tested, %f hit rate\n", sn, hwt);
401 >        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
402 >                        sn, ncnts-sn, hwt);
403 >        eputs(errmsg);
404   #endif
405                                          /* add in untested sources */
406 <        for ( ; sn < nsources; sn++) {
407 <                if (srccnt[sn].brt <= 0.0)
408 <                        break;
409 <                dtmp = hwt * (double)source[srccnt[sn].sno].nhits /
410 <                                (double)source[srccnt[sn].sno].ntests;
343 <                scalecolor(srccnt[sn].val, dtmp);
344 <                addcolor(r->rcol, srccnt[sn].val);
406 >        for ( ; sn < ncnts; sn++) {
407 >                prob = hwt * (double)source[cntord[sn].sno].nhits /
408 >                                (double)source[cntord[sn].sno].ntests;
409 >                scalecolor(srccnt[cntord[sn].sno].val, prob);
410 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
411          }
412                  
413 <        free(srccnt);
413 >        free((char *)srccnt);
414 >        free((char *)cntord);
415   }
416  
417  
# Line 353 | Line 420 | char  *p;                      /* data for f */
420                                  source[r->rsrc].so!=r->ro)
421  
422   #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
423 <                                !(r->rtype&REFLECTED))  /* hack! */
423 >                                !(r->rtype&REFLECTED) &&        /* hack! */\
424 >                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
425  
426   #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
427                                  !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
# Line 363 | Line 431 | m_light(m, r)                  /* ray hit a light source */
431   register OBJREC  *m;
432   register RAY  *r;
433   {
366                                                /* check for behind */
367        if (r->rod < 0.0)
368                return;
434                                                  /* check for over-counting */
435          if (wrongsource(m, r) || badambient(m, r))
436                  return;
# Line 379 | Line 444 | register RAY  *r;
444  
445                                                  /* otherwise treat as source */
446          } else {
447 +                                                /* check for behind */
448 +                if (r->rod < 0.0)
449 +                        return;
450                                                  /* get distribution pattern */
451                  raytexture(r, m->omod);
452                                                  /* get source color */
# Line 387 | Line 455 | register RAY  *r;
455                                    m->oargs.farg[2]);
456                                                  /* modify value */
457                  multcolor(r->rcol, r->pcol);
458 +                                                /* assign distance */
459 +                r->rt = r->rot;
460          }
461   }
462  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines