ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 2.31
Committed: Tue Mar 11 17:08:55 2003 UTC (21 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.30: +3 -1 lines
Log Message:
First working version of new "mesh" primitive, obj2mesh converter

File Contents

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