| 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 | < | #include "mgflib/parser.h" | 
| 12 | > |  | 
| 13 | > | #include "platform.h" | 
| 14 | > | #include "mgf_parser.h" | 
| 15 |  | #include "color.h" | 
| 16 |  | #include "tmesh.h" | 
| 17 |  |  | 
| 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; | 
| 56 | < | mg_ehand[MG_E_COLOR] = c_hcolor; | 
| 57 | < | mg_ehand[MG_E_CONE] = r_cone; | 
| 58 | < | mg_ehand[MG_E_CMIX] = c_hcolor; | 
| 59 | < | mg_ehand[MG_E_CSPEC] = c_hcolor; | 
| 60 | < | mg_ehand[MG_E_CXY] = c_hcolor; | 
| 61 | < | mg_ehand[MG_E_CYL] = r_cyl; | 
| 62 | < | mg_ehand[MG_E_ED] = c_hmaterial; | 
| 63 | < | mg_ehand[MG_E_FACE] = r_face; | 
| 64 | < | mg_ehand[MG_E_IES] = r_ies; | 
| 65 | < | mg_ehand[MG_E_MATERIAL] = c_hmaterial; | 
| 66 | < | mg_ehand[MG_E_NORMAL] = c_hvertex; | 
| 67 | < | mg_ehand[MG_E_OBJECT] = obj_handler; | 
| 68 | < | mg_ehand[MG_E_POINT] = c_hvertex; | 
| 69 | < | mg_ehand[MG_E_RD] = c_hmaterial; | 
| 70 | < | mg_ehand[MG_E_RING] = r_ring; | 
| 71 | < | mg_ehand[MG_E_RS] = c_hmaterial; | 
| 72 | < | mg_ehand[MG_E_SIDES] = c_hmaterial; | 
| 73 | < | mg_ehand[MG_E_SPH] = r_sph; | 
| 74 | < | mg_ehand[MG_E_TD] = c_hmaterial; | 
| 75 | < | mg_ehand[MG_E_TS] = c_hmaterial; | 
| 76 | < | mg_ehand[MG_E_VERTEX] = c_hvertex; | 
| 77 | < | mg_ehand[MG_E_XF] = xf_handler; | 
| 55 | > | mg_ehand[MG_E_COMMENT] = r_comment;     /* we pass comments */ | 
| 56 | > | mg_ehand[MG_E_COLOR] = c_hcolor;        /* they get color */ | 
| 57 | > | mg_ehand[MG_E_CONE] = r_cone;           /* we do cones */ | 
| 58 | > | mg_ehand[MG_E_CMIX] = c_hcolor;         /* they mix colors */ | 
| 59 | > | mg_ehand[MG_E_CSPEC] = c_hcolor;        /* they get spectra */ | 
| 60 | > | mg_ehand[MG_E_CXY] = c_hcolor;          /* they get chromaticities */ | 
| 61 | > | mg_ehand[MG_E_CCT] = c_hcolor;          /* they get color temp's */ | 
| 62 | > | mg_ehand[MG_E_CYL] = r_cyl;             /* we do cylinders */ | 
| 63 | > | mg_ehand[MG_E_ED] = c_hmaterial;        /* they get emission */ | 
| 64 | > | mg_ehand[MG_E_FACE] = r_face;           /* we do faces */ | 
| 65 | > | mg_ehand[MG_E_IES] = r_ies;             /* we do IES files */ | 
| 66 | > | mg_ehand[MG_E_IR] = c_hmaterial;        /* they get refractive index */ | 
| 67 | > | mg_ehand[MG_E_MATERIAL] = c_hmaterial;  /* they get materials */ | 
| 68 | > | mg_ehand[MG_E_NORMAL] = c_hvertex;      /* they get normals */ | 
| 69 | > | mg_ehand[MG_E_OBJECT] = obj_handler;    /* they track object names */ | 
| 70 | > | mg_ehand[MG_E_POINT] = c_hvertex;       /* they get points */ | 
| 71 | > | mg_ehand[MG_E_RD] = c_hmaterial;        /* they get diffuse refl. */ | 
| 72 | > | mg_ehand[MG_E_RING] = r_ring;           /* we do rings */ | 
| 73 | > | mg_ehand[MG_E_RS] = c_hmaterial;        /* they get specular refl. */ | 
| 74 | > | mg_ehand[MG_E_SIDES] = c_hmaterial;     /* they get # sides */ | 
| 75 | > | mg_ehand[MG_E_SPH] = r_sph;             /* we do spheres */ | 
| 76 | > | mg_ehand[MG_E_TD] = c_hmaterial;        /* they get diffuse trans. */ | 
| 77 | > | mg_ehand[MG_E_TS] = c_hmaterial;        /* they get specular trans. */ | 
| 78 | > | mg_ehand[MG_E_VERTEX] = c_hvertex;      /* they get vertices */ | 
| 79 | > | mg_ehand[MG_E_XF] = xf_handler;         /* they track transforms */ | 
| 80 |  | mg_init();              /* initialize the parser */ | 
| 81 | < | /* get options & print header */ | 
| 81 | > | /* get our options & print header */ | 
| 82 |  | printf("## %s", argv[0]); | 
| 83 |  | for (i = 1; i < argc && argv[i][0] == '-'; i++) { | 
| 84 |  | printf(" %s", argv[i]); | 
| 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) { | 
| 144 | > | while (--ac) {                  /* pass through verbatim */ | 
| 145 |  | putchar(' '); | 
| 146 |  | fputs(*++av, stdout); | 
| 147 |  | } | 
| 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; | 
| 162 |  | C_VERTEX        *cv1, *cv2; | 
| 163 |  | FVECT   p1, p2; | 
| 164 |  | int     inv; | 
| 165 | < |  | 
| 165 | > | /* check argument count and type */ | 
| 166 |  | if (ac != 5) | 
| 167 |  | return(MG_EARGC); | 
| 168 |  | if (!isflt(av[2]) || !isflt(av[4])) | 
| 169 |  | return(MG_ETYPE); | 
| 170 | + | /* get the endpoint vertices */ | 
| 171 |  | if ((cv1 = c_getvert(av[1])) == NULL || | 
| 172 |  | (cv2 = c_getvert(av[3])) == NULL) | 
| 173 |  | return(MG_EUNDEF); | 
| 174 | < | xf_xfmpoint(p1, cv1->p); | 
| 174 | > | xf_xfmpoint(p1, cv1->p);        /* transform endpoints */ | 
| 175 |  | xf_xfmpoint(p2, cv2->p); | 
| 176 | < | r1 = xf_scale(atof(av[2])); | 
| 176 | > | r1 = xf_scale(atof(av[2]));     /* scale radii */ | 
| 177 |  | r2 = xf_scale(atof(av[4])); | 
| 178 | < | inv = r1 < 0.; | 
| 179 | < | if (r1 == 0.) { | 
| 178 | > | inv = r1 < 0.;                  /* check for inverted cone */ | 
| 179 | > | if (r1 == 0.) {                 /* check for illegal radii */ | 
| 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; | 
| 187 |  | r2 = -r2; | 
| 188 |  | } | 
| 189 | < | if ((mat = material()) == NULL) | 
| 189 | > | if ((mat = material()) == NULL) /* get material */ | 
| 190 |  | return(MG_EBADMAT); | 
| 191 | + | /* spit the sucker out */ | 
| 192 |  | printf("\n%s %s %sc%d\n", mat, inv ? "cup" : "cone", | 
| 193 |  | object(), ++ncones); | 
| 194 |  | printf("0\n0\n8\n"); | 
| 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; | 
| 211 |  | C_VERTEX        *cv1, *cv2; | 
| 212 |  | FVECT   p1, p2; | 
| 213 |  | int     inv; | 
| 214 | < |  | 
| 214 | > | /* check argument count and type */ | 
| 215 |  | if (ac != 4) | 
| 216 |  | return(MG_EARGC); | 
| 217 |  | if (!isflt(av[2])) | 
| 218 |  | return(MG_ETYPE); | 
| 219 | + | /* get the endpoint vertices */ | 
| 220 |  | if ((cv1 = c_getvert(av[1])) == NULL || | 
| 221 |  | (cv2 = c_getvert(av[3])) == NULL) | 
| 222 |  | return(MG_EUNDEF); | 
| 223 | < | xf_xfmpoint(p1, cv1->p); | 
| 223 | > | xf_xfmpoint(p1, cv1->p);        /* transform endpoints */ | 
| 224 |  | xf_xfmpoint(p2, cv2->p); | 
| 225 | < | rad = xf_scale(atof(av[2])); | 
| 226 | < | if ((inv = rad < 0.)) | 
| 225 | > | rad = xf_scale(atof(av[2]));    /* scale radius */ | 
| 226 | > | if ((inv = rad < 0.))           /* check for inverted cylinder */ | 
| 227 |  | rad = -rad; | 
| 228 | < | if ((mat = material()) == NULL) | 
| 228 | > | if ((mat = material()) == NULL) /* get material */ | 
| 229 |  | return(MG_EBADMAT); | 
| 230 | + | /* spit out the primitive */ | 
| 231 |  | printf("\n%s %s %scy%d\n", mat, inv ? "tube" : "cylinder", | 
| 232 |  | object(), ++ncyls); | 
| 233 |  | printf("0\n0\n7\n"); | 
| 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; | 
| 250 |  | C_VERTEX        *cv; | 
| 251 |  | FVECT   cent; | 
| 252 |  | int     inv; | 
| 253 | < |  | 
| 253 | > | /* check argument count and type */ | 
| 254 |  | if (ac != 3) | 
| 255 |  | return(MG_EARGC); | 
| 256 |  | if (!isflt(av[2])) | 
| 257 |  | return(MG_ETYPE); | 
| 258 | < | if ((cv = c_getvert(av[1])) == NULL) | 
| 258 | > | if ((cv = c_getvert(av[1])) == NULL)    /* get center vertex */ | 
| 259 |  | return(MG_EUNDEF); | 
| 260 | < | xf_xfmpoint(cent, cv->p); | 
| 261 | < | rad = xf_scale(atof(av[2])); | 
| 262 | < | if ((inv = rad < 0.)) | 
| 260 | > | xf_xfmpoint(cent, cv->p);               /* transform center */ | 
| 261 | > | rad = xf_scale(atof(av[2]));            /* scale radius */ | 
| 262 | > | if ((inv = rad < 0.))                   /* check for inversion */ | 
| 263 |  | rad = -rad; | 
| 264 | < | if ((mat = material()) == NULL) | 
| 264 | > | if ((mat = material()) == NULL)         /* get material */ | 
| 265 |  | return(MG_EBADMAT); | 
| 266 | + | /* spit out primitive */ | 
| 267 |  | printf("\n%s %s %ss%d\n", mat, inv ? "bubble" : "sphere", | 
| 268 |  | object(), ++nsphs); | 
| 269 |  | printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n", | 
| 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; | 
| 283 |  | double  r1, r2; | 
| 284 |  | C_VERTEX        *cv; | 
| 285 |  | FVECT   cent, norm; | 
| 286 | < |  | 
| 286 | > | /* check argument count and type */ | 
| 287 |  | if (ac != 4) | 
| 288 |  | return(MG_EARGC); | 
| 289 |  | if (!isflt(av[2]) || !isflt(av[3])) | 
| 290 |  | return(MG_ETYPE); | 
| 291 | < | if ((cv = c_getvert(av[1])) == NULL) | 
| 291 | > | if ((cv = c_getvert(av[1])) == NULL)    /* get center vertex */ | 
| 292 |  | return(MG_EUNDEF); | 
| 293 | < | if (is0vect(cv->n)) | 
| 293 | > | if (is0vect(cv->n))                     /* make sure we have normal */ | 
| 294 |  | return(MG_EILL); | 
| 295 | < | xf_xfmpoint(cent, cv->p); | 
| 296 | < | xf_rotvect(norm, cv->n); | 
| 297 | < | r1 = xf_scale(atof(av[2])); | 
| 295 | > | xf_xfmpoint(cent, cv->p);               /* transform center */ | 
| 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) | 
| 301 | > | if ((mat = material()) == NULL)         /* get material */ | 
| 302 |  | return(MG_EBADMAT); | 
| 303 | + | /* spit out primitive */ | 
| 304 |  | printf("\n%s ring %sr%d\n", mat, object(), ++nrings); | 
| 305 |  | printf("0\n0\n8\n"); | 
| 306 |  | putv(cent); | 
| 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; | 
| 285 | – | int     rv; | 
| 325 |  |  | 
| 326 | + | /* check argument count and type */ | 
| 327 |  | if (ac < 4) | 
| 328 |  | return(MG_EARGC); | 
| 329 | < | if ((mat = material()) == NULL) | 
| 329 | > | if ((mat = material()) == NULL) /* get material */ | 
| 330 |  | return(MG_EBADMAT); | 
| 331 | < | if (ac <= 5) {                          /* check for surface normals */ | 
| 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 |  | } | 
| 360 | + | /* spit out unsmoothed primitive */ | 
| 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++) { | 
| 364 | < | if ((cv = c_getvert(av[invert ? ac-i : i])) == NULL) | 
| 363 | > | for (i = 1; i < ac; i++) {      /* get, transform, print each vertex */ | 
| 364 | > | if ((cv = c_getvert(av[myi ? ac-i : i])) == NULL) | 
| 365 |  | return(MG_EUNDEF); | 
| 366 |  | xf_xfmpoint(v, cv->p); | 
| 367 |  | putv(v); | 
| 370 |  | } | 
| 371 |  |  | 
| 372 |  |  | 
| 373 | < | r_ies(ac, av)                           /* convert an IES luminaire file */ | 
| 374 | < | int     ac; | 
| 375 | < | char    **av; | 
| 373 | > | int | 
| 374 | > | r_ies(                          /* convert an IES luminaire file */ | 
| 375 | > | int     ac, | 
| 376 | > | char    **av | 
| 377 | > | ) | 
| 378 |  | { | 
| 379 |  | int     xa0 = 2; | 
| 380 | < | char    combuf[72]; | 
| 380 | > | char    combuf[128]; | 
| 381 |  | char    fname[48]; | 
| 382 |  | char    *oname; | 
| 383 |  | register char   *op; | 
| 384 |  | register int    i; | 
| 385 | < |  | 
| 385 | > | /* check argument count */ | 
| 386 |  | if (ac < 2) | 
| 387 |  | return(MG_EARGC); | 
| 388 | < | (void)strcpy(combuf, "ies2rad"); | 
| 389 | < | op = combuf + 7; | 
| 390 | < | if (ac-xa0 >= 2 && !strcmp(av[xa0], "-m")) { | 
| 391 | < | if (!isflt(av[xa0+1])) | 
| 334 | < | return(MG_ETYPE); | 
| 335 | < | op = addarg(addarg(op, "-m"), av[xa0+1]); | 
| 336 | < | xa0 += 2; | 
| 337 | < | } | 
| 338 | < | if (access(av[1], 0) == -1) | 
| 339 | < | return(MG_ENOFILE); | 
| 340 | < | *op++ = ' ';                    /* IES filename goes last */ | 
| 341 | < | (void)strcpy(op, av[1]); | 
| 342 | < | system(combuf);                 /* run ies2rad */ | 
| 343 | < | /* now let's find the output file */ | 
| 344 | < | if ((op = strrchr(av[1], '/')) == NULL) | 
| 388 | > | /* construct output file name */ | 
| 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 | < | if (access(fname, 0) == -1) | 
| 398 | < | return(MG_EINCL); | 
| 399 | < | /* put out xform command */ | 
| 400 | < | printf("\n!xform"); | 
| 397 | > | /* see if we need to run ies2rad */ | 
| 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")) { | 
| 402 | > | if (!isflt(av[xa0+1])) | 
| 403 | > | return(MG_ETYPE); | 
| 404 | > | op = addarg(addarg(op, "-m"), av[xa0+1]); | 
| 405 | > | xa0 += 2; | 
| 406 | > | } | 
| 407 | > | *op++ = ' ';                    /* build IES filename */ | 
| 408 | > | i = 0; | 
| 409 | > | if (mg_file != NULL && | 
| 410 | > | (oname = strrchr(mg_file->fname,'/')) != NULL) { | 
| 411 | > | i = oname - mg_file->fname + 1; | 
| 412 | > | (void)strcpy(op, mg_file->fname); | 
| 413 | > | } | 
| 414 | > | (void)strcpy(op+i, av[1]); | 
| 415 | > | if (access(op, 0) == -1)        /* check for file existence */ | 
| 416 | > | return(MG_ENOFILE); | 
| 417 | > | system(combuf);                 /* run ies2rad */ | 
| 418 | > | if (access(fname, 0) == -1)     /* check success */ | 
| 419 | > | return(MG_EINCL); | 
| 420 | > | } | 
| 421 | > | printf("\n!xform");                     /* put out xform command */ | 
| 422 |  | oname = object(); | 
| 423 |  | if (*oname) { | 
| 424 |  | printf(" -n "); | 
| 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); | 
| 386 | < | } else { | 
| 387 | < | cv1 = c_getvert(vn1); | 
| 388 | < | 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); | 
| 463 |  | xf_xfmpoint(v3, cv3->p); | 
| 464 | + | /* compute barycentric coords. */ | 
| 465 |  | if (comp_baryc(&bvecs, v1, v2, v3) < 0) | 
| 466 |  | return;                         /* degenerate triangle! */ | 
| 467 | < | printf("\n%s texfunc T-nor\n", mat); | 
| 467 | > | printf("\n%s texfunc T-nor\n", mat);    /* put out texture */ | 
| 468 |  | printf("4 dx dy dz %s\n0\n", TCALNAME); | 
| 469 |  | xf_rotvect(n1, cv1->n); | 
| 470 |  | xf_rotvect(n2, cv2->n); | 
| 475 |  | bcoor[i][2] = n3[i]; | 
| 476 |  | } | 
| 477 |  | put_baryc(&bvecs, bcoor, 3); | 
| 478 | + | /* put out triangle */ | 
| 479 |  | printf("\nT-nor polygon %st%d\n", object(), ++ntris); | 
| 480 |  | printf("0\n0\n9\n"); | 
| 481 |  | putv(v1); | 
| 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; | 
| 420 | – | register int    i; | 
| 493 |  |  | 
| 494 |  | if (c_cmname != NULL) | 
| 495 |  | mname = c_cmname; | 
| 499 |  | c_cmaterial->clock = 0; | 
| 500 |  | if (c_cmaterial->ed > .1) {     /* emitter */ | 
| 501 |  | cvtcolor(radrgb, &c_cmaterial->ed_c, | 
| 502 | < | emult*c_cmaterial->ed/WHTEFFICACY); | 
| 502 | > | emult*c_cmaterial->ed/(PI*WHTEFFICACY)); | 
| 503 |  | if (glowdist < FHUGE) {         /* do a glow */ | 
| 504 |  | fprintf(matfp, "\nvoid glow %s\n0\n0\n", mname); | 
| 505 |  | fprintf(matfp, "4 %f %f %f %f\n", colval(radrgb,RED), | 
| 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 && | 
| 522 | + | c_cmaterial->ts > .25 && c_cmaterial->rs <= .125 && | 
| 523 | + | c_cmaterial->td <= .01 && c_cmaterial->rd <= .01 && | 
| 524 | + | c_cmaterial->rs_a <= .01 && c_cmaterial->ts_a <= .01) { | 
| 525 | + | cvtcolor(radrgb, &c_cmaterial->ts_c, | 
| 526 | + | c_cmaterial->ts + c_cmaterial->rs); | 
| 527 | + | if (c_cmaterial->sided) {               /* dielectric */ | 
| 528 | + | colval(radrgb,RED) = pow(colval(radrgb,RED), | 
| 529 | + | 1./C_1SIDEDTHICK); | 
| 530 | + | colval(radrgb,GRN) = pow(colval(radrgb,GRN), | 
| 531 | + | 1./C_1SIDEDTHICK); | 
| 532 | + | colval(radrgb,BLU) = pow(colval(radrgb,BLU), | 
| 533 | + | 1./C_1SIDEDTHICK); | 
| 534 | + | fprintf(matfp, "\nvoid dielectric %s\n0\n0\n", mname); | 
| 535 | + | fprintf(matfp, "5 %g %g %g %f 0\n", colval(radrgb,RED), | 
| 536 | + | colval(radrgb,GRN), colval(radrgb,BLU), | 
| 537 | + | c_cmaterial->nr); | 
| 538 | + | return(mname); | 
| 539 | + | } | 
| 540 | + | /* glass */ | 
| 541 | + | fprintf(matfp, "\nvoid glass %s\n0\n0\n", mname); | 
| 542 | + | fprintf(matfp, "4 %f %f %f %f\n", colval(radrgb,RED), | 
| 543 | + | colval(radrgb,GRN), colval(radrgb,BLU), | 
| 544 | + | c_cmaterial->nr); | 
| 545 | + | return(mname); | 
| 546 | + | } | 
| 547 |  | /* check for trans */ | 
| 548 |  | if (c_cmaterial->td > .01 || c_cmaterial->ts > .01) { | 
| 549 |  | double  ts, a5, a6; | 
| 581 |  | return(mname); | 
| 582 |  | } | 
| 583 |  | /* check for plastic */ | 
| 584 | < | if (c_cmaterial->rs < .1 && (c_cmaterial->rs < .01 || | 
| 486 | < | 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 color to Radiance */ | 
| 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; | 
| 629 |  | register char   *cp; | 
| 630 |  | int     len; | 
| 631 | < |  | 
| 631 | > | /* tracked by obj_handler */ | 
| 632 |  | i = obj_nnames - sizeof(objbuf)/16; | 
| 633 |  | if (i < 0) | 
| 634 |  | i = 0; | 
| 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 |  | } |