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 |
+ |
void |
30 |
|
markvirtuals() /* find and mark virtual sources */ |
31 |
|
{ |
32 |
|
register OBJREC *o; |
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 |
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 |
+ |
void |
73 |
|
addvirtuals(sn, nr) /* add virtuals associated with source */ |
74 |
|
int sn; |
75 |
|
int nr; |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
+ |
void |
91 |
|
vproject(o, sn, n) /* create projected source(s) if they exist */ |
92 |
|
OBJREC *o; |
93 |
|
int sn; |
101 |
|
if (o == source[sn].so) /* objects cannot project themselves */ |
102 |
|
return; |
103 |
|
/* get virtual source material */ |
104 |
< |
vsmat = sfun[objptr(o->omod)->otype].mf; |
104 |
> |
vsmat = sfun[vsmaterial(o)->otype].mf; |
105 |
|
/* project virtual sources */ |
106 |
|
for (i = 0; i < vsmat->nproj; i++) |
107 |
|
if ((*vsmat->vproj)(proj, o, &source[sn], i)) |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
+ |
OBJREC * |
119 |
+ |
vsmaterial(o) /* get virtual source material pointer */ |
120 |
+ |
OBJREC *o; |
121 |
+ |
{ |
122 |
+ |
register int i; |
123 |
+ |
register OBJREC *m; |
124 |
+ |
|
125 |
+ |
i = o->omod; |
126 |
+ |
m = objptr(i); |
127 |
+ |
if (m->otype != MAT_ILLUM || m->oargs.nsargs < 1 || |
128 |
+ |
!strcmp(m->oargs.sarg[0], VOIDID) || |
129 |
+ |
(i = lastmod(objndx(m), m->oargs.sarg[0])) == OVOID) |
130 |
+ |
return(m); /* direct modifier */ |
131 |
+ |
return(objptr(i)); /* illum alternate */ |
132 |
+ |
} |
133 |
+ |
|
134 |
+ |
|
135 |
|
int |
136 |
|
makevsrc(op, sn, pm) /* make virtual source if reasonable */ |
137 |
|
OBJREC *op; |
157 |
|
normalize(nsloc); |
158 |
|
VCOPY(ourspot.aim, ocent); |
159 |
|
ourspot.siz = PI*maxrad2; |
160 |
< |
ourspot.flen = 0.; |
160 |
> |
ourspot.flen = -1.; |
161 |
|
if (source[sn].sflags & SSPOT) { |
143 |
– |
copystruct(&theirspot, source[sn].sl.s); |
162 |
|
multp3(theirspot.aim, source[sn].sl.s->aim, pm); |
163 |
+ |
/* adjust for source size */ |
164 |
+ |
d = sqrt(dist2(ourspot.aim, theirspot.aim)); |
165 |
+ |
d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].srad; |
166 |
+ |
theirspot.siz = PI*d*d; |
167 |
+ |
ourspot.flen = theirspot.flen = source[sn].sl.s->flen; |
168 |
|
d = ourspot.siz; |
169 |
|
if (!commonbeam(&ourspot, &theirspot, nsloc)) |
170 |
|
return(-1); /* no overlap */ |
187 |
|
if (source[sn].sflags & SPROX && d > source[sn].sl.prox) |
188 |
|
return(-1); /* too far away */ |
189 |
|
ourspot.flen = 0.; |
190 |
< |
if (d*d > maxrad2) |
191 |
< |
ourspot.siz = 2.*PI*(1. - sqrt(1.-maxrad2/(d*d))); |
190 |
> |
/* adjust for source size */ |
191 |
> |
d = (sqrt(maxrad2) + source[sn].srad) / d; |
192 |
> |
if (d < 1.-FTINY) |
193 |
> |
ourspot.siz = 2.*PI*(1. - sqrt(1.-d*d)); |
194 |
|
else |
195 |
|
nsflags &= ~SSPOT; |
196 |
|
if (source[sn].sflags & SSPOT) { |
220 |
|
if (source[sn].sflags & SFLAT) { /* behind source? */ |
221 |
|
multv3(nsnorm, source[sn].snorm, pm); |
222 |
|
normalize(nsnorm); |
223 |
< |
if (!checkspot(&ourspot, nsnorm)) |
223 |
> |
if (nsflags & SSPOT && !checkspot(&ourspot, nsnorm)) |
224 |
|
return(-1); |
225 |
|
} |
226 |
|
} |
233 |
|
goto memerr; |
234 |
|
source[i].sflags = nsflags; |
235 |
|
VCOPY(source[i].sloc, nsloc); |
236 |
+ |
multv3(source[i].ss[SU], source[sn].ss[SU], pm); |
237 |
+ |
multv3(source[i].ss[SV], source[sn].ss[SV], pm); |
238 |
|
if (nsflags & SFLAT) |
239 |
|
VCOPY(source[i].snorm, nsnorm); |
240 |
< |
source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2; |
240 |
> |
else |
241 |
> |
multv3(source[i].ss[SW], source[sn].ss[SW], pm); |
242 |
> |
source[i].srad = source[sn].srad; |
243 |
> |
source[i].ss2 = source[sn].ss2; |
244 |
|
if (nsflags & SSPOT) { |
245 |
|
if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
246 |
|
goto memerr; |
299 |
|
RAY sr; |
300 |
|
FVECT onorm; |
301 |
|
FVECT offsdir; |
302 |
+ |
SRCINDEX si; |
303 |
|
double or, d; |
273 |
– |
int infront; |
304 |
|
int stestlim, ssn; |
305 |
|
int nhit, nok; |
306 |
|
register int i, n; |
311 |
|
getplaneq(onorm, o); |
312 |
|
/* set number of rays to sample */ |
313 |
|
if (source[sn].sflags & SDISTANT) { |
314 |
< |
n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)* |
315 |
< |
vspretest + .5; |
286 |
< |
infront = DOT(onorm, source[sn].sloc) > 0.; |
314 |
> |
/* 32. == heuristic constant */ |
315 |
> |
n = 32.*or2/(thescene.cusize*thescene.cusize)*vspretest + .5; |
316 |
|
} else { |
317 |
|
for (i = 0; i < 3; i++) |
318 |
|
offsdir[i] = source[sn].sloc[i] - oc[i]; |
319 |
< |
n = or2/DOT(offsdir,offsdir)*vspretest + .5; |
320 |
< |
infront = DOT(onorm, offsdir) > 0.; |
319 |
> |
d = DOT(offsdir,offsdir); |
320 |
> |
if (d <= FTINY) |
321 |
> |
n = 2.*PI * vspretest + .5; |
322 |
> |
else |
323 |
> |
n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5; |
324 |
|
} |
325 |
|
if (n < MINSAMPLES) n = MINSAMPLES; |
326 |
|
#ifdef DEBUG |
332 |
|
stestlim = n*STESTMAX; |
333 |
|
ssn = 0; |
334 |
|
nhit = nok = 0; |
335 |
+ |
initsrcindex(&si); |
336 |
|
while (n-- > 0) { |
337 |
|
/* get sample point */ |
338 |
|
do { |
342 |
|
#endif |
343 |
|
return(f); /* too small a target! */ |
344 |
|
} |
345 |
+ |
multisamp(offsdir, 3, urand(sn*931+5827+ssn)); |
346 |
|
for (i = 0; i < 3; i++) |
347 |
< |
offsdir[i] = or*(1. - |
314 |
< |
2.*urand(urind(931*i+5827,ssn))); |
347 |
> |
offsdir[i] = or*(1. - 2.*offsdir[i]); |
348 |
|
ssn++; |
349 |
< |
for (i = 0; i < 3; i++) |
350 |
< |
sr.rorg[i] = oc[i] + offsdir[i]; |
351 |
< |
d = DOT(offsdir,onorm); |
352 |
< |
if (infront) |
353 |
< |
for (i = 0; i < 3; i++) { |
354 |
< |
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 |
< |
} |
349 |
> |
d = 1. - DOT(offsdir, onorm); |
350 |
> |
for (i = 0; i < 3; i++) { |
351 |
> |
sr.rorg[i] = oc[i] + offsdir[i] + d*onorm[i]; |
352 |
> |
sr.rdir[i] = -onorm[i]; |
353 |
> |
} |
354 |
> |
sr.rmax = 0.0; |
355 |
|
rayorigin(&sr, NULL, PRIMARY, 1.0); |
356 |
|
} while (!(*ofun[o->otype].funp)(o, &sr)); |
357 |
|
/* check against source */ |
358 |
+ |
VCOPY(sr.rorg, sr.rop); /* starting from intersection */ |
359 |
|
samplendx++; |
360 |
< |
if (srcray(&sr, NULL, sn) == 0.) |
361 |
< |
continue; |
360 |
> |
if (si.sp >= si.np-1 || |
361 |
> |
!srcray(&sr, NULL, &si) || sr.rsrc != sn) { |
362 |
> |
si.sn = sn-1; /* reset index to our source */ |
363 |
> |
si.np = 0; |
364 |
> |
if (!srcray(&sr, NULL, &si) || sr.rsrc != sn) |
365 |
> |
continue; /* can't get there from here */ |
366 |
> |
} |
367 |
|
sr.revf = srcvalue; |
368 |
< |
rayvalue(&sr); |
368 |
> |
rayvalue(&sr); /* check sample validity */ |
369 |
|
if (bright(sr.rcol) <= FTINY) |
370 |
|
continue; |
371 |
< |
nok++; |
372 |
< |
/* check against obstructions */ |
373 |
< |
srcray(&sr, NULL, sn); |
371 |
> |
nok++; /* got sample; check obstructions */ |
372 |
> |
rayclear(&sr); |
373 |
> |
sr.revf = raytrace; |
374 |
|
rayvalue(&sr); |
375 |
|
if (bright(sr.rcol) > FTINY) |
376 |
|
nhit++; |
395 |
|
|
396 |
|
|
397 |
|
#ifdef DEBUG |
398 |
+ |
void |
399 |
|
virtverb(sn, fp) /* print verbose description of virtual source */ |
400 |
|
register int sn; |
401 |
|
FILE *fp; |