19 |
|
|
20 |
|
#include "random.h" |
21 |
|
|
22 |
+ |
#define MINSAMPLES 16 /* minimum number of pretest samples */ |
23 |
+ |
#define STESTMAX 32 /* maximum seeks per sample */ |
24 |
|
|
25 |
+ |
|
26 |
|
double getdisk(); |
27 |
|
|
28 |
|
static OBJECT *vobject; /* virtual source objects */ |
44 |
|
if (!isvlight(objptr(o->omod)->otype)) |
45 |
|
continue; |
46 |
|
if (sfun[o->otype].of == NULL || |
47 |
< |
sfun[o->otype].of->getpleq == NULL) |
48 |
< |
objerror(o, USER, "illegal material"); |
47 |
> |
sfun[o->otype].of->getpleq == NULL) { |
48 |
> |
objerror(o,WARNING,"secondary sources not supported"); |
49 |
> |
continue; |
50 |
> |
} |
51 |
|
if (nvobjects == 0) |
52 |
|
vobject = (OBJECT *)malloc(sizeof(OBJECT)); |
53 |
|
else |
106 |
|
for (i = 0; i < vsmat->nproj; i++) |
107 |
|
if ((*vsmat->vproj)(proj, o, &source[sn], i)) |
108 |
|
if ((ns = makevsrc(o, sn, proj)) >= 0) { |
109 |
+ |
source[ns].sa.sv.pn = i; |
110 |
|
#ifdef DEBUG |
111 |
|
virtverb(ns, stderr); |
112 |
|
#endif |
137 |
|
if (source[sn].sflags & SPROX) |
138 |
|
return(-1); /* should never get here! */ |
139 |
|
multv3(nsloc, source[sn].sloc, pm); |
140 |
+ |
normalize(nsloc); |
141 |
|
VCOPY(ourspot.aim, ocent); |
142 |
|
ourspot.siz = PI*maxrad2; |
143 |
|
ourspot.flen = 0.; |
144 |
|
if (source[sn].sflags & SSPOT) { |
138 |
– |
copystruct(&theirspot, source[sn].sl.s); |
145 |
|
multp3(theirspot.aim, source[sn].sl.s->aim, pm); |
146 |
+ |
/* adjust for source size */ |
147 |
+ |
d = sqrt(dist2(ourspot.aim, theirspot.aim)); |
148 |
+ |
d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].srad; |
149 |
+ |
theirspot.siz = PI*d*d; |
150 |
+ |
ourspot.flen = theirspot.flen = source[sn].sl.s->flen; |
151 |
|
d = ourspot.siz; |
152 |
|
if (!commonbeam(&ourspot, &theirspot, nsloc)) |
153 |
|
return(-1); /* no overlap */ |
169 |
|
return(-1); /* at source!! */ |
170 |
|
if (source[sn].sflags & SPROX && d > source[sn].sl.prox) |
171 |
|
return(-1); /* too far away */ |
161 |
– |
ourspot.siz = 2.*PI*(1. - d/sqrt(d*d+maxrad2)); |
172 |
|
ourspot.flen = 0.; |
173 |
+ |
/* adjust for source size */ |
174 |
+ |
d = (sqrt(maxrad2) + source[sn].srad) / d; |
175 |
+ |
if (d < 1.-FTINY) |
176 |
+ |
ourspot.siz = 2.*PI*(1. - sqrt(1.-d*d)); |
177 |
+ |
else |
178 |
+ |
nsflags &= ~SSPOT; |
179 |
|
if (source[sn].sflags & SSPOT) { |
180 |
|
copystruct(&theirspot, source[sn].sl.s); |
181 |
|
multv3(theirspot.aim, source[sn].sl.s->aim, pm); |
182 |
< |
d = ourspot.siz; |
183 |
< |
if (!commonspot(&ourspot, &theirspot, nsloc)) |
184 |
< |
return(-1); /* no overlap */ |
182 |
> |
normalize(theirspot.aim); |
183 |
> |
if (nsflags & SSPOT) { |
184 |
> |
ourspot.flen = theirspot.flen; |
185 |
> |
d = ourspot.siz; |
186 |
> |
if (!commonspot(&ourspot, &theirspot, nsloc)) |
187 |
> |
return(-1); /* no overlap */ |
188 |
> |
} else { |
189 |
> |
nsflags |= SSPOT; |
190 |
> |
copystruct(&ourspot, &theirspot); |
191 |
> |
d = 2.*ourspot.siz; |
192 |
> |
} |
193 |
|
if (ourspot.siz < d-FTINY) { /* it shrunk */ |
194 |
|
d = spotdisk(v, op, &ourspot, nsloc); |
195 |
|
if (d <= FTINY) |
199 |
|
VCOPY(ocent, v); |
200 |
|
} |
201 |
|
} |
178 |
– |
ourspot.flen = theirspot.flen; |
202 |
|
} |
203 |
|
if (source[sn].sflags & SFLAT) { /* behind source? */ |
204 |
|
multv3(nsnorm, source[sn].snorm, pm); |
205 |
< |
if (!checkspot(&ourspot, nsnorm)) |
205 |
> |
normalize(nsnorm); |
206 |
> |
if (nsflags & SSPOT && !checkspot(&ourspot, nsnorm)) |
207 |
|
return(-1); |
208 |
|
} |
209 |
|
} |
216 |
|
goto memerr; |
217 |
|
source[i].sflags = nsflags; |
218 |
|
VCOPY(source[i].sloc, nsloc); |
219 |
+ |
multv3(source[i].ss[SU], source[sn].ss[SU], pm); |
220 |
+ |
multv3(source[i].ss[SV], source[sn].ss[SV], pm); |
221 |
|
if (nsflags & SFLAT) |
222 |
|
VCOPY(source[i].snorm, nsnorm); |
223 |
< |
source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2; |
224 |
< |
if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
225 |
< |
goto memerr; |
226 |
< |
copystruct(source[i].sl.s, &ourspot); |
223 |
> |
else |
224 |
> |
multv3(source[i].ss[SW], source[sn].ss[SW], pm); |
225 |
> |
source[i].srad = source[sn].srad; |
226 |
> |
source[i].ss2 = source[sn].ss2; |
227 |
> |
if (nsflags & SSPOT) { |
228 |
> |
if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
229 |
> |
goto memerr; |
230 |
> |
copystruct(source[i].sl.s, &ourspot); |
231 |
> |
} |
232 |
|
if (nsflags & SPROX) |
233 |
|
source[i].sl.prox = source[sn].sl.prox; |
234 |
< |
source[i].sa.svnext = sn; |
234 |
> |
source[i].sa.sv.sn = sn; |
235 |
|
source[i].so = op; |
236 |
|
return(i); |
237 |
|
memerr: |
282 |
|
RAY sr; |
283 |
|
FVECT onorm; |
284 |
|
FVECT offsdir; |
285 |
+ |
SRCINDEX si; |
286 |
|
double or, d; |
287 |
|
int infront; |
288 |
< |
int ssn; |
288 |
> |
int stestlim, ssn; |
289 |
|
int nhit, nok; |
290 |
|
register int i, n; |
291 |
|
/* return if pretesting disabled */ |
295 |
|
getplaneq(onorm, o); |
296 |
|
/* set number of rays to sample */ |
297 |
|
if (source[sn].sflags & SDISTANT) { |
298 |
< |
n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)* |
299 |
< |
vspretest + .5; |
298 |
> |
/* 32. == heuristic constant */ |
299 |
> |
n = 32.*or2/(thescene.cusize*thescene.cusize)*vspretest + .5; |
300 |
|
infront = DOT(onorm, source[sn].sloc) > 0.; |
301 |
|
} else { |
302 |
|
for (i = 0; i < 3; i++) |
303 |
|
offsdir[i] = source[sn].sloc[i] - oc[i]; |
304 |
< |
n = or2/DOT(offsdir,offsdir)*vspretest + .5; |
304 |
> |
d = DOT(offsdir,offsdir); |
305 |
> |
if (d <= FTINY) |
306 |
> |
n = 2.*PI * vspretest + .5; |
307 |
> |
else |
308 |
> |
n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5; |
309 |
|
infront = DOT(onorm, offsdir) > 0.; |
310 |
|
} |
311 |
< |
if (n < 1) n = 1; |
311 |
> |
if (n < MINSAMPLES) n = MINSAMPLES; |
312 |
|
#ifdef DEBUG |
313 |
|
fprintf(stderr, "pretesting source %d in object %s with %d rays\n", |
314 |
|
sn, o->oname, n); |
315 |
|
#endif |
316 |
|
/* sample */ |
317 |
|
or = sqrt(or2); |
318 |
< |
ssn = 25*n; |
318 |
> |
stestlim = n*STESTMAX; |
319 |
> |
ssn = 0; |
320 |
|
nhit = nok = 0; |
321 |
|
while (n-- > 0) { |
322 |
|
/* get sample point */ |
323 |
|
do { |
324 |
< |
if (--ssn < 0) { |
324 |
> |
if (ssn >= stestlim) { |
325 |
|
#ifdef DEBUG |
326 |
|
fprintf(stderr, "\ttoo hard to hit\n"); |
327 |
|
#endif |
328 |
|
return(f); /* too small a target! */ |
329 |
|
} |
330 |
+ |
multisamp(offsdir, 3, urand(sn*931+5827+ssn)); |
331 |
|
for (i = 0; i < 3; i++) |
332 |
< |
offsdir[i] = or*(1. - |
333 |
< |
2.*urand(931*i+5827+ssn)); |
332 |
> |
offsdir[i] = or*(1. - 2.*offsdir[i]); |
333 |
> |
ssn++; |
334 |
|
for (i = 0; i < 3; i++) |
335 |
|
sr.rorg[i] = oc[i] + offsdir[i]; |
336 |
|
d = DOT(offsdir,onorm); |
347 |
|
rayorigin(&sr, NULL, PRIMARY, 1.0); |
348 |
|
} while (!(*ofun[o->otype].funp)(o, &sr)); |
349 |
|
/* check against source */ |
350 |
+ |
initsrcindex(&si); |
351 |
+ |
si.sn = sn; |
352 |
+ |
nopart(&si, &sr); |
353 |
|
samplendx++; |
354 |
< |
if (srcray(&sr, NULL, sn) == 0.) |
354 |
> |
if (!srcray(&sr, NULL, &si) || sr.rsrc != sn) |
355 |
|
continue; |
356 |
|
sr.revf = srcvalue; |
357 |
|
rayvalue(&sr); |
359 |
|
continue; |
360 |
|
nok++; |
361 |
|
/* check against obstructions */ |
362 |
< |
srcray(&sr, NULL, sn); |
362 |
> |
rayclear(&sr); |
363 |
> |
sr.revf = raytrace; |
364 |
|
rayvalue(&sr); |
365 |
|
if (bright(sr.rcol) > FTINY) |
366 |
|
nhit++; |
398 |
|
fprintf(fp, "\tat (%f,%f,%f)\n", |
399 |
|
source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]); |
400 |
|
fprintf(fp, "\tlinked to source %d (%s)\n", |
401 |
< |
source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname); |
401 |
> |
source[sn].sa.sv.sn, source[source[sn].sa.sv.sn].so->oname); |
402 |
|
if (source[sn].sflags & SFOLLOW) |
403 |
|
fprintf(fp, "\talways followed\n"); |
404 |
|
else |