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