1 |
< |
/* Copyright (c) 1990 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
18 |
|
|
19 |
|
#include "source.h" |
20 |
|
|
21 |
– |
#include "random.h" |
22 |
– |
|
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 sno; /* source number */ |
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 */ |
99 |
|
return; |
100 |
|
} |
101 |
|
markvirtuals(); /* find and add virtual sources */ |
102 |
< |
srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB)); |
103 |
< |
cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR)); |
104 |
< |
if (srccnt == NULL || cntord == NULL) |
102 |
> |
/* allocate our contribution arrays */ |
103 |
> |
maxcntr = nsources + MAXSPART; /* start with this many */ |
104 |
> |
srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB)); |
105 |
> |
cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR)); |
106 |
> |
if (srccnt == NULL | cntord == NULL) |
107 |
|
goto memerr; |
108 |
|
return; |
109 |
|
memerr: |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
< |
double |
113 |
< |
srcray(sr, r, sn) /* send a ray to a source, return domega */ |
114 |
> |
srcray(sr, r, si) /* send a ray to a source, return domega */ |
115 |
|
register RAY *sr; /* returned source ray */ |
116 |
|
RAY *r; /* ray which hit object */ |
117 |
< |
register int sn; /* source number */ |
117 |
> |
SRCINDEX *si; /* source sample index */ |
118 |
|
{ |
119 |
< |
double ddot; /* (distance times) cosine */ |
120 |
< |
FVECT vd; |
121 |
< |
double d; |
122 |
< |
register int i; |
119 |
> |
double d; /* distance to source */ |
120 |
> |
FVECT vd; |
121 |
> |
register SRCREC *srcp; |
122 |
> |
register int i; |
123 |
|
|
124 |
< |
if (source[sn].sflags & SSKIP) |
124 |
< |
return(0.0); /* skip this source */ |
124 |
> |
rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */ |
125 |
|
|
126 |
< |
rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */ |
127 |
< |
|
128 |
< |
sr->rsrc = sn; /* remember source */ |
129 |
< |
/* get source direction */ |
130 |
< |
if (source[sn].sflags & SDISTANT) { |
131 |
< |
/* constant direction */ |
132 |
< |
VCOPY(sr->rdir, source[sn].sloc); |
133 |
< |
} else { /* compute direction */ |
134 |
< |
for (i = 0; i < 3; i++) |
135 |
< |
sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i]; |
136 |
< |
|
137 |
< |
if (source[sn].sflags & SFLAT && |
138 |
< |
(ddot = -DOT(sr->rdir, source[sn].snorm)) <= FTINY) |
139 |
< |
return(0.0); /* behind surface! */ |
140 |
< |
} |
141 |
< |
if (dstrsrc > FTINY) { |
142 |
< |
/* distribute source direction */ |
143 |
< |
dimlist[ndims++] = sn; |
144 |
< |
for (i = 0; i < 3; i++) { |
145 |
< |
dimlist[ndims] = i + 8831; |
146 |
< |
vd[i] = dstrsrc * source[sn].ss * |
147 |
< |
(1.0 - 2.0*urand(urind(ilhash(dimlist,ndims+1),samplendx))); |
148 |
< |
} |
149 |
< |
ndims--; |
150 |
< |
if (source[sn].sflags & SFLAT) { /* project offset */ |
151 |
< |
d = DOT(vd, source[sn].snorm); |
126 |
> |
while ((d = nextssamp(sr, si)) != 0.0) { |
127 |
> |
sr->rsrc = si->sn; /* remember source */ |
128 |
> |
srcp = source + si->sn; |
129 |
> |
if (srcp->sflags & SDISTANT) { |
130 |
> |
if (srcp->sflags & SSPOT) { /* check location */ |
131 |
|
for (i = 0; i < 3; i++) |
132 |
< |
vd[i] -= d * source[sn].snorm[i]; |
154 |
< |
} |
155 |
< |
for (i = 0; i < 3; i++) /* offset source direction */ |
156 |
< |
sr->rdir[i] += vd[i]; |
157 |
< |
/* normalize */ |
158 |
< |
d = normalize(sr->rdir); |
159 |
< |
|
160 |
< |
} else if (!(source[sn].sflags & SDISTANT)) |
161 |
< |
/* normalize direction */ |
162 |
< |
d = normalize(sr->rdir); |
163 |
< |
|
164 |
< |
if (source[sn].sflags & SDISTANT) { |
165 |
< |
if (source[sn].sflags & SSPOT) { /* check location */ |
166 |
< |
for (i = 0; i < 3; i++) |
167 |
< |
vd[i] = source[sn].sl.s->aim[i] - sr->rorg[i]; |
132 |
> |
vd[i] = srcp->sl.s->aim[i] - sr->rorg[i]; |
133 |
|
d = DOT(sr->rdir,vd); |
134 |
|
if (d <= FTINY) |
135 |
< |
return(0.0); |
135 |
> |
continue; |
136 |
|
d = DOT(vd,vd) - d*d; |
137 |
< |
if (PI*d > source[sn].sl.s->siz) |
138 |
< |
return(0.0); |
137 |
> |
if (PI*d > srcp->sl.s->siz) |
138 |
> |
continue; |
139 |
|
} |
140 |
< |
return(source[sn].ss2); /* domega constant */ |
140 |
> |
return(1); /* sample OK */ |
141 |
|
} |
142 |
< |
/* check direction */ |
178 |
< |
if (d == 0.0) |
179 |
< |
return(0.0); |
142 |
> |
/* local source */ |
143 |
|
/* check proximity */ |
144 |
< |
if (source[sn].sflags & SPROX && |
145 |
< |
d > source[sn].sl.prox) |
183 |
< |
return(0.0); |
184 |
< |
/* compute dot product */ |
185 |
< |
if (source[sn].sflags & SFLAT) |
186 |
< |
ddot /= d; |
187 |
< |
else |
188 |
< |
ddot = 1.0; |
144 |
> |
if (srcp->sflags & SPROX && d > srcp->sl.prox) |
145 |
> |
continue; |
146 |
|
/* check angle */ |
147 |
< |
if (source[sn].sflags & SSPOT) { |
148 |
< |
if (source[sn].sl.s->siz < 2.0*PI * |
149 |
< |
(1.0 + DOT(source[sn].sl.s->aim,sr->rdir))) |
150 |
< |
return(0.0); |
151 |
< |
d += source[sn].sl.s->flen; /* adjust length */ |
147 |
> |
if (srcp->sflags & SSPOT) { |
148 |
> |
if (srcp->sl.s->siz < 2.0*PI * |
149 |
> |
(1.0 + DOT(srcp->sl.s->aim,sr->rdir))) |
150 |
> |
continue; |
151 |
> |
/* adjust solid angle */ |
152 |
> |
si->dom *= d*d; |
153 |
> |
d += srcp->sl.s->flen; |
154 |
> |
si->dom /= d*d; |
155 |
|
} |
156 |
< |
/* compute domega */ |
157 |
< |
return(ddot*source[sn].ss2/(d*d)); |
156 |
> |
return(1); /* sample OK */ |
157 |
> |
} |
158 |
> |
return(0); /* no more samples */ |
159 |
|
} |
160 |
|
|
161 |
|
|
208 |
|
int (*f)(); /* direct component coefficient function */ |
209 |
|
char *p; /* data for f */ |
210 |
|
{ |
211 |
+ |
extern int (*trace)(); |
212 |
|
extern double pow(); |
213 |
|
register int sn; |
214 |
+ |
SRCINDEX si; |
215 |
|
int nshadcheck, ncnts; |
216 |
|
int nhits; |
217 |
< |
double dom, prob, ourthresh, hwt; |
217 |
> |
double prob, ourthresh, hwt; |
218 |
|
RAY sr; |
219 |
|
/* NOTE: srccnt and cntord global so no recursion */ |
220 |
|
if (nsources <= 0) |
221 |
|
return; /* no sources?! */ |
259 |
– |
/* compute number to check */ |
260 |
– |
nshadcheck = pow((double)nsources, shadcert) + .5; |
261 |
– |
/* modify threshold */ |
262 |
– |
ourthresh = shadthresh / r->rweight; |
222 |
|
/* potential contributions */ |
223 |
< |
for (sn = 0; sn < nsources; sn++) { |
224 |
< |
cntord[sn].sno = sn; |
225 |
< |
cntord[sn].brt = 0.0; |
226 |
< |
/* get source ray */ |
227 |
< |
if ((dom = srcray(&sr, r, sn)) == 0.0) |
228 |
< |
continue; |
229 |
< |
VCOPY(srccnt[sn].dir, sr.rdir); |
223 |
> |
initsrcindex(&si); |
224 |
> |
for (sn = 0; srcray(&sr, r, &si); sn++) { |
225 |
> |
if (sn >= maxcntr) { |
226 |
> |
maxcntr = sn + MAXSPART; |
227 |
> |
srccnt = (CONTRIB *)realloc((char *)srccnt, |
228 |
> |
maxcntr*sizeof(CONTRIB)); |
229 |
> |
cntord = (CNTPTR *)realloc((char *)cntord, |
230 |
> |
maxcntr*sizeof(CNTPTR)); |
231 |
> |
if (srccnt == NULL | cntord == NULL) |
232 |
> |
error(SYSTEM, "out of memory in direct"); |
233 |
> |
} |
234 |
> |
cntord[sn].sndx = sn; |
235 |
> |
srccnt[sn].sno = sr.rsrc; |
236 |
|
/* compute coefficient */ |
237 |
< |
(*f)(srccnt[sn].coef, p, srccnt[sn].dir, dom); |
237 |
> |
(*f)(srccnt[sn].coef, p, sr.rdir, si.dom); |
238 |
|
cntord[sn].brt = bright(srccnt[sn].coef); |
239 |
|
if (cntord[sn].brt <= 0.0) |
240 |
|
continue; |
241 |
+ |
VCOPY(srccnt[sn].dir, sr.rdir); |
242 |
|
/* compute potential */ |
243 |
|
sr.revf = srcvalue; |
244 |
|
rayvalue(&sr); |
247 |
|
cntord[sn].brt = bright(srccnt[sn].val); |
248 |
|
} |
249 |
|
/* sort contributions */ |
250 |
< |
qsort(cntord, nsources, sizeof(CNTPTR), cntcmp); |
250 |
> |
qsort(cntord, sn, sizeof(CNTPTR), cntcmp); |
251 |
|
{ /* find last */ |
252 |
|
register int l, m; |
253 |
|
|
254 |
< |
sn = 0; ncnts = l = nsources; |
254 |
> |
ncnts = l = sn; |
255 |
> |
sn = 0; |
256 |
|
while ((m = (sn + ncnts) >> 1) != l) { |
257 |
|
if (cntord[m].brt > 0.0) |
258 |
|
sn = m; |
264 |
|
/* accumulate tail */ |
265 |
|
for (sn = ncnts-1; sn > 0; sn--) |
266 |
|
cntord[sn-1].brt += cntord[sn].brt; |
267 |
+ |
/* compute number to check */ |
268 |
+ |
nshadcheck = pow((double)ncnts, shadcert) + .5; |
269 |
+ |
/* modify threshold */ |
270 |
+ |
ourthresh = shadthresh / r->rweight; |
271 |
|
/* test for shadows */ |
272 |
|
nhits = 0; |
273 |
|
for (sn = 0; sn < ncnts; sn++) { |
276 |
|
cntord[sn].brt-cntord[sn+nshadcheck].brt) |
277 |
|
< ourthresh*bright(r->rcol)) |
278 |
|
break; |
308 |
– |
/* get statistics */ |
309 |
– |
source[cntord[sn].sno].ntests++; |
279 |
|
/* test for hit */ |
280 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
281 |
< |
VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir); |
282 |
< |
sr.rsrc = cntord[sn].sno; |
281 |
> |
VCOPY(sr.rdir, srccnt[cntord[sn].sndx].dir); |
282 |
> |
sr.rsrc = srccnt[cntord[sn].sndx].sno; |
283 |
> |
source[sr.rsrc].ntests++; /* keep statistics */ |
284 |
|
if (localhit(&sr, &thescene) && |
285 |
< |
( sr.ro != source[cntord[sn].sno].so || |
286 |
< |
source[cntord[sn].sno].sflags & SFOLLOW )) { |
285 |
> |
( sr.ro != source[sr.rsrc].so || |
286 |
> |
source[sr.rsrc].sflags & SFOLLOW )) { |
287 |
|
/* follow entire path */ |
288 |
|
raycont(&sr); |
289 |
+ |
if (trace != NULL) |
290 |
+ |
(*trace)(&sr); /* trace execution */ |
291 |
|
if (bright(sr.rcol) <= FTINY) |
292 |
|
continue; /* missed! */ |
293 |
< |
copycolor(srccnt[cntord[sn].sno].val, sr.rcol); |
294 |
< |
multcolor(srccnt[cntord[sn].sno].val, |
295 |
< |
srccnt[cntord[sn].sno].coef); |
293 |
> |
copycolor(srccnt[cntord[sn].sndx].val, sr.rcol); |
294 |
> |
multcolor(srccnt[cntord[sn].sndx].val, |
295 |
> |
srccnt[cntord[sn].sndx].coef); |
296 |
|
} |
297 |
|
/* add contribution if hit */ |
298 |
< |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
298 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sndx].val); |
299 |
|
nhits++; |
300 |
< |
source[cntord[sn].sno].nhits++; |
300 |
> |
source[sr.rsrc].nhits++; |
301 |
|
} |
302 |
|
/* surface hit rate */ |
303 |
|
if (sn > 0) |
311 |
|
#endif |
312 |
|
/* add in untested sources */ |
313 |
|
for ( ; sn < ncnts; sn++) { |
314 |
< |
prob = hwt * (double)source[cntord[sn].sno].nhits / |
315 |
< |
(double)source[cntord[sn].sno].ntests; |
316 |
< |
scalecolor(srccnt[cntord[sn].sno].val, prob); |
317 |
< |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
314 |
> |
sr.rsrc = srccnt[cntord[sn].sndx].sno; |
315 |
> |
prob = hwt * (double)source[sr.rsrc].nhits / |
316 |
> |
(double)source[sr.rsrc].ntests; |
317 |
> |
scalecolor(srccnt[cntord[sn].sndx].val, prob); |
318 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sndx].val); |
319 |
|
} |
320 |
|
} |