ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 1.35
Committed: Thu Jun 20 13:43:29 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.34: +50 -222 lines
Log Message:
major reorganization using dispatch table for source types

File Contents

# User Rev Content
1 greg 1.25 /* Copyright (c) 1990 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * source.c - routines dealing with illumination sources.
9     *
10     * 8/20/85
11     */
12    
13     #include "ray.h"
14    
15 greg 1.4 #include "octree.h"
16    
17 greg 1.1 #include "otypes.h"
18    
19 greg 1.35 #include "source.h"
20 greg 1.1
21     #include "random.h"
22    
23 greg 1.35 /*
24     * Structures used by direct()
25     */
26 greg 1.1
27 greg 1.35 typedef struct {
28     FVECT dir; /* source direction */
29     COLOR coef; /* material coefficient */
30     COLOR val; /* contribution */
31     } CONTRIB; /* direct contribution */
32 greg 1.1
33 greg 1.35 typedef struct {
34     int sno; /* source number */
35     float brt; /* brightness (for comparison) */
36     } CNTPTR; /* contribution pointer */
37 greg 1.1
38 greg 1.25 static CONTRIB *srccnt; /* source contributions in direct() */
39     static CNTPTR *cntord; /* source ordering in direct() */
40 greg 1.1
41 greg 1.25
42 greg 1.1 marksources() /* find and mark source objects */
43     {
44 greg 1.33 int i;
45 greg 1.1 register OBJREC *o, *m;
46 greg 1.33 register SRCREC *ns;
47 greg 1.35 /* initialize dispatch table */
48     initstypes();
49     /* find direct sources */
50 greg 1.1 for (i = 0; i < nobjects; i++) {
51    
52     o = objptr(i);
53    
54 greg 1.35 if (!issurface(o->otype) || o->omod == OVOID)
55 greg 1.1 continue;
56    
57     m = objptr(o->omod);
58    
59 greg 1.24 if (!islight(m->otype))
60 greg 1.1 continue;
61    
62 greg 1.6 if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
63     m->otype == MAT_SPOT ? 7 : 3))
64 greg 1.1 objerror(m, USER, "bad # arguments");
65    
66 greg 1.6 if (m->otype == MAT_GLOW &&
67     o->otype != OBJ_SOURCE &&
68     m->oargs.farg[3] <= FTINY)
69 greg 1.1 continue; /* don't bother */
70    
71 greg 1.35 if (sfun[o->otype].of == NULL ||
72     sfun[o->otype].of->setsrc == NULL)
73     objerror(o, USER, "illegal material");
74    
75 greg 1.33 if ((ns = newsource()) == NULL)
76 greg 1.25 goto memerr;
77 greg 1.1
78 greg 1.35 (*sfun[o->otype].of->setsrc)(ns, o);
79 greg 1.1
80 greg 1.6 if (m->otype == MAT_GLOW) {
81 greg 1.33 ns->sflags |= SPROX;
82     ns->sl.prox = m->oargs.farg[3];
83 greg 1.6 if (o->otype == OBJ_SOURCE)
84 greg 1.33 ns->sflags |= SSKIP;
85 greg 1.6 } else if (m->otype == MAT_SPOT) {
86 greg 1.33 ns->sflags |= SSPOT;
87     if ((ns->sl.s = makespot(m)) == NULL)
88     goto memerr;
89 greg 1.6 }
90 greg 1.1 }
91 greg 1.25 if (nsources <= 0) {
92     error(WARNING, "no light sources found");
93     return;
94     }
95 greg 1.33 markvirtuals(); /* find and add virtual sources */
96 greg 1.25 srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
97     cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
98     if (srccnt != NULL && cntord != NULL)
99 greg 1.33 goto memerr;
100     return;
101 greg 1.25 memerr:
102     error(SYSTEM, "out of memory in marksources");
103 greg 1.1 }
104    
105    
106     double
107     srcray(sr, r, sn) /* send a ray to a source, return domega */
108     register RAY *sr; /* returned source ray */
109     RAY *r; /* ray which hit object */
110     register int sn; /* source number */
111     {
112     double ddot; /* (distance times) cosine */
113     FVECT vd;
114     double d;
115     register int i;
116    
117 greg 1.4 if (source[sn].sflags & SSKIP)
118 greg 1.1 return(0.0); /* skip this source */
119    
120     rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */
121    
122     sr->rsrc = sn; /* remember source */
123     /* get source direction */
124 greg 1.33 if (source[sn].sflags & SDISTANT) {
125     if (source[sn].sflags & SSPOT) { /* check location */
126     for (i = 0; i < 3; i++)
127     vd[i] = sr->rorg[i] - source[sn].sl.s->aim[i];
128     d = DOT(source[sn].sloc,vd);
129     d = DOT(vd,vd) - d*d;
130     if (PI*d > source[sn].sl.s->siz)
131     return(0.0);
132     }
133 greg 1.1 /* constant direction */
134 greg 1.4 VCOPY(sr->rdir, source[sn].sloc);
135 greg 1.33 } else { /* compute direction */
136 greg 1.1 for (i = 0; i < 3; i++)
137 greg 1.4 sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
138 greg 1.1
139 greg 1.33 if (source[sn].sflags & SFLAT &&
140     (ddot = -DOT(sr->rdir, source[sn].snorm)) <= FTINY)
141 greg 1.1 return(0.0); /* behind surface! */
142     }
143     if (dstrsrc > FTINY) {
144     /* distribute source direction */
145 greg 1.31 dimlist[ndims++] = sn;
146     for (i = 0; i < 3; i++) {
147     dimlist[ndims] = i + 8831;
148     vd[i] = dstrsrc * source[sn].ss *
149     (1.0 - 2.0*urand(ilhash(dimlist,ndims+1)+samplendx));
150     }
151     ndims--;
152 greg 1.33 if (source[sn].sflags & SFLAT) { /* project offset */
153     d = DOT(vd, source[sn].snorm);
154 greg 1.1 for (i = 0; i < 3; i++)
155 greg 1.33 vd[i] -= d * source[sn].snorm[i];
156 greg 1.1 }
157     for (i = 0; i < 3; i++) /* offset source direction */
158     sr->rdir[i] += vd[i];
159    
160 greg 1.4 } else if (source[sn].sflags & SDISTANT)
161 greg 1.1 /* already normalized */
162 greg 1.4 return(source[sn].ss2);
163 greg 1.1
164     if ((d = normalize(sr->rdir)) == 0.0)
165     /* at source! */
166     return(0.0);
167    
168 greg 1.4 if (source[sn].sflags & SDISTANT)
169 greg 1.1 /* domega constant */
170 greg 1.4 return(source[sn].ss2);
171 greg 1.1
172 greg 1.6 /* check proximity */
173 greg 1.27 if (source[sn].sflags & SPROX &&
174     d > source[sn].sl.prox)
175     return(0.0);
176     /* compute dot product */
177 greg 1.33 if (source[sn].sflags & SFLAT)
178 greg 1.27 ddot /= d;
179     else
180     ddot = 1.0;
181 greg 1.6 /* check angle */
182 greg 1.27 if (source[sn].sflags & SSPOT) {
183     if (source[sn].sl.s->siz < 2.0*PI *
184 greg 1.6 (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
185 greg 1.27 return(0.0);
186     d += source[sn].sl.s->flen; /* adjust length */
187 greg 1.1 }
188 greg 1.27 /* compute domega */
189     return(ddot*source[sn].ss2/(d*d));
190 greg 1.1 }
191    
192    
193 greg 1.35 srcvalue(r) /* punch ray to source and compute value */
194     RAY *r;
195 greg 1.1 {
196 greg 1.35 register SRCREC *sp;
197 greg 1.1
198 greg 1.35 sp = &source[r->rsrc];
199     if (sp->sflags & SVIRTUAL) { /* virtual source */
200     /* check intersection */
201     if (!(*ofun[sp->so->otype].funp)(sp->so, r))
202     return;
203     raycont(r); /* compute contribution */
204     return;
205 greg 1.1 }
206 greg 1.35 /* compute intersection */
207     if (sp->sflags & SDISTANT ? sourcehit(r) :
208     (*ofun[sp->so->otype].funp)(sp->so, r)) {
209     if (sp->sa.success >= 0)
210     sp->sa.success++;
211     raycont(r); /* compute contribution */
212     return;
213 greg 1.1 }
214 greg 1.35 if (sp->sa.success < 0)
215     return; /* bitched already */
216     sp->sa.success -= AIMREQT;
217     if (sp->sa.success >= 0)
218     return; /* leniency */
219     sprintf(errmsg, "aiming failure for light source \"%s\"",
220     sp->so->oname);
221     error(WARNING, errmsg); /* issue warning */
222 greg 1.1 }
223    
224    
225 greg 1.4 static int
226     cntcmp(sc1, sc2) /* contribution compare (descending) */
227 greg 1.9 register CNTPTR *sc1, *sc2;
228 greg 1.4 {
229     if (sc1->brt > sc2->brt)
230     return(-1);
231     if (sc1->brt < sc2->brt)
232     return(1);
233     return(0);
234     }
235    
236    
237     direct(r, f, p) /* add direct component */
238     RAY *r; /* ray that hit surface */
239     int (*f)(); /* direct component coefficient function */
240     char *p; /* data for f */
241     {
242 greg 1.12 extern double pow();
243 greg 1.4 register int sn;
244 greg 1.12 int nshadcheck, ncnts;
245 greg 1.29 int nhits;
246 greg 1.34 double dom, prob, ourthresh, hwt;
247 greg 1.4 RAY sr;
248 greg 1.25 /* NOTE: srccnt and cntord global so no recursion */
249 greg 1.22 if (nsources <= 0)
250 greg 1.25 return; /* no sources?! */
251 greg 1.12 /* compute number to check */
252     nshadcheck = pow((double)nsources, shadcert) + .5;
253 greg 1.8 /* modify threshold */
254     ourthresh = shadthresh / r->rweight;
255 greg 1.4 /* potential contributions */
256     for (sn = 0; sn < nsources; sn++) {
257 greg 1.9 cntord[sn].sno = sn;
258     cntord[sn].brt = 0.0;
259 greg 1.4 /* get source ray */
260 greg 1.34 if ((dom = srcray(&sr, r, sn)) == 0.0)
261 greg 1.4 continue;
262     VCOPY(srccnt[sn].dir, sr.rdir);
263     /* compute coefficient */
264 greg 1.34 (*f)(srccnt[sn].coef, p, srccnt[sn].dir, dom);
265     cntord[sn].brt = bright(srccnt[sn].coef);
266 greg 1.15 if (cntord[sn].brt <= 0.0)
267 greg 1.4 continue;
268 greg 1.35 /* compute potential */
269     sr.revf = srcvalue;
270     rayvalue(&sr);
271 greg 1.34 copycolor(srccnt[sn].val, sr.rcol);
272     multcolor(srccnt[sn].val, srccnt[sn].coef);
273 greg 1.9 cntord[sn].brt = bright(srccnt[sn].val);
274 greg 1.4 }
275     /* sort contributions */
276 greg 1.9 qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
277 greg 1.13 { /* find last */
278     register int l, m;
279    
280     sn = 0; ncnts = l = nsources;
281     while ((m = (sn + ncnts) >> 1) != l) {
282     if (cntord[m].brt > 0.0)
283     sn = m;
284     else
285     ncnts = m;
286     l = m;
287     }
288     }
289 greg 1.12 /* accumulate tail */
290     for (sn = ncnts-1; sn > 0; sn--)
291     cntord[sn-1].brt += cntord[sn].brt;
292 greg 1.10 /* test for shadows */
293 greg 1.29 nhits = 0;
294 greg 1.12 for (sn = 0; sn < ncnts; sn++) {
295 greg 1.10 /* check threshold */
296 greg 1.12 if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
297 greg 1.27 cntord[sn].brt-cntord[sn+nshadcheck].brt)
298     < ourthresh*bright(r->rcol))
299 greg 1.4 break;
300     /* get statistics */
301 greg 1.9 source[cntord[sn].sno].ntests++;
302 greg 1.4 /* test for hit */
303     rayorigin(&sr, r, SHADOW, 1.0);
304 greg 1.9 VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
305 greg 1.19 sr.rsrc = cntord[sn].sno;
306 greg 1.4 if (localhit(&sr, &thescene) &&
307 greg 1.33 ( sr.ro != source[cntord[sn].sno].so ||
308     source[cntord[sn].sno].sflags & SFOLLOW )) {
309     /* follow entire path */
310 greg 1.19 raycont(&sr);
311 greg 1.5 if (bright(sr.rcol) <= FTINY)
312 greg 1.4 continue; /* missed! */
313 greg 1.34 copycolor(srccnt[cntord[sn].sno].val, sr.rcol);
314     multcolor(srccnt[cntord[sn].sno].val,
315     srccnt[cntord[sn].sno].coef);
316 greg 1.4 }
317     /* add contribution if hit */
318 greg 1.9 addcolor(r->rcol, srccnt[cntord[sn].sno].val);
319 greg 1.29 nhits++;
320 greg 1.9 source[cntord[sn].sno].nhits++;
321 greg 1.4 }
322 greg 1.29 /* surface hit rate */
323     if (sn > 0)
324     hwt = (double)nhits / (double)sn;
325     else
326     hwt = 0.5;
327 greg 1.20 #ifdef DEBUG
328 greg 1.12 sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
329     sn, ncnts-sn, hwt);
330     eputs(errmsg);
331 greg 1.4 #endif
332     /* add in untested sources */
333 greg 1.12 for ( ; sn < ncnts; sn++) {
334 greg 1.9 prob = hwt * (double)source[cntord[sn].sno].nhits /
335     (double)source[cntord[sn].sno].ntests;
336     scalecolor(srccnt[cntord[sn].sno].val, prob);
337     addcolor(r->rcol, srccnt[cntord[sn].sno].val);
338 greg 1.1 }
339     }