1 |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Convert MGF (Materials and Geometry Format) to Radiance |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
#include <math.h> |
13 |
#include <string.h> |
14 |
#include "mgflib/parser.h" |
15 |
#include "color.h" |
16 |
#include "tmesh.h" |
17 |
|
18 |
#define putv(v) printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2]) |
19 |
|
20 |
#define isgrey(cxy) ((cxy)->cx > .31 && (cxy)->cx < .35 && \ |
21 |
(cxy)->cy > .31 && (cxy)->cy < .35) |
22 |
|
23 |
#define is0vect(v) ((v)[0] == 0. && (v)[1] == 0. && (v)[2] == 0.) |
24 |
|
25 |
#define BIGFLT 1e8 |
26 |
|
27 |
double glowdist = 1.5*BIGFLT; /* glow test distance */ |
28 |
|
29 |
double emult = 1.; /* emmitter multiplier */ |
30 |
|
31 |
int r_comment(), r_cone(), r_cyl(), r_face(), r_ies(), r_ring(), r_sph(); |
32 |
char *material(), *object(), *addarg(); |
33 |
|
34 |
|
35 |
main(argc, argv) /* convert files to stdout */ |
36 |
int argc; |
37 |
char *argv[]; |
38 |
{ |
39 |
int i, rv; |
40 |
/* initialize dispatch table */ |
41 |
mg_ehand[MG_E_COMMENT] = r_comment; |
42 |
mg_ehand[MG_E_COLOR] = c_hcolor; |
43 |
mg_ehand[MG_E_CONE] = r_cone; |
44 |
mg_ehand[MG_E_CXY] = c_hcolor; |
45 |
mg_ehand[MG_E_CYL] = r_cyl; |
46 |
mg_ehand[MG_E_ED] = c_hmaterial; |
47 |
mg_ehand[MG_E_FACE] = r_face; |
48 |
mg_ehand[MG_E_IES] = r_ies; |
49 |
mg_ehand[MG_E_MATERIAL] = c_hmaterial; |
50 |
mg_ehand[MG_E_NORMAL] = c_hvertex; |
51 |
mg_ehand[MG_E_OBJECT] = obj_handler; |
52 |
mg_ehand[MG_E_POINT] = c_hvertex; |
53 |
mg_ehand[MG_E_RD] = c_hmaterial; |
54 |
mg_ehand[MG_E_RING] = r_ring; |
55 |
mg_ehand[MG_E_RS] = c_hmaterial; |
56 |
mg_ehand[MG_E_SPH] = r_sph; |
57 |
mg_ehand[MG_E_TD] = c_hmaterial; |
58 |
mg_ehand[MG_E_TS] = c_hmaterial; |
59 |
mg_ehand[MG_E_VERTEX] = c_hvertex; |
60 |
mg_ehand[MG_E_XF] = xf_handler; |
61 |
mg_init(); /* initialize the parser */ |
62 |
/* get options & print header */ |
63 |
printf("## %s", argv[0]); |
64 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) { |
65 |
printf(" %s", argv[i]); |
66 |
switch (argv[i][1]) { |
67 |
case 'g': /* glow distance (meters) */ |
68 |
if (argv[i][2] || badarg(argc-i, argv+i, "f")) |
69 |
goto userr; |
70 |
glowdist = atof(argv[++i]); |
71 |
printf(" %s", argv[i]); |
72 |
break; |
73 |
case 'e': /* emitter multiplier */ |
74 |
if (argv[i][2] || badarg(argc-i, argv+i, "f")) |
75 |
goto userr; |
76 |
emult = atof(argv[++i]); |
77 |
printf(" %s", argv[i]); |
78 |
break; |
79 |
default: |
80 |
goto userr; |
81 |
} |
82 |
} |
83 |
putchar('\n'); |
84 |
if (i == argc) { /* convert stdin */ |
85 |
if ((rv = mg_load(NULL)) != MG_OK) |
86 |
exit(1); |
87 |
} else /* convert each file */ |
88 |
for ( ; i < argc; i++) { |
89 |
printf("## %s %s ##############################\n", |
90 |
argv[0], argv[i]); |
91 |
if ((rv = mg_load(argv[i])) != MG_OK) |
92 |
exit(1); |
93 |
} |
94 |
exit(0); |
95 |
userr: |
96 |
fprintf(stderr, "Usage: %s [-g dist][-m mult] [file.mgf] ..\n", |
97 |
argv[0]); |
98 |
exit(1); |
99 |
} |
100 |
|
101 |
|
102 |
int |
103 |
r_comment(ac, av) /* repeat a comment verbatim */ |
104 |
register int ac; |
105 |
register char **av; |
106 |
{ |
107 |
fputs("\n#", stdout); /* use Radiance comment character */ |
108 |
while (--ac) { |
109 |
putchar(' '); |
110 |
fputs(*++av, stdout); |
111 |
} |
112 |
putchar('\n'); |
113 |
return(MG_OK); |
114 |
} |
115 |
|
116 |
|
117 |
int |
118 |
r_cone(ac, av) /* put out a cone */ |
119 |
int ac; |
120 |
char **av; |
121 |
{ |
122 |
static int ncones; |
123 |
char *mat; |
124 |
double r1, r2; |
125 |
C_VERTEX *cv1, *cv2; |
126 |
FVECT p1, p2; |
127 |
int inv; |
128 |
|
129 |
if (ac != 5) |
130 |
return(MG_EARGC); |
131 |
if (!isflt(av[2]) || !isflt(av[4])) |
132 |
return(MG_ETYPE); |
133 |
if ((cv1 = c_getvert(av[1])) == NULL || |
134 |
(cv2 = c_getvert(av[3])) == NULL) |
135 |
return(MG_EUNDEF); |
136 |
xf_xfmpoint(p1, cv1->p); |
137 |
xf_xfmpoint(p2, cv2->p); |
138 |
r1 = xf_scale(atof(av[2])); |
139 |
r2 = xf_scale(atof(av[4])); |
140 |
inv = r1 < 0.; |
141 |
if (r1 == 0.) { |
142 |
if (r2 == 0.) |
143 |
return(MG_EILL); |
144 |
inv = r2 < 0.; |
145 |
} else if (r2 != 0. && inv ^ r2 < 0.) |
146 |
return(MG_EILL); |
147 |
if (inv) { |
148 |
r1 = -r1; |
149 |
r2 = -r2; |
150 |
} |
151 |
if ((mat = material()) == NULL) |
152 |
return(MG_EBADMAT); |
153 |
printf("\n%s %s %sc%d\n", mat, inv ? "cup" : "cone", |
154 |
object(), ++ncones); |
155 |
printf("0\n0\n8\n"); |
156 |
putv(p1); |
157 |
putv(p2); |
158 |
printf("%18.12g %18.12g\n", r1, r2); |
159 |
return(MG_OK); |
160 |
} |
161 |
|
162 |
|
163 |
int |
164 |
r_cyl(ac, av) /* put out a cylinder */ |
165 |
int ac; |
166 |
char **av; |
167 |
{ |
168 |
static int ncyls; |
169 |
char *mat; |
170 |
double rad; |
171 |
C_VERTEX *cv1, *cv2; |
172 |
FVECT p1, p2; |
173 |
int inv; |
174 |
|
175 |
if (ac != 4) |
176 |
return(MG_EARGC); |
177 |
if (!isflt(av[2])) |
178 |
return(MG_ETYPE); |
179 |
if ((cv1 = c_getvert(av[1])) == NULL || |
180 |
(cv2 = c_getvert(av[3])) == NULL) |
181 |
return(MG_EUNDEF); |
182 |
xf_xfmpoint(p1, cv1->p); |
183 |
xf_xfmpoint(p2, cv2->p); |
184 |
rad = xf_scale(atof(av[2])); |
185 |
if ((inv = rad < 0.)) |
186 |
rad = -rad; |
187 |
if ((mat = material()) == NULL) |
188 |
return(MG_EBADMAT); |
189 |
printf("\n%s %s %scy%d\n", mat, inv ? "tube" : "cylinder", |
190 |
object(), ++ncyls); |
191 |
printf("0\n0\n7\n"); |
192 |
putv(p1); |
193 |
putv(p2); |
194 |
printf("%18.12g\n", rad); |
195 |
return(MG_OK); |
196 |
} |
197 |
|
198 |
|
199 |
int |
200 |
r_sph(ac, av) /* put out a sphere */ |
201 |
int ac; |
202 |
char **av; |
203 |
{ |
204 |
static int nsphs; |
205 |
char *mat; |
206 |
double rad; |
207 |
C_VERTEX *cv; |
208 |
FVECT cent; |
209 |
int inv; |
210 |
|
211 |
if (ac != 3) |
212 |
return(MG_EARGC); |
213 |
if (!isflt(av[2])) |
214 |
return(MG_ETYPE); |
215 |
if ((cv = c_getvert(av[1])) == NULL) |
216 |
return(MG_EUNDEF); |
217 |
xf_xfmpoint(cent, cv->p); |
218 |
rad = xf_scale(atof(av[2])); |
219 |
if ((inv = rad < 0.)) |
220 |
rad = -rad; |
221 |
if ((mat = material()) == NULL) |
222 |
return(MG_EBADMAT); |
223 |
printf("\n%s %s %ss%d\n", mat, inv ? "bubble" : "sphere", |
224 |
object(), ++nsphs); |
225 |
printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n", |
226 |
cent[0], cent[1], cent[2], rad); |
227 |
return(MG_OK); |
228 |
} |
229 |
|
230 |
|
231 |
int |
232 |
r_ring(ac, av) /* put out a ring */ |
233 |
int ac; |
234 |
char **av; |
235 |
{ |
236 |
static int nrings; |
237 |
char *mat; |
238 |
double r1, r2; |
239 |
C_VERTEX *cv; |
240 |
FVECT cent, norm; |
241 |
|
242 |
if (ac != 4) |
243 |
return(MG_EARGC); |
244 |
if (!isflt(av[2]) || !isflt(av[3])) |
245 |
return(MG_ETYPE); |
246 |
if ((cv = c_getvert(av[1])) == NULL) |
247 |
return(MG_EUNDEF); |
248 |
if (is0vect(cv->n)) |
249 |
return(MG_EILL); |
250 |
xf_xfmpoint(cent, cv->p); |
251 |
xf_rotvect(norm, cv->n); |
252 |
r1 = xf_scale(atof(av[2])); |
253 |
r2 = xf_scale(atof(av[3])); |
254 |
if (r1 < 0. | r2 <= r1) |
255 |
return(MG_EILL); |
256 |
if ((mat = material()) == NULL) |
257 |
return(MG_EBADMAT); |
258 |
printf("\n%s ring %sr%d\n", mat, object(), ++nrings); |
259 |
printf("0\n0\n8\n"); |
260 |
putv(cent); |
261 |
putv(norm); |
262 |
printf("%18.12g %18.12g\n", r1, r2); |
263 |
return(MG_OK); |
264 |
} |
265 |
|
266 |
|
267 |
int |
268 |
r_face(ac, av) /* convert a face */ |
269 |
int ac; |
270 |
char **av; |
271 |
{ |
272 |
static int nfaces; |
273 |
char *mat; |
274 |
register int i; |
275 |
register C_VERTEX *cv; |
276 |
FVECT v; |
277 |
int rv; |
278 |
|
279 |
if (ac < 4) |
280 |
return(MG_EARGC); |
281 |
if ((mat = material()) == NULL) |
282 |
return(MG_EBADMAT); |
283 |
if (ac < 5) { /* check for surface normals */ |
284 |
for (i = 1; i < ac; i++) { |
285 |
if ((cv = c_getvert(av[i])) == NULL) |
286 |
return(MG_EUNDEF); |
287 |
if (is0vect(cv->n)) |
288 |
break; |
289 |
} |
290 |
if (i == ac) { /* break into triangles */ |
291 |
do_tri(mat, av[1], av[2], av[3]); |
292 |
if (ac == 5) |
293 |
do_tri(mat, av[3], av[4], av[1]); |
294 |
return(MG_OK); |
295 |
} |
296 |
} |
297 |
printf("\n%s polygon %sf%d\n", mat, object(), ++nfaces); |
298 |
printf("0\n0\n%d\n", 3*(ac-1)); |
299 |
for (i = 1; i < ac; i++) { |
300 |
if ((cv = c_getvert(av[i])) == NULL) |
301 |
return(MG_EUNDEF); |
302 |
xf_xfmpoint(v, cv->p); |
303 |
putv(v); |
304 |
} |
305 |
return(MG_OK); |
306 |
} |
307 |
|
308 |
|
309 |
r_ies(ac, av) /* convert an IES luminaire file */ |
310 |
int ac; |
311 |
char **av; |
312 |
{ |
313 |
int xa0 = 2; |
314 |
char combuf[72]; |
315 |
char fname[48]; |
316 |
char *oname; |
317 |
register char *op; |
318 |
register int i; |
319 |
|
320 |
if (ac < 2) |
321 |
return(MG_EARGC); |
322 |
(void)strcpy(combuf, "ies2rad"); |
323 |
op = combuf + 7; |
324 |
if (ac-xa0 >= 2 && !strcmp(av[xa0], "-m")) { |
325 |
if (!isflt(av[xa0+1])) |
326 |
return(MG_ETYPE); |
327 |
op = addarg(addarg(op, "-m"), av[xa0+1]); |
328 |
xa0 += 2; |
329 |
} |
330 |
if (access(av[1], 0) == -1) |
331 |
return(MG_ENOFILE); |
332 |
*op++ = ' '; /* IES filename goes last */ |
333 |
(void)strcpy(op, av[1]); |
334 |
system(combuf); /* run ies2rad */ |
335 |
/* now let's find the output file */ |
336 |
if ((op = strrchr(av[1], '/')) == NULL) |
337 |
op = av[1]; |
338 |
(void)strcpy(fname, op); |
339 |
if ((op = strrchr(fname, '.')) == NULL) |
340 |
op = fname + strlen(fname); |
341 |
(void)strcpy(op, ".rad"); |
342 |
if (access(fname, 0) == -1) |
343 |
return(MG_EINCL); |
344 |
/* put out xform command */ |
345 |
printf("\n!xform"); |
346 |
oname = object(); |
347 |
if (*oname) |
348 |
printf(" -n %s", oname); |
349 |
for (i = xa0; i < ac; i++) |
350 |
printf(" %s", av[i]); |
351 |
if (ac > xa0 && xf_argc > 0) |
352 |
printf(" -i 1"); |
353 |
for (i = 0; i < xf_argc; i++) |
354 |
printf(" %s", xf_argv[i]); |
355 |
printf(" %s\n", fname); |
356 |
return(MG_OK); |
357 |
} |
358 |
|
359 |
|
360 |
do_tri(mat, vn1, vn2, vn3) /* put out smoothed triangle */ |
361 |
char *mat, *vn1, *vn2, *vn3; |
362 |
{ |
363 |
static int ntris; |
364 |
char *mod = mat; |
365 |
BARYCCM bvecs; |
366 |
FLOAT bcoor[3][3]; |
367 |
C_VERTEX *cv1, *cv2, *cv3; |
368 |
FVECT v1, v2, v3; |
369 |
FVECT n1, n2, n3; |
370 |
register int i; |
371 |
/* the following is repeat code, so assume it's OK */ |
372 |
cv1 = c_getvert(vn1); |
373 |
cv2 = c_getvert(vn2); |
374 |
cv3 = c_getvert(vn3); |
375 |
xf_xfmpoint(v1, cv1->p); |
376 |
xf_xfmpoint(v2, cv2->p); |
377 |
xf_xfmpoint(v3, cv3->p); |
378 |
if (comp_baryc(&bvecs, v1, v2, v3) == 0) { |
379 |
printf("\n%s texfunc T-nor\n", mod); |
380 |
mod = "T-nor"; |
381 |
printf("4 dx dy dz %s\n0\n", TCALNAME); |
382 |
xf_rotvect(n1, cv1->n); |
383 |
xf_rotvect(n2, cv2->n); |
384 |
xf_rotvect(n3, cv3->n); |
385 |
for (i = 0; i < 3; i++) { |
386 |
bcoor[i][0] = n1[i]; |
387 |
bcoor[i][1] = n2[i]; |
388 |
bcoor[i][2] = n3[i]; |
389 |
} |
390 |
put_baryc(&bvecs, bcoor, 3); |
391 |
} |
392 |
printf("\n%s polygon %st%d\n", mod, object(), ++ntris); |
393 |
printf("0\n0\n9\n"); |
394 |
putv(v1); |
395 |
putv(v2); |
396 |
putv(v3); |
397 |
} |
398 |
|
399 |
|
400 |
char * |
401 |
material() /* get (and print) current material */ |
402 |
{ |
403 |
char *mname = "mat"; |
404 |
COLOR radrgb, c2; |
405 |
double d; |
406 |
register int i; |
407 |
|
408 |
if (c_cmaterial->name != NULL) |
409 |
mname = c_cmaterial->name; |
410 |
if (!c_cmaterial->clock) |
411 |
return(mname); /* already current */ |
412 |
/* else update output */ |
413 |
c_cmaterial->clock = 0; |
414 |
if (c_cmaterial->ed > .1) { /* emitter */ |
415 |
cvtcolor(radrgb, &c_cmaterial->ed_c, |
416 |
emult*c_cmaterial->ed/WHTEFFICACY); |
417 |
if (glowdist < BIGFLT) { /* do a glow */ |
418 |
printf("\nvoid glow %s\n0\n0\n", mname); |
419 |
printf("4 %f %f %f %f\n", colval(radrgb,RED), |
420 |
colval(radrgb,GRN), |
421 |
colval(radrgb,BLU), glowdist); |
422 |
} else { |
423 |
printf("\nvoid light %s\n0\n0\n", mname); |
424 |
printf("3 %f %f %f\n", colval(radrgb,RED), |
425 |
colval(radrgb,GRN), |
426 |
colval(radrgb,BLU)); |
427 |
} |
428 |
return(mname); |
429 |
} |
430 |
d = c_cmaterial->rd + c_cmaterial->td + |
431 |
c_cmaterial->rs + c_cmaterial->ts; |
432 |
if (d <= 0. | d >= 1.) |
433 |
return(NULL); |
434 |
if (c_cmaterial->td > .01 || c_cmaterial->ts > .01) { /* trans */ |
435 |
double ts, a5, a6; |
436 |
|
437 |
ts = sqrt(c_cmaterial->ts); /* because we use 2 sides */ |
438 |
/* average colors */ |
439 |
d = c_cmaterial->rd + c_cmaterial->td + ts; |
440 |
cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd/d); |
441 |
cvtcolor(c2, &c_cmaterial->td_c, c_cmaterial->td/d); |
442 |
addcolor(radrgb, c2); |
443 |
cvtcolor(c2, &c_cmaterial->ts_c, ts/d); |
444 |
addcolor(radrgb, c2); |
445 |
if (c_cmaterial->rs + ts > .0001) |
446 |
a5 = (c_cmaterial->rs*c_cmaterial->rs_a + |
447 |
ts*.5*c_cmaterial->ts_a) / |
448 |
(c_cmaterial->rs + ts); |
449 |
a6 = (c_cmaterial->td + ts) / |
450 |
(c_cmaterial->rd + c_cmaterial->td + ts); |
451 |
if (a6 < .999) { |
452 |
d = c_cmaterial->rd/(1. - c_cmaterial->rs)/(1. - a6); |
453 |
scalecolor(radrgb, d); |
454 |
} |
455 |
printf("\nvoid trans %s\n0\n0\n", mname); |
456 |
printf("7 %f %f %f\n", colval(radrgb,RED), |
457 |
colval(radrgb,GRN), colval(radrgb,BLU)); |
458 |
printf("\t%f %f %f %f\n", c_cmaterial->rs, a5, a6, |
459 |
ts/(ts + c_cmaterial->td)); |
460 |
return(mname); |
461 |
} |
462 |
if (c_cmaterial->rs < .01 || isgrey(&c_cmaterial->rs_c)) { /* plastic */ |
463 |
if (c_cmaterial->rs > .999) |
464 |
cvtcolor(radrgb, &c_cmaterial->rd_c, 1.); |
465 |
else |
466 |
cvtcolor(radrgb, &c_cmaterial->rd_c, |
467 |
c_cmaterial->rd/(1.-c_cmaterial->rs)); |
468 |
printf("\nvoid plastic %s\n0\n0\n", mname); |
469 |
printf("5 %f %f %f %f %f\n", colval(radrgb,RED), |
470 |
colval(radrgb,GRN), colval(radrgb,BLU), |
471 |
c_cmaterial->rs, c_cmaterial->rs_a); |
472 |
return(mname); |
473 |
} |
474 |
/* else it's metal */ |
475 |
d = c_cmaterial->rd + c_cmaterial->rs; /* average colors */ |
476 |
cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd/d); |
477 |
cvtcolor(c2, &c_cmaterial->rs_c, c_cmaterial->rs/d); |
478 |
addcolor(radrgb, c2); |
479 |
if (c_cmaterial->rs < .999) { |
480 |
d = c_cmaterial->rd/(1. - c_cmaterial->rs); |
481 |
scalecolor(radrgb, d); |
482 |
} |
483 |
printf("\nvoid metal %s\n0\n0\n", mname); |
484 |
printf("5 %f %f %f %f %f\n", colval(radrgb,RED), |
485 |
colval(radrgb,GRN), colval(radrgb,BLU), |
486 |
c_cmaterial->rs, c_cmaterial->rs_a); |
487 |
return(mname); |
488 |
} |
489 |
|
490 |
|
491 |
cvtcolor(radrgb, ciec, intensity) /* convert a CIE color to Radiance */ |
492 |
COLOR radrgb; |
493 |
register C_COLOR *ciec; |
494 |
double intensity; |
495 |
{ |
496 |
static COLOR ciexyz; |
497 |
|
498 |
ciexyz[1] = intensity; |
499 |
ciexyz[0] = ciec->cx/ciec->cy*ciexyz[1]; |
500 |
ciexyz[2] = ciexyz[1]*(1./ciec->cy - 1.) - ciexyz[0]; |
501 |
cie_rgb(radrgb, ciexyz); |
502 |
} |
503 |
|
504 |
|
505 |
char * |
506 |
object() /* return current object name */ |
507 |
{ |
508 |
static char objbuf[64]; |
509 |
register int i; |
510 |
register char *cp; |
511 |
int len; |
512 |
|
513 |
i = obj_nnames - sizeof(objbuf)/16; |
514 |
if (i < 0) |
515 |
i = 0; |
516 |
for (cp = objbuf; i < obj_nnames && |
517 |
cp + (len=strlen(obj_name[i])) < objbuf+sizeof(objbuf)-1; |
518 |
i++, *cp++ = '.') { |
519 |
strcpy(cp, obj_name[i]); |
520 |
cp += len; |
521 |
} |
522 |
*cp = '\0'; |
523 |
return(objbuf); |
524 |
} |
525 |
|
526 |
|
527 |
char * |
528 |
addarg(op, arg) /* add argument and advance pointer */ |
529 |
register char *op, *arg; |
530 |
{ |
531 |
*op = ' '; |
532 |
while (*++op = *arg++) |
533 |
; |
534 |
return(op); |
535 |
} |