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