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.27 by greg, Mon Dec 17 14:38:12 1990 UTC vs.
Revision 1.41 by greg, Tue Aug 6 14:07:03 1991 UTC

# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "octree.h"
16  
17 #include  "source.h"
18
17   #include  "otypes.h"
18  
19 < #include  "cone.h"
19 > #include  "source.h"
20  
23 #include  "face.h"
24
21   #include  "random.h"
22  
23 + /*
24 + * Structures used by direct()
25 + */
26  
27 < extern double  dstrsrc;                 /* source distribution amount */
28 < extern double  shadthresh;              /* relative shadow threshold */
29 < extern double  shadcert;                /* shadow testing certainty */
27 > typedef struct {
28 >        FVECT  dir;             /* source direction */
29 >        COLOR  coef;            /* material coefficient */
30 >        COLOR  val;             /* contribution */
31 > }  CONTRIB;             /* direct contribution */
32  
33 < SRCREC  *source = NULL;                 /* our list of sources */
34 < int  nsources = 0;                      /* the number of sources */
33 > typedef struct {
34 >        int  sno;               /* source number */
35 >        float  brt;             /* brightness (for comparison) */
36 > }  CNTPTR;              /* contribution pointer */
37  
38   static CONTRIB  *srccnt;                /* source contributions in direct() */
39   static CNTPTR  *cntord;                 /* source ordering in direct() */
# Line 38 | Line 41 | static CNTPTR  *cntord;                        /* source ordering in direct
41  
42   marksources()                   /* find and mark source objects */
43   {
44 +        int  i;
45          register OBJREC  *o, *m;
46 <        register int  i;
47 <
46 >        register int  ns;
47 >                                        /* initialize dispatch table */
48 >        initstypes();
49 >                                        /* find direct sources */
50          for (i = 0; i < nobjects; i++) {
51          
52                  o = objptr(i);
53  
54 <                if (o->omod == OVOID)
54 >                if (!issurface(o->otype) || o->omod == OVOID)
55                          continue;
56  
57                  m = objptr(o->omod);
# Line 62 | Line 68 | marksources()                  /* find and mark source objects */
68                                  m->oargs.farg[3] <= FTINY)
69                          continue;                       /* don't bother */
70  
71 <                if (source == NULL)
72 <                        source = (SRCREC *)malloc(sizeof(SRCREC));
73 <                else
74 <                        source = (SRCREC *)realloc((char *)source,
75 <                                        (unsigned)(nsources+1)*sizeof(SRCREC));
70 <                if (source == NULL)
71 >                if (sfun[o->otype].of == NULL ||
72 >                                sfun[o->otype].of->setsrc == NULL)
73 >                        objerror(o, USER, "illegal material");
74 >
75 >                if ((ns = newsource()) < 0)
76                          goto memerr;
77  
78 <                newsource(&source[nsources], o);
78 >                setsource(&source[ns], o);
79  
80                  if (m->otype == MAT_GLOW) {
81 <                        source[nsources].sflags |= SPROX;
82 <                        source[nsources].sl.prox = m->oargs.farg[3];
81 >                        source[ns].sflags |= SPROX;
82 >                        source[ns].sl.prox = m->oargs.farg[3];
83                          if (o->otype == OBJ_SOURCE)
84 <                                source[nsources].sflags |= SSKIP;
84 >                                source[ns].sflags |= SSKIP;
85                  } else if (m->otype == MAT_SPOT) {
86 <                        source[nsources].sflags |= SSPOT;
87 <                        source[nsources].sl.s = makespot(m);
86 >                        source[ns].sflags |= SSPOT;
87 >                        if ((source[ns].sl.s = makespot(m)) == NULL)
88 >                                goto memerr;
89 >                        if (source[ns].sflags & SFLAT &&
90 >                                !checkspot(source[ns].sl.s,source[ns].snorm)) {
91 >                                objerror(o, WARNING,
92 >                                        "invalid spotlight direction");
93 >                                source[ns].sflags |= SSKIP;
94 >                        }
95                  }
84                nsources++;
96          }
97          if (nsources <= 0) {
98                  error(WARNING, "no light sources found");
99                  return;
100          }
101 +        markvirtuals();                 /* find and add virtual sources */
102          srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
103          cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
104 <        if (srccnt != NULL && cntord != NULL)
105 <                return;
106 <        /* fall through */
104 >        if (srccnt == NULL || cntord == NULL)
105 >                goto memerr;
106 >        return;
107   memerr:
108          error(SYSTEM, "out of memory in marksources");
109   }
110  
111  
100 newsource(src, so)                      /* add a source to the array */
101 register SRCREC  *src;
102 register OBJREC  *so;
103 {
104        double  cos(), tan(), sqrt();
105        double  theta;
106        FACE  *f;
107        CONE  *co;
108        int  j;
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) {
116        case OBJ_SOURCE:
117                if (so->oargs.nfargs != 4)
118                        objerror(so, USER, "bad arguments");
119                src->sflags |= SDISTANT;
120                VCOPY(src->sloc, so->oargs.farg);
121                if (normalize(src->sloc) == 0.0)
122                        objerror(so, USER, "zero direction");
123                theta = PI/180.0/2.0 * so->oargs.farg[3];
124                if (theta <= FTINY)
125                        objerror(so, USER, "zero size");
126                src->ss = theta >= PI/4 ? 1.0 : tan(theta);
127                src->ss2 = 2.0*PI * (1.0 - cos(theta));
128                break;
129        case OBJ_SPHERE:
130                VCOPY(src->sloc, so->oargs.farg);
131                src->ss = so->oargs.farg[3];
132                src->ss2 = PI * src->ss * src->ss;
133                break;
134        case OBJ_FACE:
135                                                /* get the face */
136                f = getface(so);
137                                                /* find the center */
138                for (j = 0; j < 3; j++) {
139                        src->sloc[j] = 0.0;
140                        for (i = 0; i < f->nv; i++)
141                                src->sloc[j] += VERTEX(f,i)[j];
142                        src->sloc[j] /= f->nv;
143                }
144                if (!inface(src->sloc, f))
145                        objerror(so, USER, "cannot hit center");
146                src->ss = sqrt(f->area / PI);
147                src->ss2 = f->area;
148                break;
149        case OBJ_RING:
150                                                /* get the ring */
151                co = getcone(so, 0);
152                VCOPY(src->sloc, CO_P0(co));
153                if (CO_R0(co) > 0.0)
154                        objerror(so, USER, "cannot hit center");
155                src->ss = CO_R1(co);
156                src->ss2 = PI * src->ss * src->ss;
157                break;
158        default:
159                objerror(so, USER, "illegal material");
160        }
161 }
162
163
164 SPOT *
165 makespot(m)                     /* make a spotlight */
166 register OBJREC  *m;
167 {
168        extern double  cos();
169        register SPOT  *ns;
170
171        if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
172                error(SYSTEM, "out of memory in makespot");
173        ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
174        VCOPY(ns->aim, m->oargs.farg+4);
175        if ((ns->flen = normalize(ns->aim)) == 0.0)
176                objerror(m, USER, "zero focus vector");
177        return(ns);
178 }
179
180
112   double
113   srcray(sr, r, sn)               /* send a ray to a source, return domega */
114   register RAY  *sr;              /* returned source ray */
115   RAY  *r;                        /* ray which hit object */
116   register int  sn;               /* source number */
117   {
187        register double  *norm = NULL;  /* plane normal */
118          double  ddot;                   /* (distance times) cosine */
119          FVECT  vd;
120          double  d;
# Line 197 | Line 127 | register int  sn;              /* source number */
127  
128          sr->rsrc = sn;                          /* remember source */
129                                                  /* get source direction */
130 <        if (source[sn].sflags & SDISTANT)
130 >        if (source[sn].sflags & SDISTANT) {
131                                                  /* constant direction */
132                  VCOPY(sr->rdir, source[sn].sloc);
133 <        else {                                  /* compute direction */
133 >        } else {                                /* compute direction */
134                  for (i = 0; i < 3; i++)
135                          sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
136  
137 <                if (source[sn].so->otype == OBJ_FACE)
138 <                        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)) <= FTINY)
137 >                if (source[sn].sflags & SFLAT &&
138 >                        (ddot = -DOT(sr->rdir, source[sn].snorm)) <= FTINY)
139                          return(0.0);            /* behind surface! */
140          }
141          if (dstrsrc > FTINY) {
142                                          /* distribute source direction */
143 <                for (i = 0; i < 3; i++)
144 <                        vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom());
145 <
146 <                if (norm != NULL) {             /* project offset */
147 <                        d = DOT(vd, norm);
143 >                dimlist[ndims++] = sn;
144 >                for (i = 0; i < 3; i++) {
145 >                        dimlist[ndims] = i + 8831;
146 >                        vd[i] = dstrsrc * source[sn].ss *
147 >                (1.0 - 2.0*urand(urind(ilhash(dimlist,ndims+1),samplendx)));
148 >                }
149 >                ndims--;
150 >                if (source[sn].sflags & SFLAT) {        /* project offset */
151 >                        d = DOT(vd, source[sn].snorm);
152                          for (i = 0; i < 3; i++)
153 <                                vd[i] -= d * norm[i];
153 >                                vd[i] -= d * source[sn].snorm[i];
154                  }
155                  for (i = 0; i < 3; i++)         /* offset source direction */
156                          sr->rdir[i] += vd[i];
157 +                                                /* normalize */
158 +                d = normalize(sr->rdir);
159  
160 <        } else if (source[sn].sflags & SDISTANT)
161 <                                                /* already normalized */
162 <                return(source[sn].ss2);
160 >        } else if (!(source[sn].sflags & SDISTANT))
161 >                                                /* normalize direction */
162 >                d = normalize(sr->rdir);
163  
164 <        if ((d = normalize(sr->rdir)) == 0.0)
165 <                                                /* at source! */
164 >        if (source[sn].sflags & SDISTANT) {
165 >                if (source[sn].sflags & SSPOT) {        /* check location */
166 >                        for (i = 0; i < 3; i++)
167 >                                vd[i] = source[sn].sl.s->aim[i] - sr->rorg[i];
168 >                        d = DOT(sr->rdir,vd);
169 >                        if (d <= FTINY)
170 >                                return(0.0);
171 >                        d = DOT(vd,vd) - d*d;
172 >                        if (PI*d > source[sn].sl.s->siz)
173 >                                return(0.0);
174 >                }
175 >                return(source[sn].ss2);         /* domega constant */
176 >        }
177 >                                                /* check direction */
178 >        if (d == 0.0)
179                  return(0.0);
235        
236        if (source[sn].sflags & SDISTANT)
237                                                /* domega constant */
238                return(source[sn].ss2);
239
180                                                  /* check proximity */
181          if (source[sn].sflags & SPROX &&
182                          d > source[sn].sl.prox)
183                  return(0.0);
184                                                  /* compute dot product */
185 <        if (norm != NULL)
185 >        if (source[sn].sflags & SFLAT)
186                  ddot /= d;
187          else
188                  ddot = 1.0;
# Line 258 | Line 198 | register int  sn;              /* source number */
198   }
199  
200  
201 < sourcehit(r)                    /* check to see if ray hit distant source */
202 < register RAY  *r;
201 > srcvalue(r)                     /* punch ray to source and compute value */
202 > RAY  *r;
203   {
204 <        int  first, last;
265 <        register int  i;
204 >        register SRCREC  *sp;
205  
206 <        if (r->rsrc >= 0) {             /* check only one if aimed */
207 <                first = last = r->rsrc;
208 <        } else {                        /* otherwise check all */
209 <                first = 0; last = nsources-1;
206 >        sp = &source[r->rsrc];
207 >        if (sp->sflags & SVIRTUAL) {    /* virtual source */
208 >                                        /* check intersection */
209 >                if (!(*ofun[sp->so->otype].funp)(sp->so, r))
210 >                        return;
211 >                raycont(r);             /* compute contribution */
212 >                return;
213          }
214 <        for (i = first; i <= last; i++)
215 <                if (source[i].sflags & SDISTANT)
216 <                        /*
217 <                         * Check to see if ray is within
218 <                         * solid angle of source.
219 <                         */
220 <                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
279 <                                        <= source[i].ss2) {
280 <                                r->ro = source[i].so;
281 <                                if (!(source[i].sflags & SSKIP))
282 <                                        break;
283 <                        }
284 <
285 <        if (r->ro != NULL) {
286 <                for (i = 0; i < 3; i++)
287 <                        r->ron[i] = -r->rdir[i];
288 <                r->rod = 1.0;
289 <                r->rox = NULL;
290 <                return(1);
214 >                                        /* compute intersection */
215 >        if (sp->sflags & SDISTANT ? sourcehit(r) :
216 >                        (*ofun[sp->so->otype].funp)(sp->so, r)) {
217 >                if (sp->sa.success >= 0)
218 >                        sp->sa.success++;
219 >                raycont(r);             /* compute contribution */
220 >                return;
221          }
222 <        return(0);
222 >        if (sp->sa.success < 0)
223 >                return;                 /* bitched already */
224 >        sp->sa.success -= AIMREQT;
225 >        if (sp->sa.success >= 0)
226 >                return;                 /* leniency */
227 >        sprintf(errmsg, "aiming failure for light source \"%s\"",
228 >                        sp->so->oname);
229 >        error(WARNING, errmsg);         /* issue warning */
230   }
231  
232  
# Line 313 | Line 250 | char  *p;                      /* data for f */
250          extern double  pow();
251          register int  sn;
252          int  nshadcheck, ncnts;
253 <        double  prob, ourthresh, hwt, test2, hit2;
253 >        int  nhits;
254 >        double  dom, prob, ourthresh, hwt;
255          RAY  sr;
256                          /* NOTE: srccnt and cntord global so no recursion */
257          if (nsources <= 0)
# Line 327 | Line 265 | char  *p;                      /* data for f */
265                  cntord[sn].sno = sn;
266                  cntord[sn].brt = 0.0;
267                                                  /* get source ray */
268 <                if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
268 >                if ((dom = srcray(&sr, r, sn)) == 0.0)
269                          continue;
270                  VCOPY(srccnt[sn].dir, sr.rdir);
271                                                  /* compute coefficient */
272 <                (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
273 <                cntord[sn].brt = bright(srccnt[sn].val);
272 >                (*f)(srccnt[sn].coef, p, srccnt[sn].dir, dom);
273 >                cntord[sn].brt = bright(srccnt[sn].coef);
274                  if (cntord[sn].brt <= 0.0)
275                          continue;
276 <                                                /* compute intersection */
277 <                if (!( source[sn].sflags & SDISTANT ?
278 <                                sourcehit(&sr) :
279 <                                (*ofun[source[sn].so->otype].funp)
280 <                                (source[sn].so, &sr) )) {
343 <                        sprintf(errmsg,
344 <                                "aiming failure for light source \"%s\"",
345 <                                        source[sn].so->oname);
346 <                        error(WARNING, errmsg);
347 <                        continue;
348 <                }
349 <                                                /* compute contribution */
350 <                raycont(&sr);
351 <                multcolor(srccnt[sn].val, sr.rcol);
276 >                                                /* compute potential */
277 >                sr.revf = srcvalue;
278 >                rayvalue(&sr);
279 >                copycolor(srccnt[sn].val, sr.rcol);
280 >                multcolor(srccnt[sn].val, srccnt[sn].coef);
281                  cntord[sn].brt = bright(srccnt[sn].val);
282          }
283                                                  /* sort contributions */
# Line 368 | Line 297 | char  *p;                      /* data for f */
297                                                  /* accumulate tail */
298          for (sn = ncnts-1; sn > 0; sn--)
299                  cntord[sn-1].brt += cntord[sn].brt;
371                                                /* start with prob=.5 */
372        hit2 = 0.5; test2 = 1.0;
300                                                  /* test for shadows */
301 +        nhits = 0;
302          for (sn = 0; sn < ncnts; sn++) {
303                                                  /* check threshold */
304                  if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
# Line 378 | Line 306 | char  *p;                      /* data for f */
306                                  < ourthresh*bright(r->rcol))
307                          break;
308                                                  /* get statistics */
381                hwt = (double)source[cntord[sn].sno].nhits /
382                                (double)source[cntord[sn].sno].ntests;
383                test2 += hwt;
309                  source[cntord[sn].sno].ntests++;
310                                                  /* test for hit */
311                  rayorigin(&sr, r, SHADOW, 1.0);
312                  VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
313                  sr.rsrc = cntord[sn].sno;
314                  if (localhit(&sr, &thescene) &&
315 <                                sr.ro != source[cntord[sn].sno].so) {
316 <                                                /* check for transmission */
315 >                                ( sr.ro != source[cntord[sn].sno].so ||
316 >                                source[cntord[sn].sno].sflags & SFOLLOW )) {
317 >                                                /* follow entire path */
318                          raycont(&sr);
319                          if (bright(sr.rcol) <= FTINY)
320                                  continue;       /* missed! */
321 <                        (*f)(srccnt[cntord[sn].sno].val, p,
322 <                                        srccnt[cntord[sn].sno].dir,
323 <                                        srccnt[cntord[sn].sno].dom);
398 <                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
321 >                        copycolor(srccnt[cntord[sn].sno].val, sr.rcol);
322 >                        multcolor(srccnt[cntord[sn].sno].val,
323 >                                        srccnt[cntord[sn].sno].coef);
324                  }
325                                                  /* add contribution if hit */
326                  addcolor(r->rcol, srccnt[cntord[sn].sno].val);
327 <                hit2 += hwt;
327 >                nhits++;
328                  source[cntord[sn].sno].nhits++;
329          }
330 <                                        /* weighted hit rate */
331 <        hwt = hit2 / test2;
330 >                                        /* surface hit rate */
331 >        if (sn > 0)
332 >                hwt = (double)nhits / (double)sn;
333 >        else
334 >                hwt = 0.5;
335   #ifdef DEBUG
336          sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
337                          sn, ncnts-sn, hwt);
# Line 415 | Line 343 | char  *p;                      /* data for f */
343                                  (double)source[cntord[sn].sno].ntests;
344                  scalecolor(srccnt[cntord[sn].sno].val, prob);
345                  addcolor(r->rcol, srccnt[cntord[sn].sno].val);
418        }
419 }
420
421
422 #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
423                                r->rsrc>=0 && \
424                                source[r->rsrc].so!=r->ro)
425
426 #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
427                                !(r->rtype&REFLECTED) &&        /* hack! */\
428                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
429
430 #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
431                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
432
433
434 m_light(m, r)                   /* ray hit a light source */
435 register OBJREC  *m;
436 register RAY  *r;
437 {
438                                                /* check for over-counting */
439        if (wrongsource(m, r) || badambient(m, r))
440                return;
441                                                /* check for passed illum */
442        if (passillum(m, r)) {
443
444                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
445                        raytrans(r);
446                else
447                        rayshade(r, modifier(m->oargs.sarg[0]));
448
449                                                /* otherwise treat as source */
450        } else {
451                                                /* check for behind */
452                if (r->rod < 0.0)
453                        return;
454                                                /* get distribution pattern */
455                raytexture(r, m->omod);
456                                                /* get source color */
457                setcolor(r->rcol, m->oargs.farg[0],
458                                  m->oargs.farg[1],
459                                  m->oargs.farg[2]);
460                                                /* modify value */
461                multcolor(r->rcol, r->pcol);
462                                                /* assign distance */
463                r->rt = r->rot;
346          }
347   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines