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