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.4 by greg, Wed Jun 7 08:35:32 1989 UTC vs.
Revision 1.25 by greg, Thu Dec 13 21:49:23 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)
53 >                if (!islight(m->otype))
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 63 | 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  
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 +        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 87 | Line 109 | register OBJREC  *so;
109          register int  i;
110          
111          src->sflags = 0;
112 <        src->nhits = src->ntests = 1;   /* start with hit probability = 1 */
112 >        src->nhits = 1; src->ntests = 2;        /* start probability = 1/2 */
113          src->so = so;
114  
115          switch (so->otype) {
# Line 139 | Line 161 | register OBJREC  *so;
161   }
162  
163  
164 + SPOT *
165 + makespot(m)                     /* make a spotlight */
166 + register OBJREC  *m;
167 + {
168 +        extern double  cos();
169 +        register SPOT  *ns;
170 +
171 +        if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
172 +                error(SYSTEM, "out of memory in makespot");
173 +        ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
174 +        VCOPY(ns->aim, m->oargs.farg+4);
175 +        if ((ns->flen = normalize(ns->aim)) == 0.0)
176 +                objerror(m, USER, "zero focus vector");
177 +        return(ns);
178 + }
179 +
180 +
181   double
182   srcray(sr, r, sn)               /* send a ray to a source, return domega */
183   register RAY  *sr;              /* returned source ray */
# Line 199 | Line 238 | register int  sn;              /* source number */
238                  return(source[sn].ss2);
239  
240          else {
241 +                                                /* check proximity */
242 +                if (source[sn].sflags & SPROX &&
243 +                                d > source[sn].sl.prox)
244 +                        return(0.0);
245  
246                  if (norm != NULL)
247                          ddot /= d;
248                  else
249                          ddot = 1.0;
250 +                                                /* check angle */
251 +                if (source[sn].sflags & SSPOT) {
252 +                        if (source[sn].sl.s->siz < 2.0*PI *
253 +                                (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
254 +                                return(0.0);
255 +                        d += source[sn].sl.s->flen;
256 +                }
257                                                  /* return domega */
258                  return(ddot*source[sn].ss2/(d*d));
259          }
# Line 248 | Line 298 | register RAY  *r;
298  
299   static int
300   cntcmp(sc1, sc2)                        /* contribution compare (descending) */
301 < register CONTRIB  *sc1, *sc2;
301 > register CNTPTR  *sc1, *sc2;
302   {
303          if (sc1->brt > sc2->brt)
304                  return(-1);
# Line 263 | Line 313 | RAY  *r;                       /* ray that hit surface */
313   int  (*f)();                    /* direct component coefficient function */
314   char  *p;                       /* data for f */
315   {
316 +        extern double  pow();
317          register int  sn;
318 <        register CONTRIB  *srccnt;
319 <        double  dtmp, hwt, test2, hit2;
318 >        int  nshadcheck, ncnts;
319 >        double  prob, ourthresh, hwt, test2, hit2;
320          RAY  sr;
321 <
322 <        if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL)
323 <                error(SYSTEM, "out of memory in direct");
321 >                        /* NOTE: srccnt and cntord global so no recursion */
322 >        if (nsources <= 0)
323 >                return;         /* no sources?! */
324 >                                                /* compute number to check */
325 >        nshadcheck = pow((double)nsources, shadcert) + .5;
326 >                                                /* modify threshold */
327 >        ourthresh = shadthresh / r->rweight;
328                                                  /* potential contributions */
329          for (sn = 0; sn < nsources; sn++) {
330 <                srccnt[sn].sno = sn;
331 <                setcolor(srccnt[sn].val, 0.0, 0.0, 0.0);
330 >                cntord[sn].sno = sn;
331 >                cntord[sn].brt = 0.0;
332                                                  /* get source ray */
333                  if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
334                          continue;
335                  VCOPY(srccnt[sn].dir, sr.rdir);
336                                                  /* compute coefficient */
337                  (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
338 <                srccnt[sn].brt = intens(srccnt[sn].val);
339 <                if (srccnt[sn].brt <= FTINY)
338 >                cntord[sn].brt = bright(srccnt[sn].val);
339 >                if (cntord[sn].brt <= 0.0)
340                          continue;
341                                                  /* compute intersection */
342                  if (!( source[sn].sflags & SDISTANT ?
343                                  sourcehit(&sr) :
344                                  (*ofun[source[sn].so->otype].funp)
345 <                                (source[sn].so, &sr) ))
345 >                                (source[sn].so, &sr) )) {
346 >                        sprintf(errmsg,
347 >                                "aiming failure for light source \"%s\"",
348 >                                        source[sn].so->oname);
349 >                        error(WARNING, errmsg);
350                          continue;
351 +                }
352                                                  /* compute contribution */
353 <                rayshade(&sr, sr.ro->omod);
353 >                raycont(&sr);
354                  multcolor(srccnt[sn].val, sr.rcol);
355 <                srccnt[sn].brt = intens(srccnt[sn].val);
355 >                cntord[sn].brt = bright(srccnt[sn].val);
356          }
357                                                  /* sort contributions */
358 <        qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp);
359 <        hit2 = test2 = 0.0;
358 >        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
359 >        {                                       /* find last */
360 >                register int  l, m;
361 >
362 >                sn = 0; ncnts = l = nsources;
363 >                while ((m = (sn + ncnts) >> 1) != l) {
364 >                        if (cntord[m].brt > 0.0)
365 >                                sn = m;
366 >                        else
367 >                                ncnts = m;
368 >                        l = m;
369 >                }
370 >        }
371 >                                                /* accumulate tail */
372 >        for (sn = ncnts-1; sn > 0; sn--)
373 >                cntord[sn-1].brt += cntord[sn].brt;
374 >                                                /* start with prob=.5 */
375 >        hit2 = 0.5; test2 = 1.0;
376                                                  /* test for shadows */
377 <        for (sn = 0; sn < nsources; sn++) {
377 >        for (sn = 0; sn < ncnts; sn++) {
378                                                  /* check threshold */
379 <                if (srccnt[sn].brt <= shadthresh*intens(r->rcol)/r->rweight)
379 >                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
380 >                                cntord[sn].brt-cntord[sn+nshadcheck].brt) <
381 >                                ourthresh*bright(r->rcol))
382                          break;
383                                                  /* get statistics */
384 <                hwt = (double)source[srccnt[sn].sno].nhits /
385 <                                (double)source[srccnt[sn].sno].ntests;
384 >                hwt = (double)source[cntord[sn].sno].nhits /
385 >                                (double)source[cntord[sn].sno].ntests;
386                  test2 += hwt;
387 <                source[srccnt[sn].sno].ntests++;
387 >                source[cntord[sn].sno].ntests++;
388                                                  /* test for hit */
389                  rayorigin(&sr, r, SHADOW, 1.0);
390 <                VCOPY(sr.rdir, srccnt[sn].dir);
390 >                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
391 >                sr.rsrc = cntord[sn].sno;
392                  if (localhit(&sr, &thescene) &&
393 <                                sr.ro != source[srccnt[sn].sno].so) {
393 >                                sr.ro != source[cntord[sn].sno].so) {
394                                                  /* check for transmission */
395 <                        if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
396 <                                raytrans(&sr);          /* object is clipped */
318 <                        else
319 <                                rayshade(&sr, sr.ro->omod);
320 <                        if (intens(sr.rcol) <= FTINY)
395 >                        raycont(&sr);
396 >                        if (bright(sr.rcol) <= FTINY)
397                                  continue;       /* missed! */
398 <                        (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
399 <                        multcolor(srccnt[sn].val, sr.rcol);
398 >                        (*f)(srccnt[cntord[sn].sno].val, p,
399 >                                        srccnt[cntord[sn].sno].dir,
400 >                                        srccnt[cntord[sn].sno].dom);
401 >                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
402                  }
403                                                  /* add contribution if hit */
404 <                addcolor(r->rcol, srccnt[sn].val);
404 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
405                  hit2 += hwt;
406 <                source[srccnt[sn].sno].nhits++;
406 >                source[cntord[sn].sno].nhits++;
407          }
408 <        if (test2 > FTINY)              /* weighted hit rate */
409 <                hwt = hit2 / test2;
332 <        else
333 <                hwt = 0.0;
408 >                                        /* weighted hit rate */
409 >        hwt = hit2 / test2;
410   #ifdef DEBUG
411 <        fprintf(stderr, "%d tested, %f hit rate\n", sn, hwt);
411 >        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
412 >                        sn, ncnts-sn, hwt);
413 >        eputs(errmsg);
414   #endif
415                                          /* add in untested sources */
416 <        for ( ; sn < nsources; sn++) {
417 <                if (srccnt[sn].brt <= 0.0)
418 <                        break;
419 <                dtmp = hwt * (double)source[srccnt[sn].sno].nhits /
420 <                                (double)source[srccnt[sn].sno].ntests;
343 <                scalecolor(srccnt[sn].val, dtmp);
344 <                addcolor(r->rcol, srccnt[sn].val);
416 >        for ( ; sn < ncnts; sn++) {
417 >                prob = hwt * (double)source[cntord[sn].sno].nhits /
418 >                                (double)source[cntord[sn].sno].ntests;
419 >                scalecolor(srccnt[cntord[sn].sno].val, prob);
420 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
421          }
346                
347        free(srccnt);
422   }
423  
424  
# Line 353 | Line 427 | char  *p;                      /* data for f */
427                                  source[r->rsrc].so!=r->ro)
428  
429   #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
430 <                                !(r->rtype&REFLECTED))  /* hack! */
430 >                                !(r->rtype&REFLECTED) &&        /* hack! */\
431 >                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
432  
433   #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
434                                  !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
# Line 363 | Line 438 | m_light(m, r)                  /* ray hit a light source */
438   register OBJREC  *m;
439   register RAY  *r;
440   {
366                                                /* check for behind */
367        if (r->rod < 0.0)
368                return;
441                                                  /* check for over-counting */
442          if (wrongsource(m, r) || badambient(m, r))
443                  return;
# Line 379 | Line 451 | register RAY  *r;
451  
452                                                  /* otherwise treat as source */
453          } else {
454 +                                                /* check for behind */
455 +                if (r->rod < 0.0)
456 +                        return;
457                                                  /* get distribution pattern */
458                  raytexture(r, m->omod);
459                                                  /* get source color */
# Line 387 | Line 462 | register RAY  *r;
462                                    m->oargs.farg[2]);
463                                                  /* modify value */
464                  multcolor(r->rcol, r->pcol);
465 +                                                /* assign distance */
466 +                r->rt = r->rot;
467          }
468   }
392
393
394 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines