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.10 by greg, Thu Jun 8 17:04:43 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  
31 < SOURCE  srcval[MAXSOURCE];              /* our array of sources */
31 > SRCREC  *source = NULL;                 /* our list of sources */
32   int  nsources = 0;                      /* the number of sources */
33  
34  
# Line 58 | Line 61 | marksources()                  /* find and mark source objects */
61                                  m->oargs.farg[3] <= FTINY)
62                          continue;                       /* don't bother */
63  
64 <                if (nsources >= MAXSOURCE)
65 <                        error(INTERNAL, "too many sources in marksources");
64 >                if (source == NULL)
65 >                        source = (SRCREC *)malloc(sizeof(SRCREC));
66 >                else
67 >                        source = (SRCREC *)realloc((char *)source,
68 >                                        (unsigned)(nsources+1)*sizeof(SRCREC));
69 >                if (source == NULL)
70 >                        error(SYSTEM, "out of memory in marksources");
71  
72 <                newsource(&srcval[nsources], o);
72 >                newsource(&source[nsources], o);
73  
74                  if (m->otype == MAT_GLOW) {
75 <                        srcval[nsources].sflags |= SPROX;
76 <                        srcval[nsources].sl.prox = m->oargs.farg[3];
75 >                        source[nsources].sflags |= SPROX;
76 >                        source[nsources].sl.prox = m->oargs.farg[3];
77                          if (o->otype == OBJ_SOURCE)
78 <                                srcval[nsources].sflags |= SSKIP;
78 >                                source[nsources].sflags |= SSKIP;
79                  } else if (m->otype == MAT_SPOT) {
80 <                        srcval[nsources].sflags |= SSPOT;
81 <                        srcval[nsources].sl.s = makespot(m);
80 >                        source[nsources].sflags |= SSPOT;
81 >                        source[nsources].sl.s = makespot(m);
82                  }
83                  nsources++;
84          }
# Line 78 | Line 86 | marksources()                  /* find and mark source objects */
86  
87  
88   newsource(src, so)                      /* add a source to the array */
89 < register SOURCE  *src;
89 > register SRCREC  *src;
90   register OBJREC  *so;
91   {
92          double  cos(), tan(), sqrt();
# Line 89 | Line 97 | register OBJREC  *so;
97          register int  i;
98          
99          src->sflags = 0;
100 +        src->nhits = 1; src->ntests = 2;        /* start probability = 1/2 */
101          src->so = so;
102  
103          switch (so->otype) {
# Line 169 | Line 178 | register int  sn;              /* source number */
178          double  d;
179          register int  i;
180  
181 <        if (srcval[sn].sflags & SSKIP)
181 >        if (source[sn].sflags & SSKIP)
182                  return(0.0);                    /* skip this source */
183  
184          rayorigin(sr, r, SHADOW, 1.0);          /* ignore limits */
185  
186          sr->rsrc = sn;                          /* remember source */
187                                                  /* get source direction */
188 <        if (srcval[sn].sflags & SDISTANT)
188 >        if (source[sn].sflags & SDISTANT)
189                                                  /* constant direction */
190 <                VCOPY(sr->rdir, srcval[sn].sloc);
190 >                VCOPY(sr->rdir, source[sn].sloc);
191          else {                                  /* compute direction */
192                  for (i = 0; i < 3; i++)
193 <                        sr->rdir[i] = srcval[sn].sloc[i] - sr->rorg[i];
193 >                        sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
194  
195 <                if (srcval[sn].so->otype == OBJ_FACE)
196 <                        norm = getface(srcval[sn].so)->norm;
197 <                else if (srcval[sn].so->otype == OBJ_RING)
198 <                        norm = getcone(srcval[sn].so,0)->ad;
195 >                if (source[sn].so->otype == OBJ_FACE)
196 >                        norm = getface(source[sn].so)->norm;
197 >                else if (source[sn].so->otype == OBJ_RING)
198 >                        norm = getcone(source[sn].so,0)->ad;
199  
200                  if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY)
201                          return(0.0);            /* behind surface! */
# Line 194 | Line 203 | register int  sn;              /* source number */
203          if (dstrsrc > FTINY) {
204                                          /* distribute source direction */
205                  for (i = 0; i < 3; i++)
206 <                        vd[i] = dstrsrc * srcval[sn].ss * (1.0 - 2.0*frandom());
206 >                        vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom());
207  
208                  if (norm != NULL) {             /* project offset */
209                          d = DOT(vd, norm);
# Line 204 | Line 213 | register int  sn;              /* source number */
213                  for (i = 0; i < 3; i++)         /* offset source direction */
214                          sr->rdir[i] += vd[i];
215  
216 <        } else if (srcval[sn].sflags & SDISTANT)
216 >        } else if (source[sn].sflags & SDISTANT)
217                                                  /* already normalized */
218 <                return(srcval[sn].ss2);
218 >                return(source[sn].ss2);
219  
220          if ((d = normalize(sr->rdir)) == 0.0)
221                                                  /* at source! */
222                  return(0.0);
223          
224 <        if (srcval[sn].sflags & SDISTANT)
224 >        if (source[sn].sflags & SDISTANT)
225                                                  /* domega constant */
226 <                return(srcval[sn].ss2);
226 >                return(source[sn].ss2);
227  
228          else {
229                                                  /* check proximity */
230 <                if (srcval[sn].sflags & SPROX &&
231 <                                d > srcval[sn].sl.prox)
230 >                if (source[sn].sflags & SPROX &&
231 >                                d > source[sn].sl.prox)
232                          return(0.0);
233  
234                  if (norm != NULL)
# Line 227 | Line 236 | register int  sn;              /* source number */
236                  else
237                          ddot = 1.0;
238                                                  /* check angle */
239 <                if (srcval[sn].sflags & SSPOT) {
240 <                        if (srcval[sn].sl.s->siz < 2.0*PI *
241 <                                (1.0 + DOT(srcval[sn].sl.s->aim,sr->rdir)))
239 >                if (source[sn].sflags & SSPOT) {
240 >                        if (source[sn].sl.s->siz < 2.0*PI *
241 >                                (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
242                                  return(0.0);
243 <                        d += srcval[sn].sl.s->flen;
243 >                        d += source[sn].sl.s->flen;
244                  }
245                                                  /* return domega */
246 <                return(ddot*srcval[sn].ss2/(d*d));
246 >                return(ddot*source[sn].ss2/(d*d));
247          }
248   }
249  
# Line 251 | Line 260 | register RAY  *r;
260                  first = 0; last = nsources-1;
261          }
262          for (i = first; i <= last; i++)
263 <                if (srcval[i].sflags & SDISTANT)
263 >                if (source[i].sflags & SDISTANT)
264                          /*
265                           * Check to see if ray is within
266                           * solid angle of source.
267                           */
268 <                        if (2.0*PI * (1.0 - DOT(srcval[i].sloc,r->rdir))
269 <                                        <= srcval[i].ss2) {
270 <                                r->ro = srcval[i].so;
271 <                                if (!(srcval[i].sflags & SSKIP))
268 >                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
269 >                                        <= source[i].ss2) {
270 >                                r->ro = source[i].so;
271 >                                if (!(source[i].sflags & SSKIP))
272                                          break;
273                          }
274  
# Line 275 | Line 284 | register RAY  *r;
284   }
285  
286  
287 + static int
288 + cntcmp(sc1, sc2)                        /* contribution compare (descending) */
289 + register CNTPTR  *sc1, *sc2;
290 + {
291 +        if (sc1->brt > sc2->brt)
292 +                return(-1);
293 +        if (sc1->brt < sc2->brt)
294 +                return(1);
295 +        return(0);
296 + }
297 +
298 +
299 + direct(r, f, p)                         /* add direct component */
300 + RAY  *r;                        /* ray that hit surface */
301 + int  (*f)();                    /* direct component coefficient function */
302 + char  *p;                       /* data for f */
303 + {
304 +        register int  sn;
305 +        register CONTRIB  *srccnt;
306 +        register CNTPTR  *cntord;
307 +        double  prob, ourthresh, hwt, test2, hit2;
308 +        RAY  sr;
309 +
310 +        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
311 +        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
312 +        if (srccnt == NULL || cntord == NULL)
313 +                error(SYSTEM, "out of memory in direct");
314 +                                                /* modify threshold */
315 +        ourthresh = shadthresh / r->rweight;
316 +                                                /* potential contributions */
317 +        for (sn = 0; sn < nsources; sn++) {
318 +                cntord[sn].sno = sn;
319 +                cntord[sn].brt = 0.0;
320 +                                                /* get source ray */
321 +                if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
322 +                        continue;
323 +                VCOPY(srccnt[sn].dir, sr.rdir);
324 +                                                /* compute coefficient */
325 +                (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
326 +                cntord[sn].brt = bright(srccnt[sn].val);
327 +                if (cntord[sn].brt <= FTINY)
328 +                        continue;
329 +                                                /* compute intersection */
330 +                if (!( source[sn].sflags & SDISTANT ?
331 +                                sourcehit(&sr) :
332 +                                (*ofun[source[sn].so->otype].funp)
333 +                                (source[sn].so, &sr) ))
334 +                        continue;
335 +                                                /* compute contribution */
336 +                rayshade(&sr, sr.ro->omod);
337 +                multcolor(srccnt[sn].val, sr.rcol);
338 +                cntord[sn].brt = bright(srccnt[sn].val);
339 +        }
340 +                                                /* sort contributions */
341 +        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
342 +        hit2 = 0.0; test2 = FTINY;
343 +                                                /* test for shadows */
344 +        for (sn = 0; sn < nsources; sn++) {
345 +                                                /* check threshold */
346 +                if (cntord[sn].brt <= ourthresh*bright(r->rcol))
347 +                        break;
348 +                                                /* get statistics */
349 +                hwt = (double)source[cntord[sn].sno].nhits /
350 +                                (double)source[cntord[sn].sno].ntests;
351 +                test2 += hwt;
352 +                source[cntord[sn].sno].ntests++;
353 +                                                /* test for hit */
354 +                rayorigin(&sr, r, SHADOW, 1.0);
355 +                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
356 +                if (localhit(&sr, &thescene) &&
357 +                                sr.ro != source[cntord[sn].sno].so) {
358 +                                                /* check for transmission */
359 +                        if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
360 +                                raytrans(&sr);          /* object is clipped */
361 +                        else
362 +                                rayshade(&sr, sr.ro->omod);
363 +                        if (bright(sr.rcol) <= FTINY)
364 +                                continue;       /* missed! */
365 +                        (*f)(srccnt[cntord[sn].sno].val, p,
366 +                                        srccnt[cntord[sn].sno].dir,
367 +                                        srccnt[cntord[sn].sno].dom);
368 +                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
369 +                }
370 +                                                /* add contribution if hit */
371 +                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
372 +                hit2 += hwt;
373 +                source[cntord[sn].sno].nhits++;
374 +        }
375 +                                        /* weighted hit rate */
376 +        hwt = hit2 / test2;
377 + #ifdef DEBUG
378 +        {
379 +                int  ntested = sn;
380 + #endif
381 +                                        /* add in untested sources */
382 +        for ( ; sn < nsources; sn++) {
383 +                if (cntord[sn].brt <= 0.0)
384 +                        break;
385 +                prob = hwt * (double)source[cntord[sn].sno].nhits /
386 +                                (double)source[cntord[sn].sno].ntests;
387 +                scalecolor(srccnt[cntord[sn].sno].val, prob);
388 +                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
389 +        }
390 + #ifdef DEBUG
391 +        fprintf(stderr, "%d tested, %d untested, %f hit rate\n",
392 +                        ntested, sn-ntested, hwt);
393 +        }
394 + #endif
395 +                
396 +        free(srccnt);
397 +        free(cntord);
398 + }
399 +
400 +
401   #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
402                                  r->rsrc>=0 && \
403 <                                srcval[r->rsrc].so!=r->ro)
403 >                                source[r->rsrc].so!=r->ro)
404  
405   #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
406                                  !(r->rtype&REFLECTED) &&        /* hack! */\
407                                  !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
408  
409   #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
410 <                                !(r->rsrc>=0&&srcval[r->rsrc].so==r->ro))
410 >                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
411  
412  
413   m_light(m, r)                   /* ray hit a light source */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines