| 1 |
< |
/* Copyright (c) 1994 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 8 |
|
* Convert Radiance scene description to MGF |
| 9 |
|
*/ |
| 10 |
|
|
| 11 |
< |
#include <stdio.h> |
| 11 |
> |
#include "standard.h" |
| 12 |
> |
#include <ctype.h> |
| 13 |
|
#include <string.h> |
| 13 |
– |
#include "fvect.h" |
| 14 |
|
#include "object.h" |
| 15 |
|
#include "color.h" |
| 16 |
|
#include "lookup.h" |
| 17 |
|
|
| 18 |
+ |
#define C_1SIDEDTHICK 0.005 |
| 19 |
+ |
|
| 20 |
|
int o_face(), o_cone(), o_sphere(), o_ring(), o_cylinder(); |
| 21 |
< |
int o_instance(), o_source(), o_illum(); |
| 22 |
< |
int o_plastic(), o_metal(), o_glass(), o_mirror(), o_trans(), o_light(); |
| 21 |
> |
int o_instance(), o_illum(); |
| 22 |
> |
int o_plastic(), o_metal(), o_glass(), o_dielectric(), |
| 23 |
> |
o_mirror(), o_trans(), o_light(); |
| 24 |
|
|
| 25 |
< |
extern void free(); |
| 23 |
< |
extern char *malloc(); |
| 25 |
> |
extern int free(); |
| 26 |
|
|
| 27 |
|
LUTAB rmats = LU_SINIT(free,NULL); /* defined material table */ |
| 28 |
|
|
| 35 |
|
|
| 36 |
|
#define hasmult (unit_mult < .999 || unit_mult > 1.001) |
| 37 |
|
|
| 38 |
+ |
/* |
| 39 |
+ |
* Stuff for tracking and reusing vertices: |
| 40 |
+ |
*/ |
| 41 |
|
|
| 42 |
+ |
char VKFMT[] = "%+16.9e %+16.9e %+16.9e"; |
| 43 |
+ |
#define VKLEN 64 |
| 44 |
+ |
|
| 45 |
+ |
#define mkvkey(k,v) sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2]) |
| 46 |
+ |
|
| 47 |
+ |
#define NVERTS 256 |
| 48 |
+ |
|
| 49 |
+ |
long vclock; /* incremented at each vertex request */ |
| 50 |
+ |
|
| 51 |
+ |
struct vert { |
| 52 |
+ |
long lused; /* when last used (0 if unassigned) */ |
| 53 |
+ |
FVECT p; /* track point position only */ |
| 54 |
+ |
} vert[NVERTS]; /* our vertex cache */ |
| 55 |
+ |
|
| 56 |
+ |
LUTAB vertab = LU_SINIT(free,NULL); /* our vertex lookup table */ |
| 57 |
+ |
|
| 58 |
+ |
|
| 59 |
|
main(argc, argv) |
| 60 |
|
int argc; |
| 61 |
|
char **argv; |
| 82 |
|
goto unkopt; |
| 83 |
|
} |
| 84 |
|
break; |
| 85 |
+ |
default: |
| 86 |
+ |
goto unkopt; |
| 87 |
|
} |
| 88 |
|
init(); |
| 89 |
|
if (i >= argc) |
| 190 |
|
fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id); |
| 191 |
|
exit(1); |
| 192 |
|
} |
| 193 |
< |
} else if (lu_find(&rmats, mod)->data != NULL) /* make alias */ |
| 194 |
< |
newmat(id, mod); |
| 193 |
> |
} else { /* unsupported */ |
| 194 |
> |
o_unsupported(mod, typ, id, fa); |
| 195 |
> |
if (lu_find(&rmats, mod)->data != NULL) /* make alias */ |
| 196 |
> |
newmat(id, mod); |
| 197 |
> |
} |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
|
| 259 |
|
if (end == NULL) |
| 260 |
|
end = cp; |
| 261 |
|
/* copy to current object */ |
| 262 |
< |
for (cp = id, cp2 = curobj; cp < end; *cp2++ = *cp++) |
| 262 |
> |
cp2 = curobj; |
| 263 |
> |
if (!isalpha(*id)) { /* start with letter */ |
| 264 |
> |
diff = *cp2 != 'O'; |
| 265 |
> |
*cp2++ = 'O'; |
| 266 |
> |
} |
| 267 |
> |
for (cp = id; cp < end; *cp2++ = *cp++) { |
| 268 |
> |
if (*cp < '!' | *cp > '~') /* limit to visible chars */ |
| 269 |
> |
*cp = '?'; |
| 270 |
|
diff += *cp != *cp2; |
| 271 |
+ |
} |
| 272 |
|
if (!diff && !*cp2) |
| 273 |
|
return; |
| 274 |
|
*cp2 = '\0'; |
| 279 |
|
|
| 280 |
|
init() /* initialize dispatch table and output */ |
| 281 |
|
{ |
| 282 |
+ |
lu_init(&vertab, NVERTS); |
| 283 |
|
lu_init(&rdispatch, 22); |
| 284 |
|
add2dispatch("polygon", o_face); |
| 285 |
|
add2dispatch("cone", o_cone); |
| 295 |
|
add2dispatch("metal", o_metal); |
| 296 |
|
add2dispatch("metal2", o_metal); |
| 297 |
|
add2dispatch("glass", o_glass); |
| 298 |
+ |
add2dispatch("dielectric", o_dielectric); |
| 299 |
|
add2dispatch("trans", o_trans); |
| 300 |
|
add2dispatch("trans2", o_trans); |
| 301 |
|
add2dispatch("mirror", o_mirror); |
| 303 |
|
add2dispatch("spotlight", o_light); |
| 304 |
|
add2dispatch("glow", o_light); |
| 305 |
|
add2dispatch("illum", o_illum); |
| 306 |
< |
puts("# The following was converted from Radiance scene input"); |
| 306 |
> |
puts("# The following was converted from RADIANCE scene input"); |
| 307 |
|
if (hasmult) |
| 308 |
|
printf("xf -s %.4e\n", unit_mult); |
| 309 |
|
printf("o %s\n", curobj); |
| 315 |
|
puts("o"); |
| 316 |
|
if (hasmult) |
| 317 |
|
puts("xf"); |
| 318 |
< |
puts("# End of data converted from Radiance scene input"); |
| 318 |
> |
puts("# End of data converted from RADIANCE scene input"); |
| 319 |
|
lu_done(&rdispatch); |
| 320 |
|
lu_done(&rmats); |
| 321 |
+ |
lu_done(&vertab); |
| 322 |
|
} |
| 323 |
|
|
| 324 |
|
|
| 325 |
+ |
clrverts() /* clear vertex table */ |
| 326 |
+ |
{ |
| 327 |
+ |
register int i; |
| 328 |
+ |
|
| 329 |
+ |
lu_done(&vertab); |
| 330 |
+ |
for (i = 0; i < NVERTS; i++) |
| 331 |
+ |
vert[i].lused = 0; |
| 332 |
+ |
lu_init(&vertab, NVERTS); |
| 333 |
+ |
} |
| 334 |
+ |
|
| 335 |
+ |
|
| 336 |
|
add2dispatch(name, func) /* add function to dispatch table */ |
| 337 |
|
char *name; |
| 338 |
|
int (*func)(); |
| 350 |
|
} |
| 351 |
|
|
| 352 |
|
|
| 304 |
– |
/* |
| 305 |
– |
* Stuff for tracking and reusing vertices: |
| 306 |
– |
*/ |
| 307 |
– |
|
| 308 |
– |
char VKFMT[] = "%+1.9e %+1.9e %+1.9e"; |
| 309 |
– |
#define VKLEN 64 |
| 310 |
– |
|
| 311 |
– |
#define mkvkey(k,v) sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2]) |
| 312 |
– |
|
| 313 |
– |
#define NVERTS 256 |
| 314 |
– |
|
| 315 |
– |
long clock; /* incremented at each vertex request */ |
| 316 |
– |
|
| 317 |
– |
struct vert { |
| 318 |
– |
long lused; /* when last used (0 if unassigned) */ |
| 319 |
– |
FVECT p; /* track point position only */ |
| 320 |
– |
} vert[NVERTS]; |
| 321 |
– |
|
| 322 |
– |
LUTAB vertab = LU_SINIT(free,NULL); /* our vertex lookup table */ |
| 323 |
– |
|
| 324 |
– |
|
| 353 |
|
char * |
| 354 |
|
getvertid(vname, vp) /* get/set vertex ID for this point */ |
| 355 |
|
char *vname; |
| 356 |
|
FVECT vp; |
| 357 |
|
{ |
| 358 |
< |
char vkey[VKLEN]; |
| 358 |
> |
static char vkey[VKLEN]; |
| 359 |
|
register LUENT *lp; |
| 360 |
|
register int i, vndx; |
| 361 |
|
|
| 362 |
< |
if (!vertab.tsiz && !lu_init(&vertab, NVERTS)) |
| 335 |
< |
goto memerr; |
| 336 |
< |
clock++; /* increment counter */ |
| 362 |
> |
vclock++; /* increment counter */ |
| 363 |
|
mkvkey(vkey, vp); |
| 364 |
|
if ((lp = lu_find(&vertab, vkey)) == NULL) |
| 365 |
|
goto memerr; |
| 379 |
|
mkvkey(vkey, vert[vndx].p); |
| 380 |
|
lu_delete(&vertab, vkey); |
| 381 |
|
} |
| 382 |
< |
vert[vndx].lused = clock; /* assign it */ |
| 357 |
< |
VCOPY(vert[vndx].p, vp); |
| 382 |
> |
VCOPY(vert[vndx].p, vp); /* assign it */ |
| 383 |
|
printf("v v%d =\n\tp %.15g %.15g %.15g\n", /* print it */ |
| 384 |
|
vndx, vp[0], vp[1], vp[2]); |
| 385 |
|
lp->data = (char *)&vert[vndx]; /* set it */ |
| 386 |
|
} else |
| 387 |
|
vndx = (struct vert *)lp->data - vert; |
| 388 |
+ |
vert[vndx].lused = vclock; /* record this use */ |
| 389 |
|
sprintf(vname, "v%d", vndx); |
| 390 |
|
return(vname); |
| 391 |
|
memerr: |
| 395 |
|
|
| 396 |
|
|
| 397 |
|
int |
| 398 |
+ |
o_unsupported(mod, typ, id, fa) /* mark unsupported primitive */ |
| 399 |
+ |
char *mod, *typ, *id; |
| 400 |
+ |
FUNARGS *fa; |
| 401 |
+ |
{ |
| 402 |
+ |
register int i; |
| 403 |
+ |
|
| 404 |
+ |
fputs("\n# Unsupported RADIANCE primitive:\n", stdout); |
| 405 |
+ |
printf("# %s %s %s", mod, typ, id); |
| 406 |
+ |
printf("\n# %d", fa->nsargs); |
| 407 |
+ |
for (i = 0; i < fa->nsargs; i++) |
| 408 |
+ |
printf(" %s", fa->sarg[i]); |
| 409 |
+ |
#ifdef IARGS |
| 410 |
+ |
printf("\n# %d", fa->niargs); |
| 411 |
+ |
for (i = 0; i < fa->niargs; i++) |
| 412 |
+ |
printf(" %ld", fa->iarg[i]); |
| 413 |
+ |
#else |
| 414 |
+ |
fputs("\n# 0", stdout); |
| 415 |
+ |
#endif |
| 416 |
+ |
printf("\n# %d", fa->nfargs); |
| 417 |
+ |
for (i = 0; i < fa->nfargs; i++) |
| 418 |
+ |
printf(" %g", fa->farg[i]); |
| 419 |
+ |
fputs("\n\n", stdout); |
| 420 |
+ |
return(0); |
| 421 |
+ |
} |
| 422 |
+ |
|
| 423 |
+ |
|
| 424 |
+ |
int |
| 425 |
|
o_face(mod, typ, id, fa) /* print out a polygon */ |
| 426 |
|
char *mod, *typ, *id; |
| 427 |
|
FUNARGS *fa; |
| 428 |
|
{ |
| 429 |
< |
char entbuf[512]; |
| 429 |
> |
char entbuf[2048]; |
| 430 |
|
register char *cp; |
| 431 |
|
register int i; |
| 432 |
|
|
| 565 |
|
fputs(fa->sarg[i], stdout); |
| 566 |
|
} |
| 567 |
|
putchar('\n'); |
| 568 |
+ |
clrverts(); /* vertex id's no longer reliable */ |
| 569 |
|
return(0); |
| 570 |
|
} |
| 571 |
|
|
| 572 |
|
|
| 573 |
|
int |
| 520 |
– |
o_source(mod, typ, id, fa) /* convert a source */ |
| 521 |
– |
char *mod, *typ, *id; |
| 522 |
– |
FUNARGS *fa; |
| 523 |
– |
{ |
| 524 |
– |
return(0); /* there is no MGF equivalent! */ |
| 525 |
– |
} |
| 526 |
– |
|
| 527 |
– |
|
| 528 |
– |
int |
| 574 |
|
o_illum(mod, typ, id, fa) /* convert an illum material */ |
| 575 |
|
char *mod, *typ, *id; |
| 576 |
|
FUNARGS *fa; |
| 654 |
|
newmat(id, NULL); |
| 655 |
|
if (fa->nfargs == 4) |
| 656 |
|
nrfr = fa->farg[3]; |
| 657 |
+ |
printf("\tir %f 0\n", nrfr); |
| 658 |
|
F = (1. - nrfr)/(1. + nrfr); /* use normal incidence */ |
| 659 |
|
F *= F; |
| 660 |
|
for (i = 0; i < 3; i++) { |
| 680 |
|
|
| 681 |
|
|
| 682 |
|
int |
| 683 |
+ |
o_dielectric(mod, typ, id, fa) /* convert a dielectric material */ |
| 684 |
+ |
char *mod, *typ, *id; |
| 685 |
+ |
register FUNARGS *fa; |
| 686 |
+ |
{ |
| 687 |
+ |
COLOR cxyz, trgb; |
| 688 |
+ |
double F, d; |
| 689 |
+ |
register int i; |
| 690 |
+ |
|
| 691 |
+ |
if (fa->nfargs != 5) |
| 692 |
+ |
return(-1); |
| 693 |
+ |
newmat(id, NULL); |
| 694 |
+ |
F = (1. - fa->farg[3])/(1. + fa->farg[3]); /* normal incidence */ |
| 695 |
+ |
F *= F; |
| 696 |
+ |
for (i = 0; i < 3; i++) |
| 697 |
+ |
trgb[i] = (1. - F)*pow(fa->farg[i], C_1SIDEDTHICK/unit_mult); |
| 698 |
+ |
printf("\tir %f 0\n", fa->farg[3]); /* put index of refraction */ |
| 699 |
+ |
printf("\tsides 1\n"); |
| 700 |
+ |
puts("\tc"); /* put reflected component */ |
| 701 |
+ |
printf("\trs %.4f 0\n", F); |
| 702 |
+ |
rgb_cie(cxyz, trgb); /* put transmitted component */ |
| 703 |
+ |
puts("\tc"); |
| 704 |
+ |
d = cxyz[0] + cxyz[1] + cxyz[2]; |
| 705 |
+ |
if (d > FTINY) |
| 706 |
+ |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
| 707 |
+ |
printf("\tts %.4f 0\n", cxyz[1]); |
| 708 |
+ |
return(0); |
| 709 |
+ |
} |
| 710 |
+ |
|
| 711 |
+ |
|
| 712 |
+ |
int |
| 713 |
|
o_mirror(mod, typ, id, fa) /* convert a mirror material */ |
| 714 |
|
char *mod, *typ, *id; |
| 715 |
|
register FUNARGS *fa; |
| 791 |
|
puts("\tc"); |
| 792 |
|
if (d > FTINY) |
| 793 |
|
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
| 794 |
< |
printf("\ted %.4g\n", cxyz[1]*WHTEFFICACY); |
| 794 |
> |
printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY)); |
| 795 |
|
return(0); |
| 796 |
|
} |