ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 1.24
Committed: Thu Dec 13 10:44:04 1990 UTC (33 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.23: +1 -7 lines
Log Message:
changed initialization of ofun[]

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