11 |
|
|
12 |
|
#include "ray.h" |
13 |
|
|
14 |
+ |
#include "octree.h" |
15 |
+ |
|
16 |
|
#include "otypes.h" |
17 |
|
|
18 |
|
#include "source.h" |
19 |
|
|
20 |
+ |
#include "random.h" |
21 |
|
|
22 |
< |
double intercircle(), getdisk(); |
22 |
> |
#define MINSAMPLES 3 /* minimum number of pretest samples */ |
23 |
> |
#define STESTMAX 30 /* maximum seeks per sample */ |
24 |
|
|
25 |
+ |
|
26 |
+ |
double getdisk(); |
27 |
+ |
|
28 |
|
static OBJECT *vobject; /* virtual source objects */ |
29 |
|
static int nvobjects = 0; /* number of virtual source objects */ |
30 |
|
|
62 |
|
#endif |
63 |
|
/* append virtual sources */ |
64 |
|
for (i = nsources; i-- > 0; ) |
65 |
< |
if (!(source[i].sflags & SSKIP)) |
59 |
< |
addvirtuals(i, directrelay); |
65 |
> |
addvirtuals(i, directrelay); |
66 |
|
/* done with our object list */ |
67 |
|
free((char *)vobject); |
68 |
|
nvobjects = 0; |
77 |
|
/* check relay limit first */ |
78 |
|
if (nr <= 0) |
79 |
|
return; |
80 |
+ |
if (source[sn].sflags & SSKIP) |
81 |
+ |
return; |
82 |
|
/* check each virtual object for projection */ |
83 |
|
for (i = 0; i < nvobjects; i++) |
84 |
|
/* vproject() calls us recursively */ |
118 |
|
register int sn; |
119 |
|
MAT4 pm; |
120 |
|
{ |
121 |
< |
FVECT nsloc, nsnorm, ocent; |
122 |
< |
double maxrad2; |
121 |
> |
FVECT nsloc, nsnorm, ocent, v; |
122 |
> |
double maxrad2, d; |
123 |
|
int nsflags; |
116 |
– |
double d1; |
124 |
|
SPOT theirspot, ourspot; |
125 |
|
register int i; |
126 |
|
|
140 |
|
if (source[sn].sflags & SSPOT) { |
141 |
|
copystruct(&theirspot, source[sn].sl.s); |
142 |
|
multp3(theirspot.aim, source[sn].sl.s->aim, pm); |
143 |
+ |
d = ourspot.siz; |
144 |
|
if (!commonbeam(&ourspot, &theirspot, nsloc)) |
145 |
< |
return(-1); /* no overlap */ |
145 |
> |
return(-1); /* no overlap */ |
146 |
> |
if (ourspot.siz < d-FTINY) { /* it shrunk */ |
147 |
> |
d = beamdisk(v, op, &ourspot, nsloc); |
148 |
> |
if (d <= FTINY) |
149 |
> |
return(-1); |
150 |
> |
if (d < maxrad2) { |
151 |
> |
maxrad2 = d; |
152 |
> |
VCOPY(ocent, v); |
153 |
> |
} |
154 |
> |
} |
155 |
|
} |
156 |
|
} else { /* local source */ |
157 |
|
multp3(nsloc, source[sn].sloc, pm); |
158 |
|
for (i = 0; i < 3; i++) |
159 |
|
ourspot.aim[i] = ocent[i] - nsloc[i]; |
160 |
< |
if ((d1 = normalize(ourspot.aim)) == 0.) |
160 |
> |
if ((d = normalize(ourspot.aim)) == 0.) |
161 |
|
return(-1); /* at source!! */ |
162 |
< |
if (source[sn].sflags & SPROX && d1 > source[sn].sl.prox) |
162 |
> |
if (source[sn].sflags & SPROX && d > source[sn].sl.prox) |
163 |
|
return(-1); /* too far away */ |
147 |
– |
ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2)); |
164 |
|
ourspot.flen = 0.; |
165 |
+ |
if (d*d > maxrad2) |
166 |
+ |
ourspot.siz = 2.*PI*(1. - sqrt(1.-maxrad2/(d*d))); |
167 |
+ |
else |
168 |
+ |
nsflags &= ~SSPOT; |
169 |
|
if (source[sn].sflags & SSPOT) { |
170 |
|
copystruct(&theirspot, source[sn].sl.s); |
171 |
|
multv3(theirspot.aim, source[sn].sl.s->aim, pm); |
172 |
< |
if (!commonspot(&ourspot, &theirspot, nsloc)) |
173 |
< |
return(-1); /* no overlap */ |
174 |
< |
ourspot.flen = theirspot.flen; |
172 |
> |
if (nsflags & SSPOT) { |
173 |
> |
ourspot.flen = theirspot.flen; |
174 |
> |
d = ourspot.siz; |
175 |
> |
if (!commonspot(&ourspot, &theirspot, nsloc)) |
176 |
> |
return(-1); /* no overlap */ |
177 |
> |
} else { |
178 |
> |
nsflags |= SSPOT; |
179 |
> |
copystruct(&ourspot, &theirspot); |
180 |
> |
d = 2.*ourspot.siz; |
181 |
> |
} |
182 |
> |
if (ourspot.siz < d-FTINY) { /* it shrunk */ |
183 |
> |
d = spotdisk(v, op, &ourspot, nsloc); |
184 |
> |
if (d <= FTINY) |
185 |
> |
return(-1); |
186 |
> |
if (d < maxrad2) { |
187 |
> |
maxrad2 = d; |
188 |
> |
VCOPY(ocent, v); |
189 |
> |
} |
190 |
> |
} |
191 |
|
} |
192 |
|
if (source[sn].sflags & SFLAT) { /* behind source? */ |
193 |
|
multv3(nsnorm, source[sn].snorm, pm); |
194 |
< |
if (checkspot(&ourspot, nsnorm) < 0) |
194 |
> |
if (!checkspot(&ourspot, nsnorm)) |
195 |
|
return(-1); |
196 |
|
} |
197 |
|
} |
198 |
< |
/* everything is OK, make source */ |
198 |
> |
/* pretest visibility */ |
199 |
> |
nsflags = vstestvis(nsflags, op, ocent, maxrad2, sn); |
200 |
> |
if (nsflags & SSKIP) |
201 |
> |
return(-1); /* obstructed */ |
202 |
> |
/* it all checks out, so make it */ |
203 |
|
if ((i = newsource()) < 0) |
204 |
|
goto memerr; |
205 |
|
source[i].sflags = nsflags; |
207 |
|
if (nsflags & SFLAT) |
208 |
|
VCOPY(source[i].snorm, nsnorm); |
209 |
|
source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2; |
210 |
< |
if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
211 |
< |
goto memerr; |
212 |
< |
copystruct(source[i].sl.s, &ourspot); |
210 |
> |
if (nsflags & SSPOT) { |
211 |
> |
if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
212 |
> |
goto memerr; |
213 |
> |
copystruct(source[i].sl.s, &ourspot); |
214 |
> |
} |
215 |
|
if (nsflags & SPROX) |
216 |
|
source[i].sl.prox = source[sn].sl.prox; |
217 |
|
source[i].sa.svnext = sn; |
231 |
|
double rad2, roffs, offs, d, rd, rdoto; |
232 |
|
FVECT rnrm, nrm; |
233 |
|
/* first, use object getdisk function */ |
234 |
< |
rad2 = (*sfun[op->otype].of->getdisk)(oc, op); |
234 |
> |
rad2 = getmaxdisk(oc, op); |
235 |
|
if (!(source[sn].sflags & SVIRTUAL)) |
236 |
|
return(rad2); /* all done for normal source */ |
237 |
|
/* check for correct side of relay surface */ |
238 |
< |
roffs = (*sfun[source[sn].so->otype].of->getpleq)(rnrm, source[sn].so); |
238 |
> |
roffs = getplaneq(rnrm, source[sn].so); |
239 |
|
rd = DOT(rnrm, source[sn].sloc); /* source projection */ |
240 |
|
if (!(source[sn].sflags & SDISTANT)) |
241 |
|
rd -= roffs; |
243 |
|
if ((d > 0.) ^ (rd > 0.)) |
244 |
|
return(rad2); /* OK if opposite sides */ |
245 |
|
if (d*d >= rad2) |
246 |
< |
return(.0); /* no relay is possible */ |
246 |
> |
return(0.); /* no relay is possible */ |
247 |
|
/* we need a closer look */ |
248 |
< |
offs = (*sfun[op->otype].of->getpleq)(nrm, op); |
248 |
> |
offs = getplaneq(nrm, op); |
249 |
|
rdoto = DOT(rnrm, nrm); |
250 |
|
if (d*d >= rad2*(1.-rdoto*rdoto)) |
251 |
|
return(0.); /* disk entirely on projection side */ |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
< |
commonspot(sp1, sp2, org) /* set sp1 to intersection of sp1 and sp2 */ |
258 |
< |
register SPOT *sp1, *sp2; |
259 |
< |
FVECT org; |
257 |
> |
int |
258 |
> |
vstestvis(f, o, oc, or2, sn) /* pretest source visibility */ |
259 |
> |
int f; /* virtual source flags */ |
260 |
> |
OBJREC *o; /* relay object */ |
261 |
> |
FVECT oc; /* relay object center */ |
262 |
> |
double or2; /* relay object radius squared */ |
263 |
> |
register int sn; /* target source number */ |
264 |
|
{ |
265 |
< |
FVECT cent; |
266 |
< |
double rad2, cos1, cos2; |
267 |
< |
|
268 |
< |
cos1 = 1. - sp1->siz/(2.*PI); |
269 |
< |
cos2 = 1. - sp2->siz/(2.*PI); |
270 |
< |
if (sp2->siz >= 2.*PI-FTINY) /* BIG, just check overlap */ |
271 |
< |
return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 - |
272 |
< |
sqrt((1.-cos1*cos1)*(1.-cos2*cos2))); |
273 |
< |
/* compute and check disks */ |
274 |
< |
rad2 = intercircle(cent, sp1->aim, sp2->aim, |
275 |
< |
1./(cos1*cos1) - 1., 1./(cos2*cos2) - 1.); |
276 |
< |
if (rad2 <= FTINY || normalize(cent) == 0.) |
277 |
< |
return(0); |
278 |
< |
VCOPY(sp1->aim, cent); |
279 |
< |
sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2)); |
280 |
< |
return(1); |
281 |
< |
} |
282 |
< |
|
237 |
< |
|
238 |
< |
commonbeam(sp1, sp2, dir) /* set sp1 to intersection of sp1 and sp2 */ |
239 |
< |
register SPOT *sp1, *sp2; |
240 |
< |
FVECT dir; |
241 |
< |
{ |
242 |
< |
FVECT cent, c1, c2; |
243 |
< |
double rad2, d; |
244 |
< |
register int i; |
245 |
< |
/* move centers to common plane */ |
246 |
< |
d = DOT(sp1->aim, dir); |
247 |
< |
for (i = 0; i < 3; i++) |
248 |
< |
c1[i] = sp1->aim[i] - d*dir[i]; |
249 |
< |
d = DOT(sp2->aim, dir); |
250 |
< |
for (i = 0; i < 3; i++) |
251 |
< |
c2[i] = sp2->aim[i] - d*dir[i]; |
252 |
< |
/* compute overlap */ |
253 |
< |
rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI); |
254 |
< |
if (rad2 <= FTINY) |
255 |
< |
return(0); |
256 |
< |
VCOPY(sp1->aim, cent); |
257 |
< |
sp1->siz = PI*rad2; |
258 |
< |
return(1); |
259 |
< |
} |
260 |
< |
|
261 |
< |
|
262 |
< |
checkspot(sp, nrm) /* check spotlight for behind source */ |
263 |
< |
register SPOT *sp; |
264 |
< |
FVECT nrm; |
265 |
< |
{ |
266 |
< |
double d, d1; |
267 |
< |
|
268 |
< |
d = DOT(sp->aim, nrm); |
269 |
< |
if (d > FTINY) /* center in front? */ |
270 |
< |
return(0); |
271 |
< |
/* else check horizon */ |
272 |
< |
d1 = 1. - sp->siz/(2.*PI); |
273 |
< |
return(1.-FTINY-d*d > d1*d1); |
274 |
< |
} |
275 |
< |
|
276 |
< |
|
277 |
< |
double |
278 |
< |
intercircle(cc, c1, c2, r1s, r2s) /* intersect two circles */ |
279 |
< |
FVECT cc; /* midpoint (return value) */ |
280 |
< |
FVECT c1, c2; /* circle centers */ |
281 |
< |
double r1s, r2s; /* radii squared */ |
282 |
< |
{ |
283 |
< |
double a2, d2, l; |
284 |
< |
FVECT disp; |
285 |
< |
register int i; |
286 |
< |
|
287 |
< |
for (i = 0; i < 3; i++) |
288 |
< |
disp[i] = c2[i] - c1[i]; |
289 |
< |
d2 = DOT(disp,disp); |
290 |
< |
/* circle within overlap? */ |
291 |
< |
if (r1s < r2s) { |
292 |
< |
if (r2s >= r1s + d2) { |
293 |
< |
VCOPY(cc, c1); |
294 |
< |
return(r1s); |
295 |
< |
} |
265 |
> |
RAY sr; |
266 |
> |
FVECT onorm; |
267 |
> |
FVECT offsdir; |
268 |
> |
double or, d; |
269 |
> |
int infront; |
270 |
> |
int ssn; |
271 |
> |
int nhit, nok; |
272 |
> |
register int i, n; |
273 |
> |
/* return if pretesting disabled */ |
274 |
> |
if (vspretest <= 0) |
275 |
> |
return(f); |
276 |
> |
/* get surface normal */ |
277 |
> |
getplaneq(onorm, o); |
278 |
> |
/* set number of rays to sample */ |
279 |
> |
if (source[sn].sflags & SDISTANT) { |
280 |
> |
n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)* |
281 |
> |
vspretest + .5; |
282 |
> |
infront = DOT(onorm, source[sn].sloc) > 0.; |
283 |
|
} else { |
284 |
< |
if (r1s >= r2s + d2) { |
285 |
< |
VCOPY(cc, c2); |
286 |
< |
return(r2s); |
284 |
> |
for (i = 0; i < 3; i++) |
285 |
> |
offsdir[i] = source[sn].sloc[i] - oc[i]; |
286 |
> |
n = or2/DOT(offsdir,offsdir)*vspretest + .5; |
287 |
> |
infront = DOT(onorm, offsdir) > 0.; |
288 |
> |
} |
289 |
> |
if (n < MINSAMPLES) n = MINSAMPLES; |
290 |
> |
#ifdef DEBUG |
291 |
> |
fprintf(stderr, "pretesting source %d in object %s with %d rays\n", |
292 |
> |
sn, o->oname, n); |
293 |
> |
#endif |
294 |
> |
/* sample */ |
295 |
> |
or = sqrt(or2); |
296 |
> |
ssn = STESTMAX*n; |
297 |
> |
nhit = nok = 0; |
298 |
> |
while (n-- > 0) { |
299 |
> |
/* get sample point */ |
300 |
> |
do { |
301 |
> |
if (--ssn < 0) { |
302 |
> |
#ifdef DEBUG |
303 |
> |
fprintf(stderr, "\ttoo hard to hit\n"); |
304 |
> |
#endif |
305 |
> |
return(f); /* too small a target! */ |
306 |
> |
} |
307 |
> |
for (i = 0; i < 3; i++) |
308 |
> |
offsdir[i] = or*(1. - |
309 |
> |
2.*urand(931*i+5827+ssn)); |
310 |
> |
for (i = 0; i < 3; i++) |
311 |
> |
sr.rorg[i] = oc[i] + offsdir[i]; |
312 |
> |
d = DOT(offsdir,onorm); |
313 |
> |
if (infront) |
314 |
> |
for (i = 0; i < 3; i++) { |
315 |
> |
sr.rorg[i] -= (d-.0001)*onorm[i]; |
316 |
> |
sr.rdir[i] = -onorm[i]; |
317 |
> |
} |
318 |
> |
else |
319 |
> |
for (i = 0; i < 3; i++) { |
320 |
> |
sr.rorg[i] -= (d+.0001)*onorm[i]; |
321 |
> |
sr.rdir[i] = onorm[i]; |
322 |
> |
} |
323 |
> |
rayorigin(&sr, NULL, PRIMARY, 1.0); |
324 |
> |
} while (!(*ofun[o->otype].funp)(o, &sr)); |
325 |
> |
/* check against source */ |
326 |
> |
samplendx++; |
327 |
> |
if (srcray(&sr, NULL, sn) == 0.) |
328 |
> |
continue; |
329 |
> |
sr.revf = srcvalue; |
330 |
> |
rayvalue(&sr); |
331 |
> |
if (bright(sr.rcol) <= FTINY) |
332 |
> |
continue; |
333 |
> |
nok++; |
334 |
> |
/* check against obstructions */ |
335 |
> |
srcray(&sr, NULL, sn); |
336 |
> |
rayvalue(&sr); |
337 |
> |
if (bright(sr.rcol) > FTINY) |
338 |
> |
nhit++; |
339 |
> |
if (nhit > 0 && nhit < nok) { |
340 |
> |
#ifdef DEBUG |
341 |
> |
fprintf(stderr, "\tpartially occluded\n"); |
342 |
> |
#endif |
343 |
> |
return(f); /* need to shadow test */ |
344 |
|
} |
345 |
|
} |
346 |
< |
a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2); |
347 |
< |
/* no overlap? */ |
348 |
< |
if (a2 <= 0.) |
349 |
< |
return(0.); |
350 |
< |
/* overlap, compute center */ |
351 |
< |
l = sqrt((r1s - a2)/d2); |
352 |
< |
for (i = 0; i < 3; i++) |
353 |
< |
cc[i] = c1[i] + l*disp[i]; |
354 |
< |
return(a2); |
346 |
> |
if (nhit == 0) { |
347 |
> |
#ifdef DEBUG |
348 |
> |
fprintf(stderr, "\t0%% hit rate\n"); |
349 |
> |
#endif |
350 |
> |
return(f | SSKIP); /* 0% hit rate: totally occluded */ |
351 |
> |
} |
352 |
> |
#ifdef DEBUG |
353 |
> |
fprintf(stderr, "\t100%% hit rate\n"); |
354 |
> |
#endif |
355 |
> |
return(f & ~SFOLLOW); /* 100% hit rate: no occlusion */ |
356 |
|
} |
357 |
< |
|
357 |
> |
|
358 |
|
|
359 |
|
#ifdef DEBUG |
360 |
|
virtverb(sn, fp) /* print verbose description of virtual source */ |