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.3 by greg, Thu Apr 27 12:44:13 1989 UTC vs.
Revision 1.30 by greg, Mon Feb 11 08:43:48 1991 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->aimsuccess = 2*AIMREQT-1;          /* bitch on second failure */
113 +        src->nhits = 1; src->ntests = 2;        /* start probability = 1/2 */
114          src->so = so;
115  
116          switch (so->otype) {
# Line 169 | Line 191 | register int  sn;              /* source number */
191          double  d;
192          register int  i;
193  
194 <        if (srcval[sn].sflags & SSKIP)
194 >        if (source[sn].sflags & SSKIP)
195                  return(0.0);                    /* skip this source */
196  
197          rayorigin(sr, r, SHADOW, 1.0);          /* ignore limits */
198  
199          sr->rsrc = sn;                          /* remember source */
200                                                  /* get source direction */
201 <        if (srcval[sn].sflags & SDISTANT)
201 >        if (source[sn].sflags & SDISTANT)
202                                                  /* constant direction */
203 <                VCOPY(sr->rdir, srcval[sn].sloc);
203 >                VCOPY(sr->rdir, source[sn].sloc);
204          else {                                  /* compute direction */
205                  for (i = 0; i < 3; i++)
206 <                        sr->rdir[i] = srcval[sn].sloc[i] - sr->rorg[i];
206 >                        sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
207  
208 <                if (srcval[sn].so->otype == OBJ_FACE)
209 <                        norm = getface(srcval[sn].so)->norm;
210 <                else if (srcval[sn].so->otype == OBJ_RING)
211 <                        norm = getcone(srcval[sn].so,0)->ad;
208 >                if (source[sn].so->otype == OBJ_FACE)
209 >                        norm = getface(source[sn].so)->norm;
210 >                else if (source[sn].so->otype == OBJ_RING)
211 >                        norm = getcone(source[sn].so,0)->ad;
212  
213                  if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY)
214                          return(0.0);            /* behind surface! */
# Line 194 | Line 216 | register int  sn;              /* source number */
216          if (dstrsrc > FTINY) {
217                                          /* distribute source direction */
218                  for (i = 0; i < 3; i++)
219 <                        vd[i] = dstrsrc * srcval[sn].ss * (1.0 - 2.0*frandom());
219 >                        vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom());
220  
221                  if (norm != NULL) {             /* project offset */
222                          d = DOT(vd, norm);
# Line 204 | Line 226 | register int  sn;              /* source number */
226                  for (i = 0; i < 3; i++)         /* offset source direction */
227                          sr->rdir[i] += vd[i];
228  
229 <        } else if (srcval[sn].sflags & SDISTANT)
229 >        } else if (source[sn].sflags & SDISTANT)
230                                                  /* already normalized */
231 <                return(srcval[sn].ss2);
231 >                return(source[sn].ss2);
232  
233          if ((d = normalize(sr->rdir)) == 0.0)
234                                                  /* at source! */
235                  return(0.0);
236          
237 <        if (srcval[sn].sflags & SDISTANT)
237 >        if (source[sn].sflags & SDISTANT)
238                                                  /* domega constant */
239 <                return(srcval[sn].ss2);
239 >                return(source[sn].ss2);
240  
219        else {
241                                                  /* check proximity */
242 <                if (srcval[sn].sflags & SPROX &&
243 <                                d > srcval[sn].sl.prox)
244 <                        return(0.0);
245 <
246 <                if (norm != NULL)
247 <                        ddot /= d;
248 <                else
249 <                        ddot = 1.0;
242 >        if (source[sn].sflags & SPROX &&
243 >                        d > source[sn].sl.prox)
244 >                return(0.0);
245 >                                                /* compute dot product */
246 >        if (norm != NULL)
247 >                ddot /= d;
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)))
254 <                                return(0.0);
255 <                        d += srcval[sn].sl.s->flen;
235 <                }
236 <                                                /* return domega */
237 <                return(ddot*srcval[sn].ss2/(d*d));
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;     /* adjust length */
256          }
257 +                                                /* compute domega */
258 +        return(ddot*source[sn].ss2/(d*d));
259   }
260  
261  
# Line 251 | Line 271 | register RAY  *r;
271                  first = 0; last = nsources-1;
272          }
273          for (i = first; i <= last; i++)
274 <                if (srcval[i].sflags & SDISTANT)
274 >                if (source[i].sflags & SDISTANT)
275                          /*
276                           * Check to see if ray is within
277                           * solid angle of source.
278                           */
279 <                        if (2.0*PI * (1.0 - DOT(srcval[i].sloc,r->rdir))
280 <                                        <= srcval[i].ss2) {
281 <                                r->ro = srcval[i].so;
282 <                                if (!(srcval[i].sflags & SSKIP))
279 >                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
280 >                                        <= source[i].ss2) {
281 >                                r->ro = source[i].so;
282 >                                if (!(source[i].sflags & SSKIP))
283                                          break;
284                          }
285  
# Line 267 | Line 287 | register RAY  *r;
287                  for (i = 0; i < 3; i++)
288                          r->ron[i] = -r->rdir[i];
289                  r->rod = 1.0;
290 <                r->rofs = 1.0; setident4(r->rofx);
271 <                r->robs = 1.0; setident4(r->robx);
290 >                r->rox = NULL;
291                  return(1);
292          }
293          return(0);
294   }
295  
296  
297 + static int
298 + cntcmp(sc1, sc2)                        /* contribution compare (descending) */
299 + register CNTPTR  *sc1, *sc2;
300 + {
301 +        if (sc1->brt > sc2->brt)
302 +                return(-1);
303 +        if (sc1->brt < sc2->brt)
304 +                return(1);
305 +        return(0);
306 + }
307 +
308 +
309 + direct(r, f, p)                         /* add direct component */
310 + RAY  *r;                        /* ray that hit surface */
311 + int  (*f)();                    /* direct component coefficient function */
312 + char  *p;                       /* data for f */
313 + {
314 +        extern double  pow();
315 +        register int  sn;
316 +        int  nshadcheck, ncnts;
317 +        int  nhits;
318 +        double  prob, ourthresh, hwt;
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 +                        if (source[sn].aimsuccess >= 0)
346 +                                source[sn].aimsuccess++;
347 +                } else {
348 +                        cntord[sn].brt = 0.0;
349 +                        if (source[sn].aimsuccess < 0)
350 +                                continue;       /* bitched already */
351 +                        source[sn].aimsuccess -= AIMREQT;
352 +                        if (source[sn].aimsuccess >= 0)
353 +                                continue;       /* leniency */
354 +                        sprintf(errmsg,
355 +                                "aiming failure for light source \"%s\"",
356 +                                        source[sn].so->oname);
357 +                        error(WARNING, errmsg);
358 +                        continue;
359 +                }
360 +                                                /* compute contribution */
361 +                raycont(&sr);
362 +                multcolor(srccnt[sn].val, sr.rcol);
363 +                cntord[sn].brt = bright(srccnt[sn].val);
364 +        }
365 +                                                /* sort contributions */
366 +        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
367 +        {                                       /* find last */
368 +                register int  l, m;
369 +
370 +                sn = 0; ncnts = l = nsources;
371 +                while ((m = (sn + ncnts) >> 1) != l) {
372 +                        if (cntord[m].brt > 0.0)
373 +                                sn = m;
374 +                        else
375 +                                ncnts = m;
376 +                        l = m;
377 +                }
378 +        }
379 +                                                /* accumulate tail */
380 +        for (sn = ncnts-1; sn > 0; sn--)
381 +                cntord[sn-1].brt += cntord[sn].brt;
382 +                                                /* test for shadows */
383 +        nhits = 0;
384 +        for (sn = 0; sn < ncnts; sn++) {
385 +                                                /* check threshold */
386 +                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
387 +                                cntord[sn].brt-cntord[sn+nshadcheck].brt)
388 +                                < ourthresh*bright(r->rcol))
389 +                        break;
390 +                                                /* get statistics */
391 +                source[cntord[sn].sno].ntests++;
392 +                                                /* test for hit */
393 +                rayorigin(&sr, r, SHADOW, 1.0);
394 +                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
395 +                sr.rsrc = cntord[sn].sno;
396 +                if (localhit(&sr, &thescene) &&
397 +                                sr.ro != source[cntord[sn].sno].so) {
398 +                                                /* check for transmission */
399 +                        raycont(&sr);
400 +                        if (bright(sr.rcol) <= FTINY)
401 +                                continue;       /* missed! */
402 +                        (*f)(srccnt[cntord[sn].sno].val, p,
403 +                                        srccnt[cntord[sn].sno].dir,
404 +                                        srccnt[cntord[sn].sno].dom);
405 +                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
406 +                }
407 +                                                /* add contribution if hit */
408 +                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
409 +                nhits++;
410 +                source[cntord[sn].sno].nhits++;
411 +        }
412 +                                        /* surface hit rate */
413 +        if (sn > 0)
414 +                hwt = (double)nhits / (double)sn;
415 +        else
416 +                hwt = 0.5;
417 + #ifdef DEBUG
418 +        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
419 +                        sn, ncnts-sn, hwt);
420 +        eputs(errmsg);
421 + #endif
422 +                                        /* add in untested sources */
423 +        for ( ; sn < ncnts; sn++) {
424 +                prob = hwt * (double)source[cntord[sn].sno].nhits /
425 +                                (double)source[cntord[sn].sno].ntests;
426 +                scalecolor(srccnt[cntord[sn].sno].val, prob);
427 +                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
428 +        }
429 + }
430 +
431 +
432   #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
433                                  r->rsrc>=0 && \
434 <                                srcval[r->rsrc].so!=r->ro)
434 >                                source[r->rsrc].so!=r->ro)
435  
436   #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
437                                  !(r->rtype&REFLECTED) &&        /* hack! */\
438                                  !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
439  
440   #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
441 <                                !(r->rsrc>=0&&srcval[r->rsrc].so==r->ro))
441 >                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
442  
443  
444   m_light(m, r)                   /* ray hit a light source */
445   register OBJREC  *m;
446   register RAY  *r;
447   {
294                                                /* check for behind */
295        if (r->rod < 0.0)
296                return;
448                                                  /* check for over-counting */
449          if (wrongsource(m, r) || badambient(m, r))
450                  return;
# Line 307 | Line 458 | register RAY  *r;
458  
459                                                  /* otherwise treat as source */
460          } else {
461 +                                                /* check for behind */
462 +                if (r->rod < 0.0)
463 +                        return;
464                                                  /* get distribution pattern */
465                  raytexture(r, m->omod);
466                                                  /* get source color */
# Line 315 | Line 469 | register RAY  *r;
469                                    m->oargs.farg[2]);
470                                                  /* modify value */
471                  multcolor(r->rcol, r->pcol);
472 +                                                /* assign distance */
473 +                r->rt = r->rot;
474          }
475   }
320
321
322 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines