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 |
|
|
19 |
– |
double intercircle(), getdisk(); |
22 |
|
|
23 |
+ |
double getdisk(); |
24 |
+ |
|
25 |
|
static OBJECT *vobject; /* virtual source objects */ |
26 |
|
static int nvobjects = 0; /* number of virtual source objects */ |
27 |
|
|
59 |
|
#endif |
60 |
|
/* append virtual sources */ |
61 |
|
for (i = nsources; i-- > 0; ) |
62 |
< |
if (!(source[i].sflags & SSKIP)) |
59 |
< |
addvirtuals(i, directrelay); |
62 |
> |
addvirtuals(i, directrelay); |
63 |
|
/* done with our object list */ |
64 |
|
free((char *)vobject); |
65 |
|
nvobjects = 0; |
74 |
|
/* check relay limit first */ |
75 |
|
if (nr <= 0) |
76 |
|
return; |
77 |
+ |
if (source[sn].sflags & SSKIP) |
78 |
+ |
return; |
79 |
|
/* check each virtual object for projection */ |
80 |
|
for (i = 0; i < nvobjects; i++) |
81 |
|
/* vproject() calls us recursively */ |
115 |
|
register int sn; |
116 |
|
MAT4 pm; |
117 |
|
{ |
118 |
< |
FVECT nsloc, nsnorm, ocent; |
119 |
< |
double maxrad2; |
118 |
> |
FVECT nsloc, nsnorm, ocent, v; |
119 |
> |
double maxrad2, d; |
120 |
|
int nsflags; |
116 |
– |
double d1; |
121 |
|
SPOT theirspot, ourspot; |
122 |
|
register int i; |
123 |
|
|
137 |
|
if (source[sn].sflags & SSPOT) { |
138 |
|
copystruct(&theirspot, source[sn].sl.s); |
139 |
|
multp3(theirspot.aim, source[sn].sl.s->aim, pm); |
140 |
+ |
d = ourspot.siz; |
141 |
|
if (!commonbeam(&ourspot, &theirspot, nsloc)) |
142 |
< |
return(-1); /* no overlap */ |
142 |
> |
return(-1); /* no overlap */ |
143 |
> |
if (ourspot.siz < d-FTINY) { /* it shrunk */ |
144 |
> |
d = beamdisk(v, op, &ourspot, nsloc); |
145 |
> |
if (d <= FTINY) |
146 |
> |
return(-1); |
147 |
> |
if (d < maxrad2) { |
148 |
> |
maxrad2 = d; |
149 |
> |
VCOPY(ocent, v); |
150 |
> |
} |
151 |
> |
} |
152 |
|
} |
153 |
|
} else { /* local source */ |
154 |
|
multp3(nsloc, source[sn].sloc, pm); |
155 |
|
for (i = 0; i < 3; i++) |
156 |
|
ourspot.aim[i] = ocent[i] - nsloc[i]; |
157 |
< |
if ((d1 = normalize(ourspot.aim)) == 0.) |
157 |
> |
if ((d = normalize(ourspot.aim)) == 0.) |
158 |
|
return(-1); /* at source!! */ |
159 |
< |
if (source[sn].sflags & SPROX && d1 > source[sn].sl.prox) |
159 |
> |
if (source[sn].sflags & SPROX && d > source[sn].sl.prox) |
160 |
|
return(-1); /* too far away */ |
161 |
< |
ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2)); |
161 |
> |
ourspot.siz = 2.*PI*(1. - d/sqrt(d*d+maxrad2)); |
162 |
|
ourspot.flen = 0.; |
163 |
|
if (source[sn].sflags & SSPOT) { |
164 |
|
copystruct(&theirspot, source[sn].sl.s); |
165 |
|
multv3(theirspot.aim, source[sn].sl.s->aim, pm); |
166 |
+ |
d = ourspot.siz; |
167 |
|
if (!commonspot(&ourspot, &theirspot, nsloc)) |
168 |
|
return(-1); /* no overlap */ |
169 |
+ |
if (ourspot.siz < d-FTINY) { /* it shrunk */ |
170 |
+ |
d = spotdisk(v, op, &ourspot, nsloc); |
171 |
+ |
if (d <= FTINY) |
172 |
+ |
return(-1); |
173 |
+ |
if (d < maxrad2) { |
174 |
+ |
maxrad2 = d; |
175 |
+ |
VCOPY(ocent, v); |
176 |
+ |
} |
177 |
+ |
} |
178 |
|
ourspot.flen = theirspot.flen; |
179 |
|
} |
180 |
|
if (source[sn].sflags & SFLAT) { /* behind source? */ |
181 |
|
multv3(nsnorm, source[sn].snorm, pm); |
182 |
< |
if (checkspot(&ourspot, nsnorm) < 0) |
182 |
> |
if (!checkspot(&ourspot, nsnorm)) |
183 |
|
return(-1); |
184 |
|
} |
185 |
|
} |
186 |
< |
/* everything is OK, make source */ |
186 |
> |
/* pretest visibility */ |
187 |
> |
nsflags = vstestvis(nsflags, op, ocent, maxrad2, sn); |
188 |
> |
if (nsflags & SSKIP) |
189 |
> |
return(-1); /* obstructed */ |
190 |
> |
/* it all checks out, so make it */ |
191 |
|
if ((i = newsource()) < 0) |
192 |
|
goto memerr; |
193 |
|
source[i].sflags = nsflags; |
217 |
|
double rad2, roffs, offs, d, rd, rdoto; |
218 |
|
FVECT rnrm, nrm; |
219 |
|
/* first, use object getdisk function */ |
220 |
< |
rad2 = (*sfun[op->otype].of->getdisk)(oc, op); |
220 |
> |
rad2 = getmaxdisk(oc, op); |
221 |
|
if (!(source[sn].sflags & SVIRTUAL)) |
222 |
|
return(rad2); /* all done for normal source */ |
223 |
|
/* check for correct side of relay surface */ |
224 |
< |
roffs = (*sfun[source[sn].so->otype].of->getpleq)(rnrm, source[sn].so); |
224 |
> |
roffs = getplaneq(rnrm, source[sn].so); |
225 |
|
rd = DOT(rnrm, source[sn].sloc); /* source projection */ |
226 |
|
if (!(source[sn].sflags & SDISTANT)) |
227 |
|
rd -= roffs; |
229 |
|
if ((d > 0.) ^ (rd > 0.)) |
230 |
|
return(rad2); /* OK if opposite sides */ |
231 |
|
if (d*d >= rad2) |
232 |
< |
return(.0); /* no relay is possible */ |
232 |
> |
return(0.); /* no relay is possible */ |
233 |
|
/* we need a closer look */ |
234 |
< |
offs = (*sfun[op->otype].of->getpleq)(nrm, op); |
234 |
> |
offs = getplaneq(nrm, op); |
235 |
|
rdoto = DOT(rnrm, nrm); |
236 |
|
if (d*d >= rad2*(1.-rdoto*rdoto)) |
237 |
|
return(0.); /* disk entirely on projection side */ |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
< |
commonspot(sp1, sp2, org) /* set sp1 to intersection of sp1 and sp2 */ |
244 |
< |
register SPOT *sp1, *sp2; |
245 |
< |
FVECT org; |
243 |
> |
int |
244 |
> |
vstestvis(f, o, oc, or2, sn) /* pretest source visibility */ |
245 |
> |
int f; /* virtual source flags */ |
246 |
> |
OBJREC *o; /* relay object */ |
247 |
> |
FVECT oc; /* relay object center */ |
248 |
> |
double or2; /* relay object radius squared */ |
249 |
> |
register int sn; /* target source number */ |
250 |
|
{ |
251 |
< |
FVECT cent; |
252 |
< |
double rad2, cos1, cos2; |
253 |
< |
|
254 |
< |
cos1 = 1. - sp1->siz/(2.*PI); |
255 |
< |
cos2 = 1. - sp2->siz/(2.*PI); |
256 |
< |
if (sp2->siz >= 2.*PI-FTINY) /* BIG, just check overlap */ |
257 |
< |
return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 - |
258 |
< |
sqrt((1.-cos1*cos1)*(1.-cos2*cos2))); |
259 |
< |
/* compute and check disks */ |
260 |
< |
rad2 = intercircle(cent, sp1->aim, sp2->aim, |
261 |
< |
1./(cos1*cos1) - 1., 1./(cos2*cos2) - 1.); |
262 |
< |
if (rad2 <= FTINY || normalize(cent) == 0.) |
263 |
< |
return(0); |
264 |
< |
VCOPY(sp1->aim, cent); |
265 |
< |
sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2)); |
266 |
< |
return(1); |
267 |
< |
} |
268 |
< |
|
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 |
< |
} |
251 |
> |
RAY sr; |
252 |
> |
FVECT onorm; |
253 |
> |
FVECT offsdir; |
254 |
> |
double or, d; |
255 |
> |
int infront; |
256 |
> |
int ssn; |
257 |
> |
int nhit, nok; |
258 |
> |
register int i, n; |
259 |
> |
/* return if pretesting disabled */ |
260 |
> |
if (vspretest <= 0) |
261 |
> |
return(f); |
262 |
> |
/* get surface normal */ |
263 |
> |
getplaneq(onorm, o); |
264 |
> |
/* set number of rays to sample */ |
265 |
> |
if (source[sn].sflags & SDISTANT) { |
266 |
> |
n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)* |
267 |
> |
vspretest + .5; |
268 |
> |
infront = DOT(onorm, source[sn].sloc) > 0.; |
269 |
|
} else { |
270 |
< |
if (r1s >= r2s + d2) { |
271 |
< |
VCOPY(cc, c2); |
272 |
< |
return(r2s); |
270 |
> |
for (i = 0; i < 3; i++) |
271 |
> |
offsdir[i] = source[sn].sloc[i] - oc[i]; |
272 |
> |
n = or2/DOT(offsdir,offsdir)*vspretest + .5; |
273 |
> |
infront = DOT(onorm, offsdir) > 0.; |
274 |
> |
} |
275 |
> |
if (n < 1) n = 1; |
276 |
> |
#ifdef DEBUG |
277 |
> |
fprintf(stderr, "pretesting source %d in object %s with %d rays\n", |
278 |
> |
sn, o->oname, n); |
279 |
> |
#endif |
280 |
> |
/* sample */ |
281 |
> |
or = sqrt(or2); |
282 |
> |
ssn = 25*n; |
283 |
> |
nhit = nok = 0; |
284 |
> |
while (n-- > 0) { |
285 |
> |
/* get sample point */ |
286 |
> |
do { |
287 |
> |
if (--ssn < 0) { |
288 |
> |
#ifdef DEBUG |
289 |
> |
fprintf(stderr, "\ttoo hard to hit\n"); |
290 |
> |
#endif |
291 |
> |
return(f); /* too small a target! */ |
292 |
> |
} |
293 |
> |
for (i = 0; i < 3; i++) |
294 |
> |
offsdir[i] = or*(1. - |
295 |
> |
2.*urand(931*i+5827+ssn)); |
296 |
> |
for (i = 0; i < 3; i++) |
297 |
> |
sr.rorg[i] = oc[i] + offsdir[i]; |
298 |
> |
d = DOT(offsdir,onorm); |
299 |
> |
if (infront) |
300 |
> |
for (i = 0; i < 3; i++) { |
301 |
> |
sr.rorg[i] -= (d-.0001)*onorm[i]; |
302 |
> |
sr.rdir[i] = -onorm[i]; |
303 |
> |
} |
304 |
> |
else |
305 |
> |
for (i = 0; i < 3; i++) { |
306 |
> |
sr.rorg[i] -= (d+.0001)*onorm[i]; |
307 |
> |
sr.rdir[i] = onorm[i]; |
308 |
> |
} |
309 |
> |
rayorigin(&sr, NULL, PRIMARY, 1.0); |
310 |
> |
} while (!(*ofun[o->otype].funp)(o, &sr)); |
311 |
> |
/* check against source */ |
312 |
> |
samplendx++; |
313 |
> |
if (srcray(&sr, NULL, sn) == 0.) |
314 |
> |
continue; |
315 |
> |
sr.revf = srcvalue; |
316 |
> |
rayvalue(&sr); |
317 |
> |
if (bright(sr.rcol) <= FTINY) |
318 |
> |
continue; |
319 |
> |
nok++; |
320 |
> |
/* check against obstructions */ |
321 |
> |
srcray(&sr, NULL, sn); |
322 |
> |
rayvalue(&sr); |
323 |
> |
if (bright(sr.rcol) > FTINY) |
324 |
> |
nhit++; |
325 |
> |
if (nhit > 0 && nhit < nok) { |
326 |
> |
#ifdef DEBUG |
327 |
> |
fprintf(stderr, "\tpartially occluded\n"); |
328 |
> |
#endif |
329 |
> |
return(f); /* need to shadow test */ |
330 |
|
} |
331 |
|
} |
332 |
< |
a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2); |
333 |
< |
/* no overlap? */ |
334 |
< |
if (a2 <= 0.) |
335 |
< |
return(0.); |
336 |
< |
/* overlap, compute center */ |
337 |
< |
l = sqrt((r1s - a2)/d2); |
338 |
< |
for (i = 0; i < 3; i++) |
339 |
< |
cc[i] = c1[i] + l*disp[i]; |
340 |
< |
return(a2); |
332 |
> |
if (nhit == 0) { |
333 |
> |
#ifdef DEBUG |
334 |
> |
fprintf(stderr, "\t0%% hit rate\n"); |
335 |
> |
#endif |
336 |
> |
return(f | SSKIP); /* 0% hit rate: totally occluded */ |
337 |
> |
} |
338 |
> |
#ifdef DEBUG |
339 |
> |
fprintf(stderr, "\t100%% hit rate\n"); |
340 |
> |
#endif |
341 |
> |
return(f & ~SFOLLOW); /* 100% hit rate: no occlusion */ |
342 |
|
} |
343 |
< |
|
343 |
> |
|
344 |
|
|
345 |
|
#ifdef DEBUG |
346 |
|
virtverb(sn, fp) /* print verbose description of virtual source */ |