ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 1.11
Committed: Tue Jun 13 08:20:22 1989 UTC (34 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.10: +1 -1 lines
Log Message:
Changed starting probability for source testing

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1986 Regents of the University of California */
2    
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 "source.h"
18    
19     #include "otypes.h"
20    
21     #include "cone.h"
22    
23     #include "face.h"
24    
25     #include "random.h"
26    
27    
28     extern double dstrsrc; /* source distribution amount */
29 greg 1.4 extern double shadthresh; /* relative shadow threshold */
30 greg 1.1
31 greg 1.4 SRCREC *source = NULL; /* our list of sources */
32 greg 1.1 int nsources = 0; /* the number of sources */
33    
34    
35     marksources() /* find and mark source objects */
36     {
37     register OBJREC *o, *m;
38     register int i;
39    
40     for (i = 0; i < nobjects; i++) {
41    
42     o = objptr(i);
43    
44     if (o->omod == OVOID)
45     continue;
46    
47     m = objptr(o->omod);
48    
49     if (m->otype != MAT_LIGHT &&
50     m->otype != MAT_ILLUM &&
51 greg 1.6 m->otype != MAT_GLOW &&
52     m->otype != MAT_SPOT)
53 greg 1.1 continue;
54    
55 greg 1.6 if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
56     m->otype == MAT_SPOT ? 7 : 3))
57 greg 1.1 objerror(m, USER, "bad # arguments");
58    
59 greg 1.6 if (m->otype == MAT_GLOW &&
60     o->otype != OBJ_SOURCE &&
61     m->oargs.farg[3] <= FTINY)
62 greg 1.1 continue; /* don't bother */
63    
64 greg 1.4 if (source == NULL)
65     source = (SRCREC *)malloc(sizeof(SRCREC));
66     else
67     source = (SRCREC *)realloc((char *)source,
68     (unsigned)(nsources+1)*sizeof(SRCREC));
69     if (source == NULL)
70     error(SYSTEM, "out of memory in marksources");
71 greg 1.1
72 greg 1.4 newsource(&source[nsources], o);
73 greg 1.1
74 greg 1.6 if (m->otype == MAT_GLOW) {
75     source[nsources].sflags |= SPROX;
76     source[nsources].sl.prox = m->oargs.farg[3];
77     if (o->otype == OBJ_SOURCE)
78     source[nsources].sflags |= SSKIP;
79     } else if (m->otype == MAT_SPOT) {
80     source[nsources].sflags |= SSPOT;
81     source[nsources].sl.s = makespot(m);
82     }
83 greg 1.1 nsources++;
84     }
85     }
86    
87    
88     newsource(src, so) /* add a source to the array */
89 greg 1.4 register SRCREC *src;
90 greg 1.1 register OBJREC *so;
91     {
92     double cos(), tan(), sqrt();
93     double theta;
94     FACE *f;
95     CONE *co;
96     int j;
97     register int i;
98    
99     src->sflags = 0;
100 greg 1.6 src->nhits = 1; src->ntests = 2; /* start probability = 1/2 */
101 greg 1.1 src->so = so;
102    
103     switch (so->otype) {
104     case OBJ_SOURCE:
105     if (so->oargs.nfargs != 4)
106     objerror(so, USER, "bad arguments");
107     src->sflags |= SDISTANT;
108     VCOPY(src->sloc, so->oargs.farg);
109     if (normalize(src->sloc) == 0.0)
110     objerror(so, USER, "zero direction");
111     theta = PI/180.0/2.0 * so->oargs.farg[3];
112     if (theta <= FTINY)
113     objerror(so, USER, "zero size");
114     src->ss = theta >= PI/4 ? 1.0 : tan(theta);
115     src->ss2 = 2.0*PI * (1.0 - cos(theta));
116     break;
117     case OBJ_SPHERE:
118     VCOPY(src->sloc, so->oargs.farg);
119     src->ss = so->oargs.farg[3];
120     src->ss2 = PI * src->ss * src->ss;
121     break;
122     case OBJ_FACE:
123     /* get the face */
124     f = getface(so);
125     /* find the center */
126     for (j = 0; j < 3; j++) {
127     src->sloc[j] = 0.0;
128     for (i = 0; i < f->nv; i++)
129     src->sloc[j] += VERTEX(f,i)[j];
130     src->sloc[j] /= f->nv;
131     }
132     if (!inface(src->sloc, f))
133     objerror(so, USER, "cannot hit center");
134     src->ss = sqrt(f->area / PI);
135     src->ss2 = f->area;
136     break;
137     case OBJ_RING:
138     /* get the ring */
139     co = getcone(so, 0);
140     VCOPY(src->sloc, CO_P0(co));
141     if (CO_R0(co) > 0.0)
142     objerror(so, USER, "cannot hit center");
143     src->ss = CO_R1(co);
144     src->ss2 = PI * src->ss * src->ss;
145     break;
146     default:
147     objerror(so, USER, "illegal material");
148     }
149     }
150    
151    
152 greg 1.6 SPOT *
153     makespot(m) /* make a spotlight */
154     register OBJREC *m;
155     {
156     extern double cos();
157     register SPOT *ns;
158    
159     if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
160     error(SYSTEM, "out of memory in makespot");
161     ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
162     VCOPY(ns->aim, m->oargs.farg+4);
163     if ((ns->flen = normalize(ns->aim)) == 0.0)
164     objerror(m, USER, "zero focus vector");
165     return(ns);
166     }
167    
168    
169 greg 1.1 double
170     srcray(sr, r, sn) /* send a ray to a source, return domega */
171     register RAY *sr; /* returned source ray */
172     RAY *r; /* ray which hit object */
173     register int sn; /* source number */
174     {
175     register double *norm = NULL; /* plane normal */
176     double ddot; /* (distance times) cosine */
177     FVECT vd;
178     double d;
179     register int i;
180    
181 greg 1.4 if (source[sn].sflags & SSKIP)
182 greg 1.1 return(0.0); /* skip this source */
183    
184     rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */
185    
186     sr->rsrc = sn; /* remember source */
187     /* get source direction */
188 greg 1.4 if (source[sn].sflags & SDISTANT)
189 greg 1.1 /* constant direction */
190 greg 1.4 VCOPY(sr->rdir, source[sn].sloc);
191 greg 1.1 else { /* compute direction */
192     for (i = 0; i < 3; i++)
193 greg 1.4 sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
194 greg 1.1
195 greg 1.4 if (source[sn].so->otype == OBJ_FACE)
196     norm = getface(source[sn].so)->norm;
197     else if (source[sn].so->otype == OBJ_RING)
198     norm = getcone(source[sn].so,0)->ad;
199 greg 1.1
200 greg 1.2 if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY)
201 greg 1.1 return(0.0); /* behind surface! */
202     }
203     if (dstrsrc > FTINY) {
204     /* distribute source direction */
205     for (i = 0; i < 3; i++)
206 greg 1.4 vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom());
207 greg 1.1
208     if (norm != NULL) { /* project offset */
209     d = DOT(vd, norm);
210     for (i = 0; i < 3; i++)
211     vd[i] -= d * norm[i];
212     }
213     for (i = 0; i < 3; i++) /* offset source direction */
214     sr->rdir[i] += vd[i];
215    
216 greg 1.4 } else if (source[sn].sflags & SDISTANT)
217 greg 1.1 /* already normalized */
218 greg 1.4 return(source[sn].ss2);
219 greg 1.1
220     if ((d = normalize(sr->rdir)) == 0.0)
221     /* at source! */
222     return(0.0);
223    
224 greg 1.4 if (source[sn].sflags & SDISTANT)
225 greg 1.1 /* domega constant */
226 greg 1.4 return(source[sn].ss2);
227 greg 1.1
228     else {
229 greg 1.6 /* check proximity */
230     if (source[sn].sflags & SPROX &&
231     d > source[sn].sl.prox)
232     return(0.0);
233 greg 1.1
234     if (norm != NULL)
235     ddot /= d;
236     else
237     ddot = 1.0;
238 greg 1.6 /* check angle */
239     if (source[sn].sflags & SSPOT) {
240     if (source[sn].sl.s->siz < 2.0*PI *
241     (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
242     return(0.0);
243     d += source[sn].sl.s->flen;
244     }
245 greg 1.1 /* return domega */
246 greg 1.4 return(ddot*source[sn].ss2/(d*d));
247 greg 1.1 }
248     }
249    
250    
251     sourcehit(r) /* check to see if ray hit distant source */
252     register RAY *r;
253     {
254     int first, last;
255     register int i;
256    
257     if (r->rsrc >= 0) { /* check only one if aimed */
258     first = last = r->rsrc;
259     } else { /* otherwise check all */
260     first = 0; last = nsources-1;
261     }
262     for (i = first; i <= last; i++)
263 greg 1.4 if (source[i].sflags & SDISTANT)
264 greg 1.1 /*
265     * Check to see if ray is within
266     * solid angle of source.
267     */
268 greg 1.4 if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
269     <= source[i].ss2) {
270     r->ro = source[i].so;
271     if (!(source[i].sflags & SSKIP))
272 greg 1.1 break;
273     }
274    
275     if (r->ro != NULL) {
276     for (i = 0; i < 3; i++)
277     r->ron[i] = -r->rdir[i];
278     r->rod = 1.0;
279 greg 1.3 r->rofs = 1.0; setident4(r->rofx);
280     r->robs = 1.0; setident4(r->robx);
281 greg 1.1 return(1);
282     }
283     return(0);
284     }
285    
286    
287 greg 1.4 static int
288     cntcmp(sc1, sc2) /* contribution compare (descending) */
289 greg 1.9 register CNTPTR *sc1, *sc2;
290 greg 1.4 {
291     if (sc1->brt > sc2->brt)
292     return(-1);
293     if (sc1->brt < sc2->brt)
294     return(1);
295     return(0);
296     }
297    
298    
299     direct(r, f, p) /* add direct component */
300     RAY *r; /* ray that hit surface */
301     int (*f)(); /* direct component coefficient function */
302     char *p; /* data for f */
303     {
304     register int sn;
305     register CONTRIB *srccnt;
306 greg 1.9 register CNTPTR *cntord;
307 greg 1.10 double prob, ourthresh, hwt, test2, hit2;
308 greg 1.4 RAY sr;
309    
310 greg 1.9 srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
311     cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
312     if (srccnt == NULL || cntord == NULL)
313 greg 1.4 error(SYSTEM, "out of memory in direct");
314 greg 1.8 /* modify threshold */
315     ourthresh = shadthresh / r->rweight;
316 greg 1.4 /* potential contributions */
317     for (sn = 0; sn < nsources; sn++) {
318 greg 1.9 cntord[sn].sno = sn;
319     cntord[sn].brt = 0.0;
320 greg 1.4 /* get source ray */
321     if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
322     continue;
323     VCOPY(srccnt[sn].dir, sr.rdir);
324     /* compute coefficient */
325     (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
326 greg 1.9 cntord[sn].brt = bright(srccnt[sn].val);
327     if (cntord[sn].brt <= FTINY)
328 greg 1.4 continue;
329     /* compute intersection */
330     if (!( source[sn].sflags & SDISTANT ?
331     sourcehit(&sr) :
332     (*ofun[source[sn].so->otype].funp)
333     (source[sn].so, &sr) ))
334     continue;
335     /* compute contribution */
336     rayshade(&sr, sr.ro->omod);
337     multcolor(srccnt[sn].val, sr.rcol);
338 greg 1.9 cntord[sn].brt = bright(srccnt[sn].val);
339 greg 1.4 }
340     /* sort contributions */
341 greg 1.9 qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
342 greg 1.11 hit2 = 0.5; test2 = 1.0; /* start with prob=.5 */
343 greg 1.10 /* test for shadows */
344     for (sn = 0; sn < nsources; sn++) {
345     /* check threshold */
346     if (cntord[sn].brt <= ourthresh*bright(r->rcol))
347 greg 1.4 break;
348     /* get statistics */
349 greg 1.9 hwt = (double)source[cntord[sn].sno].nhits /
350     (double)source[cntord[sn].sno].ntests;
351 greg 1.4 test2 += hwt;
352 greg 1.9 source[cntord[sn].sno].ntests++;
353 greg 1.4 /* test for hit */
354     rayorigin(&sr, r, SHADOW, 1.0);
355 greg 1.9 VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
356 greg 1.4 if (localhit(&sr, &thescene) &&
357 greg 1.9 sr.ro != source[cntord[sn].sno].so) {
358 greg 1.4 /* check for transmission */
359     if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
360     raytrans(&sr); /* object is clipped */
361     else
362     rayshade(&sr, sr.ro->omod);
363 greg 1.5 if (bright(sr.rcol) <= FTINY)
364 greg 1.4 continue; /* missed! */
365 greg 1.9 (*f)(srccnt[cntord[sn].sno].val, p,
366     srccnt[cntord[sn].sno].dir,
367     srccnt[cntord[sn].sno].dom);
368     multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
369 greg 1.4 }
370     /* add contribution if hit */
371 greg 1.9 addcolor(r->rcol, srccnt[cntord[sn].sno].val);
372 greg 1.4 hit2 += hwt;
373 greg 1.9 source[cntord[sn].sno].nhits++;
374 greg 1.4 }
375 greg 1.7 /* weighted hit rate */
376     hwt = hit2 / test2;
377 greg 1.4 #ifdef DEBUG
378 greg 1.10 {
379     int ntested = sn;
380 greg 1.4 #endif
381     /* add in untested sources */
382 greg 1.10 for ( ; sn < nsources; sn++) {
383     if (cntord[sn].brt <= 0.0)
384     break;
385 greg 1.9 prob = hwt * (double)source[cntord[sn].sno].nhits /
386     (double)source[cntord[sn].sno].ntests;
387     scalecolor(srccnt[cntord[sn].sno].val, prob);
388     addcolor(r->rcol, srccnt[cntord[sn].sno].val);
389 greg 1.4 }
390 greg 1.10 #ifdef DEBUG
391     fprintf(stderr, "%d tested, %d untested, %f hit rate\n",
392     ntested, sn-ntested, hwt);
393     }
394     #endif
395    
396 greg 1.4 free(srccnt);
397 greg 1.9 free(cntord);
398 greg 1.4 }
399    
400    
401 greg 1.1 #define wrongsource(m, r) (m->otype!=MAT_ILLUM && \
402     r->rsrc>=0 && \
403 greg 1.4 source[r->rsrc].so!=r->ro)
404 greg 1.1
405     #define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
406 greg 1.6 !(r->rtype&REFLECTED) && /* hack! */\
407     !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
408 greg 1.1
409     #define passillum(m, r) (m->otype==MAT_ILLUM && \
410 greg 1.4 !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
411 greg 1.1
412    
413     m_light(m, r) /* ray hit a light source */
414     register OBJREC *m;
415     register RAY *r;
416     {
417     /* check for behind */
418     if (r->rod < 0.0)
419     return;
420     /* check for over-counting */
421     if (wrongsource(m, r) || badambient(m, r))
422     return;
423     /* check for passed illum */
424     if (passillum(m, r)) {
425    
426     if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
427     raytrans(r);
428     else
429     rayshade(r, modifier(m->oargs.sarg[0]));
430    
431     /* otherwise treat as source */
432     } else {
433     /* get distribution pattern */
434     raytexture(r, m->omod);
435     /* get source color */
436     setcolor(r->rcol, m->oargs.farg[0],
437     m->oargs.farg[1],
438     m->oargs.farg[2]);
439     /* modify value */
440     multcolor(r->rcol, r->pcol);
441     }
442     }
443    
444    
445     o_source() {} /* intersection with a source is done elsewhere */