1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Routines for simulating virtual light sources |
6 |
|
* Thus far, we only support planar mirrors. |
7 |
+ |
* |
8 |
+ |
* External symbols declared in source.h |
9 |
|
*/ |
10 |
|
|
11 |
+ |
#include "copyright.h" |
12 |
+ |
|
13 |
|
#include "ray.h" |
14 |
|
|
14 |
– |
#include "octree.h" |
15 |
– |
|
15 |
|
#include "otypes.h" |
16 |
|
|
17 |
|
#include "source.h" |
18 |
|
|
19 |
|
#include "random.h" |
20 |
|
|
21 |
< |
#define MINSAMPLES 5 /* minimum number of pretest samples */ |
22 |
< |
#define STESTMAX 30 /* maximum seeks per sample */ |
21 |
> |
#define MINSAMPLES 16 /* minimum number of pretest samples */ |
22 |
> |
#define STESTMAX 32 /* maximum seeks per sample */ |
23 |
|
|
24 |
|
|
26 |
– |
double getdisk(); |
27 |
– |
|
25 |
|
static OBJECT *vobject; /* virtual source objects */ |
26 |
|
static int nvobjects = 0; /* number of virtual source objects */ |
27 |
|
|
28 |
|
|
29 |
< |
markvirtuals() /* find and mark virtual sources */ |
29 |
> |
extern void |
30 |
> |
markvirtuals(void) /* find and mark virtual sources */ |
31 |
|
{ |
32 |
|
register OBJREC *o; |
33 |
|
register int i; |
35 |
|
if (directrelay <= 0) |
36 |
|
return; |
37 |
|
/* find virtual source objects */ |
38 |
< |
for (i = 0; i < nobjects; i++) { |
38 |
> |
for (i = 0; i < nsceneobjs; i++) { |
39 |
|
o = objptr(i); |
40 |
|
if (!issurface(o->otype) || o->omod == OVOID) |
41 |
|
continue; |
42 |
< |
if (!isvlight(objptr(o->omod)->otype)) |
42 |
> |
if (!isvlight(vsmaterial(o)->otype)) |
43 |
|
continue; |
44 |
|
if (sfun[o->otype].of == NULL || |
45 |
< |
sfun[o->otype].of->getpleq == NULL) |
46 |
< |
objerror(o, USER, "illegal material"); |
45 |
> |
sfun[o->otype].of->getpleq == NULL) { |
46 |
> |
objerror(o,WARNING,"secondary sources not supported"); |
47 |
> |
continue; |
48 |
> |
} |
49 |
|
if (nvobjects == 0) |
50 |
|
vobject = (OBJECT *)malloc(sizeof(OBJECT)); |
51 |
|
else |
52 |
< |
vobject = (OBJECT *)realloc((char *)vobject, |
52 |
> |
vobject = (OBJECT *)realloc((void *)vobject, |
53 |
|
(unsigned)(nvobjects+1)*sizeof(OBJECT)); |
54 |
|
if (vobject == NULL) |
55 |
|
error(SYSTEM, "out of memory in addvirtuals"); |
64 |
|
for (i = nsources; i-- > 0; ) |
65 |
|
addvirtuals(i, directrelay); |
66 |
|
/* done with our object list */ |
67 |
< |
free((char *)vobject); |
67 |
> |
free((void *)vobject); |
68 |
|
nvobjects = 0; |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
< |
addvirtuals(sn, nr) /* add virtuals associated with source */ |
73 |
< |
int sn; |
74 |
< |
int nr; |
72 |
> |
extern void |
73 |
> |
addvirtuals( /* add virtuals associated with source */ |
74 |
> |
int sn, |
75 |
> |
int nr |
76 |
> |
) |
77 |
|
{ |
78 |
|
register int i; |
79 |
|
/* check relay limit first */ |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
< |
vproject(o, sn, n) /* create projected source(s) if they exist */ |
92 |
< |
OBJREC *o; |
93 |
< |
int sn; |
94 |
< |
int n; |
91 |
> |
extern void |
92 |
> |
vproject( /* create projected source(s) if they exist */ |
93 |
> |
OBJREC *o, |
94 |
> |
int sn, |
95 |
> |
int n |
96 |
> |
) |
97 |
|
{ |
98 |
|
register int i; |
99 |
|
register VSMATERIAL *vsmat; |
103 |
|
if (o == source[sn].so) /* objects cannot project themselves */ |
104 |
|
return; |
105 |
|
/* get virtual source material */ |
106 |
< |
vsmat = sfun[objptr(o->omod)->otype].mf; |
106 |
> |
vsmat = sfun[vsmaterial(o)->otype].mf; |
107 |
|
/* project virtual sources */ |
108 |
|
for (i = 0; i < vsmat->nproj; i++) |
109 |
|
if ((*vsmat->vproj)(proj, o, &source[sn], i)) |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
< |
int |
121 |
< |
makevsrc(op, sn, pm) /* make virtual source if reasonable */ |
122 |
< |
OBJREC *op; |
123 |
< |
register int sn; |
120 |
< |
MAT4 pm; |
120 |
> |
extern OBJREC * |
121 |
> |
vsmaterial( /* get virtual source material pointer */ |
122 |
> |
OBJREC *o |
123 |
> |
) |
124 |
|
{ |
125 |
+ |
register int i; |
126 |
+ |
register OBJREC *m; |
127 |
+ |
|
128 |
+ |
i = o->omod; |
129 |
+ |
m = findmaterial(objptr(i)); |
130 |
+ |
if (m == NULL) |
131 |
+ |
return(objptr(i)); |
132 |
+ |
if (m->otype != MAT_ILLUM || m->oargs.nsargs < 1 || |
133 |
+ |
!strcmp(m->oargs.sarg[0], VOIDID) || |
134 |
+ |
(i = lastmod(objndx(m), m->oargs.sarg[0])) == OVOID) |
135 |
+ |
return(m); /* direct modifier */ |
136 |
+ |
return(objptr(i)); /* illum alternate */ |
137 |
+ |
} |
138 |
+ |
|
139 |
+ |
|
140 |
+ |
extern int |
141 |
+ |
makevsrc( /* make virtual source if reasonable */ |
142 |
+ |
OBJREC *op, |
143 |
+ |
register int sn, |
144 |
+ |
MAT4 pm |
145 |
+ |
) |
146 |
+ |
{ |
147 |
|
FVECT nsloc, nsnorm, ocent, v; |
148 |
|
double maxrad2, d; |
149 |
|
int nsflags; |
163 |
|
normalize(nsloc); |
164 |
|
VCOPY(ourspot.aim, ocent); |
165 |
|
ourspot.siz = PI*maxrad2; |
166 |
< |
ourspot.flen = 0.; |
166 |
> |
ourspot.flen = -1.; |
167 |
|
if (source[sn].sflags & SSPOT) { |
143 |
– |
copystruct(&theirspot, source[sn].sl.s); |
168 |
|
multp3(theirspot.aim, source[sn].sl.s->aim, pm); |
169 |
+ |
/* adjust for source size */ |
170 |
+ |
d = sqrt(dist2(ourspot.aim, theirspot.aim)); |
171 |
+ |
d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].srad; |
172 |
+ |
theirspot.siz = PI*d*d; |
173 |
+ |
ourspot.flen = theirspot.flen = source[sn].sl.s->flen; |
174 |
|
d = ourspot.siz; |
175 |
|
if (!commonbeam(&ourspot, &theirspot, nsloc)) |
176 |
|
return(-1); /* no overlap */ |
193 |
|
if (source[sn].sflags & SPROX && d > source[sn].sl.prox) |
194 |
|
return(-1); /* too far away */ |
195 |
|
ourspot.flen = 0.; |
196 |
< |
if (d*d > maxrad2) |
197 |
< |
ourspot.siz = 2.*PI*(1. - sqrt(1.-maxrad2/(d*d))); |
196 |
> |
/* adjust for source size */ |
197 |
> |
d = (sqrt(maxrad2) + source[sn].srad) / d; |
198 |
> |
if (d < 1.-FTINY) |
199 |
> |
ourspot.siz = 2.*PI*(1. - sqrt(1.-d*d)); |
200 |
|
else |
201 |
|
nsflags &= ~SSPOT; |
202 |
|
if (source[sn].sflags & SSPOT) { |
203 |
< |
copystruct(&theirspot, source[sn].sl.s); |
203 |
> |
theirspot = *(source[sn].sl.s); |
204 |
|
multv3(theirspot.aim, source[sn].sl.s->aim, pm); |
205 |
|
normalize(theirspot.aim); |
206 |
|
if (nsflags & SSPOT) { |
210 |
|
return(-1); /* no overlap */ |
211 |
|
} else { |
212 |
|
nsflags |= SSPOT; |
213 |
< |
copystruct(&ourspot, &theirspot); |
213 |
> |
ourspot = theirspot; |
214 |
|
d = 2.*ourspot.siz; |
215 |
|
} |
216 |
|
if (ourspot.siz < d-FTINY) { /* it shrunk */ |
226 |
|
if (source[sn].sflags & SFLAT) { /* behind source? */ |
227 |
|
multv3(nsnorm, source[sn].snorm, pm); |
228 |
|
normalize(nsnorm); |
229 |
< |
if (!checkspot(&ourspot, nsnorm)) |
229 |
> |
if (nsflags & SSPOT && !checkspot(&ourspot, nsnorm)) |
230 |
|
return(-1); |
231 |
|
} |
232 |
|
} |
239 |
|
goto memerr; |
240 |
|
source[i].sflags = nsflags; |
241 |
|
VCOPY(source[i].sloc, nsloc); |
242 |
+ |
multv3(source[i].ss[SU], source[sn].ss[SU], pm); |
243 |
+ |
multv3(source[i].ss[SV], source[sn].ss[SV], pm); |
244 |
|
if (nsflags & SFLAT) |
245 |
|
VCOPY(source[i].snorm, nsnorm); |
246 |
< |
source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2; |
246 |
> |
else |
247 |
> |
multv3(source[i].ss[SW], source[sn].ss[SW], pm); |
248 |
> |
source[i].srad = source[sn].srad; |
249 |
> |
source[i].ss2 = source[sn].ss2; |
250 |
|
if (nsflags & SSPOT) { |
251 |
|
if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
252 |
|
goto memerr; |
253 |
< |
copystruct(source[i].sl.s, &ourspot); |
253 |
> |
*(source[i].sl.s) = ourspot; |
254 |
|
} |
255 |
|
if (nsflags & SPROX) |
256 |
|
source[i].sl.prox = source[sn].sl.prox; |
259 |
|
return(i); |
260 |
|
memerr: |
261 |
|
error(SYSTEM, "out of memory in makevsrc"); |
262 |
+ |
return -1; /* pro forma return */ |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
< |
double |
267 |
< |
getdisk(oc, op, sn) /* get visible object disk */ |
268 |
< |
FVECT oc; |
269 |
< |
OBJREC *op; |
270 |
< |
register int sn; |
266 |
> |
extern double |
267 |
> |
getdisk( /* get visible object disk */ |
268 |
> |
FVECT oc, |
269 |
> |
OBJREC *op, |
270 |
> |
register int sn |
271 |
> |
) |
272 |
|
{ |
273 |
|
double rad2, roffs, offs, d, rd, rdoto; |
274 |
|
FVECT rnrm, nrm; |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
< |
int |
300 |
< |
vstestvis(f, o, oc, or2, sn) /* pretest source visibility */ |
301 |
< |
int f; /* virtual source flags */ |
302 |
< |
OBJREC *o; /* relay object */ |
303 |
< |
FVECT oc; /* relay object center */ |
304 |
< |
double or2; /* relay object radius squared */ |
305 |
< |
register int sn; /* target source number */ |
299 |
> |
extern int |
300 |
> |
vstestvis( /* pretest source visibility */ |
301 |
> |
int f, /* virtual source flags */ |
302 |
> |
OBJREC *o, /* relay object */ |
303 |
> |
FVECT oc, /* relay object center */ |
304 |
> |
double or2, /* relay object radius squared */ |
305 |
> |
register int sn /* target source number */ |
306 |
> |
) |
307 |
|
{ |
308 |
|
RAY sr; |
309 |
|
FVECT onorm; |
310 |
|
FVECT offsdir; |
311 |
+ |
SRCINDEX si; |
312 |
|
double or, d; |
273 |
– |
int infront; |
313 |
|
int stestlim, ssn; |
314 |
|
int nhit, nok; |
315 |
|
register int i, n; |
320 |
|
getplaneq(onorm, o); |
321 |
|
/* set number of rays to sample */ |
322 |
|
if (source[sn].sflags & SDISTANT) { |
323 |
< |
n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)* |
324 |
< |
vspretest + .5; |
286 |
< |
infront = DOT(onorm, source[sn].sloc) > 0.; |
323 |
> |
/* 32. == heuristic constant */ |
324 |
> |
n = 32.*or2/(thescene.cusize*thescene.cusize)*vspretest + .5; |
325 |
|
} else { |
326 |
|
for (i = 0; i < 3; i++) |
327 |
|
offsdir[i] = source[sn].sloc[i] - oc[i]; |
328 |
< |
n = or2/DOT(offsdir,offsdir)*vspretest + .5; |
329 |
< |
infront = DOT(onorm, offsdir) > 0.; |
328 |
> |
d = DOT(offsdir,offsdir); |
329 |
> |
if (d <= FTINY) |
330 |
> |
n = 2.*PI * vspretest + .5; |
331 |
> |
else |
332 |
> |
n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5; |
333 |
|
} |
334 |
|
if (n < MINSAMPLES) n = MINSAMPLES; |
335 |
|
#ifdef DEBUG |
341 |
|
stestlim = n*STESTMAX; |
342 |
|
ssn = 0; |
343 |
|
nhit = nok = 0; |
344 |
+ |
initsrcindex(&si); |
345 |
|
while (n-- > 0) { |
346 |
|
/* get sample point */ |
347 |
|
do { |
351 |
|
#endif |
352 |
|
return(f); /* too small a target! */ |
353 |
|
} |
354 |
+ |
multisamp(offsdir, 3, urand(sn*931+5827+ssn)); |
355 |
|
for (i = 0; i < 3; i++) |
356 |
< |
offsdir[i] = or*(1. - |
314 |
< |
2.*urand(urind(931*i+5827,ssn))); |
356 |
> |
offsdir[i] = or*(1. - 2.*offsdir[i]); |
357 |
|
ssn++; |
358 |
< |
for (i = 0; i < 3; i++) |
359 |
< |
sr.rorg[i] = oc[i] + offsdir[i]; |
360 |
< |
d = DOT(offsdir,onorm); |
361 |
< |
if (infront) |
362 |
< |
for (i = 0; i < 3; i++) { |
363 |
< |
sr.rorg[i] -= (d-.0001)*onorm[i]; |
322 |
< |
sr.rdir[i] = -onorm[i]; |
323 |
< |
} |
324 |
< |
else |
325 |
< |
for (i = 0; i < 3; i++) { |
326 |
< |
sr.rorg[i] -= (d+.0001)*onorm[i]; |
327 |
< |
sr.rdir[i] = onorm[i]; |
328 |
< |
} |
358 |
> |
d = 1. - DOT(offsdir, onorm); |
359 |
> |
for (i = 0; i < 3; i++) { |
360 |
> |
sr.rorg[i] = oc[i] + offsdir[i] + d*onorm[i]; |
361 |
> |
sr.rdir[i] = -onorm[i]; |
362 |
> |
} |
363 |
> |
sr.rmax = 0.0; |
364 |
|
rayorigin(&sr, NULL, PRIMARY, 1.0); |
365 |
|
} while (!(*ofun[o->otype].funp)(o, &sr)); |
366 |
|
/* check against source */ |
367 |
+ |
VCOPY(sr.rorg, sr.rop); /* starting from intersection */ |
368 |
|
samplendx++; |
369 |
< |
if (srcray(&sr, NULL, sn) == 0.) |
370 |
< |
continue; |
371 |
< |
sr.revf = srcvalue; |
372 |
< |
rayvalue(&sr); |
369 |
> |
if (si.sp >= si.np-1 || |
370 |
> |
!srcray(&sr, NULL, &si) || sr.rsrc != sn) { |
371 |
> |
si.sn = sn-1; /* reset index to our source */ |
372 |
> |
si.np = 0; |
373 |
> |
if (!srcray(&sr, NULL, &si) || sr.rsrc != sn) |
374 |
> |
continue; /* can't get there from here */ |
375 |
> |
} |
376 |
> |
srcvalue(&sr); /* check sample validity */ |
377 |
|
if (bright(sr.rcol) <= FTINY) |
378 |
|
continue; |
379 |
< |
nok++; |
380 |
< |
/* check against obstructions */ |
341 |
< |
srcray(&sr, NULL, sn); |
379 |
> |
nok++; /* got sample; check obstructions */ |
380 |
> |
rayclear(&sr); |
381 |
|
rayvalue(&sr); |
382 |
|
if (bright(sr.rcol) > FTINY) |
383 |
|
nhit++; |
402 |
|
|
403 |
|
|
404 |
|
#ifdef DEBUG |
405 |
< |
virtverb(sn, fp) /* print verbose description of virtual source */ |
406 |
< |
register int sn; |
407 |
< |
FILE *fp; |
405 |
> |
extern void |
406 |
> |
virtverb( /* print verbose description of virtual source */ |
407 |
> |
register int sn, |
408 |
> |
FILE *fp |
409 |
> |
) |
410 |
|
{ |
411 |
|
register int i; |
412 |
|
|