ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 2.21
Committed: Mon Dec 11 15:00:24 1995 UTC (28 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.20: +6 -10 lines
Log Message:
fixed bug in srcscatter() for source subsampling

File Contents

# User Rev Content
1 greg 2.17 /* Copyright (c) 1995 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 greg 2.19 #include "random.h"
22    
23     extern double ssampdist; /* scatter sampling distance */
24    
25 greg 1.35 /*
26     * Structures used by direct()
27     */
28 greg 1.1
29 greg 1.35 typedef struct {
30 greg 1.45 int sno; /* source number */
31 greg 1.35 FVECT dir; /* source direction */
32     COLOR coef; /* material coefficient */
33     COLOR val; /* contribution */
34     } CONTRIB; /* direct contribution */
35 greg 1.1
36 greg 1.35 typedef struct {
37 greg 1.45 int sndx; /* source index (to CONTRIB array) */
38 greg 1.35 float brt; /* brightness (for comparison) */
39     } CNTPTR; /* contribution pointer */
40 greg 1.1
41 greg 1.25 static CONTRIB *srccnt; /* source contributions in direct() */
42     static CNTPTR *cntord; /* source ordering in direct() */
43 greg 1.45 static int maxcntr = 0; /* size of contribution arrays */
44 greg 1.1
45 greg 1.25
46 greg 1.1 marksources() /* find and mark source objects */
47     {
48 greg 2.3 int foundsource = 0;
49 greg 1.33 int i;
50 greg 1.1 register OBJREC *o, *m;
51 greg 1.36 register int ns;
52 greg 1.35 /* initialize dispatch table */
53     initstypes();
54     /* find direct sources */
55 greg 1.1 for (i = 0; i < nobjects; i++) {
56    
57     o = objptr(i);
58    
59 greg 1.35 if (!issurface(o->otype) || o->omod == OVOID)
60 greg 1.1 continue;
61    
62     m = objptr(o->omod);
63    
64 greg 1.24 if (!islight(m->otype))
65 greg 1.1 continue;
66    
67 greg 1.6 if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
68     m->otype == MAT_SPOT ? 7 : 3))
69 greg 1.1 objerror(m, USER, "bad # arguments");
70    
71 greg 1.6 if (m->otype == MAT_GLOW &&
72     o->otype != OBJ_SOURCE &&
73     m->oargs.farg[3] <= FTINY)
74 greg 1.1 continue; /* don't bother */
75 greg 2.16 if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY &&
76     m->oargs.farg[2] <= FTINY)
77     continue; /* don't bother */
78 greg 1.1
79 greg 1.35 if (sfun[o->otype].of == NULL ||
80     sfun[o->otype].of->setsrc == NULL)
81     objerror(o, USER, "illegal material");
82    
83 greg 1.36 if ((ns = newsource()) < 0)
84 greg 1.25 goto memerr;
85 greg 1.1
86 greg 1.37 setsource(&source[ns], o);
87 greg 1.1
88 greg 1.6 if (m->otype == MAT_GLOW) {
89 greg 1.36 source[ns].sflags |= SPROX;
90     source[ns].sl.prox = m->oargs.farg[3];
91 greg 2.8 if (source[ns].sflags & SDISTANT)
92 greg 1.36 source[ns].sflags |= SSKIP;
93 greg 1.6 } else if (m->otype == MAT_SPOT) {
94 greg 1.36 source[ns].sflags |= SSPOT;
95     if ((source[ns].sl.s = makespot(m)) == NULL)
96 greg 1.33 goto memerr;
97 greg 1.38 if (source[ns].sflags & SFLAT &&
98     !checkspot(source[ns].sl.s,source[ns].snorm)) {
99     objerror(o, WARNING,
100     "invalid spotlight direction");
101     source[ns].sflags |= SSKIP;
102     }
103 greg 1.6 }
104 greg 2.3 if (!(source[ns].sflags & SSKIP))
105     foundsource++;
106 greg 1.1 }
107 greg 2.3 if (!foundsource) {
108 greg 1.25 error(WARNING, "no light sources found");
109     return;
110     }
111 greg 1.33 markvirtuals(); /* find and add virtual sources */
112 greg 1.45 /* allocate our contribution arrays */
113 greg 1.48 maxcntr = nsources + MAXSPART; /* start with this many */
114 greg 1.45 srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB));
115     cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR));
116 greg 1.48 if (srccnt == NULL | cntord == NULL)
117 greg 1.33 goto memerr;
118     return;
119 greg 1.25 memerr:
120     error(SYSTEM, "out of memory in marksources");
121 greg 1.1 }
122    
123    
124 greg 1.45 srcray(sr, r, si) /* send a ray to a source, return domega */
125 greg 1.1 register RAY *sr; /* returned source ray */
126     RAY *r; /* ray which hit object */
127 greg 1.45 SRCINDEX *si; /* source sample index */
128 greg 1.1 {
129 greg 1.45 double d; /* distance to source */
130     register SRCREC *srcp;
131 greg 1.1
132 greg 1.45 rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */
133 greg 1.1
134 greg 1.47 while ((d = nextssamp(sr, si)) != 0.0) {
135 greg 1.45 sr->rsrc = si->sn; /* remember source */
136     srcp = source + si->sn;
137     if (srcp->sflags & SDISTANT) {
138 greg 2.18 if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s))
139 greg 2.4 continue;
140 greg 1.45 return(1); /* sample OK */
141 greg 1.40 }
142 greg 1.45 /* local source */
143 greg 1.6 /* check proximity */
144 greg 1.45 if (srcp->sflags & SPROX && d > srcp->sl.prox)
145     continue;
146 greg 1.6 /* check angle */
147 greg 1.45 if (srcp->sflags & SSPOT) {
148 greg 2.18 if (spotout(sr, srcp->sl.s))
149 greg 1.45 continue;
150     /* adjust solid angle */
151     si->dom *= d*d;
152     d += srcp->sl.s->flen;
153     si->dom /= d*d;
154 greg 1.1 }
155 greg 1.45 return(1); /* sample OK */
156     }
157     return(0); /* no more samples */
158 greg 1.1 }
159    
160    
161 greg 1.35 srcvalue(r) /* punch ray to source and compute value */
162 greg 2.15 register RAY *r;
163 greg 1.1 {
164 greg 1.35 register SRCREC *sp;
165 greg 1.1
166 greg 1.35 sp = &source[r->rsrc];
167     if (sp->sflags & SVIRTUAL) { /* virtual source */
168     /* check intersection */
169     if (!(*ofun[sp->so->otype].funp)(sp->so, r))
170     return;
171 greg 2.15 if (!rayshade(r, r->ro->omod)) /* compute contribution */
172     goto nomat;
173 greg 2.19 rayparticipate(r);
174 greg 1.35 return;
175 greg 1.1 }
176 greg 1.35 /* compute intersection */
177     if (sp->sflags & SDISTANT ? sourcehit(r) :
178     (*ofun[sp->so->otype].funp)(sp->so, r)) {
179     if (sp->sa.success >= 0)
180     sp->sa.success++;
181 greg 2.15 if (!rayshade(r, r->ro->omod)) /* compute contribution */
182     goto nomat;
183 greg 2.19 rayparticipate(r);
184 greg 1.35 return;
185 greg 1.1 }
186 greg 2.15 /* we missed our mark! */
187 greg 1.35 if (sp->sa.success < 0)
188     return; /* bitched already */
189     sp->sa.success -= AIMREQT;
190     if (sp->sa.success >= 0)
191     return; /* leniency */
192     sprintf(errmsg, "aiming failure for light source \"%s\"",
193     sp->so->oname);
194     error(WARNING, errmsg); /* issue warning */
195 greg 2.15 return;
196     nomat:
197     objerror(r->ro, USER, "material not found");
198 greg 1.1 }
199    
200    
201 greg 2.5 sourcehit(r) /* check to see if ray hit distant source */
202     register RAY *r;
203     {
204     int first, last;
205     register int i;
206    
207     if (r->rsrc >= 0) { /* check only one if aimed */
208     first = last = r->rsrc;
209     } else { /* otherwise check all */
210     first = 0; last = nsources-1;
211     }
212     for (i = first; i <= last; i++)
213     if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT)
214     /*
215     * Check to see if ray is within
216     * solid angle of source.
217     */
218     if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
219     <= source[i].ss2) {
220     r->ro = source[i].so;
221     if (!(source[i].sflags & SSKIP))
222     break;
223     }
224    
225     if (r->ro != NULL) {
226     for (i = 0; i < 3; i++)
227     r->ron[i] = -r->rdir[i];
228     r->rod = 1.0;
229     r->rox = NULL;
230     return(1);
231     }
232     return(0);
233     }
234    
235    
236 greg 1.4 static int
237     cntcmp(sc1, sc2) /* contribution compare (descending) */
238 greg 1.9 register CNTPTR *sc1, *sc2;
239 greg 1.4 {
240     if (sc1->brt > sc2->brt)
241     return(-1);
242     if (sc1->brt < sc2->brt)
243     return(1);
244     return(0);
245     }
246    
247    
248     direct(r, f, p) /* add direct component */
249     RAY *r; /* ray that hit surface */
250     int (*f)(); /* direct component coefficient function */
251     char *p; /* data for f */
252     {
253 greg 1.42 extern int (*trace)();
254 greg 1.4 register int sn;
255 greg 2.7 register CONTRIB *scp;
256 greg 1.45 SRCINDEX si;
257 greg 1.12 int nshadcheck, ncnts;
258 greg 1.29 int nhits;
259 greg 1.45 double prob, ourthresh, hwt;
260 greg 1.4 RAY sr;
261 greg 1.25 /* NOTE: srccnt and cntord global so no recursion */
262 greg 1.22 if (nsources <= 0)
263 greg 1.25 return; /* no sources?! */
264 greg 1.4 /* potential contributions */
265 greg 1.45 initsrcindex(&si);
266     for (sn = 0; srcray(&sr, r, &si); sn++) {
267     if (sn >= maxcntr) {
268     maxcntr = sn + MAXSPART;
269     srccnt = (CONTRIB *)realloc((char *)srccnt,
270     maxcntr*sizeof(CONTRIB));
271     cntord = (CNTPTR *)realloc((char *)cntord,
272     maxcntr*sizeof(CNTPTR));
273 greg 1.48 if (srccnt == NULL | cntord == NULL)
274 greg 1.45 error(SYSTEM, "out of memory in direct");
275     }
276     cntord[sn].sndx = sn;
277 greg 2.7 scp = srccnt + sn;
278     scp->sno = sr.rsrc;
279 greg 1.4 /* compute coefficient */
280 greg 2.7 (*f)(scp->coef, p, sr.rdir, si.dom);
281     cntord[sn].brt = bright(scp->coef);
282 greg 1.15 if (cntord[sn].brt <= 0.0)
283 greg 1.4 continue;
284 greg 2.7 VCOPY(scp->dir, sr.rdir);
285 greg 1.35 /* compute potential */
286     sr.revf = srcvalue;
287     rayvalue(&sr);
288 greg 2.7 copycolor(scp->val, sr.rcol);
289     multcolor(scp->val, scp->coef);
290     cntord[sn].brt = bright(scp->val);
291 greg 1.4 }
292     /* sort contributions */
293 greg 1.45 qsort(cntord, sn, sizeof(CNTPTR), cntcmp);
294 greg 1.13 { /* find last */
295     register int l, m;
296    
297 greg 1.45 ncnts = l = sn;
298     sn = 0;
299 greg 1.13 while ((m = (sn + ncnts) >> 1) != l) {
300     if (cntord[m].brt > 0.0)
301     sn = m;
302     else
303     ncnts = m;
304     l = m;
305     }
306     }
307 greg 2.2 if (ncnts == 0)
308     return; /* no contributions! */
309 greg 1.12 /* accumulate tail */
310     for (sn = ncnts-1; sn > 0; sn--)
311     cntord[sn-1].brt += cntord[sn].brt;
312 greg 1.45 /* compute number to check */
313     nshadcheck = pow((double)ncnts, shadcert) + .5;
314     /* modify threshold */
315     ourthresh = shadthresh / r->rweight;
316 greg 1.10 /* test for shadows */
317 greg 2.13 for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts;
318     hwt += (double)source[scp->sno].nhits /
319     (double)source[scp->sno].ntests,
320     sn++) {
321 greg 1.10 /* check threshold */
322 greg 1.12 if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
323 greg 1.27 cntord[sn].brt-cntord[sn+nshadcheck].brt)
324     < ourthresh*bright(r->rcol))
325 greg 1.4 break;
326 greg 2.7 scp = srccnt + cntord[sn].sndx;
327 greg 1.4 /* test for hit */
328     rayorigin(&sr, r, SHADOW, 1.0);
329 greg 2.7 VCOPY(sr.rdir, scp->dir);
330     sr.rsrc = scp->sno;
331     source[scp->sno].ntests++; /* keep statistics */
332 greg 1.4 if (localhit(&sr, &thescene) &&
333 greg 2.7 ( sr.ro != source[scp->sno].so ||
334     source[scp->sno].sflags & SFOLLOW )) {
335 greg 1.33 /* follow entire path */
336 greg 2.15 if (!raycont(&sr))
337     objerror(sr.ro, USER, "material not found");
338 greg 2.19 rayparticipate(&sr);
339 greg 1.42 if (trace != NULL)
340     (*trace)(&sr); /* trace execution */
341 greg 1.5 if (bright(sr.rcol) <= FTINY)
342 greg 1.4 continue; /* missed! */
343 greg 2.7 copycolor(scp->val, sr.rcol);
344     multcolor(scp->val, scp->coef);
345 greg 1.4 }
346     /* add contribution if hit */
347 greg 2.7 addcolor(r->rcol, scp->val);
348 greg 1.29 nhits++;
349 greg 2.7 source[scp->sno].nhits++;
350 greg 1.4 }
351 greg 2.13 /* source hit rate */
352     if (hwt > FTINY)
353     hwt = (double)nhits / hwt;
354 greg 1.29 else
355     hwt = 0.5;
356 greg 1.20 #ifdef DEBUG
357 greg 2.13 sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n",
358 greg 1.12 sn, ncnts-sn, hwt);
359     eputs(errmsg);
360 greg 1.4 #endif
361     /* add in untested sources */
362 greg 1.12 for ( ; sn < ncnts; sn++) {
363 greg 2.7 scp = srccnt + cntord[sn].sndx;
364     prob = hwt * (double)source[scp->sno].nhits /
365     (double)source[scp->sno].ntests;
366 greg 2.13 if (prob > 1.0)
367     prob = 1.0;
368 greg 2.7 scalecolor(scp->val, prob);
369     addcolor(r->rcol, scp->val);
370 greg 2.19 }
371     }
372    
373    
374     srcscatter(r) /* compute source scattering into ray */
375     register RAY *r;
376     {
377 greg 2.20 int oldsampndx;
378 greg 2.19 int nsamps;
379     RAY sr;
380     SRCINDEX si;
381     double t, lastt, d;
382     COLOR cumval, ctmp;
383     int i, j;
384    
385     if (r->slights == NULL || r->slights[0] == 0 || r->gecc >= 1.-FTINY)
386     return;
387     if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1)
388     nsamps = 1;
389 greg 2.20 oldsampndx = samplendx;
390     samplendx = random()&0x7fff; /* randomize */
391 greg 2.19 for (i = r->slights[0]; i > 0; i--) { /* for each source */
392     setcolor(cumval, 0., 0., 0.);
393     lastt = r->rot;
394     for (j = nsamps; j-- > 0; ) { /* for each sample position */
395     samplendx++;
396     t = r->rot * (j+frandom())/nsamps;
397     sr.rorg[0] = r->rorg[0] + r->rdir[0]*t;
398     sr.rorg[1] = r->rorg[1] + r->rdir[1]*t;
399     sr.rorg[2] = r->rorg[2] + r->rdir[2]*t;
400     sr.rmax = 0.;
401 greg 2.21 initsrcindex(&si); /* sample ray to this source */
402     si.sn = r->slights[i];
403     nopart(&si, &sr);
404     if (!srcray(&sr, NULL, &si) ||
405     sr.rsrc != r->slights[i])
406     continue; /* no path */
407 greg 2.19 copycolor(sr.cext, r->cext);
408     sr.albedo = r->albedo;
409     sr.gecc = r->gecc;
410     rayvalue(&sr); /* eval. source ray */
411     if (bright(sr.rcol) <= FTINY)
412     continue;
413     /* compute fall-off */
414     d = lastt - t;
415     setcolor(ctmp, 1.-d*colval(r->cext,RED),
416     1.-d*colval(r->cext,GRN),
417     1.-d*colval(r->cext,BLU));
418     multcolor(cumval, ctmp);
419     lastt = t;
420     if (r->gecc <= FTINY) /* compute P(theta) */
421     d = 1.;
422     else {
423     d = DOT(r->rdir, sr.rdir);
424     d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d);
425     d = (1. - r->gecc*r->gecc) / (d*d*d);
426     }
427     /* other factors */
428     d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps);
429     multcolor(sr.rcol, r->cext);
430     scalecolor(sr.rcol, d);
431     addcolor(cumval, sr.rcol);
432     }
433     /* final fall-off */
434     setcolor(ctmp, 1.-lastt*colval(r->cext,RED),
435     1.-lastt*colval(r->cext,GRN),
436     1.-lastt*colval(r->cext,BLU));
437     multcolor(cumval, ctmp);
438     addcolor(r->rcol, cumval); /* sum into ray result */
439 greg 1.1 }
440 greg 2.20 samplendx = oldsampndx;
441 greg 2.4 }
442    
443    
444     /****************************************************************
445     * The following macros were separated from the m_light() routine
446     * because they are very nasty and difficult to understand.
447     */
448    
449 greg 2.11 /* illumblock *
450 greg 2.4 *
451     * We cannot allow an illum to pass to another illum, because that
452     * would almost certainly constitute overcounting.
453     * However, we do allow an illum to pass to another illum
454     * that is actually going to relay to a virtual light source.
455 greg 2.11 * We also prevent an illum from passing to a glow; this provides a
456     * convenient mechanism for defining detailed light source
457     * geometry behind (or inside) an effective radiator.
458 greg 2.4 */
459    
460 greg 2.11 static int weaksrcmod(obj) int obj; /* efficiency booster function */
461     {register OBJREC *o = objptr(obj);
462     return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);}
463 greg 2.4
464 greg 2.11 #define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \
465 greg 2.12 r->rod > 0.0 && \
466 greg 2.11 weaksrcmod(source[r->rsrc].so->omod))
467    
468 greg 2.4 /* wrongsource *
469     *
470     * This source is the wrong source (ie. overcounted) if we are
471     * aimed to a different source than the one we hit and the one
472 greg 2.11 * we hit is not an illum that should be passed.
473 greg 2.4 */
474    
475     #define wrongsource(m, r) (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \
476 greg 2.11 (m->otype!=MAT_ILLUM || illumblock(m,r)))
477 greg 2.4
478     /* distglow *
479     *
480     * A distant glow is an object that sometimes acts as a light source,
481     * but is too far away from the test point to be one in this case.
482 greg 2.11 * (Glows with negative radii should NEVER participate in illumination.)
483 greg 2.4 */
484    
485 greg 2.17 #define distglow(m, r, d) (m->otype==MAT_GLOW && \
486 greg 2.10 m->oargs.farg[3] >= -FTINY && \
487 greg 2.17 d > m->oargs.farg[3])
488 greg 2.4
489     /* badcomponent *
490     *
491     * We must avoid counting light sources in the ambient calculation,
492     * since the direct component is handled separately. Therefore, any
493     * ambient ray which hits an active light source must be discarded.
494     * The same is true for stray specular samples, since the specular
495     * contribution from light sources is calculated separately.
496     */
497    
498     #define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \
499     !(r->crtype&SHADOW || r->rod < 0.0 || \
500 greg 2.17 /* not 100% correct */ distglow(m, r, r->rot)))
501 greg 2.4
502     /* passillum *
503     *
504     * An illum passes to another material type when we didn't hit it
505     * on purpose (as part of a direct calculation), or it is relaying
506     * a virtual light source.
507     */
508    
509     #define passillum(m, r) (m->otype==MAT_ILLUM && \
510     (r->rsrc<0 || source[r->rsrc].so!=r->ro || \
511     source[r->rsrc].sflags&SVIRTUAL))
512    
513     /* srcignore *
514     *
515 greg 2.10 * The -dv flag is normally on for sources to be visible.
516 greg 2.4 */
517    
518 greg 2.17 #define srcignore(m, r) !(directvis || r->crtype&SHADOW || \
519     distglow(m, r, raydist(r,PRIMARY)))
520 greg 2.4
521    
522     m_light(m, r) /* ray hit a light source */
523     register OBJREC *m;
524     register RAY *r;
525     {
526     /* check for over-counting */
527 greg 2.12 if (badcomponent(m, r))
528 greg 2.14 return(1);
529 greg 2.17 if (wrongsource(m, r))
530 greg 2.14 return(1);
531 greg 2.4 /* check for passed illum */
532     if (passillum(m, r)) {
533 greg 2.14 if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID))
534     return(rayshade(r, modifier(m->oargs.sarg[0])));
535     raytrans(r);
536     return(1);
537 greg 2.4 }
538     /* otherwise treat as source */
539     /* check for behind */
540     if (r->rod < 0.0)
541 greg 2.14 return(1);
542 greg 2.4 /* check for invisibility */
543     if (srcignore(m, r))
544 greg 2.14 return(1);
545 greg 2.4 /* check for outside spot */
546 greg 2.18 if (m->otype==MAT_SPOT && spotout(r, makespot(m)))
547 greg 2.14 return(1);
548 greg 2.4 /* get distribution pattern */
549     raytexture(r, m->omod);
550     /* get source color */
551     setcolor(r->rcol, m->oargs.farg[0],
552     m->oargs.farg[1],
553     m->oargs.farg[2]);
554     /* modify value */
555     multcolor(r->rcol, r->pcol);
556 greg 2.14 return(1);
557 greg 1.1 }