| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: rad2mgf.c,v 2.25 2005/12/28 18:39:07 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* Convert Radiance scene description to MGF
|
| 6 |
*/
|
| 7 |
|
| 8 |
#include <ctype.h>
|
| 9 |
#include <string.h>
|
| 10 |
#include <stdio.h>
|
| 11 |
|
| 12 |
#include "platform.h"
|
| 13 |
#include "rtmath.h"
|
| 14 |
#include "rtio.h"
|
| 15 |
#include "rtprocess.h"
|
| 16 |
#include "object.h"
|
| 17 |
#include "color.h"
|
| 18 |
#include "lookup.h"
|
| 19 |
|
| 20 |
#define C_1SIDEDTHICK 0.005
|
| 21 |
|
| 22 |
|
| 23 |
LUTAB rmats = LU_SINIT(free,NULL); /* defined material table */
|
| 24 |
|
| 25 |
LUTAB rdispatch = LU_SINIT(NULL,NULL); /* function dispatch table */
|
| 26 |
|
| 27 |
char curmat[80]; /* current material */
|
| 28 |
char curobj[128] = "Untitled"; /* current object name */
|
| 29 |
|
| 30 |
double unit_mult = 1.; /* units multiplier */
|
| 31 |
|
| 32 |
#define hasmult (unit_mult < .999 || unit_mult > 1.001)
|
| 33 |
|
| 34 |
/*
|
| 35 |
* Stuff for tracking and reusing vertices:
|
| 36 |
*/
|
| 37 |
|
| 38 |
char VKFMT[] = "%+16.9e %+16.9e %+16.9e";
|
| 39 |
#define VKLEN 64
|
| 40 |
|
| 41 |
#define mkvkey(k,v) sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2])
|
| 42 |
|
| 43 |
#define NVERTS 256
|
| 44 |
|
| 45 |
long vclock; /* incremented at each vertex request */
|
| 46 |
|
| 47 |
struct vert {
|
| 48 |
long lused; /* when last used (0 if unassigned) */
|
| 49 |
FVECT p; /* track point position only */
|
| 50 |
} vert[NVERTS]; /* our vertex cache */
|
| 51 |
|
| 52 |
LUTAB vertab = LU_SINIT(free,NULL); /* our vertex lookup table */
|
| 53 |
|
| 54 |
void rad2mgf(char *inp);
|
| 55 |
void cvtprim(char *inp, char *mod, char *typ, char *id, FUNARGS *fa);
|
| 56 |
void newmat(char *id, char *alias);
|
| 57 |
void setmat(char *id);
|
| 58 |
void setobj(char *id);
|
| 59 |
void init(void);
|
| 60 |
void uninit(void);
|
| 61 |
void clrverts(void);
|
| 62 |
void unspace(char *s);
|
| 63 |
void add2dispatch(char *name, int (*func)());
|
| 64 |
char *getvertid(char *vname, FVECT vp);
|
| 65 |
int o_unsupported(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 66 |
int o_face(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 67 |
int o_cone(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 68 |
int o_sphere(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 69 |
int o_cylinder(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 70 |
int o_ring(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 71 |
int o_instance(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 72 |
int o_illum(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 73 |
int o_plastic(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 74 |
int o_metal(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 75 |
int o_glass(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 76 |
int o_dielectric(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 77 |
int o_mirror(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 78 |
int o_trans(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 79 |
int o_light(char *mod, char *typ, char *id, FUNARGS *fa);
|
| 80 |
|
| 81 |
|
| 82 |
int
|
| 83 |
main(
|
| 84 |
int argc,
|
| 85 |
char **argv
|
| 86 |
)
|
| 87 |
{
|
| 88 |
int i;
|
| 89 |
|
| 90 |
for (i = 1; i < argc && argv[i][0] == '-'; i++)
|
| 91 |
switch (argv[i][1]) {
|
| 92 |
case 'd': /* units */
|
| 93 |
switch (argv[i][2]) {
|
| 94 |
case 'm': /* meters */
|
| 95 |
unit_mult = 1.;
|
| 96 |
break;
|
| 97 |
case 'c': /* centimeters */
|
| 98 |
unit_mult = .01;
|
| 99 |
break;
|
| 100 |
case 'f': /* feet */
|
| 101 |
unit_mult = 12.*.0254;
|
| 102 |
break;
|
| 103 |
case 'i': /* inches */
|
| 104 |
unit_mult = .0254;
|
| 105 |
break;
|
| 106 |
default:
|
| 107 |
goto unkopt;
|
| 108 |
}
|
| 109 |
break;
|
| 110 |
default:
|
| 111 |
goto unkopt;
|
| 112 |
}
|
| 113 |
init();
|
| 114 |
if (i >= argc)
|
| 115 |
rad2mgf(NULL);
|
| 116 |
else
|
| 117 |
for ( ; i < argc; i++)
|
| 118 |
rad2mgf(argv[i]);
|
| 119 |
uninit();
|
| 120 |
exit(0);
|
| 121 |
unkopt:
|
| 122 |
fprintf(stderr, "Usage: %s [-d{m|c|f|i}] file ..\n", argv[0]);
|
| 123 |
exit(1);
|
| 124 |
}
|
| 125 |
|
| 126 |
|
| 127 |
void
|
| 128 |
rad2mgf( /* convert a Radiance file to MGF */
|
| 129 |
char *inp
|
| 130 |
)
|
| 131 |
{
|
| 132 |
char buf[512];
|
| 133 |
char mod[128], typ[32], id[128], alias[128];
|
| 134 |
FUNARGS fa;
|
| 135 |
register FILE *fp;
|
| 136 |
register int c;
|
| 137 |
|
| 138 |
if (inp == NULL) {
|
| 139 |
inp = "standard input";
|
| 140 |
fp = stdin;
|
| 141 |
} else if (inp[0] == '!') {
|
| 142 |
if ((fp = popen(inp+1, "r")) == NULL) {
|
| 143 |
fputs(inp, stderr);
|
| 144 |
fputs(": cannot execute\n", stderr);
|
| 145 |
exit(1);
|
| 146 |
}
|
| 147 |
} else if ((fp = fopen(inp, "r")) == NULL) {
|
| 148 |
fputs(inp, stderr);
|
| 149 |
fputs(": cannot open\n", stderr);
|
| 150 |
exit(1);
|
| 151 |
}
|
| 152 |
printf("# Begin conversion from: %s\n", inp);
|
| 153 |
while ((c = getc(fp)) != EOF)
|
| 154 |
switch (c) {
|
| 155 |
case ' ': /* white space */
|
| 156 |
case '\t':
|
| 157 |
case '\n':
|
| 158 |
case '\r':
|
| 159 |
case '\f':
|
| 160 |
break;
|
| 161 |
case '#': /* comment */
|
| 162 |
if (fgets(buf, sizeof(buf), fp) != NULL)
|
| 163 |
printf("# %s", buf);
|
| 164 |
break;
|
| 165 |
case '!': /* inline command */
|
| 166 |
ungetc(c, fp);
|
| 167 |
fgetline(buf, sizeof(buf), fp);
|
| 168 |
rad2mgf(buf);
|
| 169 |
break;
|
| 170 |
default: /* Radiance primitive */
|
| 171 |
ungetc(c, fp);
|
| 172 |
if (fgetword(mod, sizeof(mod), fp) == NULL ||
|
| 173 |
fgetword(typ, sizeof(typ), fp) == NULL ||
|
| 174 |
fgetword(id, sizeof(id), fp) == NULL) {
|
| 175 |
fputs(inp, stderr);
|
| 176 |
fputs(": unexpected EOF\n", stderr);
|
| 177 |
exit(1);
|
| 178 |
}
|
| 179 |
unspace(mod);
|
| 180 |
unspace(id);
|
| 181 |
if (!strcmp(typ, "alias")) {
|
| 182 |
strcpy(alias, "EOF");
|
| 183 |
fgetword(alias, sizeof(alias), fp);
|
| 184 |
unspace(alias);
|
| 185 |
newmat(id, alias);
|
| 186 |
} else {
|
| 187 |
if (!readfargs(&fa, fp)) {
|
| 188 |
fprintf(stderr,
|
| 189 |
"%s: bad argument syntax for %s \"%s\"\n",
|
| 190 |
inp, typ, id);
|
| 191 |
exit(1);
|
| 192 |
}
|
| 193 |
cvtprim(inp, mod, typ, id, &fa);
|
| 194 |
freefargs(&fa);
|
| 195 |
}
|
| 196 |
break;
|
| 197 |
}
|
| 198 |
printf("# End conversion from: %s\n", inp);
|
| 199 |
if (inp[0] == '!')
|
| 200 |
pclose(fp);
|
| 201 |
else
|
| 202 |
fclose(fp);
|
| 203 |
}
|
| 204 |
|
| 205 |
|
| 206 |
void
|
| 207 |
unspace( /* replace spaces with underscores in s */
|
| 208 |
char *s
|
| 209 |
)
|
| 210 |
{
|
| 211 |
while (*s) {
|
| 212 |
if (isspace(*s))
|
| 213 |
*s = '_';
|
| 214 |
++s;
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
|
| 219 |
void
|
| 220 |
cvtprim( /* process Radiance primitive */
|
| 221 |
char *inp,
|
| 222 |
char *mod,
|
| 223 |
char *typ,
|
| 224 |
char *id,
|
| 225 |
FUNARGS *fa
|
| 226 |
)
|
| 227 |
{
|
| 228 |
int (*df)();
|
| 229 |
|
| 230 |
df = (int (*)())lu_find(&rdispatch, typ)->data;
|
| 231 |
if (df != NULL) { /* convert */
|
| 232 |
if ((*df)(mod, typ, id, fa) < 0) {
|
| 233 |
fprintf(stderr, "%s: bad %s \"%s\"\n", "rad2mgf", typ, id);
|
| 234 |
exit(1);
|
| 235 |
}
|
| 236 |
} else { /* unsupported */
|
| 237 |
o_unsupported(mod, typ, id, fa);
|
| 238 |
if (lu_find(&rmats, mod)->data != NULL) /* make alias */
|
| 239 |
newmat(id, mod);
|
| 240 |
}
|
| 241 |
}
|
| 242 |
|
| 243 |
|
| 244 |
void
|
| 245 |
newmat( /* add a modifier to the alias list */
|
| 246 |
char *id,
|
| 247 |
char *alias
|
| 248 |
)
|
| 249 |
{
|
| 250 |
register LUENT *lp, *lpa;
|
| 251 |
|
| 252 |
if (alias != NULL) { /* look up alias */
|
| 253 |
if ((lpa = lu_find(&rmats, alias)) == NULL)
|
| 254 |
goto memerr;
|
| 255 |
if (lpa->data == NULL)
|
| 256 |
alias = NULL; /* doesn't exist! */
|
| 257 |
}
|
| 258 |
if ((lp = lu_find(&rmats, id)) == NULL) /* look up material */
|
| 259 |
goto memerr;
|
| 260 |
if (alias != NULL && lp->data == lpa->key)
|
| 261 |
return; /* alias set already */
|
| 262 |
if (lp->data == NULL) { /* allocate material */
|
| 263 |
if ((lp->key = (char *)malloc(strlen(id)+1)) == NULL)
|
| 264 |
goto memerr;
|
| 265 |
strcpy(lp->key, id);
|
| 266 |
}
|
| 267 |
if (alias == NULL) { /* set this material */
|
| 268 |
lp->data = lp->key;
|
| 269 |
printf("m %s =\n", id);
|
| 270 |
} else { /* set this alias */
|
| 271 |
lp->data = lpa->key;
|
| 272 |
printf("m %s = %s\n", id, alias);
|
| 273 |
}
|
| 274 |
strcpy(curmat, id);
|
| 275 |
return;
|
| 276 |
memerr:
|
| 277 |
fputs("Out of memory in newmat!\n", stderr);
|
| 278 |
exit(1);
|
| 279 |
}
|
| 280 |
|
| 281 |
|
| 282 |
void
|
| 283 |
setmat( /* set material to this one */
|
| 284 |
char *id
|
| 285 |
)
|
| 286 |
{
|
| 287 |
if (!strcmp(id, curmat)) /* already set? */
|
| 288 |
return;
|
| 289 |
if (!strcmp(id, VOIDID)) /* cannot set */
|
| 290 |
return;
|
| 291 |
printf("m %s\n", id);
|
| 292 |
strcpy(curmat, id);
|
| 293 |
}
|
| 294 |
|
| 295 |
|
| 296 |
void
|
| 297 |
setobj( /* set object name to this one */
|
| 298 |
char *id
|
| 299 |
)
|
| 300 |
{
|
| 301 |
register char *cp, *cp2;
|
| 302 |
char *end = NULL;
|
| 303 |
int diff = 0;
|
| 304 |
/* use all but final suffix */
|
| 305 |
for (cp = id; *cp; cp++)
|
| 306 |
if (*cp == '.')
|
| 307 |
end = cp;
|
| 308 |
if (end == NULL)
|
| 309 |
end = cp;
|
| 310 |
/* copy to current object */
|
| 311 |
cp2 = curobj;
|
| 312 |
if (!isalpha(*id)) { /* start with letter */
|
| 313 |
diff = *cp2 != 'O';
|
| 314 |
*cp2++ = 'O';
|
| 315 |
}
|
| 316 |
for (cp = id; cp < end; *cp2++ = *cp++) {
|
| 317 |
if ((*cp < '!') | (*cp > '~')) /* limit to visible chars */
|
| 318 |
*cp = '?';
|
| 319 |
diff += *cp != *cp2;
|
| 320 |
}
|
| 321 |
if (!diff && !*cp2)
|
| 322 |
return;
|
| 323 |
*cp2 = '\0';
|
| 324 |
fputs("o\no ", stdout);
|
| 325 |
puts(curobj);
|
| 326 |
}
|
| 327 |
|
| 328 |
|
| 329 |
void
|
| 330 |
init(void) /* initialize dispatch table and output */
|
| 331 |
{
|
| 332 |
lu_init(&vertab, NVERTS);
|
| 333 |
lu_init(&rdispatch, 22);
|
| 334 |
add2dispatch("polygon", o_face);
|
| 335 |
add2dispatch("cone", o_cone);
|
| 336 |
add2dispatch("cup", o_cone);
|
| 337 |
add2dispatch("sphere", o_sphere);
|
| 338 |
add2dispatch("bubble", o_sphere);
|
| 339 |
add2dispatch("cylinder", o_cylinder);
|
| 340 |
add2dispatch("tube", o_cylinder);
|
| 341 |
add2dispatch("ring", o_ring);
|
| 342 |
add2dispatch("instance", o_instance);
|
| 343 |
add2dispatch("mesh", o_instance);
|
| 344 |
add2dispatch("plastic", o_plastic);
|
| 345 |
add2dispatch("plastic2", o_plastic);
|
| 346 |
add2dispatch("metal", o_metal);
|
| 347 |
add2dispatch("metal2", o_metal);
|
| 348 |
add2dispatch("glass", o_glass);
|
| 349 |
add2dispatch("dielectric", o_dielectric);
|
| 350 |
add2dispatch("trans", o_trans);
|
| 351 |
add2dispatch("trans2", o_trans);
|
| 352 |
add2dispatch("mirror", o_mirror);
|
| 353 |
add2dispatch("light", o_light);
|
| 354 |
add2dispatch("spotlight", o_light);
|
| 355 |
add2dispatch("glow", o_light);
|
| 356 |
add2dispatch("illum", o_illum);
|
| 357 |
puts("# The following was converted from RADIANCE scene input");
|
| 358 |
if (hasmult)
|
| 359 |
printf("xf -s %.4e\n", unit_mult);
|
| 360 |
printf("o %s\n", curobj);
|
| 361 |
}
|
| 362 |
|
| 363 |
|
| 364 |
void
|
| 365 |
uninit(void) /* mark end of MGF file */
|
| 366 |
{
|
| 367 |
puts("o");
|
| 368 |
if (hasmult)
|
| 369 |
puts("xf");
|
| 370 |
puts("# End of data converted from RADIANCE scene input");
|
| 371 |
lu_done(&rdispatch);
|
| 372 |
lu_done(&rmats);
|
| 373 |
lu_done(&vertab);
|
| 374 |
}
|
| 375 |
|
| 376 |
|
| 377 |
void
|
| 378 |
clrverts(void) /* clear vertex table */
|
| 379 |
{
|
| 380 |
register int i;
|
| 381 |
|
| 382 |
lu_done(&vertab);
|
| 383 |
for (i = 0; i < NVERTS; i++)
|
| 384 |
vert[i].lused = 0;
|
| 385 |
lu_init(&vertab, NVERTS);
|
| 386 |
}
|
| 387 |
|
| 388 |
|
| 389 |
void
|
| 390 |
add2dispatch( /* add function to dispatch table */
|
| 391 |
char *name,
|
| 392 |
int (*func)()
|
| 393 |
)
|
| 394 |
{
|
| 395 |
register LUENT *lp;
|
| 396 |
|
| 397 |
lp = lu_find(&rdispatch, name);
|
| 398 |
if (lp->key != NULL) {
|
| 399 |
fputs(name, stderr);
|
| 400 |
fputs(": duplicate dispatch entry!\n", stderr);
|
| 401 |
exit(1);
|
| 402 |
}
|
| 403 |
lp->key = name;
|
| 404 |
lp->data = (char *)func;
|
| 405 |
}
|
| 406 |
|
| 407 |
|
| 408 |
char *
|
| 409 |
getvertid( /* get/set vertex ID for this point */
|
| 410 |
char *vname,
|
| 411 |
FVECT vp
|
| 412 |
)
|
| 413 |
{
|
| 414 |
static char vkey[VKLEN];
|
| 415 |
register LUENT *lp;
|
| 416 |
register int i, vndx;
|
| 417 |
|
| 418 |
vclock++; /* increment counter */
|
| 419 |
mkvkey(vkey, vp);
|
| 420 |
if ((lp = lu_find(&vertab, vkey)) == NULL)
|
| 421 |
goto memerr;
|
| 422 |
if (lp->data == NULL) { /* allocate new vertex entry */
|
| 423 |
if (lp->key != NULL) /* reclaim deleted entry */
|
| 424 |
vertab.ndel--;
|
| 425 |
else {
|
| 426 |
if ((lp->key = (char *)malloc(VKLEN)) == NULL)
|
| 427 |
goto memerr;
|
| 428 |
strcpy(lp->key, vkey);
|
| 429 |
}
|
| 430 |
vndx = 0; /* find oldest vertex */
|
| 431 |
for (i = 1; i < NVERTS; i++)
|
| 432 |
if (vert[i].lused < vert[vndx].lused)
|
| 433 |
vndx = i;
|
| 434 |
if (vert[vndx].lused) { /* free old entry first */
|
| 435 |
mkvkey(vkey, vert[vndx].p);
|
| 436 |
lu_delete(&vertab, vkey);
|
| 437 |
}
|
| 438 |
VCOPY(vert[vndx].p, vp); /* assign it */
|
| 439 |
printf("v v%d =\n\tp %.15g %.15g %.15g\n", /* print it */
|
| 440 |
vndx, vp[0], vp[1], vp[2]);
|
| 441 |
lp->data = (char *)&vert[vndx]; /* set it */
|
| 442 |
} else
|
| 443 |
vndx = (struct vert *)lp->data - vert;
|
| 444 |
vert[vndx].lused = vclock; /* record this use */
|
| 445 |
sprintf(vname, "v%d", vndx);
|
| 446 |
return(vname);
|
| 447 |
memerr:
|
| 448 |
fputs("Out of memory in getvertid!\n", stderr);
|
| 449 |
exit(1);
|
| 450 |
}
|
| 451 |
|
| 452 |
|
| 453 |
int
|
| 454 |
o_unsupported( /* mark unsupported primitive */
|
| 455 |
char *mod,
|
| 456 |
char *typ,
|
| 457 |
char *id,
|
| 458 |
FUNARGS *fa
|
| 459 |
)
|
| 460 |
{
|
| 461 |
register int i;
|
| 462 |
|
| 463 |
fputs("\n# Unsupported RADIANCE primitive:\n", stdout);
|
| 464 |
printf("# %s %s %s", mod, typ, id);
|
| 465 |
printf("\n# %d", fa->nsargs);
|
| 466 |
for (i = 0; i < fa->nsargs; i++)
|
| 467 |
printf(" %s", fa->sarg[i]);
|
| 468 |
#ifdef IARGS
|
| 469 |
printf("\n# %d", fa->niargs);
|
| 470 |
for (i = 0; i < fa->niargs; i++)
|
| 471 |
printf(" %ld", fa->iarg[i]);
|
| 472 |
#else
|
| 473 |
fputs("\n# 0", stdout);
|
| 474 |
#endif
|
| 475 |
printf("\n# %d", fa->nfargs);
|
| 476 |
for (i = 0; i < fa->nfargs; i++)
|
| 477 |
printf(" %g", fa->farg[i]);
|
| 478 |
fputs("\n\n", stdout);
|
| 479 |
return(0);
|
| 480 |
}
|
| 481 |
|
| 482 |
|
| 483 |
int
|
| 484 |
o_face( /* print out a polygon */
|
| 485 |
char *mod,
|
| 486 |
char *typ,
|
| 487 |
char *id,
|
| 488 |
FUNARGS *fa
|
| 489 |
)
|
| 490 |
{
|
| 491 |
char entbuf[2048], *linestart;
|
| 492 |
register char *cp;
|
| 493 |
register int i;
|
| 494 |
|
| 495 |
if ((fa->nfargs < 9) | (fa->nfargs % 3))
|
| 496 |
return(-1);
|
| 497 |
setmat(mod);
|
| 498 |
setobj(id);
|
| 499 |
cp = linestart = entbuf;
|
| 500 |
*cp++ = 'f';
|
| 501 |
for (i = 0; i < fa->nfargs; i += 3) {
|
| 502 |
*cp++ = ' ';
|
| 503 |
if (cp - linestart > 72) {
|
| 504 |
*cp++ = '\\'; *cp++ = '\n';
|
| 505 |
linestart = cp;
|
| 506 |
*cp++ = ' '; *cp++ = ' ';
|
| 507 |
}
|
| 508 |
getvertid(cp, fa->farg + i);
|
| 509 |
while (*cp)
|
| 510 |
cp++;
|
| 511 |
}
|
| 512 |
puts(entbuf);
|
| 513 |
return(0);
|
| 514 |
}
|
| 515 |
|
| 516 |
|
| 517 |
int
|
| 518 |
o_cone( /* print out a cone */
|
| 519 |
char *mod,
|
| 520 |
char *typ,
|
| 521 |
char *id,
|
| 522 |
register FUNARGS *fa
|
| 523 |
)
|
| 524 |
{
|
| 525 |
char v1[6], v2[6];
|
| 526 |
|
| 527 |
if (fa->nfargs != 8)
|
| 528 |
return(-1);
|
| 529 |
setmat(mod);
|
| 530 |
setobj(id);
|
| 531 |
getvertid(v1, fa->farg);
|
| 532 |
getvertid(v2, fa->farg + 3);
|
| 533 |
if (typ[1] == 'u') /* cup -> inverted cone */
|
| 534 |
printf("cone %s %.12g %s %.12g\n",
|
| 535 |
v1, -fa->farg[6], v2, -fa->farg[7]);
|
| 536 |
else
|
| 537 |
printf("cone %s %.12g %s %.12g\n",
|
| 538 |
v1, fa->farg[6], v2, fa->farg[7]);
|
| 539 |
return(0);
|
| 540 |
}
|
| 541 |
|
| 542 |
|
| 543 |
int
|
| 544 |
o_sphere( /* print out a sphere */
|
| 545 |
char *mod,
|
| 546 |
char *typ,
|
| 547 |
char *id,
|
| 548 |
register FUNARGS *fa
|
| 549 |
)
|
| 550 |
{
|
| 551 |
char cent[6];
|
| 552 |
|
| 553 |
if (fa->nfargs != 4)
|
| 554 |
return(-1);
|
| 555 |
setmat(mod);
|
| 556 |
setobj(id);
|
| 557 |
printf("sph %s %.12g\n", getvertid(cent, fa->farg),
|
| 558 |
typ[0]=='b' ? -fa->farg[3] : fa->farg[3]);
|
| 559 |
return(0);
|
| 560 |
}
|
| 561 |
|
| 562 |
|
| 563 |
int
|
| 564 |
o_cylinder( /* print out a cylinder */
|
| 565 |
char *mod,
|
| 566 |
char *typ,
|
| 567 |
char *id,
|
| 568 |
register FUNARGS *fa
|
| 569 |
)
|
| 570 |
{
|
| 571 |
char v1[6], v2[6];
|
| 572 |
|
| 573 |
if (fa->nfargs != 7)
|
| 574 |
return(-1);
|
| 575 |
setmat(mod);
|
| 576 |
setobj(id);
|
| 577 |
getvertid(v1, fa->farg);
|
| 578 |
getvertid(v2, fa->farg + 3);
|
| 579 |
printf("cyl %s %.12g %s\n", v1,
|
| 580 |
typ[0]=='t' ? -fa->farg[6] : fa->farg[6], v2);
|
| 581 |
return(0);
|
| 582 |
}
|
| 583 |
|
| 584 |
|
| 585 |
int
|
| 586 |
o_ring( /* print out a ring */
|
| 587 |
char *mod,
|
| 588 |
char *typ,
|
| 589 |
char *id,
|
| 590 |
register FUNARGS *fa
|
| 591 |
)
|
| 592 |
{
|
| 593 |
if (fa->nfargs != 8)
|
| 594 |
return(-1);
|
| 595 |
setmat(mod);
|
| 596 |
setobj(id);
|
| 597 |
printf("v cent =\n\tp %.12g %.12g %.12g\n",
|
| 598 |
fa->farg[0], fa->farg[1], fa->farg[2]);
|
| 599 |
printf("\tn %.12g %.12g %.12g\n",
|
| 600 |
fa->farg[3], fa->farg[4], fa->farg[5]);
|
| 601 |
if (fa->farg[6] < fa->farg[7])
|
| 602 |
printf("ring cent %.12g %.12g\n",
|
| 603 |
fa->farg[6], fa->farg[7]);
|
| 604 |
else
|
| 605 |
printf("ring cent %.12g %.12g\n",
|
| 606 |
fa->farg[7], fa->farg[6]);
|
| 607 |
return(0);
|
| 608 |
}
|
| 609 |
|
| 610 |
|
| 611 |
int
|
| 612 |
o_instance( /* convert an instance (or mesh) */
|
| 613 |
char *mod,
|
| 614 |
char *typ,
|
| 615 |
char *id,
|
| 616 |
FUNARGS *fa
|
| 617 |
)
|
| 618 |
{
|
| 619 |
register int i;
|
| 620 |
register char *cp;
|
| 621 |
char *start = NULL, *end = NULL;
|
| 622 |
/*
|
| 623 |
* We don't really know how to do this, so we just create
|
| 624 |
* a reference to an undefined MGF file and it's the user's
|
| 625 |
* responsibility to create this file and put the appropriate
|
| 626 |
* stuff into it.
|
| 627 |
*/
|
| 628 |
if (fa->nsargs < 1)
|
| 629 |
return(-1);
|
| 630 |
setmat(mod); /* only works if surfaces are void */
|
| 631 |
setobj(id);
|
| 632 |
for (cp = fa->sarg[0]; *cp; cp++) /* construct MGF file name */
|
| 633 |
if (*cp == '/')
|
| 634 |
start = cp+1;
|
| 635 |
else if (*cp == '.')
|
| 636 |
end = cp;
|
| 637 |
if (start == NULL)
|
| 638 |
start = fa->sarg[0];
|
| 639 |
if (end == NULL || start >= end)
|
| 640 |
end = cp;
|
| 641 |
fputs("i ", stdout); /* print include entity */
|
| 642 |
for (cp = start; cp < end; cp++)
|
| 643 |
putchar(*cp);
|
| 644 |
fputs(".mgf", stdout); /* add MGF suffix */
|
| 645 |
for (i = 1; i < fa->nsargs; i++) { /* add transform */
|
| 646 |
putchar(' ');
|
| 647 |
fputs(fa->sarg[i], stdout);
|
| 648 |
}
|
| 649 |
putchar('\n');
|
| 650 |
clrverts(); /* vertex id's no longer reliable */
|
| 651 |
return(0);
|
| 652 |
}
|
| 653 |
|
| 654 |
|
| 655 |
int
|
| 656 |
o_illum( /* convert an illum material */
|
| 657 |
char *mod,
|
| 658 |
char *typ,
|
| 659 |
char *id,
|
| 660 |
FUNARGS *fa
|
| 661 |
)
|
| 662 |
{
|
| 663 |
if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) {
|
| 664 |
newmat(id, fa->sarg[0]); /* just create alias */
|
| 665 |
return(0);
|
| 666 |
}
|
| 667 |
/* else create invisible material */
|
| 668 |
newmat(id, NULL);
|
| 669 |
puts("\tts 1 0");
|
| 670 |
return(0);
|
| 671 |
}
|
| 672 |
|
| 673 |
|
| 674 |
int
|
| 675 |
o_plastic( /* convert a plastic material */
|
| 676 |
char *mod,
|
| 677 |
char *typ,
|
| 678 |
char *id,
|
| 679 |
register FUNARGS *fa
|
| 680 |
)
|
| 681 |
{
|
| 682 |
COLOR cxyz, rrgb;
|
| 683 |
double d;
|
| 684 |
|
| 685 |
if (fa->nfargs != (typ[7]=='2' ? 6 : 5))
|
| 686 |
return(-1);
|
| 687 |
newmat(id, NULL);
|
| 688 |
rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
|
| 689 |
rgb_cie(cxyz, rrgb);
|
| 690 |
puts("\tc"); /* put diffuse component */
|
| 691 |
d = cxyz[0] + cxyz[1] + cxyz[2];
|
| 692 |
if (d > FTINY)
|
| 693 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
|
| 694 |
printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
|
| 695 |
if (fa->farg[3] > FTINY) { /* put specular component */
|
| 696 |
puts("\tc");
|
| 697 |
printf("\trs %.4f %.4f\n", fa->farg[3],
|
| 698 |
typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
|
| 699 |
fa->farg[4]);
|
| 700 |
}
|
| 701 |
return(0);
|
| 702 |
}
|
| 703 |
|
| 704 |
|
| 705 |
int
|
| 706 |
o_metal( /* convert a metal material */
|
| 707 |
char *mod,
|
| 708 |
char *typ,
|
| 709 |
char *id,
|
| 710 |
register FUNARGS *fa
|
| 711 |
)
|
| 712 |
{
|
| 713 |
COLOR cxyz, rrgb;
|
| 714 |
double d;
|
| 715 |
|
| 716 |
if (fa->nfargs != (typ[5]=='2' ? 6 : 5))
|
| 717 |
return(-1);
|
| 718 |
newmat(id, NULL);
|
| 719 |
rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
|
| 720 |
rgb_cie(cxyz, rrgb);
|
| 721 |
puts("\tc"); /* put diffuse component */
|
| 722 |
d = cxyz[0] + cxyz[1] + cxyz[2];
|
| 723 |
if (d > FTINY)
|
| 724 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
|
| 725 |
printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
|
| 726 |
/* put specular component */
|
| 727 |
printf("\trs %.4f %.4f\n", cxyz[1]*fa->farg[3],
|
| 728 |
typ[5]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
|
| 729 |
fa->farg[4]);
|
| 730 |
return(0);
|
| 731 |
}
|
| 732 |
|
| 733 |
|
| 734 |
int
|
| 735 |
o_glass( /* convert a glass material */
|
| 736 |
char *mod,
|
| 737 |
char *typ,
|
| 738 |
char *id,
|
| 739 |
register FUNARGS *fa
|
| 740 |
)
|
| 741 |
{
|
| 742 |
COLOR cxyz, rrgb, trgb;
|
| 743 |
double nrfr = 1.52, F, d;
|
| 744 |
register int i;
|
| 745 |
|
| 746 |
if (fa->nfargs != 3 && fa->nfargs != 4)
|
| 747 |
return(-1);
|
| 748 |
newmat(id, NULL);
|
| 749 |
if (fa->nfargs == 4)
|
| 750 |
nrfr = fa->farg[3];
|
| 751 |
printf("\tir %f 0\n", nrfr);
|
| 752 |
F = (1. - nrfr)/(1. + nrfr); /* use normal incidence */
|
| 753 |
F *= F;
|
| 754 |
for (i = 0; i < 3; i++) {
|
| 755 |
trgb[i] = fa->farg[i] * (1. - F)*(1. - F) /
|
| 756 |
(1. - F*F*fa->farg[i]*fa->farg[i]);
|
| 757 |
rrgb[i] = F * (1. + (1. - 2.*F)*fa->farg[i]) /
|
| 758 |
(1. - F*F*fa->farg[i]*fa->farg[i]);
|
| 759 |
}
|
| 760 |
rgb_cie(cxyz, rrgb); /* put reflected component */
|
| 761 |
puts("\tc");
|
| 762 |
d = cxyz[0] + cxyz[1] + cxyz[2];
|
| 763 |
if (d > FTINY)
|
| 764 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
|
| 765 |
printf("\trs %.4f 0\n", cxyz[1]);
|
| 766 |
rgb_cie(cxyz, trgb); /* put transmitted component */
|
| 767 |
puts("\tc");
|
| 768 |
d = cxyz[0] + cxyz[1] + cxyz[2];
|
| 769 |
if (d > FTINY)
|
| 770 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
|
| 771 |
printf("\tts %.4f 0\n", cxyz[1]);
|
| 772 |
return(0);
|
| 773 |
}
|
| 774 |
|
| 775 |
|
| 776 |
int
|
| 777 |
o_dielectric( /* convert a dielectric material */
|
| 778 |
char *mod,
|
| 779 |
char *typ,
|
| 780 |
char *id,
|
| 781 |
register FUNARGS *fa
|
| 782 |
)
|
| 783 |
{
|
| 784 |
COLOR cxyz, trgb;
|
| 785 |
double F, d;
|
| 786 |
register int i;
|
| 787 |
|
| 788 |
if (fa->nfargs != 5)
|
| 789 |
return(-1);
|
| 790 |
newmat(id, NULL);
|
| 791 |
F = (1. - fa->farg[3])/(1. + fa->farg[3]); /* normal incidence */
|
| 792 |
F *= F;
|
| 793 |
for (i = 0; i < 3; i++)
|
| 794 |
trgb[i] = (1. - F)*pow(fa->farg[i], C_1SIDEDTHICK/unit_mult);
|
| 795 |
printf("\tir %f 0\n", fa->farg[3]); /* put index of refraction */
|
| 796 |
printf("\tsides 1\n");
|
| 797 |
puts("\tc"); /* put reflected component */
|
| 798 |
printf("\trs %.4f 0\n", F);
|
| 799 |
rgb_cie(cxyz, trgb); /* put transmitted component */
|
| 800 |
puts("\tc");
|
| 801 |
d = cxyz[0] + cxyz[1] + cxyz[2];
|
| 802 |
if (d > FTINY)
|
| 803 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
|
| 804 |
printf("\tts %.4f 0\n", cxyz[1]);
|
| 805 |
return(0);
|
| 806 |
}
|
| 807 |
|
| 808 |
|
| 809 |
int
|
| 810 |
o_mirror( /* convert a mirror material */
|
| 811 |
char *mod,
|
| 812 |
char *typ,
|
| 813 |
char *id,
|
| 814 |
register FUNARGS *fa
|
| 815 |
)
|
| 816 |
{
|
| 817 |
COLOR cxyz, rrgb;
|
| 818 |
double d;
|
| 819 |
|
| 820 |
if (fa->nsargs == 1) { /* use alternate material */
|
| 821 |
newmat(id, fa->sarg[0]);
|
| 822 |
return(0);
|
| 823 |
}
|
| 824 |
if (fa->nfargs != 3)
|
| 825 |
return(-1);
|
| 826 |
newmat(id, NULL);
|
| 827 |
rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
|
| 828 |
rgb_cie(cxyz, rrgb);
|
| 829 |
puts("\tc"); /* put specular component */
|
| 830 |
d = cxyz[0] + cxyz[1] + cxyz[2];
|
| 831 |
if (d > FTINY)
|
| 832 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
|
| 833 |
printf("\trs %.4f 0\n", cxyz[1]);
|
| 834 |
return(0);
|
| 835 |
}
|
| 836 |
|
| 837 |
|
| 838 |
int
|
| 839 |
o_trans( /* convert a trans material */
|
| 840 |
char *mod,
|
| 841 |
char *typ,
|
| 842 |
char *id,
|
| 843 |
register FUNARGS *fa
|
| 844 |
)
|
| 845 |
{
|
| 846 |
COLOR cxyz, rrgb;
|
| 847 |
double rough, trans, tspec, d;
|
| 848 |
|
| 849 |
if (typ[5] == '2') { /* trans2 */
|
| 850 |
if (fa->nfargs != 8)
|
| 851 |
return(-1);
|
| 852 |
rough = .5*(fa->farg[4] + fa->farg[5]);
|
| 853 |
trans = fa->farg[6];
|
| 854 |
tspec = fa->farg[7];
|
| 855 |
} else { /* trans */
|
| 856 |
if (fa->nfargs != 7)
|
| 857 |
return(-1);
|
| 858 |
rough = fa->farg[4];
|
| 859 |
trans = fa->farg[5];
|
| 860 |
tspec = fa->farg[6];
|
| 861 |
}
|
| 862 |
newmat(id, NULL);
|
| 863 |
rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
|
| 864 |
rgb_cie(cxyz, rrgb);
|
| 865 |
puts("\tc"); /* put transmitted diffuse */
|
| 866 |
d = cxyz[0] + cxyz[1] + cxyz[2];
|
| 867 |
if (d > FTINY)
|
| 868 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
|
| 869 |
printf("\ttd %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec));
|
| 870 |
/* put transmitted specular */
|
| 871 |
printf("\tts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
|
| 872 |
/* put reflected diffuse */
|
| 873 |
printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans));
|
| 874 |
puts("\tc"); /* put reflected specular */
|
| 875 |
printf("\trs %.4f %.4f\n", fa->farg[3], rough);
|
| 876 |
return(0);
|
| 877 |
}
|
| 878 |
|
| 879 |
|
| 880 |
int
|
| 881 |
o_light( /* convert a light type */
|
| 882 |
char *mod,
|
| 883 |
char *typ,
|
| 884 |
char *id,
|
| 885 |
register FUNARGS *fa
|
| 886 |
)
|
| 887 |
{
|
| 888 |
COLOR cxyz, rrgb;
|
| 889 |
double d;
|
| 890 |
|
| 891 |
if (fa->nfargs < 3)
|
| 892 |
return(-1);
|
| 893 |
newmat(id, NULL);
|
| 894 |
rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
|
| 895 |
rgb_cie(cxyz, rrgb);
|
| 896 |
d = cxyz[0] + cxyz[1] + cxyz[2];
|
| 897 |
puts("\tc");
|
| 898 |
if (d > FTINY)
|
| 899 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
|
| 900 |
printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY));
|
| 901 |
return(0);
|
| 902 |
}
|