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.28 by greg, Fri Dec 21 11:32:39 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 198 | Line 237 | register int  sn;              /* source number */
237                                                  /* domega constant */
238                  return(source[sn].ss2);
239  
240 <        else {
241 <
242 <                if (norm != NULL)
243 <                        ddot /= d;
244 <                else
245 <                        ddot = 1.0;
246 <                                                /* return domega */
247 <                return(ddot*source[sn].ss2/(d*d));
240 >                                                /* check proximity */
241 >        if (source[sn].sflags & SPROX &&
242 >                        d > source[sn].sl.prox)
243 >                return(0.0);
244 >                                                /* compute dot product */
245 >        if (norm != NULL)
246 >                ddot /= d;
247 >        else
248 >                ddot = 1.0;
249 >                                                /* check angle */
250 >        if (source[sn].sflags & SSPOT) {
251 >                if (source[sn].sl.s->siz < 2.0*PI *
252 >                                (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
253 >                        return(0.0);
254 >                d += source[sn].sl.s->flen;     /* adjust length */
255          }
256 +                                                /* compute domega */
257 +        return(ddot*source[sn].ss2/(d*d));
258   }
259  
260  
# Line 238 | Line 286 | register RAY  *r;
286                  for (i = 0; i < 3; i++)
287                          r->ron[i] = -r->rdir[i];
288                  r->rod = 1.0;
289 <                r->rofs = 1.0; setident4(r->rofx);
242 <                r->robs = 1.0; setident4(r->robx);
289 >                r->rox = NULL;
290                  return(1);
291          }
292          return(0);
# Line 248 | Line 295 | register RAY  *r;
295  
296   static int
297   cntcmp(sc1, sc2)                        /* contribution compare (descending) */
298 < register CONTRIB  *sc1, *sc2;
298 > register CNTPTR  *sc1, *sc2;
299   {
300          if (sc1->brt > sc2->brt)
301                  return(-1);
# Line 263 | Line 310 | RAY  *r;                       /* ray that hit surface */
310   int  (*f)();                    /* direct component coefficient function */
311   char  *p;                       /* data for f */
312   {
313 +        extern double  pow();
314          register int  sn;
315 <        register CONTRIB  *srccnt;
316 <        double  dtmp, hwt, test2, hit2;
315 >        int  nshadcheck, ncnts;
316 >        double  prob, ourthresh, hwt, test2, hit2;
317          RAY  sr;
318 <
319 <        if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL)
320 <                error(SYSTEM, "out of memory in direct");
318 >                        /* NOTE: srccnt and cntord global so no recursion */
319 >        if (nsources <= 0)
320 >                return;         /* no sources?! */
321 >                                                /* compute number to check */
322 >        nshadcheck = pow((double)nsources, shadcert) + .5;
323 >                                                /* modify threshold */
324 >        ourthresh = shadthresh / r->rweight;
325                                                  /* potential contributions */
326          for (sn = 0; sn < nsources; sn++) {
327 <                srccnt[sn].sno = sn;
328 <                setcolor(srccnt[sn].val, 0.0, 0.0, 0.0);
327 >                cntord[sn].sno = sn;
328 >                cntord[sn].brt = 0.0;
329                                                  /* get source ray */
330                  if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
331                          continue;
332                  VCOPY(srccnt[sn].dir, sr.rdir);
333                                                  /* compute coefficient */
334                  (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
335 <                srccnt[sn].brt = bright(srccnt[sn].val);
336 <                if (srccnt[sn].brt <= FTINY)
335 >                cntord[sn].brt = bright(srccnt[sn].val);
336 >                if (cntord[sn].brt <= 0.0)
337                          continue;
338                                                  /* compute intersection */
339                  if (!( source[sn].sflags & SDISTANT ?
340                                  sourcehit(&sr) :
341                                  (*ofun[source[sn].so->otype].funp)
342 <                                (source[sn].so, &sr) ))
342 >                                (source[sn].so, &sr) )) {
343 >                        sprintf(errmsg,
344 >                                "aiming failure for light source \"%s\"",
345 >                                        source[sn].so->oname);
346 >                        error(WARNING, errmsg);
347 >                        cntord[sn].brt = 0.0;
348                          continue;
349 +                }
350                                                  /* compute contribution */
351 <                rayshade(&sr, sr.ro->omod);
351 >                raycont(&sr);
352                  multcolor(srccnt[sn].val, sr.rcol);
353 <                srccnt[sn].brt = bright(srccnt[sn].val);
353 >                cntord[sn].brt = bright(srccnt[sn].val);
354          }
355                                                  /* sort contributions */
356 <        qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp);
357 <        hit2 = test2 = 0.0;
356 >        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
357 >        {                                       /* find last */
358 >                register int  l, m;
359 >
360 >                sn = 0; ncnts = l = nsources;
361 >                while ((m = (sn + ncnts) >> 1) != l) {
362 >                        if (cntord[m].brt > 0.0)
363 >                                sn = m;
364 >                        else
365 >                                ncnts = m;
366 >                        l = m;
367 >                }
368 >        }
369 >                                                /* accumulate tail */
370 >        for (sn = ncnts-1; sn > 0; sn--)
371 >                cntord[sn-1].brt += cntord[sn].brt;
372 >                                                /* start with prob=.5 */
373 >        hit2 = 0.5; test2 = 1.0;
374                                                  /* test for shadows */
375 <        for (sn = 0; sn < nsources; sn++) {
375 >        for (sn = 0; sn < ncnts; sn++) {
376                                                  /* check threshold */
377 <                if (srccnt[sn].brt <= shadthresh*bright(r->rcol)/r->rweight)
377 >                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
378 >                                cntord[sn].brt-cntord[sn+nshadcheck].brt)
379 >                                < ourthresh*bright(r->rcol))
380                          break;
381                                                  /* get statistics */
382 <                hwt = (double)source[srccnt[sn].sno].nhits /
383 <                                (double)source[srccnt[sn].sno].ntests;
382 >                hwt = (double)source[cntord[sn].sno].nhits /
383 >                                (double)source[cntord[sn].sno].ntests;
384                  test2 += hwt;
385 <                source[srccnt[sn].sno].ntests++;
385 >                source[cntord[sn].sno].ntests++;
386                                                  /* test for hit */
387                  rayorigin(&sr, r, SHADOW, 1.0);
388 <                VCOPY(sr.rdir, srccnt[sn].dir);
388 >                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
389 >                sr.rsrc = cntord[sn].sno;
390                  if (localhit(&sr, &thescene) &&
391 <                                sr.ro != source[srccnt[sn].sno].so) {
391 >                                sr.ro != source[cntord[sn].sno].so) {
392                                                  /* check for transmission */
393 <                        if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
317 <                                raytrans(&sr);          /* object is clipped */
318 <                        else
319 <                                rayshade(&sr, sr.ro->omod);
393 >                        raycont(&sr);
394                          if (bright(sr.rcol) <= FTINY)
395                                  continue;       /* missed! */
396 <                        (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
397 <                        multcolor(srccnt[sn].val, sr.rcol);
396 >                        (*f)(srccnt[cntord[sn].sno].val, p,
397 >                                        srccnt[cntord[sn].sno].dir,
398 >                                        srccnt[cntord[sn].sno].dom);
399 >                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
400                  }
401                                                  /* add contribution if hit */
402 <                addcolor(r->rcol, srccnt[sn].val);
402 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
403                  hit2 += hwt;
404 <                source[srccnt[sn].sno].nhits++;
404 >                source[cntord[sn].sno].nhits++;
405          }
406 <        if (test2 > FTINY)              /* weighted hit rate */
407 <                hwt = hit2 / test2;
332 <        else
333 <                hwt = 0.0;
406 >                                        /* weighted hit rate */
407 >        hwt = hit2 / test2;
408   #ifdef DEBUG
409 <        fprintf(stderr, "%d tested, %f hit rate\n", sn, hwt);
409 >        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
410 >                        sn, ncnts-sn, hwt);
411 >        eputs(errmsg);
412   #endif
413                                          /* add in untested sources */
414 <        for ( ; sn < nsources; sn++) {
415 <                if (srccnt[sn].brt <= 0.0)
416 <                        break;
417 <                dtmp = hwt * (double)source[srccnt[sn].sno].nhits /
418 <                                (double)source[srccnt[sn].sno].ntests;
343 <                scalecolor(srccnt[sn].val, dtmp);
344 <                addcolor(r->rcol, srccnt[sn].val);
414 >        for ( ; sn < ncnts; sn++) {
415 >                prob = hwt * (double)source[cntord[sn].sno].nhits /
416 >                                (double)source[cntord[sn].sno].ntests;
417 >                scalecolor(srccnt[cntord[sn].sno].val, prob);
418 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
419          }
346                
347        free(srccnt);
420   }
421  
422  
# Line 353 | Line 425 | char  *p;                      /* data for f */
425                                  source[r->rsrc].so!=r->ro)
426  
427   #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
428 <                                !(r->rtype&REFLECTED))  /* hack! */
428 >                                !(r->rtype&REFLECTED) &&        /* hack! */\
429 >                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
430  
431   #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
432                                  !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
# Line 363 | Line 436 | m_light(m, r)                  /* ray hit a light source */
436   register OBJREC  *m;
437   register RAY  *r;
438   {
366                                                /* check for behind */
367        if (r->rod < 0.0)
368                return;
439                                                  /* check for over-counting */
440          if (wrongsource(m, r) || badambient(m, r))
441                  return;
# Line 379 | Line 449 | register RAY  *r;
449  
450                                                  /* otherwise treat as source */
451          } else {
452 +                                                /* check for behind */
453 +                if (r->rod < 0.0)
454 +                        return;
455                                                  /* get distribution pattern */
456                  raytexture(r, m->omod);
457                                                  /* get source color */
# Line 387 | Line 460 | register RAY  *r;
460                                    m->oargs.farg[2]);
461                                                  /* modify value */
462                  multcolor(r->rcol, r->pcol);
463 +                                                /* assign distance */
464 +                r->rt = r->rot;
465          }
466   }
392
393
394 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines