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 |
< |
//void o_default(OBJREC *ob, struct illum_args *il, struct rtproc *rt, char *nm); |
17 |
< |
void o_face(OBJREC *ob, struct illum_args *il, struct rtproc *rt, char *nm); |
18 |
< |
void o_sphere(OBJREC *ob, struct illum_args *il, struct rtproc *rt, char *nm); |
18 |
< |
void o_ring(OBJREC *ob, struct illum_args *il, struct rtproc *rt, char *nm); |
19 |
< |
void raysamp(float res[3], FVECT org, FVECT dir, struct rtproc *rt); |
20 |
< |
void rayflush(struct rtproc *rt); |
21 |
< |
void mkaxes(FVECT u, FVECT v, FVECT n); |
22 |
< |
void rounddir(FVECT dv, double alt, double azi); |
23 |
< |
void flatdir(FVECT dv, double alt, double azi); |
16 |
> |
#ifndef R_EPS |
17 |
> |
#define R_EPS 0.005 /* relative epsilon for ray origin */ |
18 |
> |
#endif |
19 |
|
|
20 |
+ |
COLORV * distarr = NULL; /* distribution array */ |
21 |
+ |
int distsiz = 0; |
22 |
|
|
23 |
< |
int /* XXX type conflict with otypes.h */ |
24 |
< |
o_default( /* default illum action */ |
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 |
> |
double eps, |
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 |
> |
samplendx++; /* increment sample counter */ |
115 |
> |
if (!srcray(&sr, NULL, &si)) |
116 |
> |
break; /* end of sources */ |
117 |
> |
/* index direction */ |
118 |
> |
if (ixfm != NULL) |
119 |
> |
multv3(v, sr.rdir, ixfm); |
120 |
> |
else |
121 |
> |
VCOPY(v, sr.rdir); |
122 |
> |
if (v[2] >= -FTINY) |
123 |
> |
continue; /* only sample transmission */ |
124 |
> |
v[0] = -v[0]; v[1] = -v[1]; v[2] = -v[2]; |
125 |
> |
sr.rno = flatindex(v, nalt, nazi); |
126 |
> |
d = nalt*nazi*(1./PI) * v[2]; |
127 |
> |
d *= si.dom; /* solid angle correction */ |
128 |
> |
scalecolor(sr.rcoef, d); |
129 |
> |
VSUM(sr.rorg, sr.rorg, sr.rdir, -eps); |
130 |
> |
process_ray(&sr, ray_pqueue(&sr)); |
131 |
> |
} |
132 |
> |
} |
133 |
> |
|
134 |
> |
|
135 |
> |
void |
136 |
> |
rayclean() /* finish all pending rays */ |
137 |
> |
{ |
138 |
> |
RAY myRay; |
139 |
> |
|
140 |
> |
while (process_ray(&myRay, ray_presult(&myRay, 0))) |
141 |
> |
; |
142 |
> |
} |
143 |
> |
|
144 |
> |
|
145 |
> |
static void |
146 |
> |
mkaxes( /* compute u and v to go with n */ |
147 |
> |
FVECT u, |
148 |
> |
FVECT v, |
149 |
> |
FVECT n |
150 |
> |
) |
151 |
> |
{ |
152 |
> |
getperpendicular(u, n, 1); |
153 |
> |
fcross(v, n, u); |
154 |
> |
} |
155 |
> |
|
156 |
> |
|
157 |
> |
static void |
158 |
> |
rounddir( /* compute uniform spherical direction */ |
159 |
> |
FVECT dv, |
160 |
> |
double alt, |
161 |
> |
double azi |
162 |
> |
) |
163 |
> |
{ |
164 |
> |
double d1, d2; |
165 |
> |
|
166 |
> |
dv[2] = 1. - 2.*alt; |
167 |
> |
d1 = sqrt(1. - dv[2]*dv[2]); |
168 |
> |
d2 = 2.*PI * azi; |
169 |
> |
dv[0] = d1*cos(d2); |
170 |
> |
dv[1] = d1*sin(d2); |
171 |
> |
} |
172 |
> |
|
173 |
> |
|
174 |
> |
void |
175 |
> |
flatdir( /* compute uniform hemispherical direction */ |
176 |
> |
FVECT dv, |
177 |
> |
double alt, |
178 |
> |
double azi |
179 |
> |
) |
180 |
> |
{ |
181 |
> |
double d1, d2; |
182 |
> |
|
183 |
> |
d1 = sqrt(alt); |
184 |
> |
d2 = 2.*PI * azi; |
185 |
> |
dv[0] = d1*cos(d2); |
186 |
> |
dv[1] = d1*sin(d2); |
187 |
> |
dv[2] = sqrt(1. - alt); |
188 |
> |
} |
189 |
> |
|
190 |
> |
|
191 |
> |
int |
192 |
> |
flatindex( /* compute index for hemispherical direction */ |
193 |
> |
FVECT dv, |
194 |
> |
int nalt, |
195 |
> |
int nazi |
196 |
> |
) |
197 |
> |
{ |
198 |
> |
double d; |
199 |
> |
int i, j; |
200 |
> |
|
201 |
> |
d = 1.0 - dv[2]*dv[2]; |
202 |
> |
i = d*nalt; |
203 |
> |
d = atan2(dv[1], dv[0]) * (0.5/PI); |
204 |
> |
if (d < 0.0) d += 1.0; |
205 |
> |
j = d*nazi + 0.5; |
206 |
> |
if (j >= nazi) j = 0; |
207 |
> |
return(i*nazi + j); |
208 |
> |
} |
209 |
> |
|
210 |
> |
|
211 |
> |
int |
212 |
> |
my_default( /* default illum action */ |
213 |
|
OBJREC *ob, |
214 |
|
struct illum_args *il, |
30 |
– |
struct rtproc *rt, |
215 |
|
char *nm |
216 |
|
) |
217 |
|
{ |
219 |
|
nm, ofun[ob->otype].funame, ob->oname); |
220 |
|
error(WARNING, errmsg); |
221 |
|
printobj(il->altmat, ob); |
222 |
+ |
return(1); |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
< |
void |
227 |
< |
o_face( /* make an illum face */ |
226 |
> |
int |
227 |
> |
my_face( /* make an illum face */ |
228 |
|
OBJREC *ob, |
229 |
|
struct illum_args *il, |
45 |
– |
struct rtproc *rt, |
230 |
|
char *nm |
231 |
|
) |
232 |
|
{ |
233 |
< |
#define MAXMISS (5*n*il->nsamps) |
234 |
< |
int dim[3]; |
51 |
< |
int n, nalt, nazi, h; |
52 |
< |
float *distarr; |
233 |
> |
int dim[2]; |
234 |
> |
int n, nalt, nazi, alti; |
235 |
|
double sp[2], r1, r2; |
236 |
+ |
int h; |
237 |
|
FVECT dn, org, dir; |
238 |
|
FVECT u, v; |
239 |
|
double ur[2], vr[2]; |
240 |
< |
int nmisses; |
241 |
< |
register FACE *fa; |
242 |
< |
register int i, j; |
240 |
> |
double epsilon; |
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); |
65 |
< |
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) |
78 |
< |
error(SYSTEM, "out of memory in o_face"); |
79 |
< |
/* 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]++) |
291 |
< |
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
289 |
> |
nallow = 5*n*il->nsamps; |
290 |
> |
epsilon = R_EPS*sqrt(fa->area); |
291 |
> |
for (dim[1] = 0; dim[1] < n; dim[1]++) |
292 |
|
for (i = 0; i < il->nsamps; i++) { |
293 |
< |
/* random direction */ |
294 |
< |
h = ilhash(dim, 3) + i; |
293 |
> |
/* randomize direction */ |
294 |
> |
h = ilhash(dim, 2) + i; |
295 |
|
multisamp(sp, 2, urand(h)); |
296 |
< |
r1 = (dim[1] + sp[0])/nalt; |
297 |
< |
r2 = (dim[2] + sp[1] - .5)/nazi; |
296 |
> |
alti = dim[1]/nazi; |
297 |
> |
r1 = (alti + sp[0])/nalt; |
298 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
299 |
|
flatdir(dn, r1, r2); |
300 |
|
for (j = 0; j < 3; j++) |
301 |
< |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j]; |
302 |
< |
/* random location */ |
301 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - |
302 |
> |
dn[2]*fa->norm[j]; |
303 |
> |
/* randomize location */ |
304 |
|
do { |
305 |
< |
multisamp(sp, 2, urand(h+4862+nmisses)); |
305 |
> |
multisamp(sp, 2, urand(h+4862+nallow)); |
306 |
|
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
307 |
|
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
308 |
|
for (j = 0; j < 3; j++) |
309 |
|
org[j] = r1*u[j] + r2*v[j] |
310 |
|
+ fa->offset*fa->norm[j]; |
311 |
< |
} while (!inface(org, fa) && nmisses++ < MAXMISS); |
312 |
< |
if (nmisses > MAXMISS) { |
311 |
> |
} while (!inface(org, fa) && nallow-- > 0); |
312 |
> |
if (nallow < 0) { |
313 |
|
objerror(ob, WARNING, "bad aspect"); |
314 |
< |
rt->nrays = 0; |
314 |
> |
rayclean(); |
315 |
|
freeface(ob); |
316 |
< |
free((void *)distarr); |
132 |
< |
o_default(ob, il, rt, nm); |
133 |
< |
return; |
316 |
> |
return(my_default(ob, il, nm)); |
317 |
|
} |
318 |
< |
for (j = 0; j < 3; j++) |
136 |
< |
org[j] += .001*fa->norm[j]; |
318 |
> |
VSUM(org, org, dir, -epsilon); |
319 |
|
/* send sample */ |
320 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
320 |
> |
raysamp(dim[1], org, dir); |
321 |
|
} |
322 |
< |
rayflush(rt); |
322 |
> |
/* add in direct component? */ |
323 |
> |
if (il->flags & IL_LIGHT) { |
324 |
> |
MAT4 ixfm; |
325 |
> |
for (i = 3; i--; ) { |
326 |
> |
ixfm[i][0] = u[i]; |
327 |
> |
ixfm[i][1] = v[i]; |
328 |
> |
ixfm[i][2] = fa->norm[i]; |
329 |
> |
ixfm[i][3] = 0.; |
330 |
> |
} |
331 |
> |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
332 |
> |
ixfm[3][3] = 1.; |
333 |
> |
dim[0] = random(); |
334 |
> |
nallow = 10*il->nsamps; |
335 |
> |
for (i = 0; i < il->nsamps; i++) { |
336 |
> |
/* randomize location */ |
337 |
> |
h = dim[0] + samplendx++; |
338 |
> |
do { |
339 |
> |
multisamp(sp, 2, urand(h+nallow)); |
340 |
> |
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
341 |
> |
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
342 |
> |
for (j = 0; j < 3; j++) |
343 |
> |
org[j] = r1*u[j] + r2*v[j] |
344 |
> |
+ fa->offset*fa->norm[j]; |
345 |
> |
} while (!inface(org, fa) && nallow-- > 0); |
346 |
> |
if (nallow < 0) { |
347 |
> |
objerror(ob, WARNING, "bad aspect"); |
348 |
> |
rayclean(); |
349 |
> |
freeface(ob); |
350 |
> |
return(my_default(ob, il, nm)); |
351 |
> |
} |
352 |
> |
/* sample source rays */ |
353 |
> |
srcsamps(il, org, epsilon, ixfm); |
354 |
> |
} |
355 |
> |
} |
356 |
> |
/* wait for all rays to finish */ |
357 |
> |
rayclean(); |
358 |
|
/* write out the face and its distribution */ |
359 |
< |
if (average(il, distarr, nalt*nazi)) { |
359 |
> |
if (average(il, distarr, n)) { |
360 |
|
if (il->sampdens > 0) |
361 |
|
flatout(il, distarr, nalt, nazi, u, v, fa->norm); |
362 |
|
illumout(il, ob); |
364 |
|
printobj(il->altmat, ob); |
365 |
|
/* clean up */ |
366 |
|
freeface(ob); |
367 |
< |
free((void *)distarr); |
151 |
< |
#undef MAXMISS |
367 |
> |
return(0); |
368 |
|
} |
369 |
|
|
370 |
|
|
371 |
< |
void |
372 |
< |
o_sphere( /* make an illum sphere */ |
373 |
< |
register OBJREC *ob, |
371 |
> |
int |
372 |
> |
my_sphere( /* make an illum sphere */ |
373 |
> |
OBJREC *ob, |
374 |
|
struct illum_args *il, |
159 |
– |
struct rtproc *rt, |
375 |
|
char *nm |
376 |
|
) |
377 |
|
{ |
378 |
|
int dim[3]; |
379 |
|
int n, nalt, nazi; |
165 |
– |
float *distarr; |
380 |
|
double sp[4], r1, r2, r3; |
381 |
|
FVECT org, dir; |
382 |
|
FVECT u, v; |
383 |
< |
register int i, j; |
383 |
> |
int i, j; |
384 |
|
/* check arguments */ |
385 |
|
if (ob->oargs.nfargs != 4) |
386 |
|
objerror(ob, USER, "bad # of arguments"); |
393 |
|
nazi = PI/2.*nalt + .5; |
394 |
|
} |
395 |
|
n = nalt*nazi; |
396 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
183 |
< |
if (distarr == NULL) |
184 |
< |
error(SYSTEM, "out of memory in o_sphere"); |
396 |
> |
newdist(n); |
397 |
|
dim[0] = random(); |
398 |
|
/* sample sphere */ |
399 |
|
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
418 |
|
dir[j] = -dir[j]; |
419 |
|
} |
420 |
|
/* send sample */ |
421 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
421 |
> |
raysamp(dim[1]*nazi+dim[2], org, dir); |
422 |
|
} |
423 |
< |
rayflush(rt); |
423 |
> |
/* wait for all rays to finish */ |
424 |
> |
rayclean(); |
425 |
|
/* write out the sphere and its distribution */ |
426 |
< |
if (average(il, distarr, nalt*nazi)) { |
426 |
> |
if (average(il, distarr, n)) { |
427 |
|
if (il->sampdens > 0) |
428 |
|
roundout(il, distarr, nalt, nazi); |
429 |
|
else |
432 |
|
} else |
433 |
|
printobj(il->altmat, ob); |
434 |
|
/* clean up */ |
435 |
< |
free((void *)distarr); |
435 |
> |
return(1); |
436 |
|
} |
437 |
|
|
438 |
|
|
439 |
< |
void |
440 |
< |
o_ring( /* make an illum ring */ |
439 |
> |
int |
440 |
> |
my_ring( /* make an illum ring */ |
441 |
|
OBJREC *ob, |
442 |
|
struct illum_args *il, |
230 |
– |
struct rtproc *rt, |
443 |
|
char *nm |
444 |
|
) |
445 |
|
{ |
446 |
< |
int dim[3]; |
447 |
< |
int n, nalt, nazi; |
448 |
< |
float *distarr; |
449 |
< |
double sp[4], r1, r2, r3; |
446 |
> |
int dim[2]; |
447 |
> |
int n, nalt, nazi, alti; |
448 |
> |
double sp[2], r1, r2, r3; |
449 |
> |
double epsilon; |
450 |
> |
int h; |
451 |
|
FVECT dn, org, dir; |
452 |
|
FVECT u, v; |
453 |
< |
register CONE *co; |
454 |
< |
register int i, j; |
455 |
< |
/* get/check arguments */ |
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 { |
458 |
> |
if (co == NULL) |
459 |
> |
objerror(ob, USER, "cannot create illum"); |
460 |
> |
/* set up sampling */ |
461 |
> |
if (il->sampdens <= 0) { |
462 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
463 |
> |
} else { |
464 |
|
n = PI * il->sampdens; |
465 |
|
nalt = sqrt(n/PI) + .5; |
466 |
|
nazi = PI*nalt + .5; |
467 |
|
} |
468 |
< |
n = nalt*nazi; |
469 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
470 |
< |
if (distarr == NULL) |
255 |
< |
error(SYSTEM, "out of memory in o_ring"); |
468 |
> |
epsilon = R_EPS*CO_R1(co); |
469 |
> |
n = nazi*nalt; |
470 |
> |
newdist(n); |
471 |
|
mkaxes(u, v, co->ad); |
472 |
|
dim[0] = random(); |
473 |
|
/* sample disk */ |
474 |
< |
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
260 |
< |
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
474 |
> |
for (dim[1] = 0; dim[1] < n; dim[1]++) |
475 |
|
for (i = 0; i < il->nsamps; i++) { |
476 |
|
/* next sample point */ |
477 |
< |
multisamp(sp, 4, urand(ilhash(dim,3)+i)); |
478 |
< |
/* random direction */ |
479 |
< |
r1 = (dim[1] + sp[0])/nalt; |
480 |
< |
r2 = (dim[2] + sp[1] - .5)/nazi; |
477 |
> |
h = ilhash(dim,2) + i; |
478 |
> |
/* randomize direction */ |
479 |
> |
multisamp(sp, 2, urand(h)); |
480 |
> |
alti = dim[1]/nazi; |
481 |
> |
r1 = (alti + sp[0])/nalt; |
482 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
483 |
|
flatdir(dn, r1, r2); |
484 |
|
for (j = 0; j < 3; j++) |
485 |
|
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
486 |
< |
/* random location */ |
486 |
> |
/* randomize location */ |
487 |
> |
multisamp(sp, 2, urand(h+8371)); |
488 |
|
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
489 |
< |
sp[2]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
490 |
< |
r2 = 2.*PI*sp[3]; |
489 |
> |
sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
490 |
> |
r2 = 2.*PI*sp[1]; |
491 |
|
r1 = r3*cos(r2); |
492 |
|
r2 = r3*sin(r2); |
493 |
|
for (j = 0; j < 3; j++) |
494 |
|
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + |
495 |
< |
.001*co->ad[j]; |
279 |
< |
|
495 |
> |
epsilon*co->ad[j]; |
496 |
|
/* send sample */ |
497 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
497 |
> |
raysamp(dim[1], org, dir); |
498 |
|
} |
499 |
< |
rayflush(rt); |
499 |
> |
/* add in direct component? */ |
500 |
> |
if (il->flags & IL_LIGHT) { |
501 |
> |
MAT4 ixfm; |
502 |
> |
for (i = 3; i--; ) { |
503 |
> |
ixfm[i][0] = u[i]; |
504 |
> |
ixfm[i][1] = v[i]; |
505 |
> |
ixfm[i][2] = co->ad[i]; |
506 |
> |
ixfm[i][3] = 0.; |
507 |
> |
} |
508 |
> |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
509 |
> |
ixfm[3][3] = 1.; |
510 |
> |
dim[0] = random(); |
511 |
> |
for (i = 0; i < il->nsamps; i++) { |
512 |
> |
/* randomize location */ |
513 |
> |
h = dim[0] + samplendx++; |
514 |
> |
multisamp(sp, 2, urand(h)); |
515 |
> |
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
516 |
> |
sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
517 |
> |
r2 = 2.*PI*sp[1]; |
518 |
> |
r1 = r3*cos(r2); |
519 |
> |
r2 = r3*sin(r2); |
520 |
> |
for (j = 0; j < 3; j++) |
521 |
> |
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j]; |
522 |
> |
/* sample source rays */ |
523 |
> |
srcsamps(il, org, epsilon, ixfm); |
524 |
> |
} |
525 |
> |
} |
526 |
> |
/* wait for all rays to finish */ |
527 |
> |
rayclean(); |
528 |
|
/* write out the ring and its distribution */ |
529 |
< |
if (average(il, distarr, nalt*nazi)) { |
529 |
> |
if (average(il, distarr, n)) { |
530 |
|
if (il->sampdens > 0) |
531 |
|
flatout(il, distarr, nalt, nazi, u, v, co->ad); |
532 |
|
illumout(il, ob); |
534 |
|
printobj(il->altmat, ob); |
535 |
|
/* clean up */ |
536 |
|
freecone(ob); |
537 |
< |
free((void *)distarr); |
294 |
< |
} |
295 |
< |
|
296 |
< |
|
297 |
< |
void |
298 |
< |
raysamp( /* compute a ray sample */ |
299 |
< |
float res[3], |
300 |
< |
FVECT org, |
301 |
< |
FVECT dir, |
302 |
< |
register struct rtproc *rt |
303 |
< |
) |
304 |
< |
{ |
305 |
< |
register float *fp; |
306 |
< |
|
307 |
< |
if (rt->nrays == rt->bsiz) |
308 |
< |
rayflush(rt); |
309 |
< |
rt->dest[rt->nrays] = res; |
310 |
< |
fp = rt->buf + 6*rt->nrays++; |
311 |
< |
*fp++ = org[0]; *fp++ = org[1]; *fp++ = org[2]; |
312 |
< |
*fp++ = dir[0]; *fp++ = dir[1]; *fp = dir[2]; |
313 |
< |
} |
314 |
< |
|
315 |
< |
|
316 |
< |
void |
317 |
< |
rayflush( /* flush buffered rays */ |
318 |
< |
register struct rtproc *rt |
319 |
< |
) |
320 |
< |
{ |
321 |
< |
register int i; |
322 |
< |
|
323 |
< |
if (rt->nrays <= 0) |
324 |
< |
return; |
325 |
< |
memset(rt->buf+6*rt->nrays, '\0', 6*sizeof(float)); |
326 |
< |
errno = 0; |
327 |
< |
if ( process(&(rt->pd), (char *)rt->buf, (char *)rt->buf, |
328 |
< |
3*sizeof(float)*(rt->nrays+1), |
329 |
< |
6*sizeof(float)*(rt->nrays+1)) < |
330 |
< |
3*sizeof(float)*(rt->nrays+1) ) |
331 |
< |
error(SYSTEM, "error reading from rtrace process"); |
332 |
< |
i = rt->nrays; |
333 |
< |
while (i--) { |
334 |
< |
rt->dest[i][0] += rt->buf[3*i]; |
335 |
< |
rt->dest[i][1] += rt->buf[3*i+1]; |
336 |
< |
rt->dest[i][2] += rt->buf[3*i+2]; |
337 |
< |
} |
338 |
< |
rt->nrays = 0; |
339 |
< |
} |
340 |
< |
|
341 |
< |
|
342 |
< |
void |
343 |
< |
mkaxes( /* compute u and v to go with n */ |
344 |
< |
FVECT u, |
345 |
< |
FVECT v, |
346 |
< |
FVECT n |
347 |
< |
) |
348 |
< |
{ |
349 |
< |
register int i; |
350 |
< |
|
351 |
< |
v[0] = v[1] = v[2] = 0.0; |
352 |
< |
for (i = 0; i < 3; i++) |
353 |
< |
if (n[i] < 0.6 && n[i] > -0.6) |
354 |
< |
break; |
355 |
< |
v[i] = 1.0; |
356 |
< |
fcross(u, v, n); |
357 |
< |
normalize(u); |
358 |
< |
fcross(v, n, u); |
359 |
< |
} |
360 |
< |
|
361 |
< |
|
362 |
< |
void |
363 |
< |
rounddir( /* compute uniform spherical direction */ |
364 |
< |
register FVECT dv, |
365 |
< |
double alt, |
366 |
< |
double azi |
367 |
< |
) |
368 |
< |
{ |
369 |
< |
double d1, d2; |
370 |
< |
|
371 |
< |
dv[2] = 1. - 2.*alt; |
372 |
< |
d1 = sqrt(1. - dv[2]*dv[2]); |
373 |
< |
d2 = 2.*PI * azi; |
374 |
< |
dv[0] = d1*cos(d2); |
375 |
< |
dv[1] = d1*sin(d2); |
376 |
< |
} |
377 |
< |
|
378 |
< |
|
379 |
< |
void |
380 |
< |
flatdir( /* compute uniform hemispherical direction */ |
381 |
< |
register FVECT dv, |
382 |
< |
double alt, |
383 |
< |
double azi |
384 |
< |
) |
385 |
< |
{ |
386 |
< |
double d1, d2; |
387 |
< |
|
388 |
< |
d1 = sqrt(alt); |
389 |
< |
d2 = 2.*PI * azi; |
390 |
< |
dv[0] = d1*cos(d2); |
391 |
< |
dv[1] = d1*sin(d2); |
392 |
< |
dv[2] = sqrt(1. - alt); |
537 |
> |
return(1); |
538 |
|
} |