1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: mkillum2.c,v 2.20 2007/09/18 19:51:07 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Routines to do the actual calculation for mkillum |
6 |
*/ |
7 |
|
8 |
#include <string.h> |
9 |
|
10 |
#include "mkillum.h" |
11 |
#include "face.h" |
12 |
#include "cone.h" |
13 |
#include "random.h" |
14 |
|
15 |
|
16 |
COLORV * distarr = NULL; /* distribution array */ |
17 |
int distsiz = 0; |
18 |
|
19 |
|
20 |
void |
21 |
newdist( /* allocate & clear distribution array */ |
22 |
int siz |
23 |
) |
24 |
{ |
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(RAY *r, int rv) |
46 |
{ |
47 |
COLORV *colp; |
48 |
|
49 |
if (rv == 0) |
50 |
return(0); |
51 |
if (rv < 0) |
52 |
error(USER, "ray tracing process died"); |
53 |
if (r->rno >= distsiz) |
54 |
error(INTERNAL, "bad returned index in process_ray"); |
55 |
colp = &distarr[r->rno * 3]; |
56 |
addcolor(colp, r->rcol); |
57 |
return(1); |
58 |
} |
59 |
|
60 |
|
61 |
void |
62 |
raysamp( /* queue a ray sample */ |
63 |
int ndx, |
64 |
FVECT org, |
65 |
FVECT dir |
66 |
) |
67 |
{ |
68 |
RAY myRay; |
69 |
int rv; |
70 |
|
71 |
if ((ndx < 0) | (ndx >= distsiz)) |
72 |
error(INTERNAL, "bad index in raysamp"); |
73 |
VCOPY(myRay.rorg, org); |
74 |
VCOPY(myRay.rdir, dir); |
75 |
myRay.rmax = .0; |
76 |
rayorigin(&myRay, PRIMARY, NULL, NULL); |
77 |
myRay.rno = ndx; |
78 |
/* queue ray, check result */ |
79 |
process_ray(&myRay, ray_pqueue(&myRay)); |
80 |
} |
81 |
|
82 |
|
83 |
void |
84 |
rayclean() /* finish all pending rays */ |
85 |
{ |
86 |
RAY myRay; |
87 |
|
88 |
while (process_ray(&myRay, ray_presult(&myRay, 0))) |
89 |
; |
90 |
} |
91 |
|
92 |
|
93 |
static void |
94 |
mkaxes( /* compute u and v to go with n */ |
95 |
FVECT u, |
96 |
FVECT v, |
97 |
FVECT n |
98 |
) |
99 |
{ |
100 |
register int i; |
101 |
|
102 |
v[0] = v[1] = v[2] = 0.0; |
103 |
for (i = 0; i < 3; i++) |
104 |
if (n[i] < 0.6 && n[i] > -0.6) |
105 |
break; |
106 |
v[i] = 1.0; |
107 |
fcross(u, v, n); |
108 |
normalize(u); |
109 |
fcross(v, n, u); |
110 |
} |
111 |
|
112 |
|
113 |
static void |
114 |
rounddir( /* compute uniform spherical direction */ |
115 |
register FVECT dv, |
116 |
double alt, |
117 |
double azi |
118 |
) |
119 |
{ |
120 |
double d1, d2; |
121 |
|
122 |
dv[2] = 1. - 2.*alt; |
123 |
d1 = sqrt(1. - dv[2]*dv[2]); |
124 |
d2 = 2.*PI * azi; |
125 |
dv[0] = d1*cos(d2); |
126 |
dv[1] = d1*sin(d2); |
127 |
} |
128 |
|
129 |
|
130 |
static void |
131 |
flatdir( /* compute uniform hemispherical direction */ |
132 |
register FVECT dv, |
133 |
double alt, |
134 |
double azi |
135 |
) |
136 |
{ |
137 |
double d1, d2; |
138 |
|
139 |
d1 = sqrt(alt); |
140 |
d2 = 2.*PI * azi; |
141 |
dv[0] = d1*cos(d2); |
142 |
dv[1] = d1*sin(d2); |
143 |
dv[2] = sqrt(1. - alt); |
144 |
} |
145 |
|
146 |
|
147 |
int |
148 |
my_default( /* default illum action */ |
149 |
OBJREC *ob, |
150 |
struct illum_args *il, |
151 |
char *nm |
152 |
) |
153 |
{ |
154 |
sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"", |
155 |
nm, ofun[ob->otype].funame, ob->oname); |
156 |
error(WARNING, errmsg); |
157 |
printobj(il->altmat, ob); |
158 |
return(1); |
159 |
} |
160 |
|
161 |
|
162 |
int |
163 |
my_face( /* make an illum face */ |
164 |
OBJREC *ob, |
165 |
struct illum_args *il, |
166 |
char *nm |
167 |
) |
168 |
{ |
169 |
#define MAXMISS (5*n*il->nsamps) |
170 |
int dim[2]; |
171 |
int n, nalt, nazi, h, alti; |
172 |
double sp[2], r1, r2; |
173 |
FVECT dn, org, dir; |
174 |
FVECT u, v; |
175 |
double ur[2], vr[2]; |
176 |
MAT4 xfm; |
177 |
int nmisses; |
178 |
FACE *fa; |
179 |
register int i, j; |
180 |
/* get/check arguments */ |
181 |
fa = getface(ob); |
182 |
if (fa->area == 0.0) { |
183 |
freeface(ob); |
184 |
return(my_default(ob, il, nm)); |
185 |
} |
186 |
/* set up sampling */ |
187 |
if (il->sampdens <= 0) { |
188 |
nalt = nazi = 1; /* diffuse assumption */ |
189 |
} else { |
190 |
n = PI * il->sampdens; |
191 |
nalt = sqrt(n/PI) + .5; |
192 |
nazi = PI*nalt + .5; |
193 |
} |
194 |
if (il->sd != NULL) { |
195 |
if (!getBSDF_xfm(xfm, fa->norm, il->udir)) { |
196 |
objerror(ob, WARNING, "illegal up direction"); |
197 |
freeface(ob); |
198 |
return(my_default(ob, il, nm)); |
199 |
} |
200 |
n = il->sd->ninc; |
201 |
} else |
202 |
n = nazi*nalt; |
203 |
newdist(n); |
204 |
/* take first edge >= sqrt(area) */ |
205 |
for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) { |
206 |
u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0]; |
207 |
u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1]; |
208 |
u[2] = VERTEX(fa,i)[2] - VERTEX(fa,j)[2]; |
209 |
if ((r1 = DOT(u,u)) >= fa->area-FTINY) |
210 |
break; |
211 |
} |
212 |
if (i < fa->nv) { /* got one! -- let's align our axes */ |
213 |
r2 = 1.0/sqrt(r1); |
214 |
u[0] *= r2; u[1] *= r2; u[2] *= r2; |
215 |
fcross(v, fa->norm, u); |
216 |
} else /* oh well, we'll just have to wing it */ |
217 |
mkaxes(u, v, fa->norm); |
218 |
/* now, find limits in (u,v) coordinates */ |
219 |
ur[0] = vr[0] = FHUGE; |
220 |
ur[1] = vr[1] = -FHUGE; |
221 |
for (i = 0; i < fa->nv; i++) { |
222 |
r1 = DOT(VERTEX(fa,i),u); |
223 |
if (r1 < ur[0]) ur[0] = r1; |
224 |
if (r1 > ur[1]) ur[1] = r1; |
225 |
r2 = DOT(VERTEX(fa,i),v); |
226 |
if (r2 < vr[0]) vr[0] = r2; |
227 |
if (r2 > vr[1]) vr[1] = r2; |
228 |
} |
229 |
dim[0] = random(); |
230 |
/* sample polygon */ |
231 |
nmisses = 0; |
232 |
for (dim[1] = 0; dim[1] < n; dim[1]++) |
233 |
for (i = 0; i < il->nsamps; i++) { |
234 |
/* random direction */ |
235 |
h = ilhash(dim, 2) + i; |
236 |
if (il->sd != NULL) { |
237 |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
238 |
} else { |
239 |
multisamp(sp, 2, urand(h)); |
240 |
alti = dim[1]/nazi; |
241 |
r1 = (alti + sp[0])/nalt; |
242 |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
243 |
flatdir(dn, r1, r2); |
244 |
for (j = 0; j < 3; j++) |
245 |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - |
246 |
dn[2]*fa->norm[j]; |
247 |
} |
248 |
/* random location */ |
249 |
do { |
250 |
multisamp(sp, 2, urand(h+4862+nmisses)); |
251 |
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
252 |
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
253 |
for (j = 0; j < 3; j++) |
254 |
org[j] = r1*u[j] + r2*v[j] |
255 |
+ fa->offset*fa->norm[j]; |
256 |
} while (!inface(org, fa) && nmisses++ < MAXMISS); |
257 |
if (nmisses > MAXMISS) { |
258 |
objerror(ob, WARNING, "bad aspect"); |
259 |
rayclean(); |
260 |
freeface(ob); |
261 |
return(my_default(ob, il, nm)); |
262 |
} |
263 |
if (il->sd != NULL && DOT(dir, fa->norm) < -FTINY) |
264 |
r1 = -1.0001*il->thick - .0001; |
265 |
else |
266 |
r1 = .0001; |
267 |
for (j = 0; j < 3; j++) |
268 |
org[j] += r1*fa->norm[j]; |
269 |
/* send sample */ |
270 |
raysamp(dim[1], org, dir); |
271 |
} |
272 |
rayclean(); |
273 |
if (il->sd != NULL) /* run distribution through BSDF */ |
274 |
redistribute(il->sd, nalt, nazi, u, v, fa->norm, xfm); |
275 |
/* write out the face and its distribution */ |
276 |
if (average(il, distarr, n)) { |
277 |
if (il->sampdens > 0) |
278 |
flatout(il, distarr, nalt, nazi, u, v, fa->norm); |
279 |
illumout(il, ob); |
280 |
} else |
281 |
printobj(il->altmat, ob); |
282 |
/* clean up */ |
283 |
freeface(ob); |
284 |
return(0); |
285 |
#undef MAXMISS |
286 |
} |
287 |
|
288 |
|
289 |
int |
290 |
my_sphere( /* make an illum sphere */ |
291 |
register OBJREC *ob, |
292 |
struct illum_args *il, |
293 |
char *nm |
294 |
) |
295 |
{ |
296 |
int dim[3]; |
297 |
int n, nalt, nazi; |
298 |
double sp[4], r1, r2, r3; |
299 |
FVECT org, dir; |
300 |
FVECT u, v; |
301 |
register int i, j; |
302 |
/* check arguments */ |
303 |
if (ob->oargs.nfargs != 4) |
304 |
objerror(ob, USER, "bad # of arguments"); |
305 |
/* set up sampling */ |
306 |
if (il->sampdens <= 0) |
307 |
nalt = nazi = 1; |
308 |
else { |
309 |
n = 4.*PI * il->sampdens; |
310 |
nalt = sqrt(2./PI*n) + .5; |
311 |
nazi = PI/2.*nalt + .5; |
312 |
} |
313 |
if (il->sd != NULL) |
314 |
objerror(ob, WARNING, "BSDF ignored"); |
315 |
n = nalt*nazi; |
316 |
newdist(n); |
317 |
dim[0] = random(); |
318 |
/* sample sphere */ |
319 |
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
320 |
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
321 |
for (i = 0; i < il->nsamps; i++) { |
322 |
/* next sample point */ |
323 |
multisamp(sp, 4, urand(ilhash(dim,3)+i)); |
324 |
/* random direction */ |
325 |
r1 = (dim[1] + sp[0])/nalt; |
326 |
r2 = (dim[2] + sp[1] - .5)/nazi; |
327 |
rounddir(dir, r1, r2); |
328 |
/* random location */ |
329 |
mkaxes(u, v, dir); /* yuck! */ |
330 |
r3 = sqrt(sp[2]); |
331 |
r2 = 2.*PI*sp[3]; |
332 |
r1 = r3*ob->oargs.farg[3]*cos(r2); |
333 |
r2 = r3*ob->oargs.farg[3]*sin(r2); |
334 |
r3 = ob->oargs.farg[3]*sqrt(1.01-r3*r3); |
335 |
for (j = 0; j < 3; j++) { |
336 |
org[j] = ob->oargs.farg[j] + r1*u[j] + r2*v[j] + |
337 |
r3*dir[j]; |
338 |
dir[j] = -dir[j]; |
339 |
} |
340 |
/* send sample */ |
341 |
raysamp(dim[1]*nazi+dim[2], org, dir); |
342 |
} |
343 |
rayclean(); |
344 |
/* write out the sphere and its distribution */ |
345 |
if (average(il, distarr, n)) { |
346 |
if (il->sampdens > 0) |
347 |
roundout(il, distarr, nalt, nazi); |
348 |
else |
349 |
objerror(ob, WARNING, "diffuse distribution"); |
350 |
illumout(il, ob); |
351 |
} else |
352 |
printobj(il->altmat, ob); |
353 |
/* clean up */ |
354 |
return(1); |
355 |
} |
356 |
|
357 |
|
358 |
int |
359 |
my_ring( /* make an illum ring */ |
360 |
OBJREC *ob, |
361 |
struct illum_args *il, |
362 |
char *nm |
363 |
) |
364 |
{ |
365 |
int dim[2]; |
366 |
int n, nalt, nazi, alti; |
367 |
double sp[2], r1, r2, r3; |
368 |
int h; |
369 |
FVECT dn, org, dir; |
370 |
FVECT u, v; |
371 |
MAT4 xfm; |
372 |
CONE *co; |
373 |
register int i, j; |
374 |
/* get/check arguments */ |
375 |
co = getcone(ob, 0); |
376 |
/* set up sampling */ |
377 |
if (il->sampdens <= 0) |
378 |
nalt = nazi = 1; |
379 |
else { |
380 |
n = PI * il->sampdens; |
381 |
nalt = sqrt(n/PI) + .5; |
382 |
nazi = PI*nalt + .5; |
383 |
} |
384 |
if (il->sd != NULL) { |
385 |
if (!getBSDF_xfm(xfm, co->ad, il->udir)) { |
386 |
objerror(ob, WARNING, "illegal up direction"); |
387 |
freecone(ob); |
388 |
return(my_default(ob, il, nm)); |
389 |
} |
390 |
n = il->sd->ninc; |
391 |
} else |
392 |
n = nazi*nalt; |
393 |
newdist(n); |
394 |
mkaxes(u, v, co->ad); |
395 |
dim[0] = random(); |
396 |
/* sample disk */ |
397 |
for (dim[1] = 0; dim[1] < n; dim[1]++) |
398 |
for (i = 0; i < il->nsamps; i++) { |
399 |
/* next sample point */ |
400 |
h = ilhash(dim,2) + i; |
401 |
/* random direction */ |
402 |
if (il->sd != NULL) { |
403 |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
404 |
} else { |
405 |
multisamp(sp, 2, urand(h)); |
406 |
alti = dim[1]/nazi; |
407 |
r1 = (alti + sp[0])/nalt; |
408 |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
409 |
flatdir(dn, r1, r2); |
410 |
for (j = 0; j < 3; j++) |
411 |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
412 |
} |
413 |
/* random location */ |
414 |
multisamp(sp, 2, urand(h+8371)); |
415 |
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
416 |
sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
417 |
r2 = 2.*PI*sp[1]; |
418 |
r1 = r3*cos(r2); |
419 |
r2 = r3*sin(r2); |
420 |
if (il->sd != NULL && DOT(dir, co->ad) < -FTINY) |
421 |
r3 = -1.0001*il->thick - .0001; |
422 |
else |
423 |
r3 = .0001; |
424 |
for (j = 0; j < 3; j++) |
425 |
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + |
426 |
r3*co->ad[j]; |
427 |
/* send sample */ |
428 |
raysamp(dim[1], org, dir); |
429 |
} |
430 |
rayclean(); |
431 |
if (il->sd != NULL) /* run distribution through BSDF */ |
432 |
redistribute(il->sd, nalt, nazi, u, v, co->ad, xfm); |
433 |
/* write out the ring and its distribution */ |
434 |
if (average(il, distarr, n)) { |
435 |
if (il->sampdens > 0) |
436 |
flatout(il, distarr, nalt, nazi, u, v, co->ad); |
437 |
illumout(il, ob); |
438 |
} else |
439 |
printobj(il->altmat, ob); |
440 |
/* clean up */ |
441 |
freecone(ob); |
442 |
return(1); |
443 |
} |