10 |
|
#include "mkillum.h" |
11 |
|
#include "face.h" |
12 |
|
#include "cone.h" |
13 |
< |
#include "random.h" |
14 |
< |
#include "selcall.h" |
13 |
> |
#include "source.h" |
14 |
|
|
15 |
|
|
16 |
< |
static void mkaxes(FVECT u, FVECT v, FVECT n); |
17 |
< |
static void rounddir(FVECT dv, double alt, double azi); |
19 |
< |
static void flatdir(FVECT dv, double alt, double azi); |
16 |
> |
COLORV * distarr = NULL; /* distribution array */ |
17 |
> |
int distsiz = 0; |
18 |
|
|
19 |
|
|
20 |
< |
static void |
21 |
< |
rayclean( /* finish all pending rays */ |
22 |
< |
struct rtproc *rt0 |
20 |
> |
void |
21 |
> |
newdist( /* allocate & clear distribution array */ |
22 |
> |
int siz |
23 |
|
) |
24 |
|
{ |
25 |
< |
rayflush(rt0, 1); |
26 |
< |
while (raywait(rt0) != NULL) |
25 |
> |
if (siz <= 0) { |
26 |
> |
if (distsiz > 0) |
27 |
> |
free((void *)distarr); |
28 |
> |
distarr = NULL; |
29 |
> |
distsiz = 0; |
30 |
> |
return; |
31 |
> |
} |
32 |
> |
if (distsiz < siz) { |
33 |
> |
if (distsiz > 0) |
34 |
> |
free((void *)distarr); |
35 |
> |
distarr = (COLORV *)malloc(sizeof(COLOR)*siz); |
36 |
> |
if (distarr == NULL) |
37 |
> |
error(SYSTEM, "out of memory in newdist"); |
38 |
> |
distsiz = siz; |
39 |
> |
} |
40 |
> |
memset(distarr, '\0', sizeof(COLOR)*siz); |
41 |
> |
} |
42 |
> |
|
43 |
> |
|
44 |
> |
int |
45 |
> |
process_ray( /* process a ray result or report error */ |
46 |
> |
RAY *r, |
47 |
> |
int rv |
48 |
> |
) |
49 |
> |
{ |
50 |
> |
COLORV *colp; |
51 |
> |
|
52 |
> |
if (rv == 0) /* no result ready */ |
53 |
> |
return(0); |
54 |
> |
if (rv < 0) |
55 |
> |
error(USER, "ray tracing process died"); |
56 |
> |
if (r->rno >= distsiz) |
57 |
> |
error(INTERNAL, "bad returned index in process_ray"); |
58 |
> |
multcolor(r->rcol, r->rcoef); /* in case it's a source ray */ |
59 |
> |
colp = &distarr[r->rno * 3]; |
60 |
> |
addcolor(colp, r->rcol); |
61 |
> |
return(1); |
62 |
> |
} |
63 |
> |
|
64 |
> |
|
65 |
> |
void |
66 |
> |
raysamp( /* queue a ray sample */ |
67 |
> |
int ndx, |
68 |
> |
FVECT org, |
69 |
> |
FVECT dir |
70 |
> |
) |
71 |
> |
{ |
72 |
> |
RAY myRay; |
73 |
> |
int rv; |
74 |
> |
|
75 |
> |
if ((ndx < 0) | (ndx >= distsiz)) |
76 |
> |
error(INTERNAL, "bad index in raysamp"); |
77 |
> |
VCOPY(myRay.rorg, org); |
78 |
> |
VCOPY(myRay.rdir, dir); |
79 |
> |
myRay.rmax = .0; |
80 |
> |
rayorigin(&myRay, PRIMARY, NULL, NULL); |
81 |
> |
myRay.rno = ndx; |
82 |
> |
/* queue ray, check result */ |
83 |
> |
process_ray(&myRay, ray_pqueue(&myRay)); |
84 |
> |
} |
85 |
> |
|
86 |
> |
|
87 |
> |
void |
88 |
> |
srcsamps( /* sample sources from this surface position */ |
89 |
> |
struct illum_args *il, |
90 |
> |
FVECT org, |
91 |
> |
FVECT nrm, |
92 |
> |
MAT4 ixfm |
93 |
> |
) |
94 |
> |
{ |
95 |
> |
int nalt, nazi; |
96 |
> |
SRCINDEX si; |
97 |
> |
RAY sr; |
98 |
> |
FVECT v; |
99 |
> |
double d; |
100 |
> |
int i, j; |
101 |
> |
/* get sampling density */ |
102 |
> |
if (il->sampdens <= 0) { |
103 |
> |
nalt = nazi = 1; |
104 |
> |
} else { |
105 |
> |
i = PI * il->sampdens; |
106 |
> |
nalt = sqrt(i/PI) + .5; |
107 |
> |
nazi = PI*nalt + .5; |
108 |
> |
} |
109 |
> |
initsrcindex(&si); /* loop over (sub)sources */ |
110 |
> |
for ( ; ; ) { |
111 |
> |
VCOPY(sr.rorg, org); /* pick side to shoot from */ |
112 |
> |
if (il->sd != NULL) { |
113 |
> |
int sn = si.sn; |
114 |
> |
if (si.sp+1 >= si.np) ++sn; |
115 |
> |
if (sn >= nsources) break; |
116 |
> |
if (source[sn].sflags & SDISTANT) |
117 |
> |
d = DOT(source[sn].sloc, nrm); |
118 |
> |
else { |
119 |
> |
VSUB(v, source[sn].sloc, org); |
120 |
> |
d = DOT(v, nrm); |
121 |
> |
} |
122 |
> |
} else |
123 |
> |
d = 1.0; /* only transmission */ |
124 |
> |
if (d < 0.0) |
125 |
> |
d = -1.0001*il->thick - 5.*FTINY; |
126 |
> |
else |
127 |
> |
d = 5.*FTINY; |
128 |
> |
for (i = 3; i--; ) |
129 |
> |
sr.rorg[i] += d*nrm[i]; |
130 |
> |
if (!srcray(&sr, NULL, &si)) |
131 |
> |
break; /* end of sources */ |
132 |
> |
/* index direction */ |
133 |
> |
if (ixfm != NULL) |
134 |
> |
multv3(v, sr.rdir, ixfm); |
135 |
> |
else |
136 |
> |
VCOPY(v, sr.rdir); |
137 |
> |
if (il->sd != NULL) { |
138 |
> |
i = getBSDF_incndx(il->sd, v); |
139 |
> |
if (i < 0) |
140 |
> |
continue; /* must not be important */ |
141 |
> |
sr.rno = i; |
142 |
> |
d = getBSDF_incrad(il->sd, i); |
143 |
> |
d = 1.0/(PI*d*d); |
144 |
> |
} else { |
145 |
> |
if (v[2] >= -FTINY) |
146 |
> |
continue; /* only sample transmission */ |
147 |
> |
d = 1.0 - v[2]*v[2]; |
148 |
> |
i = d*nalt; |
149 |
> |
d = atan2(-v[1], -v[0])/(2.*PI); |
150 |
> |
if (d < 0.0) d += 1.0; |
151 |
> |
j = d*nazi + 0.5; |
152 |
> |
if (j >= nazi) j = 0; |
153 |
> |
sr.rno = i*nazi + j; |
154 |
> |
d = nalt*nazi/PI; |
155 |
> |
} |
156 |
> |
d *= si.dom; /* solid angle correction */ |
157 |
> |
scalecolor(sr.rcoef, d); |
158 |
> |
process_ray(&sr, ray_pqueue(&sr)); |
159 |
> |
} |
160 |
> |
} |
161 |
> |
|
162 |
> |
|
163 |
> |
void |
164 |
> |
rayclean() /* finish all pending rays */ |
165 |
> |
{ |
166 |
> |
RAY myRay; |
167 |
> |
|
168 |
> |
while (process_ray(&myRay, ray_presult(&myRay, 0))) |
169 |
|
; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
< |
int /* XXX type conflict with otypes.h */ |
174 |
< |
o_default( /* default illum action */ |
173 |
> |
static void |
174 |
> |
mkaxes( /* compute u and v to go with n */ |
175 |
> |
FVECT u, |
176 |
> |
FVECT v, |
177 |
> |
FVECT n |
178 |
> |
) |
179 |
> |
{ |
180 |
> |
register int i; |
181 |
> |
|
182 |
> |
v[0] = v[1] = v[2] = 0.0; |
183 |
> |
for (i = 0; i < 3; i++) |
184 |
> |
if (n[i] < 0.6 && n[i] > -0.6) |
185 |
> |
break; |
186 |
> |
v[i] = 1.0; |
187 |
> |
fcross(u, v, n); |
188 |
> |
normalize(u); |
189 |
> |
fcross(v, n, u); |
190 |
> |
} |
191 |
> |
|
192 |
> |
|
193 |
> |
static void |
194 |
> |
rounddir( /* compute uniform spherical direction */ |
195 |
> |
register FVECT dv, |
196 |
> |
double alt, |
197 |
> |
double azi |
198 |
> |
) |
199 |
> |
{ |
200 |
> |
double d1, d2; |
201 |
> |
|
202 |
> |
dv[2] = 1. - 2.*alt; |
203 |
> |
d1 = sqrt(1. - dv[2]*dv[2]); |
204 |
> |
d2 = 2.*PI * azi; |
205 |
> |
dv[0] = d1*cos(d2); |
206 |
> |
dv[1] = d1*sin(d2); |
207 |
> |
} |
208 |
> |
|
209 |
> |
|
210 |
> |
void |
211 |
> |
flatdir( /* compute uniform hemispherical direction */ |
212 |
> |
FVECT dv, |
213 |
> |
double alt, |
214 |
> |
double azi |
215 |
> |
) |
216 |
> |
{ |
217 |
> |
double d1, d2; |
218 |
> |
|
219 |
> |
d1 = sqrt(alt); |
220 |
> |
d2 = 2.*PI * azi; |
221 |
> |
dv[0] = d1*cos(d2); |
222 |
> |
dv[1] = d1*sin(d2); |
223 |
> |
dv[2] = sqrt(1. - alt); |
224 |
> |
} |
225 |
> |
|
226 |
> |
|
227 |
> |
int |
228 |
> |
my_default( /* default illum action */ |
229 |
|
OBJREC *ob, |
230 |
|
struct illum_args *il, |
37 |
– |
struct rtproc *rt0, |
231 |
|
char *nm |
232 |
|
) |
233 |
|
{ |
240 |
|
|
241 |
|
|
242 |
|
int |
243 |
< |
o_face( /* make an illum face */ |
243 |
> |
my_face( /* make an illum face */ |
244 |
|
OBJREC *ob, |
245 |
|
struct illum_args *il, |
53 |
– |
struct rtproc *rt0, |
246 |
|
char *nm |
247 |
|
) |
248 |
|
{ |
249 |
< |
#define MAXMISS (5*n*il->nsamps) |
250 |
< |
int dim[3]; |
59 |
< |
int n, nalt, nazi, h; |
60 |
< |
float *distarr; |
249 |
> |
int dim[2]; |
250 |
> |
int n, nalt, nazi, alti; |
251 |
|
double sp[2], r1, r2; |
252 |
+ |
int h; |
253 |
|
FVECT dn, org, dir; |
254 |
|
FVECT u, v; |
255 |
|
double ur[2], vr[2]; |
256 |
< |
int nmisses; |
257 |
< |
register FACE *fa; |
258 |
< |
register int i, j; |
256 |
> |
MAT4 xfm; |
257 |
> |
int nallow; |
258 |
> |
FACE *fa; |
259 |
> |
int i, j; |
260 |
|
/* get/check arguments */ |
261 |
|
fa = getface(ob); |
262 |
|
if (fa->area == 0.0) { |
263 |
|
freeface(ob); |
264 |
< |
return(o_default(ob, il, rt0, nm)); |
264 |
> |
return(my_default(ob, il, nm)); |
265 |
|
} |
266 |
|
/* set up sampling */ |
267 |
< |
if (il->sampdens <= 0) |
268 |
< |
nalt = nazi = 1; |
269 |
< |
else { |
270 |
< |
n = PI * il->sampdens; |
271 |
< |
nalt = sqrt(n/PI) + .5; |
272 |
< |
nazi = PI*nalt + .5; |
267 |
> |
if (il->sd != NULL) { |
268 |
> |
if (!getBSDF_xfm(xfm, fa->norm, il->udir)) { |
269 |
> |
objerror(ob, WARNING, "illegal up direction"); |
270 |
> |
freeface(ob); |
271 |
> |
return(my_default(ob, il, nm)); |
272 |
> |
} |
273 |
> |
n = il->sd->ninc; |
274 |
> |
} else { |
275 |
> |
if (il->sampdens <= 0) { |
276 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
277 |
> |
} else { |
278 |
> |
n = PI * il->sampdens; |
279 |
> |
nalt = sqrt(n/PI) + .5; |
280 |
> |
nazi = PI*nalt + .5; |
281 |
> |
} |
282 |
> |
n = nazi*nalt; |
283 |
|
} |
284 |
< |
n = nalt*nazi; |
285 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
84 |
< |
if (distarr == NULL) |
85 |
< |
error(SYSTEM, "out of memory in o_face"); |
86 |
< |
/* take first edge longer than sqrt(area) */ |
284 |
> |
newdist(n); |
285 |
> |
/* take first edge >= sqrt(area) */ |
286 |
|
for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) { |
287 |
|
u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0]; |
288 |
|
u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1]; |
309 |
|
} |
310 |
|
dim[0] = random(); |
311 |
|
/* sample polygon */ |
312 |
< |
nmisses = 0; |
313 |
< |
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
115 |
< |
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
312 |
> |
nallow = 5*n*il->nsamps; |
313 |
> |
for (dim[1] = 0; dim[1] < n; dim[1]++) |
314 |
|
for (i = 0; i < il->nsamps; i++) { |
315 |
< |
/* random direction */ |
316 |
< |
h = ilhash(dim, 3) + i; |
317 |
< |
multisamp(sp, 2, urand(h)); |
318 |
< |
r1 = (dim[1] + sp[0])/nalt; |
319 |
< |
r2 = (dim[2] + sp[1] - .5)/nazi; |
320 |
< |
flatdir(dn, r1, r2); |
321 |
< |
for (j = 0; j < 3; j++) |
322 |
< |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j]; |
323 |
< |
/* random location */ |
315 |
> |
/* randomize direction */ |
316 |
> |
h = ilhash(dim, 2) + i; |
317 |
> |
if (il->sd != NULL) { |
318 |
> |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
319 |
> |
} else { |
320 |
> |
multisamp(sp, 2, urand(h)); |
321 |
> |
alti = dim[1]/nazi; |
322 |
> |
r1 = (alti + sp[0])/nalt; |
323 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
324 |
> |
flatdir(dn, r1, r2); |
325 |
> |
for (j = 0; j < 3; j++) |
326 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - |
327 |
> |
dn[2]*fa->norm[j]; |
328 |
> |
} |
329 |
> |
/* randomize location */ |
330 |
|
do { |
331 |
< |
multisamp(sp, 2, urand(h+4862+nmisses)); |
331 |
> |
multisamp(sp, 2, urand(h+4862+nallow)); |
332 |
|
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
333 |
|
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
334 |
|
for (j = 0; j < 3; j++) |
335 |
|
org[j] = r1*u[j] + r2*v[j] |
336 |
|
+ fa->offset*fa->norm[j]; |
337 |
< |
} while (!inface(org, fa) && nmisses++ < MAXMISS); |
338 |
< |
if (nmisses > MAXMISS) { |
337 |
> |
} while (!inface(org, fa) && nallow-- > 0); |
338 |
> |
if (nallow < 0) { |
339 |
|
objerror(ob, WARNING, "bad aspect"); |
340 |
< |
rayclean(rt0); |
340 |
> |
rayclean(); |
341 |
|
freeface(ob); |
342 |
< |
free((void *)distarr); |
139 |
< |
return(o_default(ob, il, rt0, nm)); |
342 |
> |
return(my_default(ob, il, nm)); |
343 |
|
} |
344 |
+ |
if (il->sd != NULL && DOT(dir, fa->norm) < -FTINY) |
345 |
+ |
r1 = -1.0001*il->thick - 5.*FTINY; |
346 |
+ |
else |
347 |
+ |
r1 = 5.*FTINY; |
348 |
|
for (j = 0; j < 3; j++) |
349 |
< |
org[j] += .001*fa->norm[j]; |
349 |
> |
org[j] += r1*fa->norm[j]; |
350 |
|
/* send sample */ |
351 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0); |
351 |
> |
raysamp(dim[1], org, dir); |
352 |
|
} |
353 |
< |
rayclean(rt0); |
353 |
> |
/* add in direct component? */ |
354 |
> |
if (!directvis && il->flags & IL_LIGHT) { |
355 |
> |
MAT4 ixfm; |
356 |
> |
if (il->sd == NULL) { |
357 |
> |
for (i = 3; i--; ) { |
358 |
> |
ixfm[i][0] = u[i]; |
359 |
> |
ixfm[i][1] = v[i]; |
360 |
> |
ixfm[i][2] = fa->norm[i]; |
361 |
> |
ixfm[i][3] = 0.; |
362 |
> |
} |
363 |
> |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
364 |
> |
ixfm[3][3] = 1.; |
365 |
> |
} else if (!invmat4(ixfm, xfm)) |
366 |
> |
objerror(ob, INTERNAL, "cannot invert BSDF transform"); |
367 |
> |
dim[0] = random(); |
368 |
> |
nallow = 10*il->nsamps; |
369 |
> |
for (i = 0; i < il->nsamps; i++) { |
370 |
> |
/* randomize location */ |
371 |
> |
h = dim[0] + samplendx++; |
372 |
> |
do { |
373 |
> |
multisamp(sp, 2, urand(h+nallow)); |
374 |
> |
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
375 |
> |
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
376 |
> |
for (j = 0; j < 3; j++) |
377 |
> |
org[j] = r1*u[j] + r2*v[j] |
378 |
> |
+ fa->offset*fa->norm[j]; |
379 |
> |
} while (!inface(org, fa) && nallow-- > 0); |
380 |
> |
if (nallow < 0) { |
381 |
> |
objerror(ob, WARNING, "bad aspect"); |
382 |
> |
rayclean(); |
383 |
> |
freeface(ob); |
384 |
> |
return(my_default(ob, il, nm)); |
385 |
> |
} |
386 |
> |
/* sample source rays */ |
387 |
> |
srcsamps(il, org, fa->norm, ixfm); |
388 |
> |
} |
389 |
> |
} |
390 |
> |
/* wait for all rays to finish */ |
391 |
> |
rayclean(); |
392 |
> |
if (il->sd != NULL) { /* run distribution through BSDF */ |
393 |
> |
nalt = sqrt(il->sd->nout/PI) + .5; |
394 |
> |
nazi = PI*nalt + .5; |
395 |
> |
redistribute(il->sd, nalt, nazi, u, v, fa->norm, xfm); |
396 |
> |
} |
397 |
|
/* write out the face and its distribution */ |
398 |
< |
if (average(il, distarr, nalt*nazi)) { |
398 |
> |
if (average(il, distarr, n)) { |
399 |
|
if (il->sampdens > 0) |
400 |
|
flatout(il, distarr, nalt, nazi, u, v, fa->norm); |
401 |
|
illumout(il, ob); |
403 |
|
printobj(il->altmat, ob); |
404 |
|
/* clean up */ |
405 |
|
freeface(ob); |
156 |
– |
free((void *)distarr); |
406 |
|
return(0); |
158 |
– |
#undef MAXMISS |
407 |
|
} |
408 |
|
|
409 |
|
|
410 |
|
int |
411 |
< |
o_sphere( /* make an illum sphere */ |
411 |
> |
my_sphere( /* make an illum sphere */ |
412 |
|
register OBJREC *ob, |
413 |
|
struct illum_args *il, |
166 |
– |
struct rtproc *rt0, |
414 |
|
char *nm |
415 |
|
) |
416 |
|
{ |
417 |
|
int dim[3]; |
418 |
|
int n, nalt, nazi; |
172 |
– |
float *distarr; |
419 |
|
double sp[4], r1, r2, r3; |
420 |
|
FVECT org, dir; |
421 |
|
FVECT u, v; |
431 |
|
nalt = sqrt(2./PI*n) + .5; |
432 |
|
nazi = PI/2.*nalt + .5; |
433 |
|
} |
434 |
+ |
if (il->sd != NULL) |
435 |
+ |
objerror(ob, WARNING, "BSDF ignored"); |
436 |
|
n = nalt*nazi; |
437 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
190 |
< |
if (distarr == NULL) |
191 |
< |
error(SYSTEM, "out of memory in o_sphere"); |
437 |
> |
newdist(n); |
438 |
|
dim[0] = random(); |
439 |
|
/* sample sphere */ |
440 |
|
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
459 |
|
dir[j] = -dir[j]; |
460 |
|
} |
461 |
|
/* send sample */ |
462 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0); |
462 |
> |
raysamp(dim[1]*nazi+dim[2], org, dir); |
463 |
|
} |
464 |
< |
rayclean(rt0); |
464 |
> |
/* wait for all rays to finish */ |
465 |
> |
rayclean(); |
466 |
|
/* write out the sphere and its distribution */ |
467 |
< |
if (average(il, distarr, nalt*nazi)) { |
467 |
> |
if (average(il, distarr, n)) { |
468 |
|
if (il->sampdens > 0) |
469 |
|
roundout(il, distarr, nalt, nazi); |
470 |
|
else |
473 |
|
} else |
474 |
|
printobj(il->altmat, ob); |
475 |
|
/* clean up */ |
229 |
– |
free((void *)distarr); |
476 |
|
return(1); |
477 |
|
} |
478 |
|
|
479 |
|
|
480 |
|
int |
481 |
< |
o_ring( /* make an illum ring */ |
481 |
> |
my_ring( /* make an illum ring */ |
482 |
|
OBJREC *ob, |
483 |
|
struct illum_args *il, |
238 |
– |
struct rtproc *rt0, |
484 |
|
char *nm |
485 |
|
) |
486 |
|
{ |
487 |
< |
int dim[3]; |
488 |
< |
int n, nalt, nazi; |
489 |
< |
float *distarr; |
490 |
< |
double sp[4], r1, r2, r3; |
487 |
> |
int dim[2]; |
488 |
> |
int n, nalt, nazi, alti; |
489 |
> |
double sp[2], r1, r2, r3; |
490 |
> |
int h; |
491 |
|
FVECT dn, org, dir; |
492 |
|
FVECT u, v; |
493 |
< |
register CONE *co; |
494 |
< |
register int i, j; |
493 |
> |
MAT4 xfm; |
494 |
> |
CONE *co; |
495 |
> |
int i, j; |
496 |
|
/* get/check arguments */ |
497 |
|
co = getcone(ob, 0); |
498 |
|
/* set up sampling */ |
499 |
< |
if (il->sampdens <= 0) |
500 |
< |
nalt = nazi = 1; |
501 |
< |
else { |
502 |
< |
n = PI * il->sampdens; |
503 |
< |
nalt = sqrt(n/PI) + .5; |
504 |
< |
nazi = PI*nalt + .5; |
499 |
> |
if (il->sd != NULL) { |
500 |
> |
if (!getBSDF_xfm(xfm, co->ad, il->udir)) { |
501 |
> |
objerror(ob, WARNING, "illegal up direction"); |
502 |
> |
freecone(ob); |
503 |
> |
return(my_default(ob, il, nm)); |
504 |
> |
} |
505 |
> |
n = il->sd->ninc; |
506 |
> |
} else { |
507 |
> |
if (il->sampdens <= 0) { |
508 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
509 |
> |
} else { |
510 |
> |
n = PI * il->sampdens; |
511 |
> |
nalt = sqrt(n/PI) + .5; |
512 |
> |
nazi = PI*nalt + .5; |
513 |
> |
} |
514 |
> |
n = nazi*nalt; |
515 |
|
} |
516 |
< |
n = nalt*nazi; |
261 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
262 |
< |
if (distarr == NULL) |
263 |
< |
error(SYSTEM, "out of memory in o_ring"); |
516 |
> |
newdist(n); |
517 |
|
mkaxes(u, v, co->ad); |
518 |
|
dim[0] = random(); |
519 |
|
/* sample disk */ |
520 |
< |
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
268 |
< |
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
520 |
> |
for (dim[1] = 0; dim[1] < n; dim[1]++) |
521 |
|
for (i = 0; i < il->nsamps; i++) { |
522 |
|
/* next sample point */ |
523 |
< |
multisamp(sp, 4, urand(ilhash(dim,3)+i)); |
524 |
< |
/* random direction */ |
525 |
< |
r1 = (dim[1] + sp[0])/nalt; |
526 |
< |
r2 = (dim[2] + sp[1] - .5)/nazi; |
527 |
< |
flatdir(dn, r1, r2); |
528 |
< |
for (j = 0; j < 3; j++) |
529 |
< |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
530 |
< |
/* random location */ |
523 |
> |
h = ilhash(dim,2) + i; |
524 |
> |
/* randomize direction */ |
525 |
> |
if (il->sd != NULL) { |
526 |
> |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
527 |
> |
} else { |
528 |
> |
multisamp(sp, 2, urand(h)); |
529 |
> |
alti = dim[1]/nazi; |
530 |
> |
r1 = (alti + sp[0])/nalt; |
531 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
532 |
> |
flatdir(dn, r1, r2); |
533 |
> |
for (j = 0; j < 3; j++) |
534 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
535 |
> |
} |
536 |
> |
/* randomize location */ |
537 |
> |
multisamp(sp, 2, urand(h+8371)); |
538 |
|
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
539 |
< |
sp[2]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
540 |
< |
r2 = 2.*PI*sp[3]; |
539 |
> |
sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
540 |
> |
r2 = 2.*PI*sp[1]; |
541 |
|
r1 = r3*cos(r2); |
542 |
|
r2 = r3*sin(r2); |
543 |
+ |
if (il->sd != NULL && DOT(dir, co->ad) < -FTINY) |
544 |
+ |
r3 = -1.0001*il->thick - 5.*FTINY; |
545 |
+ |
else |
546 |
+ |
r3 = 5.*FTINY; |
547 |
|
for (j = 0; j < 3; j++) |
548 |
|
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + |
549 |
< |
.001*co->ad[j]; |
287 |
< |
|
549 |
> |
r3*co->ad[j]; |
550 |
|
/* send sample */ |
551 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0); |
551 |
> |
raysamp(dim[1], org, dir); |
552 |
|
} |
553 |
< |
rayclean(rt0); |
553 |
> |
/* add in direct component? */ |
554 |
> |
if (!directvis && il->flags & IL_LIGHT) { |
555 |
> |
MAT4 ixfm; |
556 |
> |
if (il->sd == NULL) { |
557 |
> |
for (i = 3; i--; ) { |
558 |
> |
ixfm[i][0] = u[i]; |
559 |
> |
ixfm[i][1] = v[i]; |
560 |
> |
ixfm[i][2] = co->ad[i]; |
561 |
> |
ixfm[i][3] = 0.; |
562 |
> |
} |
563 |
> |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
564 |
> |
ixfm[3][3] = 1.; |
565 |
> |
} else if (!invmat4(ixfm, xfm)) |
566 |
> |
objerror(ob, INTERNAL, "cannot invert BSDF transform"); |
567 |
> |
dim[0] = random(); |
568 |
> |
for (i = 0; i < il->nsamps; i++) { |
569 |
> |
/* randomize location */ |
570 |
> |
h = dim[0] + samplendx++; |
571 |
> |
multisamp(sp, 2, urand(h)); |
572 |
> |
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
573 |
> |
sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
574 |
> |
r2 = 2.*PI*sp[1]; |
575 |
> |
r1 = r3*cos(r2); |
576 |
> |
r2 = r3*sin(r2); |
577 |
> |
for (j = 0; j < 3; j++) |
578 |
> |
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j]; |
579 |
> |
/* sample source rays */ |
580 |
> |
srcsamps(il, org, co->ad, ixfm); |
581 |
> |
} |
582 |
> |
} |
583 |
> |
/* wait for all rays to finish */ |
584 |
> |
rayclean(); |
585 |
> |
if (il->sd != NULL) { /* run distribution through BSDF */ |
586 |
> |
nalt = sqrt(il->sd->nout/PI) + .5; |
587 |
> |
nazi = PI*nalt + .5; |
588 |
> |
redistribute(il->sd, nalt, nazi, u, v, co->ad, xfm); |
589 |
> |
} |
590 |
|
/* write out the ring and its distribution */ |
591 |
< |
if (average(il, distarr, nalt*nazi)) { |
591 |
> |
if (average(il, distarr, n)) { |
592 |
|
if (il->sampdens > 0) |
593 |
|
flatout(il, distarr, nalt, nazi, u, v, co->ad); |
594 |
|
illumout(il, ob); |
596 |
|
printobj(il->altmat, ob); |
597 |
|
/* clean up */ |
598 |
|
freecone(ob); |
301 |
– |
free((void *)distarr); |
599 |
|
return(1); |
303 |
– |
} |
304 |
– |
|
305 |
– |
|
306 |
– |
void |
307 |
– |
raysamp( /* queue a ray sample */ |
308 |
– |
float res[3], |
309 |
– |
FVECT org, |
310 |
– |
FVECT dir, |
311 |
– |
struct rtproc *rt0 |
312 |
– |
) |
313 |
– |
{ |
314 |
– |
register struct rtproc *rt; |
315 |
– |
register float *fp; |
316 |
– |
|
317 |
– |
for (rt = rt0; rt != NULL; rt = rt->next) |
318 |
– |
if (rt->nrays < rt->bsiz && rt->dest[rt->nrays] == NULL) |
319 |
– |
break; |
320 |
– |
if (rt == NULL) /* need to free up buffer? */ |
321 |
– |
rt = raywait(rt0); |
322 |
– |
if (rt == NULL) |
323 |
– |
error(SYSTEM, "raywait() returned NULL"); |
324 |
– |
fp = rt->buf + 6*rt->nrays; |
325 |
– |
*fp++ = org[0]; *fp++ = org[1]; *fp++ = org[2]; |
326 |
– |
*fp++ = dir[0]; *fp++ = dir[1]; *fp = dir[2]; |
327 |
– |
rt->dest[rt->nrays++] = res; |
328 |
– |
if (rt->nrays == rt->bsiz) |
329 |
– |
rayflush(rt, 0); |
330 |
– |
} |
331 |
– |
|
332 |
– |
|
333 |
– |
void |
334 |
– |
rayflush( /* flush queued rays to rtrace */ |
335 |
– |
register struct rtproc *rt, |
336 |
– |
int doall |
337 |
– |
) |
338 |
– |
{ |
339 |
– |
int nw; |
340 |
– |
|
341 |
– |
do { |
342 |
– |
if (rt->nrays <= 0) |
343 |
– |
continue; |
344 |
– |
memset(rt->buf+6*rt->nrays, 0, 6*sizeof(float)); |
345 |
– |
nw = 6*sizeof(float)*(rt->nrays+1); |
346 |
– |
errno = 0; |
347 |
– |
if (writebuf(rt->pd.w, (char *)rt->buf, nw) < nw) |
348 |
– |
error(SYSTEM, "error writing to rtrace process"); |
349 |
– |
rt->nrays = 0; /* flag buffer as flushed */ |
350 |
– |
} while (doall && (rt = rt->next) != NULL); |
351 |
– |
} |
352 |
– |
|
353 |
– |
|
354 |
– |
struct rtproc * |
355 |
– |
raywait( /* retrieve rtrace results */ |
356 |
– |
struct rtproc *rt0 |
357 |
– |
) |
358 |
– |
{ |
359 |
– |
fd_set readset, errset; |
360 |
– |
int nr; |
361 |
– |
struct rtproc *rt, *rtfree; |
362 |
– |
register int n; |
363 |
– |
/* prepare select call */ |
364 |
– |
FD_ZERO(&readset); FD_ZERO(&errset); n = 0; |
365 |
– |
nr = 0; |
366 |
– |
for (rt = rt0; rt != NULL; rt = rt->next) { |
367 |
– |
if (rt->nrays == 0 && rt->dest[0] != NULL) { |
368 |
– |
FD_SET(rt->pd.r, &readset); |
369 |
– |
++nr; |
370 |
– |
} |
371 |
– |
FD_SET(rt->pd.r, &errset); |
372 |
– |
if (rt->pd.r >= n) |
373 |
– |
n = rt->pd.r + 1; |
374 |
– |
} |
375 |
– |
if (!nr) /* no rays pending */ |
376 |
– |
return(NULL); |
377 |
– |
if (nr > 1) /* call select for multiple processes */ |
378 |
– |
n = select(n, &readset, (fd_set *)NULL, &errset, |
379 |
– |
(struct timeval *)NULL); |
380 |
– |
else |
381 |
– |
FD_ZERO(&errset); |
382 |
– |
if (n <= 0) |
383 |
– |
return(NULL); |
384 |
– |
rtfree = NULL; /* read from ready process(es) */ |
385 |
– |
for (rt = rt0; rt != NULL; rt = rt->next) { |
386 |
– |
if (!FD_ISSET(rt->pd.r, &readset) && |
387 |
– |
!FD_ISSET(rt->pd.r, &errset)) |
388 |
– |
continue; |
389 |
– |
for (n = 0; n < rt->bsiz && rt->dest[n] != NULL; n++) |
390 |
– |
; |
391 |
– |
errno = 0; |
392 |
– |
nr = read(rt->pd.r, (char *)rt->buf, 3*sizeof(float)*(n+1)); |
393 |
– |
if (nr < 0) |
394 |
– |
error(SYSTEM, "read error in raywait()"); |
395 |
– |
if (nr == 0) /* unexpected EOF */ |
396 |
– |
error(USER, "rtrace process died"); |
397 |
– |
if (nr < 3*sizeof(float)*(n+1)) { /* read the rest */ |
398 |
– |
nr = readbuf(rt->pd.r, (char *)rt->buf, |
399 |
– |
3*sizeof(float)*(n+1) - nr); |
400 |
– |
if (nr < 0) |
401 |
– |
error(USER, "readbuf error in raywait()"); |
402 |
– |
} |
403 |
– |
while (n-- > 0) { |
404 |
– |
rt->dest[n][0] += rt->buf[3*n]; |
405 |
– |
rt->dest[n][1] += rt->buf[3*n+1]; |
406 |
– |
rt->dest[n][2] += rt->buf[3*n+2]; |
407 |
– |
rt->dest[n] = NULL; |
408 |
– |
} |
409 |
– |
rtfree = rt; |
410 |
– |
} |
411 |
– |
return(rtfree); |
412 |
– |
} |
413 |
– |
|
414 |
– |
|
415 |
– |
static void |
416 |
– |
mkaxes( /* compute u and v to go with n */ |
417 |
– |
FVECT u, |
418 |
– |
FVECT v, |
419 |
– |
FVECT n |
420 |
– |
) |
421 |
– |
{ |
422 |
– |
register int i; |
423 |
– |
|
424 |
– |
v[0] = v[1] = v[2] = 0.0; |
425 |
– |
for (i = 0; i < 3; i++) |
426 |
– |
if (n[i] < 0.6 && n[i] > -0.6) |
427 |
– |
break; |
428 |
– |
v[i] = 1.0; |
429 |
– |
fcross(u, v, n); |
430 |
– |
normalize(u); |
431 |
– |
fcross(v, n, u); |
432 |
– |
} |
433 |
– |
|
434 |
– |
|
435 |
– |
static void |
436 |
– |
rounddir( /* compute uniform spherical direction */ |
437 |
– |
register FVECT dv, |
438 |
– |
double alt, |
439 |
– |
double azi |
440 |
– |
) |
441 |
– |
{ |
442 |
– |
double d1, d2; |
443 |
– |
|
444 |
– |
dv[2] = 1. - 2.*alt; |
445 |
– |
d1 = sqrt(1. - dv[2]*dv[2]); |
446 |
– |
d2 = 2.*PI * azi; |
447 |
– |
dv[0] = d1*cos(d2); |
448 |
– |
dv[1] = d1*sin(d2); |
449 |
– |
} |
450 |
– |
|
451 |
– |
|
452 |
– |
static void |
453 |
– |
flatdir( /* compute uniform hemispherical direction */ |
454 |
– |
register FVECT dv, |
455 |
– |
double alt, |
456 |
– |
double azi |
457 |
– |
) |
458 |
– |
{ |
459 |
– |
double d1, d2; |
460 |
– |
|
461 |
– |
d1 = sqrt(alt); |
462 |
– |
d2 = 2.*PI * azi; |
463 |
– |
dv[0] = d1*cos(d2); |
464 |
– |
dv[1] = d1*sin(d2); |
465 |
– |
dv[2] = sqrt(1. - alt); |
600 |
|
} |