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.42 by greg, Mon Aug 12 08:20:49 1991 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 12 | Line 12 | static char SCCSid[] = "$SunId$ LBL";
12  
13   #include  "ray.h"
14  
15 < #include  "source.h"
15 > #include  "octree.h"
16  
17   #include  "otypes.h"
18  
19 < #include  "cone.h"
19 > #include  "source.h"
20  
21 #include  "face.h"
22
21   #include  "random.h"
22  
23 + /*
24 + * Structures used by direct()
25 + */
26  
27 < extern double  dstrsrc;                 /* source distribution amount */
27 > typedef struct {
28 >        FVECT  dir;             /* source direction */
29 >        COLOR  coef;            /* material coefficient */
30 >        COLOR  val;             /* contribution */
31 > }  CONTRIB;             /* direct contribution */
32  
33 < SOURCE  srcval[MAXSOURCE];              /* our array 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 &&
47 <                                m->otype != MAT_ILLUM &&
48 <                                m->otype != MAT_GLOW &&
49 <                                m->otype != MAT_SPOT)
59 >                if (!islight(m->otype))
60                          continue;
61          
62                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
# Line 58 | Line 68 | marksources()                  /* find and mark source objects */
68                                  m->oargs.farg[3] <= FTINY)
69                          continue;                       /* don't bother */
70  
71 <                if (nsources >= MAXSOURCE)
72 <                        error(INTERNAL, "too many sources 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(&srcval[nsources], o);
75 >                if ((ns = newsource()) < 0)
76 >                        goto memerr;
77  
78 +                setsource(&source[ns], o);
79 +
80                  if (m->otype == MAT_GLOW) {
81 <                        srcval[nsources].sflags |= SPROX;
82 <                        srcval[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 <                                srcval[nsources].sflags |= SSKIP;
84 >                                source[ns].sflags |= SSKIP;
85                  } else if (m->otype == MAT_SPOT) {
86 <                        srcval[nsources].sflags |= SSPOT;
87 <                        srcval[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                  }
75                nsources++;
96          }
97 < }
98 <
99 <
80 < newsource(src, so)                      /* add a source to the array */
81 < register SOURCE  *src;
82 < register OBJREC  *so;
83 < {
84 <        double  cos(), tan(), sqrt();
85 <        double  theta;
86 <        FACE  *f;
87 <        CONE  *co;
88 <        int  j;
89 <        register int  i;
90 <        
91 <        src->sflags = 0;
92 <        src->so = so;
93 <
94 <        switch (so->otype) {
95 <        case OBJ_SOURCE:
96 <                if (so->oargs.nfargs != 4)
97 <                        objerror(so, USER, "bad arguments");
98 <                src->sflags |= SDISTANT;
99 <                VCOPY(src->sloc, so->oargs.farg);
100 <                if (normalize(src->sloc) == 0.0)
101 <                        objerror(so, USER, "zero direction");
102 <                theta = PI/180.0/2.0 * so->oargs.farg[3];
103 <                if (theta <= FTINY)
104 <                        objerror(so, USER, "zero size");
105 <                src->ss = theta >= PI/4 ? 1.0 : tan(theta);
106 <                src->ss2 = 2.0*PI * (1.0 - cos(theta));
107 <                break;
108 <        case OBJ_SPHERE:
109 <                VCOPY(src->sloc, so->oargs.farg);
110 <                src->ss = so->oargs.farg[3];
111 <                src->ss2 = PI * src->ss * src->ss;
112 <                break;
113 <        case OBJ_FACE:
114 <                                                /* get the face */
115 <                f = getface(so);
116 <                                                /* find the center */
117 <                for (j = 0; j < 3; j++) {
118 <                        src->sloc[j] = 0.0;
119 <                        for (i = 0; i < f->nv; i++)
120 <                                src->sloc[j] += VERTEX(f,i)[j];
121 <                        src->sloc[j] /= f->nv;
122 <                }
123 <                if (!inface(src->sloc, f))
124 <                        objerror(so, USER, "cannot hit center");
125 <                src->ss = sqrt(f->area / PI);
126 <                src->ss2 = f->area;
127 <                break;
128 <        case OBJ_RING:
129 <                                                /* get the ring */
130 <                co = getcone(so, 0);
131 <                VCOPY(src->sloc, CO_P0(co));
132 <                if (CO_R0(co) > 0.0)
133 <                        objerror(so, USER, "cannot hit center");
134 <                src->ss = CO_R1(co);
135 <                src->ss2 = PI * src->ss * src->ss;
136 <                break;
137 <        default:
138 <                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  
143 SPOT *
144 makespot(m)                     /* make a spotlight */
145 register OBJREC  *m;
146 {
147        extern double  cos();
148        register SPOT  *ns;
149
150        if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
151                error(SYSTEM, "out of memory in makespot");
152        ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
153        VCOPY(ns->aim, m->oargs.farg+4);
154        if ((ns->flen = normalize(ns->aim)) == 0.0)
155                objerror(m, USER, "zero focus vector");
156        return(ns);
157 }
158
159
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   {
166        register double  *norm = NULL;  /* plane normal */
118          double  ddot;                   /* (distance times) cosine */
119          FVECT  vd;
120          double  d;
121          register int  i;
122  
123 <        if (srcval[sn].sflags & SSKIP)
123 >        if (source[sn].sflags & SSKIP)
124                  return(0.0);                    /* skip this source */
125  
126          rayorigin(sr, r, SHADOW, 1.0);          /* ignore limits */
127  
128          sr->rsrc = sn;                          /* remember source */
129                                                  /* get source direction */
130 <        if (srcval[sn].sflags & SDISTANT)
130 >        if (source[sn].sflags & SDISTANT) {
131                                                  /* constant direction */
132 <                VCOPY(sr->rdir, srcval[sn].sloc);
133 <        else {                                  /* compute direction */
132 >                VCOPY(sr->rdir, source[sn].sloc);
133 >        } else {                                /* compute direction */
134                  for (i = 0; i < 3; i++)
135 <                        sr->rdir[i] = srcval[sn].sloc[i] - sr->rorg[i];
135 >                        sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
136  
137 <                if (srcval[sn].so->otype == OBJ_FACE)
138 <                        norm = getface(srcval[sn].so)->norm;
188 <                else if (srcval[sn].so->otype == OBJ_RING)
189 <                        norm = getcone(srcval[sn].so,0)->ad;
190 <
191 <                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 * srcval[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 (srcval[sn].sflags & SDISTANT)
161 <                                                /* already normalized */
162 <                return(srcval[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);
214        
215        if (srcval[sn].sflags & SDISTANT)
216                                                /* domega constant */
217                return(srcval[sn].ss2);
218
219        else {
180                                                  /* check proximity */
181 <                if (srcval[sn].sflags & SPROX &&
182 <                                d > srcval[sn].sl.prox)
183 <                        return(0.0);
184 <
185 <                if (norm != NULL)
186 <                        ddot /= d;
187 <                else
188 <                        ddot = 1.0;
181 >        if (source[sn].sflags & SPROX &&
182 >                        d > source[sn].sl.prox)
183 >                return(0.0);
184 >                                                /* compute dot product */
185 >        if (source[sn].sflags & SFLAT)
186 >                ddot /= d;
187 >        else
188 >                ddot = 1.0;
189                                                  /* check angle */
190 <                if (srcval[sn].sflags & SSPOT) {
191 <                        if (srcval[sn].sl.s->siz < 2.0*PI *
192 <                                (1.0 + DOT(srcval[sn].sl.s->aim,sr->rdir)))
193 <                                return(0.0);
194 <                        d += srcval[sn].sl.s->flen;
235 <                }
236 <                                                /* return domega */
237 <                return(ddot*srcval[sn].ss2/(d*d));
190 >        if (source[sn].sflags & SSPOT) {
191 >                if (source[sn].sl.s->siz < 2.0*PI *
192 >                                (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
193 >                        return(0.0);
194 >                d += source[sn].sl.s->flen;     /* adjust length */
195          }
196 +                                                /* compute domega */
197 +        return(ddot*source[sn].ss2/(d*d));
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;
246 <        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 (srcval[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(srcval[i].sloc,r->rdir))
221 <                                        <= srcval[i].ss2) {
222 <                                r->ro = srcval[i].so;
223 <                                if (!(srcval[i].sflags & SSKIP))
224 <                                        break;
225 <                        }
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 >        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 <        if (r->ro != NULL) {
233 <                for (i = 0; i < 3; i++)
234 <                        r->ron[i] = -r->rdir[i];
235 <                r->rod = 1.0;
236 <                r->rofs = 1.0; setident4(r->rofx);
237 <                r->robs = 1.0; setident4(r->robx);
232 >
233 > static int
234 > cntcmp(sc1, sc2)                        /* contribution compare (descending) */
235 > register CNTPTR  *sc1, *sc2;
236 > {
237 >        if (sc1->brt > sc2->brt)
238 >                return(-1);
239 >        if (sc1->brt < sc2->brt)
240                  return(1);
273        }
241          return(0);
242   }
243  
244  
245 < #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
246 <                                r->rsrc>=0 && \
247 <                                srcval[r->rsrc].so!=r->ro)
248 <
282 < #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
283 <                                !(r->rtype&REFLECTED) &&        /* hack! */\
284 <                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
285 <
286 < #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
287 <                                !(r->rsrc>=0&&srcval[r->rsrc].so==r->ro))
288 <
289 <
290 < m_light(m, r)                   /* ray hit a light source */
291 < register OBJREC  *m;
292 < register RAY  *r;
245 > direct(r, f, p)                         /* add direct component */
246 > RAY  *r;                        /* ray that hit surface */
247 > int  (*f)();                    /* direct component coefficient function */
248 > char  *p;                       /* data for f */
249   {
250 <                                                /* check for behind */
251 <        if (r->rod < 0.0)
252 <                return;
253 <                                                /* check for over-counting */
254 <        if (wrongsource(m, r) || badambient(m, r))
255 <                return;
256 <                                                /* check for passed illum */
257 <        if (passillum(m, r)) {
250 >        extern int  (*trace)();
251 >        extern double  pow();
252 >        register int  sn;
253 >        int  nshadcheck, ncnts;
254 >        int  nhits;
255 >        double  dom, prob, ourthresh, hwt;
256 >        RAY  sr;
257 >                        /* NOTE: srccnt and cntord global so no recursion */
258 >        if (nsources <= 0)
259 >                return;         /* no sources?! */
260 >                                                /* compute number to check */
261 >        nshadcheck = pow((double)nsources, shadcert) + .5;
262 >                                                /* modify threshold */
263 >        ourthresh = shadthresh / r->rweight;
264 >                                                /* potential contributions */
265 >        for (sn = 0; sn < nsources; sn++) {
266 >                cntord[sn].sno = sn;
267 >                cntord[sn].brt = 0.0;
268 >                                                /* get source ray */
269 >                if ((dom = srcray(&sr, r, sn)) == 0.0)
270 >                        continue;
271 >                VCOPY(srccnt[sn].dir, sr.rdir);
272 >                                                /* compute coefficient */
273 >                (*f)(srccnt[sn].coef, p, srccnt[sn].dir, dom);
274 >                cntord[sn].brt = bright(srccnt[sn].coef);
275 >                if (cntord[sn].brt <= 0.0)
276 >                        continue;
277 >                                                /* compute potential */
278 >                sr.revf = srcvalue;
279 >                rayvalue(&sr);
280 >                copycolor(srccnt[sn].val, sr.rcol);
281 >                multcolor(srccnt[sn].val, srccnt[sn].coef);
282 >                cntord[sn].brt = bright(srccnt[sn].val);
283 >        }
284 >                                                /* sort contributions */
285 >        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
286 >        {                                       /* find last */
287 >                register int  l, m;
288  
289 <                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
290 <                        raytrans(r);
291 <                else
292 <                        rayshade(r, modifier(m->oargs.sarg[0]));
293 <
294 <                                                /* otherwise treat as source */
295 <        } else {
296 <                                                /* get distribution pattern */
311 <                raytexture(r, m->omod);
312 <                                                /* get source color */
313 <                setcolor(r->rcol, m->oargs.farg[0],
314 <                                  m->oargs.farg[1],
315 <                                  m->oargs.farg[2]);
316 <                                                /* modify value */
317 <                multcolor(r->rcol, r->pcol);
289 >                sn = 0; ncnts = l = nsources;
290 >                while ((m = (sn + ncnts) >> 1) != l) {
291 >                        if (cntord[m].brt > 0.0)
292 >                                sn = m;
293 >                        else
294 >                                ncnts = m;
295 >                        l = m;
296 >                }
297          }
298 +                                                /* accumulate tail */
299 +        for (sn = ncnts-1; sn > 0; sn--)
300 +                cntord[sn-1].brt += cntord[sn].brt;
301 +                                                /* test for shadows */
302 +        nhits = 0;
303 +        for (sn = 0; sn < ncnts; sn++) {
304 +                                                /* check threshold */
305 +                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
306 +                                cntord[sn].brt-cntord[sn+nshadcheck].brt)
307 +                                < ourthresh*bright(r->rcol))
308 +                        break;
309 +                                                /* get statistics */
310 +                source[cntord[sn].sno].ntests++;
311 +                                                /* test for hit */
312 +                rayorigin(&sr, r, SHADOW, 1.0);
313 +                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
314 +                sr.rsrc = cntord[sn].sno;
315 +                if (localhit(&sr, &thescene) &&
316 +                                ( sr.ro != source[cntord[sn].sno].so ||
317 +                                source[cntord[sn].sno].sflags & SFOLLOW )) {
318 +                                                /* follow entire path */
319 +                        raycont(&sr);
320 +                        if (trace != NULL)
321 +                                (*trace)(&sr);  /* trace execution */
322 +                        if (bright(sr.rcol) <= FTINY)
323 +                                continue;       /* missed! */
324 +                        copycolor(srccnt[cntord[sn].sno].val, sr.rcol);
325 +                        multcolor(srccnt[cntord[sn].sno].val,
326 +                                        srccnt[cntord[sn].sno].coef);
327 +                }
328 +                                                /* add contribution if hit */
329 +                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
330 +                nhits++;
331 +                source[cntord[sn].sno].nhits++;
332 +        }
333 +                                        /* surface hit rate */
334 +        if (sn > 0)
335 +                hwt = (double)nhits / (double)sn;
336 +        else
337 +                hwt = 0.5;
338 + #ifdef DEBUG
339 +        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
340 +                        sn, ncnts-sn, hwt);
341 +        eputs(errmsg);
342 + #endif
343 +                                        /* add in untested sources */
344 +        for ( ; sn < ncnts; sn++) {
345 +                prob = hwt * (double)source[cntord[sn].sno].nhits /
346 +                                (double)source[cntord[sn].sno].ntests;
347 +                scalecolor(srccnt[cntord[sn].sno].val, prob);
348 +                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
349 +        }
350   }
320
321
322 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines