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.15 by greg, Thu Jul 20 13:51:34 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 32 | Line 32 | extern double  shadcert;               /* shadow testing certainty
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 47 | Line 50 | marksources()                  /* find and mark source objects */
50  
51                  m = objptr(o->omod);
52  
53 <                if (m->otype != MAT_LIGHT &&
51 <                                m->otype != MAT_ILLUM &&
52 <                                m->otype != MAT_GLOW &&
53 <                                m->otype != MAT_SPOT)
53 >                if (!islight(m->otype))
54                          continue;
55          
56                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
# Line 68 | 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  
# Line 83 | Line 83 | marksources()                  /* find and mark source objects */
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 98 | 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  
# Line 226 | Line 238 | register int  sn;              /* source number */
238                                                  /* domega constant */
239                  return(source[sn].ss2);
240  
229        else {
241                                                  /* check proximity */
242 <                if (source[sn].sflags & SPROX &&
243 <                                d > source[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 (source[sn].sflags & SSPOT) {
252 <                        if (source[sn].sl.s->siz < 2.0*PI *
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;
245 <                }
246 <                                                /* return domega */
247 <                return(ddot*source[sn].ss2/(d*d));
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 277 | 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);
281 <                r->robs = 1.0; setident4(r->robx);
290 >                r->rox = NULL;
291                  return(1);
292          }
293          return(0);
# Line 304 | Line 313 | char  *p;                      /* data for f */
313   {
314          extern double  pow();
315          register int  sn;
307        register CONTRIB  *srccnt;
308        register CNTPTR  *cntord;
316          int  nshadcheck, ncnts;
317 <        double  prob, ourthresh, hwt, test2, hit2;
317 >        int  nhits;
318 >        double  prob, ourthresh, hwt;
319          RAY  sr;
320 <
321 <        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
322 <        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
315 <        if (srccnt == NULL || cntord == NULL)
316 <                error(SYSTEM, "out of memory in direct");
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 */
# Line 332 | Line 338 | char  *p;                      /* data for f */
338                  if (cntord[sn].brt <= 0.0)
339                          continue;
340                                                  /* compute intersection */
341 <                if (!( source[sn].sflags & SDISTANT ?
341 >                if (source[sn].sflags & SDISTANT ?
342                                  sourcehit(&sr) :
343                                  (*ofun[source[sn].so->otype].funp)
344 <                                (source[sn].so, &sr) ))
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 <                rayshade(&sr, sr.ro->omod);
361 >                raycont(&sr);
362                  multcolor(srccnt[sn].val, sr.rcol);
363                  cntord[sn].brt = bright(srccnt[sn].val);
364          }
# Line 359 | Line 379 | char  *p;                      /* data for f */
379                                                  /* accumulate tail */
380          for (sn = ncnts-1; sn > 0; sn--)
381                  cntord[sn-1].brt += cntord[sn].brt;
362                                                /* start with prob=.5 */
363        hit2 = 0.5; test2 = 1.0;
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))
387 >                                cntord[sn].brt-cntord[sn+nshadcheck].brt)
388 >                                < ourthresh*bright(r->rcol))
389                          break;
390                                                  /* get statistics */
372                hwt = (double)source[cntord[sn].sno].nhits /
373                                (double)source[cntord[sn].sno].ntests;
374                test2 += hwt;
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 <                        rayshade(&sr, sr.ro->omod);
399 >                        raycont(&sr);
400                          if (bright(sr.rcol) <= FTINY)
401                                  continue;       /* missed! */
402                          (*f)(srccnt[cntord[sn].sno].val, p,
# Line 389 | Line 406 | char  *p;                      /* data for f */
406                  }
407                                                  /* add contribution if hit */
408                  addcolor(r->rcol, srccnt[cntord[sn].sno].val);
409 <                hit2 += hwt;
409 >                nhits++;
410                  source[cntord[sn].sno].nhits++;
411          }
412 <                                        /* weighted hit rate */
413 <        hwt = hit2 / test2;
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);
# Line 406 | Line 426 | char  *p;                      /* data for f */
426                  scalecolor(srccnt[cntord[sn].sno].val, prob);
427                  addcolor(r->rcol, srccnt[cntord[sn].sno].val);
428          }
409                
410        free(srccnt);
411        free(cntord);
429   }
430  
431  
# Line 428 | Line 445 | m_light(m, r)                  /* ray hit a light source */
445   register OBJREC  *m;
446   register RAY  *r;
447   {
431                                                /* check for behind */
432        if (r->rod < 0.0)
433                return;
448                                                  /* check for over-counting */
449          if (wrongsource(m, r) || badambient(m, r))
450                  return;
# Line 444 | 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 452 | 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   }
457
458
459 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines