1 |
– |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Convert MGF (Materials and Geometry Format) to Radiance |
6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdio.h> |
9 |
+ |
#include <stdlib.h> |
10 |
|
#include <math.h> |
11 |
|
#include <string.h> |
12 |
+ |
|
13 |
+ |
#include "platform.h" |
14 |
|
#include "mgflib/parser.h" |
15 |
|
#include "color.h" |
16 |
|
#include "tmesh.h" |
23 |
|
|
24 |
|
double emult = 1.; /* emitter multiplier */ |
25 |
|
|
26 |
< |
FILE *matfp = stdout; /* material output file */ |
26 |
> |
FILE *matfp; /* material output file */ |
27 |
|
|
28 |
– |
int r_comment(), r_cone(), r_cyl(), r_face(), r_ies(), r_ring(), r_sph(); |
29 |
– |
char *material(), *object(), *addarg(); |
28 |
|
|
29 |
+ |
int r_comment(int ac, char **av); |
30 |
+ |
int r_cone(int ac, char **av); |
31 |
+ |
int r_cyl(int ac, char **av); |
32 |
+ |
int r_sph(int ac, char **av); |
33 |
+ |
int r_ring(int ac, char **av); |
34 |
+ |
int r_face(int ac, char **av); |
35 |
+ |
int r_ies(int ac, char **av); |
36 |
+ |
char * material(void); |
37 |
+ |
char * object(void); |
38 |
+ |
char * addarg(char *op, char *arg); |
39 |
+ |
void do_tri(char *mat, C_VERTEX *cv1, C_VERTEX *cv2, C_VERTEX *cv3, int iv); |
40 |
+ |
void cvtcolor(COLOR radrgb, register C_COLOR *ciec, double intensity); |
41 |
|
|
42 |
< |
main(argc, argv) /* convert files to stdout */ |
43 |
< |
int argc; |
44 |
< |
char *argv[]; |
42 |
> |
|
43 |
> |
int |
44 |
> |
main( |
45 |
> |
int argc, |
46 |
> |
char *argv[] |
47 |
> |
) |
48 |
|
{ |
49 |
< |
int i, rv; |
49 |
> |
int i; |
50 |
> |
|
51 |
> |
matfp = stdout; |
52 |
> |
/* print out parser version */ |
53 |
> |
printf("## Translated from MGF Version %d.%d\n", MG_VMAJOR, MG_VMINOR); |
54 |
|
/* initialize dispatch table */ |
55 |
|
mg_ehand[MG_E_COMMENT] = r_comment; /* we pass comments */ |
56 |
|
mg_ehand[MG_E_COLOR] = c_hcolor; /* they get color */ |
109 |
|
} |
110 |
|
putchar('\n'); |
111 |
|
if (i == argc) { /* convert stdin */ |
112 |
< |
if ((rv = mg_load(NULL)) != MG_OK) |
112 |
> |
if (mg_load(NULL) != MG_OK) |
113 |
|
exit(1); |
114 |
+ |
if (mg_nunknown) |
115 |
+ |
printf("## %s: %u unknown entities\n", |
116 |
+ |
argv[0], mg_nunknown); |
117 |
|
} else /* convert each file */ |
118 |
|
for ( ; i < argc; i++) { |
119 |
|
printf("## %s %s ##############################\n", |
120 |
|
argv[0], argv[i]); |
121 |
< |
if ((rv = mg_load(argv[i])) != MG_OK) |
121 |
> |
if (mg_load(argv[i]) != MG_OK) |
122 |
|
exit(1); |
123 |
+ |
if (mg_nunknown) { |
124 |
+ |
printf("## %s %s: %u unknown entities\n", |
125 |
+ |
argv[0], argv[i], mg_nunknown); |
126 |
+ |
mg_nunknown = 0; |
127 |
+ |
} |
128 |
|
} |
129 |
|
exit(0); |
130 |
|
userr: |
135 |
|
|
136 |
|
|
137 |
|
int |
138 |
< |
r_comment(ac, av) /* repeat a comment verbatim */ |
139 |
< |
register int ac; |
140 |
< |
register char **av; |
138 |
> |
r_comment( /* repeat a comment verbatim */ |
139 |
> |
register int ac, |
140 |
> |
register char **av |
141 |
> |
) |
142 |
|
{ |
143 |
|
putchar('#'); /* use Radiance comment character */ |
144 |
|
while (--ac) { /* pass through verbatim */ |
151 |
|
|
152 |
|
|
153 |
|
int |
154 |
< |
r_cone(ac, av) /* put out a cone */ |
155 |
< |
int ac; |
156 |
< |
char **av; |
154 |
> |
r_cone( /* put out a cone */ |
155 |
> |
int ac, |
156 |
> |
char **av |
157 |
> |
) |
158 |
|
{ |
159 |
|
static int ncones; |
160 |
|
char *mat; |
180 |
|
if (r2 == 0.) |
181 |
|
return(MG_EILL); |
182 |
|
inv = r2 < 0.; |
183 |
< |
} else if (r2 != 0. && inv ^ r2 < 0.) |
183 |
> |
} else if (r2 != 0. && inv ^ (r2 < 0.)) |
184 |
|
return(MG_EILL); |
185 |
|
if (inv) { |
186 |
|
r1 = -r1; |
200 |
|
|
201 |
|
|
202 |
|
int |
203 |
< |
r_cyl(ac, av) /* put out a cylinder */ |
204 |
< |
int ac; |
205 |
< |
char **av; |
203 |
> |
r_cyl( /* put out a cylinder */ |
204 |
> |
int ac, |
205 |
> |
char **av |
206 |
> |
) |
207 |
|
{ |
208 |
|
static int ncyls; |
209 |
|
char *mat; |
239 |
|
|
240 |
|
|
241 |
|
int |
242 |
< |
r_sph(ac, av) /* put out a sphere */ |
243 |
< |
int ac; |
244 |
< |
char **av; |
242 |
> |
r_sph( /* put out a sphere */ |
243 |
> |
int ac, |
244 |
> |
char **av |
245 |
> |
) |
246 |
|
{ |
247 |
|
static int nsphs; |
248 |
|
char *mat; |
273 |
|
|
274 |
|
|
275 |
|
int |
276 |
< |
r_ring(ac, av) /* put out a ring */ |
277 |
< |
int ac; |
278 |
< |
char **av; |
276 |
> |
r_ring( /* put out a ring */ |
277 |
> |
int ac, |
278 |
> |
char **av |
279 |
> |
) |
280 |
|
{ |
281 |
|
static int nrings; |
282 |
|
char *mat; |
296 |
|
xf_rotvect(norm, cv->n); /* rotate normal */ |
297 |
|
r1 = xf_scale(atof(av[2])); /* scale radii */ |
298 |
|
r2 = xf_scale(atof(av[3])); |
299 |
< |
if (r1 < 0. | r2 <= r1) |
299 |
> |
if ((r1 < 0.) | (r2 <= r1)) |
300 |
|
return(MG_EILL); |
301 |
|
if ((mat = material()) == NULL) /* get material */ |
302 |
|
return(MG_EBADMAT); |
311 |
|
|
312 |
|
|
313 |
|
int |
314 |
< |
r_face(ac, av) /* convert a face */ |
315 |
< |
int ac; |
316 |
< |
char **av; |
314 |
> |
r_face( /* convert a face */ |
315 |
> |
int ac, |
316 |
> |
char **av |
317 |
> |
) |
318 |
|
{ |
319 |
|
static int nfaces; |
320 |
+ |
int myi = invert; |
321 |
|
char *mat; |
322 |
|
register int i; |
323 |
|
register C_VERTEX *cv; |
324 |
|
FVECT v; |
325 |
< |
int rv; |
325 |
> |
|
326 |
|
/* check argument count and type */ |
327 |
|
if (ac < 4) |
328 |
|
return(MG_EARGC); |
329 |
|
if ((mat = material()) == NULL) /* get material */ |
330 |
|
return(MG_EBADMAT); |
331 |
|
if (ac <= 5) { /* check for smoothing */ |
332 |
+ |
C_VERTEX *cva[5]; |
333 |
|
for (i = 1; i < ac; i++) { |
334 |
< |
if ((cv = c_getvert(av[i])) == NULL) |
334 |
> |
if ((cva[i-1] = c_getvert(av[i])) == NULL) |
335 |
|
return(MG_EUNDEF); |
336 |
< |
if (is0vect(cv->n)) |
336 |
> |
if (is0vect(cva[i-1]->n)) |
337 |
|
break; |
338 |
|
} |
339 |
< |
if (i == ac) { /* break into triangles */ |
340 |
< |
do_tri(mat, av[1], av[2], av[3]); |
339 |
> |
if (i < ac) |
340 |
> |
i = ISFLAT; |
341 |
> |
else |
342 |
> |
i = flat_tri(cva[0]->p, cva[1]->p, cva[2]->p, |
343 |
> |
cva[0]->n, cva[1]->n, cva[2]->n); |
344 |
> |
if (i == DEGEN) |
345 |
> |
return(MG_OK); /* degenerate (error?) */ |
346 |
> |
if (i == RVBENT) { |
347 |
> |
myi = !myi; |
348 |
> |
i = ISBENT; |
349 |
> |
} else if (i == RVFLAT) { |
350 |
> |
myi = !myi; |
351 |
> |
i = ISFLAT; |
352 |
> |
} |
353 |
> |
if (i == ISBENT) { /* smoothed triangles */ |
354 |
> |
do_tri(mat, cva[0], cva[1], cva[2], myi); |
355 |
|
if (ac == 5) |
356 |
< |
do_tri(mat, av[3], av[4], av[1]); |
356 |
> |
do_tri(mat, cva[2], cva[3], cva[0], myi); |
357 |
|
return(MG_OK); |
358 |
|
} |
359 |
|
} |
361 |
|
printf("\n%s polygon %sf%d\n", mat, object(), ++nfaces); |
362 |
|
printf("0\n0\n%d\n", 3*(ac-1)); |
363 |
|
for (i = 1; i < ac; i++) { /* get, transform, print each vertex */ |
364 |
< |
if ((cv = c_getvert(av[invert ? ac-i : i])) == NULL) |
364 |
> |
if ((cv = c_getvert(av[myi ? ac-i : i])) == NULL) |
365 |
|
return(MG_EUNDEF); |
366 |
|
xf_xfmpoint(v, cv->p); |
367 |
|
putv(v); |
371 |
|
|
372 |
|
|
373 |
|
int |
374 |
< |
r_ies(ac, av) /* convert an IES luminaire file */ |
375 |
< |
int ac; |
376 |
< |
char **av; |
374 |
> |
r_ies( /* convert an IES luminaire file */ |
375 |
> |
int ac, |
376 |
> |
char **av |
377 |
> |
) |
378 |
|
{ |
379 |
|
int xa0 = 2; |
380 |
|
char combuf[128]; |
386 |
|
if (ac < 2) |
387 |
|
return(MG_EARGC); |
388 |
|
/* construct output file name */ |
389 |
< |
if ((op = strrchr(av[1], '/')) == NULL) |
389 |
> |
if ((op = strrchr(av[1], '/')) != NULL) |
390 |
> |
op++; |
391 |
> |
else |
392 |
|
op = av[1]; |
393 |
|
(void)strcpy(fname, op); |
394 |
|
if ((op = strrchr(fname, '.')) == NULL) |
395 |
|
op = fname + strlen(fname); |
396 |
|
(void)strcpy(op, ".rad"); |
397 |
|
/* see if we need to run ies2rad */ |
398 |
< |
if (access(op, 0) == -1) { |
398 |
> |
if (access(fname, 0) == -1) { |
399 |
|
(void)strcpy(combuf, "ies2rad");/* build ies2rad command */ |
400 |
|
op = combuf + 7; /* get -m option (first) */ |
401 |
|
if (ac-xa0 >= 2 && !strcmp(av[xa0], "-m")) { |
436 |
|
} |
437 |
|
|
438 |
|
|
439 |
< |
do_tri(mat, vn1, vn2, vn3) /* put out smoothed triangle */ |
440 |
< |
char *mat, *vn1, *vn2, *vn3; |
439 |
> |
void |
440 |
> |
do_tri( /* put out smoothed triangle */ |
441 |
> |
char *mat, |
442 |
> |
C_VERTEX *cv1, |
443 |
> |
C_VERTEX *cv2, |
444 |
> |
C_VERTEX *cv3, |
445 |
> |
int iv |
446 |
> |
) |
447 |
|
{ |
448 |
|
static int ntris; |
449 |
|
BARYCCM bvecs; |
450 |
< |
FLOAT bcoor[3][3]; |
451 |
< |
C_VERTEX *cv1, *cv2, *cv3; |
450 |
> |
RREAL bcoor[3][3]; |
451 |
> |
C_VERTEX *cvt; |
452 |
|
FVECT v1, v2, v3; |
453 |
|
FVECT n1, n2, n3; |
454 |
|
register int i; |
455 |
< |
/* the following is repeat code, so assume it's OK */ |
456 |
< |
cv2 = c_getvert(vn2); |
457 |
< |
if (invert) { |
458 |
< |
cv3 = c_getvert(vn1); |
459 |
< |
cv1 = c_getvert(vn3); |
404 |
< |
} else { |
405 |
< |
cv1 = c_getvert(vn1); |
406 |
< |
cv3 = c_getvert(vn3); |
455 |
> |
|
456 |
> |
if (iv) { /* swap vertex order if inverted */ |
457 |
> |
cvt = cv1; |
458 |
> |
cv1 = cv3; |
459 |
> |
cv3 = cvt; |
460 |
|
} |
461 |
|
xf_xfmpoint(v1, cv1->p); |
462 |
|
xf_xfmpoint(v2, cv2->p); |
485 |
|
|
486 |
|
|
487 |
|
char * |
488 |
< |
material() /* get (and print) current material */ |
488 |
> |
material(void) /* get (and print) current material */ |
489 |
|
{ |
490 |
|
char *mname = "mat"; |
491 |
|
COLOR radrgb, c2; |
492 |
|
double d; |
440 |
– |
register int i; |
493 |
|
|
494 |
|
if (c_cmname != NULL) |
495 |
|
mname = c_cmname; |
515 |
|
} |
516 |
|
d = c_cmaterial->rd + c_cmaterial->td + |
517 |
|
c_cmaterial->rs + c_cmaterial->ts; |
518 |
< |
if (d < 0. | d > 1.) |
518 |
> |
if ((d < 0.) | (d > 1.)) |
519 |
|
return(NULL); |
520 |
|
/* check for glass/dielectric */ |
521 |
|
if (c_cmaterial->nr > 1.1 && |
581 |
|
return(mname); |
582 |
|
} |
583 |
|
/* check for plastic */ |
584 |
< |
if (c_cmaterial->rs < .1 && (c_cmaterial->rs < .01 || |
533 |
< |
c_isgrey(&c_cmaterial->rs_c))) { |
584 |
> |
if (c_cmaterial->rs < .1) { |
585 |
|
cvtcolor(radrgb, &c_cmaterial->rd_c, |
586 |
|
c_cmaterial->rd/(1.-c_cmaterial->rs)); |
587 |
|
fprintf(matfp, "\nvoid plastic %s\n0\n0\n", mname); |
604 |
|
} |
605 |
|
|
606 |
|
|
607 |
< |
cvtcolor(radrgb, ciec, intensity) /* convert a CIE XYZ color to RGB */ |
608 |
< |
COLOR radrgb; |
609 |
< |
register C_COLOR *ciec; |
610 |
< |
double intensity; |
607 |
> |
void |
608 |
> |
cvtcolor( /* convert a CIE XYZ color to RGB */ |
609 |
> |
COLOR radrgb, |
610 |
> |
register C_COLOR *ciec, |
611 |
> |
double intensity |
612 |
> |
) |
613 |
|
{ |
614 |
|
static COLOR ciexyz; |
615 |
|
|
622 |
|
|
623 |
|
|
624 |
|
char * |
625 |
< |
object() /* return current object name */ |
625 |
> |
object(void) /* return current object name */ |
626 |
|
{ |
627 |
|
static char objbuf[64]; |
628 |
|
register int i; |
644 |
|
|
645 |
|
|
646 |
|
char * |
647 |
< |
addarg(op, arg) /* add argument and advance pointer */ |
648 |
< |
register char *op, *arg; |
647 |
> |
addarg( /* add argument and advance pointer */ |
648 |
> |
register char *op, |
649 |
> |
register char *arg |
650 |
> |
) |
651 |
|
{ |
652 |
|
*op = ' '; |
653 |
< |
while (*++op = *arg++) |
653 |
> |
while ( (*++op = *arg++) ) |
654 |
|
; |
655 |
|
return(op); |
656 |
|
} |