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