ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 2.27
Committed: Fri Aug 15 10:24:24 1997 UTC (26 years, 8 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 2.26: +1 -0 lines
Log Message:
added setting of r->robj for rv2.c

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