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.2 by greg, Fri Mar 3 20:58:33 1989 UTC vs.
Revision 1.13 by greg, Mon Jun 19 11:44:58 1989 UTC

# 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  
# 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 >                        error(SYSTEM, "out of memory in marksources");
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          }
# Line 78 | Line 87 | marksources()                  /* find and mark source objects */
87  
88  
89   newsource(src, so)                      /* add a source to the array */
90 < register SOURCE  *src;
90 > register SRCREC  *src;
91   register OBJREC  *so;
92   {
93          double  cos(), tan(), sqrt();
# Line 89 | Line 98 | register OBJREC  *so;
98          register int  i;
99          
100          src->sflags = 0;
101 +        src->nhits = 1; src->ntests = 2;        /* start probability = 1/2 */
102          src->so = so;
103  
104          switch (so->otype) {
# Line 169 | Line 179 | register int  sn;              /* source number */
179          double  d;
180          register int  i;
181  
182 <        if (srcval[sn].sflags & SSKIP)
182 >        if (source[sn].sflags & SSKIP)
183                  return(0.0);                    /* skip this source */
184  
185          rayorigin(sr, r, SHADOW, 1.0);          /* ignore limits */
186  
187          sr->rsrc = sn;                          /* remember source */
188                                                  /* get source direction */
189 <        if (srcval[sn].sflags & SDISTANT)
189 >        if (source[sn].sflags & SDISTANT)
190                                                  /* constant direction */
191 <                VCOPY(sr->rdir, srcval[sn].sloc);
191 >                VCOPY(sr->rdir, source[sn].sloc);
192          else {                                  /* compute direction */
193                  for (i = 0; i < 3; i++)
194 <                        sr->rdir[i] = srcval[sn].sloc[i] - sr->rorg[i];
194 >                        sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
195  
196 <                if (srcval[sn].so->otype == OBJ_FACE)
197 <                        norm = getface(srcval[sn].so)->norm;
198 <                else if (srcval[sn].so->otype == OBJ_RING)
199 <                        norm = getcone(srcval[sn].so,0)->ad;
196 >                if (source[sn].so->otype == OBJ_FACE)
197 >                        norm = getface(source[sn].so)->norm;
198 >                else if (source[sn].so->otype == OBJ_RING)
199 >                        norm = getcone(source[sn].so,0)->ad;
200  
201                  if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY)
202                          return(0.0);            /* behind surface! */
# Line 194 | Line 204 | register int  sn;              /* source number */
204          if (dstrsrc > FTINY) {
205                                          /* distribute source direction */
206                  for (i = 0; i < 3; i++)
207 <                        vd[i] = dstrsrc * srcval[sn].ss * (1.0 - 2.0*frandom());
207 >                        vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom());
208  
209                  if (norm != NULL) {             /* project offset */
210                          d = DOT(vd, norm);
# Line 204 | Line 214 | register int  sn;              /* source number */
214                  for (i = 0; i < 3; i++)         /* offset source direction */
215                          sr->rdir[i] += vd[i];
216  
217 <        } else if (srcval[sn].sflags & SDISTANT)
217 >        } else if (source[sn].sflags & SDISTANT)
218                                                  /* already normalized */
219 <                return(srcval[sn].ss2);
219 >                return(source[sn].ss2);
220  
221          if ((d = normalize(sr->rdir)) == 0.0)
222                                                  /* at source! */
223                  return(0.0);
224          
225 <        if (srcval[sn].sflags & SDISTANT)
225 >        if (source[sn].sflags & SDISTANT)
226                                                  /* domega constant */
227 <                return(srcval[sn].ss2);
227 >                return(source[sn].ss2);
228  
229          else {
230                                                  /* check proximity */
231 <                if (srcval[sn].sflags & SPROX &&
232 <                                d > srcval[sn].sl.prox)
231 >                if (source[sn].sflags & SPROX &&
232 >                                d > source[sn].sl.prox)
233                          return(0.0);
234  
235                  if (norm != NULL)
# Line 227 | Line 237 | register int  sn;              /* source number */
237                  else
238                          ddot = 1.0;
239                                                  /* check angle */
240 <                if (srcval[sn].sflags & SSPOT) {
241 <                        if (srcval[sn].sl.s->siz < 2.0*PI *
242 <                                (1.0 + DOT(srcval[sn].sl.s->aim,sr->rdir)))
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 += srcval[sn].sl.s->flen;
244 >                        d += source[sn].sl.s->flen;
245                  }
246                                                  /* return domega */
247 <                return(ddot*srcval[sn].ss2/(d*d));
247 >                return(ddot*source[sn].ss2/(d*d));
248          }
249   }
250  
# Line 251 | Line 261 | register RAY  *r;
261                  first = 0; last = nsources-1;
262          }
263          for (i = first; i <= last; i++)
264 <                if (srcval[i].sflags & SDISTANT)
264 >                if (source[i].sflags & SDISTANT)
265                          /*
266                           * Check to see if ray is within
267                           * solid angle of source.
268                           */
269 <                        if (2.0*PI * (1.0 - DOT(srcval[i].sloc,r->rdir))
270 <                                        <= srcval[i].ss2) {
271 <                                r->ro = srcval[i].so;
272 <                                if (!(srcval[i].sflags & SSKIP))
269 >                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
270 >                                        <= source[i].ss2) {
271 >                                r->ro = source[i].so;
272 >                                if (!(source[i].sflags & SSKIP))
273                                          break;
274                          }
275  
# Line 267 | Line 277 | register RAY  *r;
277                  for (i = 0; i < 3; i++)
278                          r->ron[i] = -r->rdir[i];
279                  r->rod = 1.0;
280 +                r->rofs = 1.0; setident4(r->rofx);
281 +                r->robs = 1.0; setident4(r->robx);
282                  return(1);
283          }
284          return(0);
285   }
286  
287  
288 + static int
289 + cntcmp(sc1, sc2)                        /* contribution compare (descending) */
290 + register CNTPTR  *sc1, *sc2;
291 + {
292 +        if (sc1->brt > sc2->brt)
293 +                return(-1);
294 +        if (sc1->brt < sc2->brt)
295 +                return(1);
296 +        return(0);
297 + }
298 +
299 +
300 + direct(r, f, p)                         /* add direct component */
301 + 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 +        register CNTPTR  *cntord;
309 +        int  nshadcheck, ncnts;
310 +        double  prob, ourthresh, hwt, test2, hit2;
311 +        RAY  sr;
312 +
313 +        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
314 +        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
315 +        if (srccnt == NULL || cntord == NULL)
316 +                error(SYSTEM, "out of memory in direct");
317 +                                                /* compute number to check */
318 +        nshadcheck = pow((double)nsources, shadcert) + .5;
319 +                                                /* modify threshold */
320 +        ourthresh = shadthresh / r->rweight;
321 +                                                /* potential contributions */
322 +        for (sn = 0; sn < nsources; sn++) {
323 +                cntord[sn].sno = sn;
324 +                cntord[sn].brt = 0.0;
325 +                                                /* get source ray */
326 +                if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
327 +                        continue;
328 +                VCOPY(srccnt[sn].dir, sr.rdir);
329 +                                                /* compute coefficient */
330 +                (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
331 +                cntord[sn].brt = bright(srccnt[sn].val);
332 +                if (cntord[sn].brt <= FTINY)
333 +                        continue;
334 +                                                /* compute intersection */
335 +                if (!( source[sn].sflags & SDISTANT ?
336 +                                sourcehit(&sr) :
337 +                                (*ofun[source[sn].so->otype].funp)
338 +                                (source[sn].so, &sr) ))
339 +                        continue;
340 +                                                /* compute contribution */
341 +                rayshade(&sr, sr.ro->omod);
342 +                multcolor(srccnt[sn].val, sr.rcol);
343 +                cntord[sn].brt = bright(srccnt[sn].val);
344 +        }
345 +                                                /* sort contributions */
346 +        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
347 +        {                                       /* find last */
348 +                register int  l, m;
349 +
350 +                sn = 0; ncnts = l = nsources;
351 +                while ((m = (sn + ncnts) >> 1) != l) {
352 +                        if (cntord[m].brt > 0.0)
353 +                                sn = m;
354 +                        else
355 +                                ncnts = m;
356 +                        l = m;
357 +                }
358 +        }
359 +                                                /* accumulate tail */
360 +        for (sn = ncnts-1; sn > 0; sn--)
361 +                cntord[sn-1].brt += cntord[sn].brt;
362 +                                                /* start with prob=.5 */
363 +        hit2 = 0.5; test2 = 1.0;
364 +                                                /* test for shadows */
365 +        for (sn = 0; sn < ncnts; sn++) {
366 +                                                /* check threshold */
367 +                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
368 +                                cntord[sn].brt-cntord[sn+nshadcheck].brt) <
369 +                                ourthresh*bright(r->rcol))
370 +                        break;
371 +                                                /* get statistics */
372 +                hwt = (double)source[cntord[sn].sno].nhits /
373 +                                (double)source[cntord[sn].sno].ntests;
374 +                test2 += hwt;
375 +                source[cntord[sn].sno].ntests++;
376 +                                                /* test for hit */
377 +                rayorigin(&sr, r, SHADOW, 1.0);
378 +                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
379 +                if (localhit(&sr, &thescene) &&
380 +                                sr.ro != source[cntord[sn].sno].so) {
381 +                                                /* check for transmission */
382 +                        if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
383 +                                raytrans(&sr);          /* object is clipped */
384 +                        else
385 +                                rayshade(&sr, sr.ro->omod);
386 +                        if (bright(sr.rcol) <= FTINY)
387 +                                continue;       /* missed! */
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[cntord[sn].sno].val);
395 +                hit2 += hwt;
396 +                source[cntord[sn].sno].nhits++;
397 +        }
398 +                                        /* weighted hit rate */
399 +        hwt = hit2 / test2;
400 + #ifdef DEBUG
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 < 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);
414 +        free(cntord);
415 + }
416 +
417 +
418   #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
419                                  r->rsrc>=0 && \
420 <                                srcval[r->rsrc].so!=r->ro)
420 >                                source[r->rsrc].so!=r->ro)
421  
422   #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
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&&srcval[r->rsrc].so==r->ro))
427 >                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
428  
429  
430   m_light(m, r)                   /* ray hit a light source */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines