| 1 | #ifndef lint | 
| 2 | static const char       RCSid[] = "$Id: mgf2rad.c,v 2.27 2003/07/27 22:12:01 schorsch Exp $"; | 
| 3 | #endif | 
| 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" | 
| 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 | FILE    *matfp;                         /* material output file */ | 
| 27 |  | 
| 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 |  | 
| 43 | int | 
| 44 | main( | 
| 45 | int     argc, | 
| 46 | char    *argv[] | 
| 47 | ) | 
| 48 | { | 
| 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 */ | 
| 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 our options & print header */ | 
| 82 | printf("## %s", argv[0]); | 
| 83 | for (i = 1; i < argc && argv[i][0] == '-'; i++) { | 
| 84 | printf(" %s", argv[i]); | 
| 85 | switch (argv[i][1]) { | 
| 86 | case 'g':                       /* glow distance (meters) */ | 
| 87 | if (argv[i][2] || badarg(argc-i-1, argv+i+1, "f")) | 
| 88 | goto userr; | 
| 89 | glowdist = atof(argv[++i]); | 
| 90 | printf(" %s", argv[i]); | 
| 91 | break; | 
| 92 | case 'e':                       /* emitter multiplier */ | 
| 93 | if (argv[i][2] || badarg(argc-i-1, argv+i+1, "f")) | 
| 94 | goto userr; | 
| 95 | emult = atof(argv[++i]); | 
| 96 | printf(" %s", argv[i]); | 
| 97 | break; | 
| 98 | case 'm':                       /* materials file */ | 
| 99 | matfp = fopen(argv[++i], "a"); | 
| 100 | if (matfp == NULL) { | 
| 101 | fprintf(stderr, "%s: cannot append\n", argv[i]); | 
| 102 | exit(1); | 
| 103 | } | 
| 104 | printf(" %s", argv[i]); | 
| 105 | break; | 
| 106 | default: | 
| 107 | goto userr; | 
| 108 | } | 
| 109 | } | 
| 110 | putchar('\n'); | 
| 111 | if (i == argc) {                /* convert stdin */ | 
| 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 (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: | 
| 131 | fprintf(stderr, "Usage: %s [-g dist][-e mult][-m matf] [file.mgf] ..\n", | 
| 132 | argv[0]); | 
| 133 | exit(1); | 
| 134 | } | 
| 135 |  | 
| 136 |  | 
| 137 | int | 
| 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 */ | 
| 145 | putchar(' '); | 
| 146 | fputs(*++av, stdout); | 
| 147 | } | 
| 148 | putchar('\n'); | 
| 149 | return(MG_OK); | 
| 150 | } | 
| 151 |  | 
| 152 |  | 
| 153 | int | 
| 154 | r_cone(                 /* put out a cone */ | 
| 155 | int     ac, | 
| 156 | char    **av | 
| 157 | ) | 
| 158 | { | 
| 159 | static int      ncones; | 
| 160 | char    *mat; | 
| 161 | double  r1, r2; | 
| 162 | C_VERTEX        *cv1, *cv2; | 
| 163 | FVECT   p1, p2; | 
| 164 | int     inv; | 
| 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);        /* transform endpoints */ | 
| 175 | xf_xfmpoint(p2, cv2->p); | 
| 176 | r1 = xf_scale(atof(av[2]));     /* scale radii */ | 
| 177 | r2 = xf_scale(atof(av[4])); | 
| 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.)) | 
| 184 | return(MG_EILL); | 
| 185 | if (inv) { | 
| 186 | r1 = -r1; | 
| 187 | r2 = -r2; | 
| 188 | } | 
| 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"); | 
| 195 | putv(p1); | 
| 196 | putv(p2); | 
| 197 | printf("%18.12g %18.12g\n", r1, r2); | 
| 198 | return(MG_OK); | 
| 199 | } | 
| 200 |  | 
| 201 |  | 
| 202 | int | 
| 203 | r_cyl(                  /* put out a cylinder */ | 
| 204 | int     ac, | 
| 205 | char    **av | 
| 206 | ) | 
| 207 | { | 
| 208 | static int      ncyls; | 
| 209 | char    *mat; | 
| 210 | double  rad; | 
| 211 | C_VERTEX        *cv1, *cv2; | 
| 212 | FVECT   p1, p2; | 
| 213 | int     inv; | 
| 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);        /* transform endpoints */ | 
| 224 | xf_xfmpoint(p2, cv2->p); | 
| 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) /* 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"); | 
| 234 | putv(p1); | 
| 235 | putv(p2); | 
| 236 | printf("%18.12g\n", rad); | 
| 237 | return(MG_OK); | 
| 238 | } | 
| 239 |  | 
| 240 |  | 
| 241 | int | 
| 242 | r_sph(                  /* put out a sphere */ | 
| 243 | int     ac, | 
| 244 | char    **av | 
| 245 | ) | 
| 246 | { | 
| 247 | static int      nsphs; | 
| 248 | char    *mat; | 
| 249 | double  rad; | 
| 250 | C_VERTEX        *cv; | 
| 251 | FVECT   cent; | 
| 252 | int     inv; | 
| 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)    /* get center vertex */ | 
| 259 | return(MG_EUNDEF); | 
| 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)         /* 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", | 
| 270 | cent[0], cent[1], cent[2], rad); | 
| 271 | return(MG_OK); | 
| 272 | } | 
| 273 |  | 
| 274 |  | 
| 275 | int | 
| 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 | /* 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)    /* get center vertex */ | 
| 292 | return(MG_EUNDEF); | 
| 293 | if (is0vect(cv->n))                     /* make sure we have normal */ | 
| 294 | return(MG_EILL); | 
| 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)) | 
| 300 | return(MG_EILL); | 
| 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); | 
| 307 | putv(norm); | 
| 308 | printf("%18.12g %18.12g\n", r1, r2); | 
| 309 | return(MG_OK); | 
| 310 | } | 
| 311 |  | 
| 312 |  | 
| 313 | int | 
| 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 |  | 
| 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 ((cva[i-1] = c_getvert(av[i])) == NULL) | 
| 335 | return(MG_EUNDEF); | 
| 336 | if (is0vect(cva[i-1]->n)) | 
| 337 | break; | 
| 338 | } | 
| 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, 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++) {      /* 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); | 
| 368 | } | 
| 369 | return(MG_OK); | 
| 370 | } | 
| 371 |  | 
| 372 |  | 
| 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[128]; | 
| 381 | char    fname[48]; | 
| 382 | char    *oname; | 
| 383 | register char   *op; | 
| 384 | register int    i; | 
| 385 | /* check argument count */ | 
| 386 | if (ac < 2) | 
| 387 | return(MG_EARGC); | 
| 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 | /* 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 "); | 
| 425 | for (op = oname; op[1]; op++)   /* remove trailing separator */ | 
| 426 | putchar(*op); | 
| 427 | } | 
| 428 | for (i = xa0; i < ac; i++) | 
| 429 | printf(" %s", av[i]); | 
| 430 | if (ac > xa0 && xf_argc > 0) | 
| 431 | printf(" -i 1"); | 
| 432 | for (i = 0; i < xf_argc; i++) | 
| 433 | printf(" %s", xf_argv[i]); | 
| 434 | printf(" %s\n", fname); | 
| 435 | return(MG_OK); | 
| 436 | } | 
| 437 |  | 
| 438 |  | 
| 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 | RREAL   bcoor[3][3]; | 
| 451 | C_VERTEX        *cvt; | 
| 452 | FVECT   v1, v2, v3; | 
| 453 | FVECT   n1, n2, n3; | 
| 454 | register int    i; | 
| 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);    /* 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); | 
| 471 | xf_rotvect(n3, cv3->n); | 
| 472 | for (i = 0; i < 3; i++) { | 
| 473 | bcoor[i][0] = n1[i]; | 
| 474 | bcoor[i][1] = n2[i]; | 
| 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); | 
| 482 | putv(v2); | 
| 483 | putv(v3); | 
| 484 | } | 
| 485 |  | 
| 486 |  | 
| 487 | char * | 
| 488 | material(void)                  /* get (and print) current material */ | 
| 489 | { | 
| 490 | char    *mname = "mat"; | 
| 491 | COLOR   radrgb, c2; | 
| 492 | double  d; | 
| 493 |  | 
| 494 | if (c_cmname != NULL) | 
| 495 | mname = c_cmname; | 
| 496 | if (!c_cmaterial->clock) | 
| 497 | return(mname);          /* already current */ | 
| 498 | /* else update output */ | 
| 499 | c_cmaterial->clock = 0; | 
| 500 | if (c_cmaterial->ed > .1) {     /* emitter */ | 
| 501 | cvtcolor(radrgb, &c_cmaterial->ed_c, | 
| 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), | 
| 506 | colval(radrgb,GRN), | 
| 507 | colval(radrgb,BLU), glowdist); | 
| 508 | } else { | 
| 509 | fprintf(matfp, "\nvoid light %s\n0\n0\n", mname); | 
| 510 | fprintf(matfp, "3 %f %f %f\n", colval(radrgb,RED), | 
| 511 | colval(radrgb,GRN), | 
| 512 | colval(radrgb,BLU)); | 
| 513 | } | 
| 514 | return(mname); | 
| 515 | } | 
| 516 | d = c_cmaterial->rd + c_cmaterial->td + | 
| 517 | c_cmaterial->rs + c_cmaterial->ts; | 
| 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; | 
| 550 |  | 
| 551 | if (c_cmaterial->sided) { | 
| 552 | ts = sqrt(c_cmaterial->ts);     /* approximate */ | 
| 553 | a5 = .5; | 
| 554 | } else { | 
| 555 | ts = c_cmaterial->ts; | 
| 556 | a5 = 1.; | 
| 557 | } | 
| 558 | /* average colors */ | 
| 559 | d = c_cmaterial->rd + c_cmaterial->td + ts; | 
| 560 | cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd/d); | 
| 561 | cvtcolor(c2, &c_cmaterial->td_c, c_cmaterial->td/d); | 
| 562 | addcolor(radrgb, c2); | 
| 563 | cvtcolor(c2, &c_cmaterial->ts_c, ts/d); | 
| 564 | addcolor(radrgb, c2); | 
| 565 | if (c_cmaterial->rs + ts > .0001) | 
| 566 | a5 = (c_cmaterial->rs*c_cmaterial->rs_a + | 
| 567 | ts*a5*c_cmaterial->ts_a) / | 
| 568 | (c_cmaterial->rs + ts); | 
| 569 | a6 = (c_cmaterial->td + ts) / | 
| 570 | (c_cmaterial->rd + c_cmaterial->td + ts); | 
| 571 | if (a6 < .999) | 
| 572 | d = c_cmaterial->rd/(1. - c_cmaterial->rs)/(1. - a6); | 
| 573 | else | 
| 574 | d = c_cmaterial->td + ts; | 
| 575 | scalecolor(radrgb, d); | 
| 576 | fprintf(matfp, "\nvoid trans %s\n0\n0\n", mname); | 
| 577 | fprintf(matfp, "7 %f %f %f\n", colval(radrgb,RED), | 
| 578 | colval(radrgb,GRN), colval(radrgb,BLU)); | 
| 579 | fprintf(matfp, "\t%f %f %f %f\n", c_cmaterial->rs, a5, a6, | 
| 580 | ts/(ts + c_cmaterial->td)); | 
| 581 | return(mname); | 
| 582 | } | 
| 583 | /* check for plastic */ | 
| 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); | 
| 588 | fprintf(matfp, "5 %f %f %f %f %f\n", colval(radrgb,RED), | 
| 589 | colval(radrgb,GRN), colval(radrgb,BLU), | 
| 590 | c_cmaterial->rs, c_cmaterial->rs_a); | 
| 591 | return(mname); | 
| 592 | } | 
| 593 | /* else it's metal */ | 
| 594 | /* average colors */ | 
| 595 | cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd); | 
| 596 | cvtcolor(c2, &c_cmaterial->rs_c, c_cmaterial->rs); | 
| 597 | addcolor(radrgb, c2); | 
| 598 | fprintf(matfp, "\nvoid metal %s\n0\n0\n", mname); | 
| 599 | fprintf(matfp, "5 %f %f %f %f %f\n", colval(radrgb,RED), | 
| 600 | colval(radrgb,GRN), colval(radrgb,BLU), | 
| 601 | c_cmaterial->rs/(c_cmaterial->rd + c_cmaterial->rs), | 
| 602 | c_cmaterial->rs_a); | 
| 603 | return(mname); | 
| 604 | } | 
| 605 |  | 
| 606 |  | 
| 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 |  | 
| 616 | c_ccvt(ciec, C_CSXY);           /* get xy representation */ | 
| 617 | ciexyz[1] = intensity; | 
| 618 | ciexyz[0] = ciec->cx/ciec->cy*ciexyz[1]; | 
| 619 | ciexyz[2] = ciexyz[1]*(1./ciec->cy - 1.) - ciexyz[0]; | 
| 620 | cie_rgb(radrgb, ciexyz); | 
| 621 | } | 
| 622 |  | 
| 623 |  | 
| 624 | char * | 
| 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 | /* tracked by obj_handler */ | 
| 632 | i = obj_nnames - sizeof(objbuf)/16; | 
| 633 | if (i < 0) | 
| 634 | i = 0; | 
| 635 | for (cp = objbuf; i < obj_nnames && | 
| 636 | cp + (len=strlen(obj_name[i])) < objbuf+sizeof(objbuf)-1; | 
| 637 | i++, *cp++ = '.') { | 
| 638 | strcpy(cp, obj_name[i]); | 
| 639 | cp += len; | 
| 640 | } | 
| 641 | *cp = '\0'; | 
| 642 | return(objbuf); | 
| 643 | } | 
| 644 |  | 
| 645 |  | 
| 646 | char * | 
| 647 | addarg(                         /* add argument and advance pointer */ | 
| 648 | register char *op, | 
| 649 | register char *arg | 
| 650 | ) | 
| 651 | { | 
| 652 | *op = ' '; | 
| 653 | while ( (*++op = *arg++) ) | 
| 654 | ; | 
| 655 | return(op); | 
| 656 | } |