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