10 |
|
#include "mkillum.h" |
11 |
|
#include "face.h" |
12 |
|
#include "cone.h" |
13 |
< |
#include "random.h" |
13 |
> |
#include "source.h" |
14 |
> |
#include "paths.h" |
15 |
|
|
16 |
+ |
#ifndef NBSDFSAMPS |
17 |
+ |
#define NBSDFSAMPS 256 /* BSDF resampling count */ |
18 |
+ |
#endif |
19 |
|
|
20 |
< |
o_default(ob, il, rt, nm) /* default illum action */ |
21 |
< |
OBJREC *ob; |
22 |
< |
struct illum_args *il; |
23 |
< |
struct rtproc *rt; |
24 |
< |
char *nm; |
20 |
> |
COLORV * distarr = NULL; /* distribution array */ |
21 |
> |
int distsiz = 0; |
22 |
> |
|
23 |
> |
|
24 |
> |
void |
25 |
> |
newdist( /* allocate & clear distribution array */ |
26 |
> |
int siz |
27 |
> |
) |
28 |
|
{ |
29 |
+ |
if (siz <= 0) { |
30 |
+ |
if (distsiz > 0) |
31 |
+ |
free(distarr); |
32 |
+ |
distarr = NULL; |
33 |
+ |
distsiz = 0; |
34 |
+ |
return; |
35 |
+ |
} |
36 |
+ |
if (distsiz < siz) { |
37 |
+ |
if (distsiz > 0) |
38 |
+ |
free(distarr); |
39 |
+ |
distarr = (COLORV *)malloc(sizeof(COLOR)*siz); |
40 |
+ |
if (distarr == NULL) |
41 |
+ |
error(SYSTEM, "out of memory in newdist"); |
42 |
+ |
distsiz = siz; |
43 |
+ |
} |
44 |
+ |
memset(distarr, '\0', sizeof(COLOR)*siz); |
45 |
+ |
} |
46 |
+ |
|
47 |
+ |
|
48 |
+ |
int |
49 |
+ |
process_ray( /* process a ray result or report error */ |
50 |
+ |
RAY *r, |
51 |
+ |
int rv |
52 |
+ |
) |
53 |
+ |
{ |
54 |
+ |
COLORV *colp; |
55 |
+ |
|
56 |
+ |
if (rv == 0) /* no result ready */ |
57 |
+ |
return(0); |
58 |
+ |
if (rv < 0) |
59 |
+ |
error(USER, "ray tracing process died"); |
60 |
+ |
if (r->rno >= distsiz) |
61 |
+ |
error(INTERNAL, "bad returned index in process_ray"); |
62 |
+ |
multcolor(r->rcol, r->rcoef); /* in case it's a source ray */ |
63 |
+ |
colp = &distarr[r->rno * 3]; |
64 |
+ |
addcolor(colp, r->rcol); |
65 |
+ |
return(1); |
66 |
+ |
} |
67 |
+ |
|
68 |
+ |
|
69 |
+ |
void |
70 |
+ |
raysamp( /* queue a ray sample */ |
71 |
+ |
int ndx, |
72 |
+ |
FVECT org, |
73 |
+ |
FVECT dir |
74 |
+ |
) |
75 |
+ |
{ |
76 |
+ |
RAY myRay; |
77 |
+ |
int rv; |
78 |
+ |
|
79 |
+ |
if ((ndx < 0) | (ndx >= distsiz)) |
80 |
+ |
error(INTERNAL, "bad index in raysamp"); |
81 |
+ |
VCOPY(myRay.rorg, org); |
82 |
+ |
VCOPY(myRay.rdir, dir); |
83 |
+ |
myRay.rmax = .0; |
84 |
+ |
rayorigin(&myRay, PRIMARY|SPECULAR, NULL, NULL); |
85 |
+ |
myRay.rno = ndx; |
86 |
+ |
/* queue ray, check result */ |
87 |
+ |
process_ray(&myRay, ray_pqueue(&myRay)); |
88 |
+ |
} |
89 |
+ |
|
90 |
+ |
|
91 |
+ |
void |
92 |
+ |
srcsamps( /* sample sources from this surface position */ |
93 |
+ |
struct illum_args *il, |
94 |
+ |
FVECT org, |
95 |
+ |
FVECT nrm, |
96 |
+ |
MAT4 ixfm |
97 |
+ |
) |
98 |
+ |
{ |
99 |
+ |
int nalt=1, nazi=1; |
100 |
+ |
SRCINDEX si; |
101 |
+ |
RAY sr; |
102 |
+ |
FVECT v; |
103 |
+ |
double d; |
104 |
+ |
int i, j; |
105 |
+ |
/* get sampling density */ |
106 |
+ |
if (il->sampdens > 0) { |
107 |
+ |
i = PI * il->sampdens; |
108 |
+ |
nalt = sqrt(i/PI) + .5; |
109 |
+ |
nazi = PI*nalt + .5; |
110 |
+ |
} |
111 |
+ |
initsrcindex(&si); /* loop over (sub)sources */ |
112 |
+ |
for ( ; ; ) { |
113 |
+ |
VCOPY(sr.rorg, org); /* pick side to shoot from */ |
114 |
+ |
d = 5.*FTINY; |
115 |
+ |
VSUM(sr.rorg, sr.rorg, nrm, d); |
116 |
+ |
samplendx++; /* increment sample counter */ |
117 |
+ |
if (!srcray(&sr, NULL, &si)) |
118 |
+ |
break; /* end of sources */ |
119 |
+ |
/* index direction */ |
120 |
+ |
if (ixfm != NULL) |
121 |
+ |
multv3(v, sr.rdir, ixfm); |
122 |
+ |
else |
123 |
+ |
VCOPY(v, sr.rdir); |
124 |
+ |
if (v[2] >= -FTINY) |
125 |
+ |
continue; /* only sample transmission */ |
126 |
+ |
v[0] = -v[0]; v[1] = -v[1]; v[2] = -v[2]; |
127 |
+ |
sr.rno = flatindex(v, nalt, nazi); |
128 |
+ |
d = nalt*nazi*(1./PI) * v[2]; |
129 |
+ |
d *= si.dom; /* solid angle correction */ |
130 |
+ |
scalecolor(sr.rcoef, d); |
131 |
+ |
process_ray(&sr, ray_pqueue(&sr)); |
132 |
+ |
} |
133 |
+ |
} |
134 |
+ |
|
135 |
+ |
|
136 |
+ |
void |
137 |
+ |
rayclean() /* finish all pending rays */ |
138 |
+ |
{ |
139 |
+ |
RAY myRay; |
140 |
+ |
|
141 |
+ |
while (process_ray(&myRay, ray_presult(&myRay, 0))) |
142 |
+ |
; |
143 |
+ |
} |
144 |
+ |
|
145 |
+ |
|
146 |
+ |
static void |
147 |
+ |
mkaxes( /* compute u and v to go with n */ |
148 |
+ |
FVECT u, |
149 |
+ |
FVECT v, |
150 |
+ |
FVECT n |
151 |
+ |
) |
152 |
+ |
{ |
153 |
+ |
getperpendicular(u, n); |
154 |
+ |
fcross(v, n, u); |
155 |
+ |
} |
156 |
+ |
|
157 |
+ |
|
158 |
+ |
static void |
159 |
+ |
rounddir( /* compute uniform spherical direction */ |
160 |
+ |
FVECT dv, |
161 |
+ |
double alt, |
162 |
+ |
double azi |
163 |
+ |
) |
164 |
+ |
{ |
165 |
+ |
double d1, d2; |
166 |
+ |
|
167 |
+ |
dv[2] = 1. - 2.*alt; |
168 |
+ |
d1 = sqrt(1. - dv[2]*dv[2]); |
169 |
+ |
d2 = 2.*PI * azi; |
170 |
+ |
dv[0] = d1*cos(d2); |
171 |
+ |
dv[1] = d1*sin(d2); |
172 |
+ |
} |
173 |
+ |
|
174 |
+ |
|
175 |
+ |
void |
176 |
+ |
flatdir( /* compute uniform hemispherical direction */ |
177 |
+ |
FVECT dv, |
178 |
+ |
double alt, |
179 |
+ |
double azi |
180 |
+ |
) |
181 |
+ |
{ |
182 |
+ |
double d1, d2; |
183 |
+ |
|
184 |
+ |
d1 = sqrt(alt); |
185 |
+ |
d2 = 2.*PI * azi; |
186 |
+ |
dv[0] = d1*cos(d2); |
187 |
+ |
dv[1] = d1*sin(d2); |
188 |
+ |
dv[2] = sqrt(1. - alt); |
189 |
+ |
} |
190 |
+ |
|
191 |
+ |
|
192 |
+ |
int |
193 |
+ |
flatindex( /* compute index for hemispherical direction */ |
194 |
+ |
FVECT dv, |
195 |
+ |
int nalt, |
196 |
+ |
int nazi |
197 |
+ |
) |
198 |
+ |
{ |
199 |
+ |
double d; |
200 |
+ |
int i, j; |
201 |
+ |
|
202 |
+ |
d = 1.0 - dv[2]*dv[2]; |
203 |
+ |
i = d*nalt; |
204 |
+ |
d = atan2(dv[1], dv[0]) * (0.5/PI); |
205 |
+ |
if (d < 0.0) d += 1.0; |
206 |
+ |
j = d*nazi + 0.5; |
207 |
+ |
if (j >= nazi) j = 0; |
208 |
+ |
return(i*nazi + j); |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
|
212 |
+ |
int |
213 |
+ |
my_default( /* default illum action */ |
214 |
+ |
OBJREC *ob, |
215 |
+ |
struct illum_args *il, |
216 |
+ |
char *nm |
217 |
+ |
) |
218 |
+ |
{ |
219 |
|
sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"", |
220 |
|
nm, ofun[ob->otype].funame, ob->oname); |
221 |
|
error(WARNING, errmsg); |
222 |
|
printobj(il->altmat, ob); |
223 |
+ |
return(1); |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
< |
o_face(ob, il, rt, nm) /* make an illum face */ |
228 |
< |
OBJREC *ob; |
229 |
< |
struct illum_args *il; |
230 |
< |
struct rtproc *rt; |
231 |
< |
char *nm; |
227 |
> |
int |
228 |
> |
my_face( /* make an illum face */ |
229 |
> |
OBJREC *ob, |
230 |
> |
struct illum_args *il, |
231 |
> |
char *nm |
232 |
> |
) |
233 |
|
{ |
234 |
< |
#define MAXMISS (5*n*il->nsamps) |
235 |
< |
int dim[3]; |
37 |
< |
int n, nalt, nazi, h; |
38 |
< |
float *distarr; |
234 |
> |
int dim[2]; |
235 |
> |
int n, nalt, nazi, alti; |
236 |
|
double sp[2], r1, r2; |
237 |
+ |
int h; |
238 |
|
FVECT dn, org, dir; |
239 |
|
FVECT u, v; |
240 |
|
double ur[2], vr[2]; |
241 |
< |
int nmisses; |
242 |
< |
register FACE *fa; |
243 |
< |
register int i, j; |
241 |
> |
MAT4 xfm; |
242 |
> |
char xfrot[64]; |
243 |
> |
int nallow; |
244 |
> |
FACE *fa; |
245 |
> |
int i, j; |
246 |
|
/* get/check arguments */ |
247 |
|
fa = getface(ob); |
248 |
|
if (fa->area == 0.0) { |
249 |
|
freeface(ob); |
250 |
< |
o_default(ob, il, rt, nm); |
51 |
< |
return; |
250 |
> |
return(my_default(ob, il, nm)); |
251 |
|
} |
252 |
|
/* set up sampling */ |
253 |
< |
if (il->sampdens <= 0) |
254 |
< |
nalt = nazi = 1; |
255 |
< |
else { |
253 |
> |
if (il->sampdens <= 0) { |
254 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
255 |
> |
} else { |
256 |
|
n = PI * il->sampdens; |
257 |
|
nalt = sqrt(n/PI) + .5; |
258 |
|
nazi = PI*nalt + .5; |
259 |
|
} |
260 |
< |
n = nalt*nazi; |
261 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
262 |
< |
if (distarr == NULL) |
64 |
< |
error(SYSTEM, "out of memory in o_face"); |
65 |
< |
/* take first edge longer than sqrt(area) */ |
260 |
> |
n = nazi*nalt; |
261 |
> |
newdist(n); |
262 |
> |
/* take first edge >= sqrt(area) */ |
263 |
|
for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) { |
264 |
|
u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0]; |
265 |
|
u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1]; |
286 |
|
} |
287 |
|
dim[0] = random(); |
288 |
|
/* sample polygon */ |
289 |
< |
nmisses = 0; |
290 |
< |
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
94 |
< |
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
289 |
> |
nallow = 5*n*il->nsamps; |
290 |
> |
for (dim[1] = 0; dim[1] < n; dim[1]++) |
291 |
|
for (i = 0; i < il->nsamps; i++) { |
292 |
< |
/* random direction */ |
293 |
< |
h = ilhash(dim, 3) + i; |
292 |
> |
/* randomize direction */ |
293 |
> |
h = ilhash(dim, 2) + i; |
294 |
|
multisamp(sp, 2, urand(h)); |
295 |
< |
r1 = (dim[1] + sp[0])/nalt; |
296 |
< |
r2 = (dim[2] + sp[1] - .5)/nazi; |
295 |
> |
alti = dim[1]/nazi; |
296 |
> |
r1 = (alti + sp[0])/nalt; |
297 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
298 |
|
flatdir(dn, r1, r2); |
299 |
|
for (j = 0; j < 3; j++) |
300 |
< |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j]; |
301 |
< |
/* random location */ |
300 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - |
301 |
> |
dn[2]*fa->norm[j]; |
302 |
> |
/* randomize location */ |
303 |
|
do { |
304 |
< |
multisamp(sp, 2, urand(h+4862+nmisses)); |
304 |
> |
multisamp(sp, 2, urand(h+4862+nallow)); |
305 |
|
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
306 |
|
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
307 |
|
for (j = 0; j < 3; j++) |
308 |
|
org[j] = r1*u[j] + r2*v[j] |
309 |
|
+ fa->offset*fa->norm[j]; |
310 |
< |
} while (!inface(org, fa) && nmisses++ < MAXMISS); |
311 |
< |
if (nmisses > MAXMISS) { |
310 |
> |
} while (!inface(org, fa) && nallow-- > 0); |
311 |
> |
if (nallow < 0) { |
312 |
|
objerror(ob, WARNING, "bad aspect"); |
313 |
< |
rt->nrays = 0; |
313 |
> |
rayclean(); |
314 |
|
freeface(ob); |
315 |
< |
free((void *)distarr); |
118 |
< |
o_default(ob, il, rt, nm); |
119 |
< |
return; |
315 |
> |
return(my_default(ob, il, nm)); |
316 |
|
} |
317 |
+ |
r1 = 5.*FTINY; |
318 |
|
for (j = 0; j < 3; j++) |
319 |
< |
org[j] += .001*fa->norm[j]; |
319 |
> |
org[j] += r1*fa->norm[j]; |
320 |
|
/* send sample */ |
321 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
321 |
> |
raysamp(dim[1], org, dir); |
322 |
|
} |
323 |
< |
rayflush(rt); |
323 |
> |
/* add in direct component? */ |
324 |
> |
if (il->flags & IL_LIGHT) { |
325 |
> |
MAT4 ixfm; |
326 |
> |
for (i = 3; i--; ) { |
327 |
> |
ixfm[i][0] = u[i]; |
328 |
> |
ixfm[i][1] = v[i]; |
329 |
> |
ixfm[i][2] = fa->norm[i]; |
330 |
> |
ixfm[i][3] = 0.; |
331 |
> |
} |
332 |
> |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
333 |
> |
ixfm[3][3] = 1.; |
334 |
> |
dim[0] = random(); |
335 |
> |
nallow = 10*il->nsamps; |
336 |
> |
for (i = 0; i < il->nsamps; i++) { |
337 |
> |
/* randomize location */ |
338 |
> |
h = dim[0] + samplendx++; |
339 |
> |
do { |
340 |
> |
multisamp(sp, 2, urand(h+nallow)); |
341 |
> |
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
342 |
> |
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
343 |
> |
for (j = 0; j < 3; j++) |
344 |
> |
org[j] = r1*u[j] + r2*v[j] |
345 |
> |
+ fa->offset*fa->norm[j]; |
346 |
> |
} while (!inface(org, fa) && nallow-- > 0); |
347 |
> |
if (nallow < 0) { |
348 |
> |
objerror(ob, WARNING, "bad aspect"); |
349 |
> |
rayclean(); |
350 |
> |
freeface(ob); |
351 |
> |
return(my_default(ob, il, nm)); |
352 |
> |
} |
353 |
> |
/* sample source rays */ |
354 |
> |
srcsamps(il, org, fa->norm, ixfm); |
355 |
> |
} |
356 |
> |
} |
357 |
> |
/* wait for all rays to finish */ |
358 |
> |
rayclean(); |
359 |
|
/* write out the face and its distribution */ |
360 |
< |
if (average(il, distarr, nalt*nazi)) { |
360 |
> |
if (average(il, distarr, n)) { |
361 |
|
if (il->sampdens > 0) |
362 |
|
flatout(il, distarr, nalt, nazi, u, v, fa->norm); |
363 |
|
illumout(il, ob); |
365 |
|
printobj(il->altmat, ob); |
366 |
|
/* clean up */ |
367 |
|
freeface(ob); |
368 |
< |
free((void *)distarr); |
137 |
< |
#undef MAXMISS |
368 |
> |
return(0); |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
< |
o_sphere(ob, il, rt, nm) /* make an illum sphere */ |
373 |
< |
register OBJREC *ob; |
374 |
< |
struct illum_args *il; |
375 |
< |
struct rtproc *rt; |
376 |
< |
char *nm; |
372 |
> |
int |
373 |
> |
my_sphere( /* make an illum sphere */ |
374 |
> |
OBJREC *ob, |
375 |
> |
struct illum_args *il, |
376 |
> |
char *nm |
377 |
> |
) |
378 |
|
{ |
379 |
|
int dim[3]; |
380 |
|
int n, nalt, nazi; |
149 |
– |
float *distarr; |
381 |
|
double sp[4], r1, r2, r3; |
382 |
|
FVECT org, dir; |
383 |
|
FVECT u, v; |
384 |
< |
register int i, j; |
384 |
> |
int i, j; |
385 |
|
/* check arguments */ |
386 |
|
if (ob->oargs.nfargs != 4) |
387 |
|
objerror(ob, USER, "bad # of arguments"); |
394 |
|
nazi = PI/2.*nalt + .5; |
395 |
|
} |
396 |
|
n = nalt*nazi; |
397 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
167 |
< |
if (distarr == NULL) |
168 |
< |
error(SYSTEM, "out of memory in o_sphere"); |
397 |
> |
newdist(n); |
398 |
|
dim[0] = random(); |
399 |
|
/* sample sphere */ |
400 |
|
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
419 |
|
dir[j] = -dir[j]; |
420 |
|
} |
421 |
|
/* send sample */ |
422 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
422 |
> |
raysamp(dim[1]*nazi+dim[2], org, dir); |
423 |
|
} |
424 |
< |
rayflush(rt); |
424 |
> |
/* wait for all rays to finish */ |
425 |
> |
rayclean(); |
426 |
|
/* write out the sphere and its distribution */ |
427 |
< |
if (average(il, distarr, nalt*nazi)) { |
427 |
> |
if (average(il, distarr, n)) { |
428 |
|
if (il->sampdens > 0) |
429 |
|
roundout(il, distarr, nalt, nazi); |
430 |
|
else |
433 |
|
} else |
434 |
|
printobj(il->altmat, ob); |
435 |
|
/* clean up */ |
436 |
< |
free((void *)distarr); |
436 |
> |
return(1); |
437 |
|
} |
438 |
|
|
439 |
|
|
440 |
< |
o_ring(ob, il, rt, nm) /* make an illum ring */ |
441 |
< |
OBJREC *ob; |
442 |
< |
struct illum_args *il; |
443 |
< |
struct rtproc *rt; |
444 |
< |
char *nm; |
440 |
> |
int |
441 |
> |
my_ring( /* make an illum ring */ |
442 |
> |
OBJREC *ob, |
443 |
> |
struct illum_args *il, |
444 |
> |
char *nm |
445 |
> |
) |
446 |
|
{ |
447 |
< |
int dim[3]; |
448 |
< |
int n, nalt, nazi; |
449 |
< |
float *distarr; |
450 |
< |
double sp[4], r1, r2, r3; |
447 |
> |
int dim[2]; |
448 |
> |
int n, nalt, nazi, alti; |
449 |
> |
double sp[2], r1, r2, r3; |
450 |
> |
int h; |
451 |
|
FVECT dn, org, dir; |
452 |
|
FVECT u, v; |
453 |
< |
register CONE *co; |
454 |
< |
register int i, j; |
453 |
> |
MAT4 xfm; |
454 |
> |
CONE *co; |
455 |
> |
int i, j; |
456 |
|
/* get/check arguments */ |
457 |
|
co = getcone(ob, 0); |
458 |
|
/* set up sampling */ |
459 |
< |
if (il->sampdens <= 0) |
460 |
< |
nalt = nazi = 1; |
461 |
< |
else { |
459 |
> |
if (il->sampdens <= 0) { |
460 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
461 |
> |
} else { |
462 |
|
n = PI * il->sampdens; |
463 |
|
nalt = sqrt(n/PI) + .5; |
464 |
|
nazi = PI*nalt + .5; |
465 |
|
} |
466 |
< |
n = nalt*nazi; |
467 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
236 |
< |
if (distarr == NULL) |
237 |
< |
error(SYSTEM, "out of memory in o_ring"); |
466 |
> |
n = nazi*nalt; |
467 |
> |
newdist(n); |
468 |
|
mkaxes(u, v, co->ad); |
469 |
|
dim[0] = random(); |
470 |
|
/* sample disk */ |
471 |
< |
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
242 |
< |
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
471 |
> |
for (dim[1] = 0; dim[1] < n; dim[1]++) |
472 |
|
for (i = 0; i < il->nsamps; i++) { |
473 |
|
/* next sample point */ |
474 |
< |
multisamp(sp, 4, urand(ilhash(dim,3)+i)); |
475 |
< |
/* random direction */ |
476 |
< |
r1 = (dim[1] + sp[0])/nalt; |
477 |
< |
r2 = (dim[2] + sp[1] - .5)/nazi; |
474 |
> |
h = ilhash(dim,2) + i; |
475 |
> |
/* randomize direction */ |
476 |
> |
multisamp(sp, 2, urand(h)); |
477 |
> |
alti = dim[1]/nazi; |
478 |
> |
r1 = (alti + sp[0])/nalt; |
479 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
480 |
|
flatdir(dn, r1, r2); |
481 |
|
for (j = 0; j < 3; j++) |
482 |
|
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
483 |
< |
/* random location */ |
483 |
> |
/* randomize location */ |
484 |
> |
multisamp(sp, 2, urand(h+8371)); |
485 |
|
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
486 |
< |
sp[2]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
487 |
< |
r2 = 2.*PI*sp[3]; |
486 |
> |
sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
487 |
> |
r2 = 2.*PI*sp[1]; |
488 |
|
r1 = r3*cos(r2); |
489 |
|
r2 = r3*sin(r2); |
490 |
+ |
r3 = 5.*FTINY; |
491 |
|
for (j = 0; j < 3; j++) |
492 |
|
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + |
493 |
< |
.001*co->ad[j]; |
261 |
< |
|
493 |
> |
r3*co->ad[j]; |
494 |
|
/* send sample */ |
495 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
495 |
> |
raysamp(dim[1], org, dir); |
496 |
|
} |
497 |
< |
rayflush(rt); |
497 |
> |
/* add in direct component? */ |
498 |
> |
if (il->flags & IL_LIGHT) { |
499 |
> |
MAT4 ixfm; |
500 |
> |
for (i = 3; i--; ) { |
501 |
> |
ixfm[i][0] = u[i]; |
502 |
> |
ixfm[i][1] = v[i]; |
503 |
> |
ixfm[i][2] = co->ad[i]; |
504 |
> |
ixfm[i][3] = 0.; |
505 |
> |
} |
506 |
> |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
507 |
> |
ixfm[3][3] = 1.; |
508 |
> |
dim[0] = random(); |
509 |
> |
for (i = 0; i < il->nsamps; i++) { |
510 |
> |
/* randomize location */ |
511 |
> |
h = dim[0] + samplendx++; |
512 |
> |
multisamp(sp, 2, urand(h)); |
513 |
> |
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
514 |
> |
sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
515 |
> |
r2 = 2.*PI*sp[1]; |
516 |
> |
r1 = r3*cos(r2); |
517 |
> |
r2 = r3*sin(r2); |
518 |
> |
for (j = 0; j < 3; j++) |
519 |
> |
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j]; |
520 |
> |
/* sample source rays */ |
521 |
> |
srcsamps(il, org, co->ad, ixfm); |
522 |
> |
} |
523 |
> |
} |
524 |
> |
/* wait for all rays to finish */ |
525 |
> |
rayclean(); |
526 |
|
/* write out the ring and its distribution */ |
527 |
< |
if (average(il, distarr, nalt*nazi)) { |
527 |
> |
if (average(il, distarr, n)) { |
528 |
|
if (il->sampdens > 0) |
529 |
|
flatout(il, distarr, nalt, nazi, u, v, co->ad); |
530 |
|
illumout(il, ob); |
532 |
|
printobj(il->altmat, ob); |
533 |
|
/* clean up */ |
534 |
|
freecone(ob); |
535 |
< |
free((void *)distarr); |
276 |
< |
} |
277 |
< |
|
278 |
< |
|
279 |
< |
raysamp(res, org, dir, rt) /* compute a ray sample */ |
280 |
< |
float res[3]; |
281 |
< |
FVECT org, dir; |
282 |
< |
register struct rtproc *rt; |
283 |
< |
{ |
284 |
< |
register float *fp; |
285 |
< |
|
286 |
< |
if (rt->nrays == rt->bsiz) |
287 |
< |
rayflush(rt); |
288 |
< |
rt->dest[rt->nrays] = res; |
289 |
< |
fp = rt->buf + 6*rt->nrays++; |
290 |
< |
*fp++ = org[0]; *fp++ = org[1]; *fp++ = org[2]; |
291 |
< |
*fp++ = dir[0]; *fp++ = dir[1]; *fp = dir[2]; |
292 |
< |
} |
293 |
< |
|
294 |
< |
|
295 |
< |
rayflush(rt) /* flush buffered rays */ |
296 |
< |
register struct rtproc *rt; |
297 |
< |
{ |
298 |
< |
register int i; |
299 |
< |
|
300 |
< |
if (rt->nrays <= 0) |
301 |
< |
return; |
302 |
< |
memset(rt->buf+6*rt->nrays, '\0', 6*sizeof(float)); |
303 |
< |
errno = 0; |
304 |
< |
if ( process(&(rt->pd), (char *)rt->buf, (char *)rt->buf, |
305 |
< |
3*sizeof(float)*(rt->nrays+1), |
306 |
< |
6*sizeof(float)*(rt->nrays+1)) < |
307 |
< |
3*sizeof(float)*(rt->nrays+1) ) |
308 |
< |
error(SYSTEM, "error reading from rtrace process"); |
309 |
< |
i = rt->nrays; |
310 |
< |
while (i--) { |
311 |
< |
rt->dest[i][0] += rt->buf[3*i]; |
312 |
< |
rt->dest[i][1] += rt->buf[3*i+1]; |
313 |
< |
rt->dest[i][2] += rt->buf[3*i+2]; |
314 |
< |
} |
315 |
< |
rt->nrays = 0; |
316 |
< |
} |
317 |
< |
|
318 |
< |
|
319 |
< |
mkaxes(u, v, n) /* compute u and v to go with n */ |
320 |
< |
FVECT u, v, n; |
321 |
< |
{ |
322 |
< |
register int i; |
323 |
< |
|
324 |
< |
v[0] = v[1] = v[2] = 0.0; |
325 |
< |
for (i = 0; i < 3; i++) |
326 |
< |
if (n[i] < 0.6 && n[i] > -0.6) |
327 |
< |
break; |
328 |
< |
v[i] = 1.0; |
329 |
< |
fcross(u, v, n); |
330 |
< |
normalize(u); |
331 |
< |
fcross(v, n, u); |
332 |
< |
} |
333 |
< |
|
334 |
< |
|
335 |
< |
rounddir(dv, alt, azi) /* compute uniform spherical direction */ |
336 |
< |
register FVECT dv; |
337 |
< |
double alt, azi; |
338 |
< |
{ |
339 |
< |
double d1, d2; |
340 |
< |
|
341 |
< |
dv[2] = 1. - 2.*alt; |
342 |
< |
d1 = sqrt(1. - dv[2]*dv[2]); |
343 |
< |
d2 = 2.*PI * azi; |
344 |
< |
dv[0] = d1*cos(d2); |
345 |
< |
dv[1] = d1*sin(d2); |
346 |
< |
} |
347 |
< |
|
348 |
< |
|
349 |
< |
flatdir(dv, alt, azi) /* compute uniform hemispherical direction */ |
350 |
< |
register FVECT dv; |
351 |
< |
double alt, azi; |
352 |
< |
{ |
353 |
< |
double d1, d2; |
354 |
< |
|
355 |
< |
d1 = sqrt(alt); |
356 |
< |
d2 = 2.*PI * azi; |
357 |
< |
dv[0] = d1*cos(d2); |
358 |
< |
dv[1] = d1*sin(d2); |
359 |
< |
dv[2] = sqrt(1. - alt); |
535 |
> |
return(1); |
536 |
|
} |