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.13 by greg, Mon Jun 19 11:44:58 1989 UTC vs.
Revision 1.38 by greg, Wed Jun 26 14:10:29 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 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() */
40  
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);
58  
59 <                if (m->otype != MAT_LIGHT &&
51 <                                m->otype != MAT_ILLUM &&
52 <                                m->otype != MAT_GLOW &&
53 <                                m->otype != MAT_SPOT)
59 >                if (!islight(m->otype))
60                          continue;
61          
62                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
# 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
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 >                if (sfun[o->otype].of == NULL ||
72 >                                sfun[o->otype].of->setsrc == NULL)
73 >                        objerror(o, USER, "illegal material");
74  
75 <                newsource(&source[nsources], o);
75 >                if ((ns = newsource()) < 0)
76 >                        goto memerr;
77  
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 < }
98 <
99 <
89 < newsource(src, so)                      /* add a source to the array */
90 < register SRCREC  *src;
91 < register OBJREC  *so;
92 < {
93 <        double  cos(), tan(), sqrt();
94 <        double  theta;
95 <        FACE  *f;
96 <        CONE  *co;
97 <        int  j;
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) {
105 <        case OBJ_SOURCE:
106 <                if (so->oargs.nfargs != 4)
107 <                        objerror(so, USER, "bad arguments");
108 <                src->sflags |= SDISTANT;
109 <                VCOPY(src->sloc, so->oargs.farg);
110 <                if (normalize(src->sloc) == 0.0)
111 <                        objerror(so, USER, "zero direction");
112 <                theta = PI/180.0/2.0 * so->oargs.farg[3];
113 <                if (theta <= FTINY)
114 <                        objerror(so, USER, "zero size");
115 <                src->ss = theta >= PI/4 ? 1.0 : tan(theta);
116 <                src->ss2 = 2.0*PI * (1.0 - cos(theta));
117 <                break;
118 <        case OBJ_SPHERE:
119 <                VCOPY(src->sloc, so->oargs.farg);
120 <                src->ss = so->oargs.farg[3];
121 <                src->ss2 = PI * src->ss * src->ss;
122 <                break;
123 <        case OBJ_FACE:
124 <                                                /* get the face */
125 <                f = getface(so);
126 <                                                /* find the center */
127 <                for (j = 0; j < 3; j++) {
128 <                        src->sloc[j] = 0.0;
129 <                        for (i = 0; i < f->nv; i++)
130 <                                src->sloc[j] += VERTEX(f,i)[j];
131 <                        src->sloc[j] /= f->nv;
132 <                }
133 <                if (!inface(src->sloc, f))
134 <                        objerror(so, USER, "cannot hit center");
135 <                src->ss = sqrt(f->area / PI);
136 <                src->ss2 = f->area;
137 <                break;
138 <        case OBJ_RING:
139 <                                                /* get the ring */
140 <                co = getcone(so, 0);
141 <                VCOPY(src->sloc, CO_P0(co));
142 <                if (CO_R0(co) > 0.0)
143 <                        objerror(so, USER, "cannot hit center");
144 <                src->ss = CO_R1(co);
145 <                src->ss2 = PI * src->ss * src->ss;
146 <                break;
147 <        default:
148 <                objerror(so, USER, "illegal material");
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 +                goto memerr;
106 +        return;
107 + memerr:
108 +        error(SYSTEM, "out of memory in marksources");
109   }
110  
111  
153 SPOT *
154 makespot(m)                     /* make a spotlight */
155 register OBJREC  *m;
156 {
157        extern double  cos();
158        register SPOT  *ns;
159
160        if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
161                error(SYSTEM, "out of memory in makespot");
162        ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
163        VCOPY(ns->aim, m->oargs.farg+4);
164        if ((ns->flen = normalize(ns->aim)) == 0.0)
165                objerror(m, USER, "zero focus vector");
166        return(ns);
167 }
168
169
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   {
176        register double  *norm = NULL;  /* plane normal */
118          double  ddot;                   /* (distance times) cosine */
119          FVECT  vd;
120          double  d;
# Line 186 | 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 >                if (source[sn].sflags & SSPOT) {        /* check location */
132 >                        for (i = 0; i < 3; i++)
133 >                                vd[i] = sr->rorg[i] - source[sn].sl.s->aim[i];
134 >                        d = DOT(source[sn].sloc,vd);
135 >                        d = DOT(vd,vd) - d*d;
136 >                        if (PI*d > source[sn].sl.s->siz)
137 >                                return(0.0);
138 >                }
139                                                  /* constant direction */
140                  VCOPY(sr->rdir, source[sn].sloc);
141 <        else {                                  /* compute direction */
141 >        } else {                                /* compute direction */
142                  for (i = 0; i < 3; i++)
143                          sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
144  
145 <                if (source[sn].so->otype == OBJ_FACE)
146 <                        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)
145 >                if (source[sn].sflags & SFLAT &&
146 >                        (ddot = -DOT(sr->rdir, source[sn].snorm)) <= FTINY)
147                          return(0.0);            /* behind surface! */
148          }
149          if (dstrsrc > FTINY) {
150                                          /* distribute source direction */
151 <                for (i = 0; i < 3; i++)
152 <                        vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom());
153 <
154 <                if (norm != NULL) {             /* project offset */
155 <                        d = DOT(vd, norm);
151 >                dimlist[ndims++] = sn;
152 >                for (i = 0; i < 3; i++) {
153 >                        dimlist[ndims] = i + 8831;
154 >                        vd[i] = dstrsrc * source[sn].ss *
155 >                (1.0 - 2.0*urand(ilhash(dimlist,ndims+1)+samplendx));
156 >                }
157 >                ndims--;
158 >                if (source[sn].sflags & SFLAT) {        /* project offset */
159 >                        d = DOT(vd, source[sn].snorm);
160                          for (i = 0; i < 3; i++)
161 <                                vd[i] -= d * norm[i];
161 >                                vd[i] -= d * source[sn].snorm[i];
162                  }
163                  for (i = 0; i < 3; i++)         /* offset source direction */
164                          sr->rdir[i] += vd[i];
# Line 226 | Line 175 | register int  sn;              /* source number */
175                                                  /* domega constant */
176                  return(source[sn].ss2);
177  
229        else {
178                                                  /* check proximity */
179 <                if (source[sn].sflags & SPROX &&
180 <                                d > source[sn].sl.prox)
181 <                        return(0.0);
182 <
183 <                if (norm != NULL)
184 <                        ddot /= d;
185 <                else
186 <                        ddot = 1.0;
179 >        if (source[sn].sflags & SPROX &&
180 >                        d > source[sn].sl.prox)
181 >                return(0.0);
182 >                                                /* compute dot product */
183 >        if (source[sn].sflags & SFLAT)
184 >                ddot /= d;
185 >        else
186 >                ddot = 1.0;
187                                                  /* check angle */
188 <                if (source[sn].sflags & SSPOT) {
189 <                        if (source[sn].sl.s->siz < 2.0*PI *
188 >        if (source[sn].sflags & SSPOT) {
189 >                if (source[sn].sl.s->siz < 2.0*PI *
190                                  (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
191 <                                return(0.0);
192 <                        d += source[sn].sl.s->flen;
245 <                }
246 <                                                /* return domega */
247 <                return(ddot*source[sn].ss2/(d*d));
191 >                        return(0.0);
192 >                d += source[sn].sl.s->flen;     /* adjust length */
193          }
194 +                                                /* compute domega */
195 +        return(ddot*source[sn].ss2/(d*d));
196   }
197  
198  
199 < sourcehit(r)                    /* check to see if ray hit distant source */
200 < register RAY  *r;
199 > srcvalue(r)                     /* punch ray to source and compute value */
200 > RAY  *r;
201   {
202 <        int  first, last;
256 <        register int  i;
202 >        register SRCREC  *sp;
203  
204 <        if (r->rsrc >= 0) {             /* check only one if aimed */
205 <                first = last = r->rsrc;
206 <        } else {                        /* otherwise check all */
207 <                first = 0; last = nsources-1;
204 >        sp = &source[r->rsrc];
205 >        if (sp->sflags & SVIRTUAL) {    /* virtual source */
206 >                                        /* check intersection */
207 >                if (!(*ofun[sp->so->otype].funp)(sp->so, r))
208 >                        return;
209 >                raycont(r);             /* compute contribution */
210 >                return;
211          }
212 <        for (i = first; i <= last; i++)
213 <                if (source[i].sflags & SDISTANT)
214 <                        /*
215 <                         * Check to see if ray is within
216 <                         * solid angle of source.
217 <                         */
218 <                        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 <
276 <        if (r->ro != NULL) {
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);
212 >                                        /* compute intersection */
213 >        if (sp->sflags & SDISTANT ? sourcehit(r) :
214 >                        (*ofun[sp->so->otype].funp)(sp->so, r)) {
215 >                if (sp->sa.success >= 0)
216 >                        sp->sa.success++;
217 >                raycont(r);             /* compute contribution */
218 >                return;
219          }
220 <        return(0);
220 >        if (sp->sa.success < 0)
221 >                return;                 /* bitched already */
222 >        sp->sa.success -= AIMREQT;
223 >        if (sp->sa.success >= 0)
224 >                return;                 /* leniency */
225 >        sprintf(errmsg, "aiming failure for light source \"%s\"",
226 >                        sp->so->oname);
227 >        error(WARNING, errmsg);         /* issue warning */
228   }
229  
230  
# Line 304 | Line 247 | char  *p;                      /* data for f */
247   {
248          extern double  pow();
249          register int  sn;
307        register CONTRIB  *srccnt;
308        register CNTPTR  *cntord;
250          int  nshadcheck, ncnts;
251 <        double  prob, ourthresh, hwt, test2, hit2;
251 >        int  nhits;
252 >        double  dom, prob, ourthresh, hwt;
253          RAY  sr;
254 <
255 <        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
256 <        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
315 <        if (srccnt == NULL || cntord == NULL)
316 <                error(SYSTEM, "out of memory in direct");
254 >                        /* NOTE: srccnt and cntord global so no recursion */
255 >        if (nsources <= 0)
256 >                return;         /* no sources?! */
257                                                  /* compute number to check */
258          nshadcheck = pow((double)nsources, shadcert) + .5;
259                                                  /* modify threshold */
# Line 323 | Line 263 | char  *p;                      /* data for f */
263                  cntord[sn].sno = sn;
264                  cntord[sn].brt = 0.0;
265                                                  /* get source ray */
266 <                if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
266 >                if ((dom = srcray(&sr, r, sn)) == 0.0)
267                          continue;
268                  VCOPY(srccnt[sn].dir, sr.rdir);
269                                                  /* compute coefficient */
270 <                (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
271 <                cntord[sn].brt = bright(srccnt[sn].val);
272 <                if (cntord[sn].brt <= FTINY)
270 >                (*f)(srccnt[sn].coef, p, srccnt[sn].dir, dom);
271 >                cntord[sn].brt = bright(srccnt[sn].coef);
272 >                if (cntord[sn].brt <= 0.0)
273                          continue;
274 <                                                /* compute intersection */
275 <                if (!( source[sn].sflags & SDISTANT ?
276 <                                sourcehit(&sr) :
277 <                                (*ofun[source[sn].so->otype].funp)
278 <                                (source[sn].so, &sr) ))
339 <                        continue;
340 <                                                /* compute contribution */
341 <                rayshade(&sr, sr.ro->omod);
342 <                multcolor(srccnt[sn].val, sr.rcol);
274 >                                                /* compute potential */
275 >                sr.revf = srcvalue;
276 >                rayvalue(&sr);
277 >                copycolor(srccnt[sn].val, sr.rcol);
278 >                multcolor(srccnt[sn].val, srccnt[sn].coef);
279                  cntord[sn].brt = bright(srccnt[sn].val);
280          }
281                                                  /* sort contributions */
# Line 359 | Line 295 | char  *p;                      /* data for f */
295                                                  /* accumulate tail */
296          for (sn = ncnts-1; sn > 0; sn--)
297                  cntord[sn-1].brt += cntord[sn].brt;
362                                                /* start with prob=.5 */
363        hit2 = 0.5; test2 = 1.0;
298                                                  /* test for shadows */
299 +        nhits = 0;
300          for (sn = 0; sn < ncnts; sn++) {
301                                                  /* check threshold */
302                  if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
303 <                                cntord[sn].brt-cntord[sn+nshadcheck].brt) <
304 <                                ourthresh*bright(r->rcol))
303 >                                cntord[sn].brt-cntord[sn+nshadcheck].brt)
304 >                                < ourthresh*bright(r->rcol))
305                          break;
306                                                  /* get statistics */
372                hwt = (double)source[cntord[sn].sno].nhits /
373                                (double)source[cntord[sn].sno].ntests;
374                test2 += hwt;
307                  source[cntord[sn].sno].ntests++;
308                                                  /* test for hit */
309                  rayorigin(&sr, r, SHADOW, 1.0);
310                  VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
311 +                sr.rsrc = cntord[sn].sno;
312                  if (localhit(&sr, &thescene) &&
313 <                                sr.ro != source[cntord[sn].sno].so) {
314 <                                                /* check for transmission */
315 <                        if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
316 <                                raytrans(&sr);          /* object is clipped */
384 <                        else
385 <                                rayshade(&sr, sr.ro->omod);
313 >                                ( sr.ro != source[cntord[sn].sno].so ||
314 >                                source[cntord[sn].sno].sflags & SFOLLOW )) {
315 >                                                /* follow entire path */
316 >                        raycont(&sr);
317                          if (bright(sr.rcol) <= FTINY)
318                                  continue;       /* missed! */
319 <                        (*f)(srccnt[cntord[sn].sno].val, p,
320 <                                        srccnt[cntord[sn].sno].dir,
321 <                                        srccnt[cntord[sn].sno].dom);
391 <                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
319 >                        copycolor(srccnt[cntord[sn].sno].val, sr.rcol);
320 >                        multcolor(srccnt[cntord[sn].sno].val,
321 >                                        srccnt[cntord[sn].sno].coef);
322                  }
323                                                  /* add contribution if hit */
324                  addcolor(r->rcol, srccnt[cntord[sn].sno].val);
325 <                hit2 += hwt;
325 >                nhits++;
326                  source[cntord[sn].sno].nhits++;
327          }
328 <                                        /* weighted hit rate */
329 <        hwt = hit2 / test2;
328 >                                        /* surface hit rate */
329 >        if (sn > 0)
330 >                hwt = (double)nhits / (double)sn;
331 >        else
332 >                hwt = 0.5;
333   #ifdef DEBUG
334          sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
335                          sn, ncnts-sn, hwt);
# Line 409 | Line 342 | char  *p;                      /* data for f */
342                  scalecolor(srccnt[cntord[sn].sno].val, prob);
343                  addcolor(r->rcol, srccnt[cntord[sn].sno].val);
344          }
412                
413        free(srccnt);
414        free(cntord);
345   }
416
417
418 #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
419                                r->rsrc>=0 && \
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&&source[r->rsrc].so==r->ro))
428
429
430 m_light(m, r)                   /* ray hit a light source */
431 register OBJREC  *m;
432 register RAY  *r;
433 {
434                                                /* check for behind */
435        if (r->rod < 0.0)
436                return;
437                                                /* check for over-counting */
438        if (wrongsource(m, r) || badambient(m, r))
439                return;
440                                                /* check for passed illum */
441        if (passillum(m, r)) {
442
443                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
444                        raytrans(r);
445                else
446                        rayshade(r, modifier(m->oargs.sarg[0]));
447
448                                                /* otherwise treat as source */
449        } else {
450                                                /* get distribution pattern */
451                raytexture(r, m->omod);
452                                                /* get source color */
453                setcolor(r->rcol, m->oargs.farg[0],
454                                  m->oargs.farg[1],
455                                  m->oargs.farg[2]);
456                                                /* modify value */
457                multcolor(r->rcol, r->pcol);
458        }
459 }
460
461
462 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines