ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 1.26
Committed: Sat Dec 15 15:03:34 1990 UTC (33 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.25: +1 -2 lines
Log Message:
changed handling of matrix transformations with new MAT4 & XF types
dynamic allocation of ray transformations with newrayxf()
added missing light source vector transformation to m_brdf.c

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