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.1 by greg, Thu Feb 2 10:41:41 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 12 | Line 12 | static char SCCSid[] = "$SunId$ LBL";
12  
13   #include  "ray.h"
14  
15 + #include  "octree.h"
16 +
17   #include  "source.h"
18  
19   #include  "otypes.h"
# Line 24 | Line 26 | static char SCCSid[] = "$SunId$ LBL";
26  
27  
28   extern double  dstrsrc;                 /* source distribution amount */
29 + extern double  shadthresh;              /* relative shadow threshold */
30 + extern double  shadcert;                /* shadow testing certainty */
31  
32 < SOURCE  srcval[MAXSOURCE];              /* our array of sources */
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 43 | Line 50 | marksources()                  /* find and mark source objects */
50  
51                  m = objptr(o->omod);
52  
53 <                if (m->otype != MAT_LIGHT &&
47 <                                m->otype != MAT_ILLUM &&
48 <                                m->otype != MAT_GLOW &&
49 <                                m->otype != MAT_SPOT)
53 >                if (!islight(m->otype))
54                          continue;
55          
56                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
# Line 58 | Line 62 | marksources()                  /* find and mark source objects */
62                                  m->oargs.farg[3] <= FTINY)
63                          continue;                       /* don't bother */
64  
65 <                if (nsources >= MAXSOURCE)
66 <                        error(INTERNAL, "too many sources in marksources");
65 >                if (source == NULL)
66 >                        source = (SRCREC *)malloc(sizeof(SRCREC));
67 >                else
68 >                        source = (SRCREC *)realloc((char *)source,
69 >                                        (unsigned)(nsources+1)*sizeof(SRCREC));
70 >                if (source == NULL)
71 >                        goto memerr;
72  
73 <                newsource(&srcval[nsources], o);
73 >                newsource(&source[nsources], o);
74  
75                  if (m->otype == MAT_GLOW) {
76 <                        srcval[nsources].sflags |= SPROX;
77 <                        srcval[nsources].sl.prox = m->oargs.farg[3];
76 >                        source[nsources].sflags |= SPROX;
77 >                        source[nsources].sl.prox = m->oargs.farg[3];
78                          if (o->otype == OBJ_SOURCE)
79 <                                srcval[nsources].sflags |= SSKIP;
79 >                                source[nsources].sflags |= SSKIP;
80                  } else if (m->otype == MAT_SPOT) {
81 <                        srcval[nsources].sflags |= SSPOT;
82 <                        srcval[nsources].sl.s = makespot(m);
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  
100   newsource(src, so)                      /* add a source to the array */
101 < register SOURCE  *src;
101 > register SRCREC  *src;
102   register OBJREC  *so;
103   {
104          double  cos(), tan(), sqrt();
# Line 89 | Line 109 | register OBJREC  *so;
109          register int  i;
110          
111          src->sflags = 0;
112 +        src->nhits = 1; src->ntests = 2;        /* start probability = 1/2 */
113          src->so = so;
114  
115          switch (so->otype) {
# Line 169 | Line 190 | register int  sn;              /* source number */
190          double  d;
191          register int  i;
192  
193 <        if (srcval[sn].sflags & SSKIP)
193 >        if (source[sn].sflags & SSKIP)
194                  return(0.0);                    /* skip this source */
195  
196          rayorigin(sr, r, SHADOW, 1.0);          /* ignore limits */
197  
198          sr->rsrc = sn;                          /* remember source */
199                                                  /* get source direction */
200 <        if (srcval[sn].sflags & SDISTANT)
200 >        if (source[sn].sflags & SDISTANT)
201                                                  /* constant direction */
202 <                VCOPY(sr->rdir, srcval[sn].sloc);
202 >                VCOPY(sr->rdir, source[sn].sloc);
203          else {                                  /* compute direction */
204                  for (i = 0; i < 3; i++)
205 <                        sr->rdir[i] = srcval[sn].sloc[i] - sr->rorg[i];
205 >                        sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
206  
207 <                if (srcval[sn].so->otype == OBJ_FACE)
208 <                        norm = getface(srcval[sn].so)->norm;
209 <                else if (srcval[sn].so->otype == OBJ_RING)
210 <                        norm = getcone(srcval[sn].so,0)->ad;
207 >                if (source[sn].so->otype == OBJ_FACE)
208 >                        norm = getface(source[sn].so)->norm;
209 >                else if (source[sn].so->otype == OBJ_RING)
210 >                        norm = getcone(source[sn].so,0)->ad;
211  
212 <                if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= 0.0)
212 >                if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY)
213                          return(0.0);            /* behind surface! */
214          }
215          if (dstrsrc > FTINY) {
216                                          /* distribute source direction */
217                  for (i = 0; i < 3; i++)
218 <                        vd[i] = dstrsrc * srcval[sn].ss * (1.0 - 2.0*frandom());
218 >                        vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom());
219  
220                  if (norm != NULL) {             /* project offset */
221                          d = DOT(vd, norm);
# Line 204 | Line 225 | register int  sn;              /* source number */
225                  for (i = 0; i < 3; i++)         /* offset source direction */
226                          sr->rdir[i] += vd[i];
227  
228 <        } else if (srcval[sn].sflags & SDISTANT)
228 >        } else if (source[sn].sflags & SDISTANT)
229                                                  /* already normalized */
230 <                return(srcval[sn].ss2);
230 >                return(source[sn].ss2);
231  
232          if ((d = normalize(sr->rdir)) == 0.0)
233                                                  /* at source! */
234                  return(0.0);
235          
236 <        if (srcval[sn].sflags & SDISTANT)
236 >        if (source[sn].sflags & SDISTANT)
237                                                  /* domega constant */
238 <                return(srcval[sn].ss2);
238 >                return(source[sn].ss2);
239  
240          else {
241                                                  /* check proximity */
242 <                if (srcval[sn].sflags & SPROX &&
243 <                                d > srcval[sn].sl.prox)
242 >                if (source[sn].sflags & SPROX &&
243 >                                d > source[sn].sl.prox)
244                          return(0.0);
245  
246                  if (norm != NULL)
# Line 227 | Line 248 | register int  sn;              /* source number */
248                  else
249                          ddot = 1.0;
250                                                  /* check angle */
251 <                if (srcval[sn].sflags & SSPOT) {
252 <                        if (srcval[sn].sl.s->siz < 2.0*PI *
253 <                                (1.0 + DOT(srcval[sn].sl.s->aim,sr->rdir)))
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 += srcval[sn].sl.s->flen;
255 >                        d += source[sn].sl.s->flen;
256                  }
257                                                  /* return domega */
258 <                return(ddot*srcval[sn].ss2/(d*d));
258 >                return(ddot*source[sn].ss2/(d*d));
259          }
260   }
261  
# Line 251 | Line 272 | register RAY  *r;
272                  first = 0; last = nsources-1;
273          }
274          for (i = first; i <= last; i++)
275 <                if (srcval[i].sflags & SDISTANT)
275 >                if (source[i].sflags & SDISTANT)
276                          /*
277                           * Check to see if ray is within
278                           * solid angle of source.
279                           */
280 <                        if (2.0*PI * (1.0 - DOT(srcval[i].sloc,r->rdir))
281 <                                        <= srcval[i].ss2) {
282 <                                r->ro = srcval[i].so;
283 <                                if (!(srcval[i].sflags & SSKIP))
280 >                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
281 >                                        <= source[i].ss2) {
282 >                                r->ro = source[i].so;
283 >                                if (!(source[i].sflags & SSKIP))
284                                          break;
285                          }
286  
# Line 267 | 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->rox = NULL;
292                  return(1);
293          }
294          return(0);
295   }
296  
297  
298 + static int
299 + cntcmp(sc1, sc2)                        /* contribution compare (descending) */
300 + register CNTPTR  *sc1, *sc2;
301 + {
302 +        if (sc1->brt > sc2->brt)
303 +                return(-1);
304 +        if (sc1->brt < sc2->brt)
305 +                return(1);
306 +        return(0);
307 + }
308 +
309 +
310 + direct(r, f, p)                         /* add direct component */
311 + 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 +        int  nshadcheck, ncnts;
318 +        double  prob, ourthresh, hwt, test2, hit2;
319 +        RAY  sr;
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 */
328 +        for (sn = 0; sn < nsources; sn++) {
329 +                cntord[sn].sno = sn;
330 +                cntord[sn].brt = 0.0;
331 +                                                /* get source ray */
332 +                if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
333 +                        continue;
334 +                VCOPY(srccnt[sn].dir, sr.rdir);
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 <= 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) )) {
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 +                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 +        {                                       /* 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 +                                                /* start with prob=.5 */
374 +        hit2 = 0.5; test2 = 1.0;
375 +                                                /* test for shadows */
376 +        for (sn = 0; sn < ncnts; sn++) {
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 /
384 +                                (double)source[cntord[sn].sno].ntests;
385 +                test2 += hwt;
386 +                source[cntord[sn].sno].ntests++;
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 +                        raycont(&sr);
395 +                        if (bright(sr.rcol) <= FTINY)
396 +                                continue;       /* missed! */
397 +                        (*f)(srccnt[cntord[sn].sno].val, p,
398 +                                        srccnt[cntord[sn].sno].dir,
399 +                                        srccnt[cntord[sn].sno].dom);
400 +                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
401 +                }
402 +                                                /* add contribution if hit */
403 +                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
404 +                hit2 += hwt;
405 +                source[cntord[sn].sno].nhits++;
406 +        }
407 +                                        /* weighted hit rate */
408 +        hwt = hit2 / test2;
409 + #ifdef DEBUG
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++) {
416 +                prob = hwt * (double)source[cntord[sn].sno].nhits /
417 +                                (double)source[cntord[sn].sno].ntests;
418 +                scalecolor(srccnt[cntord[sn].sno].val, prob);
419 +                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
420 +        }
421 + }
422 +
423 +
424   #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
425                                  r->rsrc>=0 && \
426 <                                srcval[r->rsrc].so!=r->ro)
426 >                                source[r->rsrc].so!=r->ro)
427  
428   #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
429                                  !(r->rtype&REFLECTED) &&        /* hack! */\
430                                  !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
431  
432   #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
433 <                                !(r->rsrc>=0&&srcval[r->rsrc].so==r->ro))
433 >                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
434  
435  
436   m_light(m, r)                   /* ray hit a light source */
437   register OBJREC  *m;
438   register RAY  *r;
439   {
292                                                /* check for behind */
293        if (r->rod < 0.0)
294                return;
440                                                  /* check for over-counting */
441          if (wrongsource(m, r) || badambient(m, r))
442                  return;
# Line 305 | 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 313 | 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   }
318
319
320 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines