ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/rad2mgf.c
Revision: 2.4
Committed: Sat Jul 9 09:42:48 1994 UTC (29 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +22 -23 lines
Log Message:
moved things around a bit

File Contents

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