ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/rad2mgf.c
Revision: 2.14
Committed: Fri Sep 4 09:09:58 1998 UTC (25 years, 7 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.13: +2 -7 lines
Log Message:
added standard.h to rad2mgf.c includes

File Contents

# Content
1 /* Copyright (c) 1995 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 "standard.h"
12 #include <ctype.h>
13 #include <string.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_illum();
22 int o_plastic(), o_metal(), o_glass(), o_dielectric(),
23 o_mirror(), o_trans(), o_light();
24
25 extern int free();
26
27 LUTAB rmats = LU_SINIT(free,NULL); /* defined material table */
28
29 LUTAB rdispatch = LU_SINIT(NULL,NULL); /* function dispatch table */
30
31 char curmat[80]; /* current material */
32 char curobj[128] = "Untitled"; /* current object name */
33
34 double unit_mult = 1.; /* units multiplier */
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;
62 {
63 int i;
64
65 for (i = 1; i < argc && argv[i][0] == '-'; i++)
66 switch (argv[i][1]) {
67 case 'd': /* units */
68 switch (argv[i][2]) {
69 case 'm': /* meters */
70 unit_mult = 1.;
71 break;
72 case 'c': /* centimeters */
73 unit_mult = .01;
74 break;
75 case 'f': /* feet */
76 unit_mult = 12.*.0254;
77 break;
78 case 'i': /* inches */
79 unit_mult = .0254;
80 break;
81 default:
82 goto unkopt;
83 }
84 break;
85 default:
86 goto unkopt;
87 }
88 init();
89 if (i >= argc)
90 rad2mgf(NULL);
91 else
92 for ( ; i < argc; i++)
93 rad2mgf(argv[i]);
94 uninit();
95 exit(0);
96 unkopt:
97 fprintf(stderr, "Usage: %s [-d{m|c|f|i}] file ..\n", argv[0]);
98 exit(1);
99 }
100
101
102 rad2mgf(inp) /* convert a Radiance file to MGF */
103 char *inp;
104 {
105 #define mod buf
106 #define typ (buf+128)
107 #define id (buf+256)
108 #define alias (buf+384)
109 char buf[512];
110 FUNARGS fa;
111 register FILE *fp;
112 register int c;
113
114 if (inp == NULL) {
115 inp = "standard input";
116 fp = stdin;
117 } else if (inp[0] == '!') {
118 if ((fp = popen(inp+1, "r")) == NULL) {
119 fputs(inp, stderr);
120 fputs(": cannot execute\n", stderr);
121 exit(1);
122 }
123 } else if ((fp = fopen(inp, "r")) == NULL) {
124 fputs(inp, stderr);
125 fputs(": cannot open\n", stderr);
126 exit(1);
127 }
128 printf("# Begin conversion from: %s\n", inp);
129 while ((c = getc(fp)) != EOF)
130 switch (c) {
131 case ' ': /* white space */
132 case '\t':
133 case '\n':
134 case '\r':
135 case '\f':
136 break;
137 case '#': /* comment */
138 if (fgets(buf, sizeof(buf), fp) != NULL)
139 printf("# %s", buf);
140 break;
141 case '!': /* inline command */
142 ungetc(c, fp);
143 fgetline(buf, sizeof(buf), fp);
144 rad2mgf(buf);
145 break;
146 default: /* Radiance primitive */
147 ungetc(c, fp);
148 if (fscanf(fp, "%s %s %s", mod, typ, id) != 3) {
149 fputs(inp, stderr);
150 fputs(": unexpected EOF\n", stderr);
151 exit(1);
152 }
153 if (!strcmp(typ, "alias")) {
154 strcpy(alias, "EOF");
155 fscanf(fp, "%s", alias);
156 newmat(id, alias);
157 } else {
158 if (!readfargs(&fa, fp)) {
159 fprintf(stderr,
160 "%s: bad argument syntax for %s \"%s\"\n",
161 inp, typ, id);
162 exit(1);
163 }
164 cvtprim(inp, mod, typ, id, &fa);
165 freefargs(&fa);
166 }
167 break;
168 }
169 printf("# End conversion from: %s\n", inp);
170 if (inp[0] == '!')
171 pclose(fp);
172 else
173 fclose(fp);
174 #undef mod
175 #undef typ
176 #undef id
177 #undef alias
178 }
179
180
181 cvtprim(inp, mod, typ, id, fa) /* process Radiance primitive */
182 char *inp, *mod, *typ, *id;
183 FUNARGS *fa;
184 {
185 int (*df)();
186
187 df = (int (*)())lu_find(&rdispatch, typ)->data;
188 if (df != NULL) { /* convert */
189 if ((*df)(mod, typ, id, fa) < 0) {
190 fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id);
191 exit(1);
192 }
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
201 newmat(id, alias) /* add a modifier to the alias list */
202 char *id;
203 char *alias;
204 {
205 register LUENT *lp, *lpa;
206
207 if (alias != NULL) { /* look up alias */
208 if ((lpa = lu_find(&rmats, alias)) == NULL)
209 goto memerr;
210 if (lpa->data == NULL)
211 alias = NULL; /* doesn't exist! */
212 }
213 if ((lp = lu_find(&rmats, id)) == NULL) /* look up material */
214 goto memerr;
215 if (alias != NULL && lp->data == lpa->key)
216 return; /* alias set already */
217 if (lp->data == NULL) { /* allocate material */
218 if ((lp->key = (char *)malloc(strlen(id)+1)) == NULL)
219 goto memerr;
220 strcpy(lp->key, id);
221 }
222 if (alias == NULL) { /* set this material */
223 lp->data = lp->key;
224 printf("m %s =\n", id);
225 } else { /* set this alias */
226 lp->data = lpa->key;
227 printf("m %s = %s\n", id, alias);
228 }
229 strcpy(curmat, id);
230 return;
231 memerr:
232 fputs("Out of memory in newmat!\n", stderr);
233 exit(1);
234 }
235
236
237 setmat(id) /* set material to this one */
238 char *id;
239 {
240 if (!strcmp(id, curmat)) /* already set? */
241 return;
242 if (!strcmp(id, VOIDID)) /* cannot set */
243 return;
244 printf("m %s\n", id);
245 strcpy(curmat, id);
246 }
247
248
249 setobj(id) /* set object name to this one */
250 char *id;
251 {
252 register char *cp, *cp2;
253 char *end = NULL;
254 int diff = 0;
255 /* use all but final suffix */
256 for (cp = id; *cp; cp++)
257 if (*cp == '.')
258 end = cp;
259 if (end == NULL)
260 end = cp;
261 /* copy to current object */
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';
275 fputs("o\no ", stdout);
276 puts(curobj);
277 }
278
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);
286 add2dispatch("cup", o_cone);
287 add2dispatch("sphere", o_sphere);
288 add2dispatch("bubble", o_sphere);
289 add2dispatch("cylinder", o_cylinder);
290 add2dispatch("tube", o_cylinder);
291 add2dispatch("ring", o_ring);
292 add2dispatch("instance", o_instance);
293 add2dispatch("plastic", o_plastic);
294 add2dispatch("plastic2", o_plastic);
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);
302 add2dispatch("light", o_light);
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");
307 if (hasmult)
308 printf("xf -s %.4e\n", unit_mult);
309 printf("o %s\n", curobj);
310 }
311
312
313 uninit() /* mark end of MGF file */
314 {
315 puts("o");
316 if (hasmult)
317 puts("xf");
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)();
339 {
340 register LUENT *lp;
341
342 lp = lu_find(&rdispatch, name);
343 if (lp->key != NULL) {
344 fputs(name, stderr);
345 fputs(": duplicate dispatch entry!\n", stderr);
346 exit(1);
347 }
348 lp->key = name;
349 lp->data = (char *)func;
350 }
351
352
353 char *
354 getvertid(vname, vp) /* get/set vertex ID for this point */
355 char *vname;
356 FVECT vp;
357 {
358 static char vkey[VKLEN];
359 register LUENT *lp;
360 register int i, vndx;
361
362 vclock++; /* increment counter */
363 mkvkey(vkey, vp);
364 if ((lp = lu_find(&vertab, vkey)) == NULL)
365 goto memerr;
366 if (lp->data == NULL) { /* allocate new vertex entry */
367 if (lp->key != NULL) /* reclaim deleted entry */
368 vertab.ndel--;
369 else {
370 if ((lp->key = (char *)malloc(VKLEN)) == NULL)
371 goto memerr;
372 strcpy(lp->key, vkey);
373 }
374 vndx = 0; /* find oldest vertex */
375 for (i = 1; i < NVERTS; i++)
376 if (vert[i].lused < vert[vndx].lused)
377 vndx = i;
378 if (vert[vndx].lused) { /* free old entry first */
379 mkvkey(vkey, vert[vndx].p);
380 lu_delete(&vertab, vkey);
381 }
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:
392 fputs("Out of memory in getvertid!\n", stderr);
393 exit(1);
394 }
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[2048];
430 register char *cp;
431 register int i;
432
433 if (fa->nfargs < 9 | fa->nfargs % 3)
434 return(-1);
435 setmat(mod);
436 setobj(id);
437 cp = entbuf;
438 *cp++ = 'f';
439 for (i = 0; i < fa->nfargs; i += 3) {
440 *cp++ = ' ';
441 getvertid(cp, fa->farg + i);
442 while (*cp)
443 cp++;
444 }
445 puts(entbuf);
446 return(0);
447 }
448
449
450 int
451 o_cone(mod, typ, id, fa) /* print out a cone */
452 char *mod, *typ, *id;
453 register FUNARGS *fa;
454 {
455 char v1[6], v2[6];
456
457 if (fa->nfargs != 8)
458 return(-1);
459 setmat(mod);
460 setobj(id);
461 getvertid(v1, fa->farg);
462 getvertid(v2, fa->farg + 3);
463 if (typ[1] == 'u') /* cup -> inverted cone */
464 printf("cone %s %.12g %s %.12g\n",
465 v1, -fa->farg[6], v2, -fa->farg[7]);
466 else
467 printf("cone %s %.12g %s %.12g\n",
468 v1, fa->farg[6], v2, fa->farg[7]);
469 return(0);
470 }
471
472
473 int
474 o_sphere(mod, typ, id, fa) /* print out a sphere */
475 char *mod, *typ, *id;
476 register FUNARGS *fa;
477 {
478 char cent[6];
479
480 if (fa->nfargs != 4)
481 return(-1);
482 setmat(mod);
483 setobj(id);
484 printf("sph %s %.12g\n", getvertid(cent, fa->farg),
485 typ[0]=='b' ? -fa->farg[3] : fa->farg[3]);
486 return(0);
487 }
488
489
490 int
491 o_cylinder(mod, typ, id, fa) /* print out a cylinder */
492 char *mod, *typ, *id;
493 register FUNARGS *fa;
494 {
495 char v1[6], v2[6];
496
497 if (fa->nfargs != 7)
498 return(-1);
499 setmat(mod);
500 setobj(id);
501 getvertid(v1, fa->farg);
502 getvertid(v2, fa->farg + 3);
503 printf("cyl %s %.12g %s\n", v1,
504 typ[0]=='t' ? -fa->farg[6] : fa->farg[6], v2);
505 return(0);
506 }
507
508
509 int
510 o_ring(mod, typ, id, fa) /* print out a ring */
511 char *mod, *typ, *id;
512 register FUNARGS *fa;
513 {
514 if (fa->nfargs != 8)
515 return(-1);
516 setmat(mod);
517 setobj(id);
518 printf("v cent =\n\tp %.12g %.12g %.12g\n",
519 fa->farg[0], fa->farg[1], fa->farg[2]);
520 printf("\tn %.12g %.12g %.12g\n",
521 fa->farg[3], fa->farg[4], fa->farg[5]);
522 if (fa->farg[6] < fa->farg[7])
523 printf("ring cent %.12g %.12g\n",
524 fa->farg[6], fa->farg[7]);
525 else
526 printf("ring cent %.12g %.12g\n",
527 fa->farg[7], fa->farg[6]);
528 return(0);
529 }
530
531
532 int
533 o_instance(mod, typ, id, fa) /* convert an instance */
534 char *mod, *typ, *id;
535 FUNARGS *fa;
536 {
537 register int i;
538 register char *cp;
539 char *start = NULL, *end = NULL;
540 /*
541 * We don't really know how to do this, so we just create
542 * a reference to an undefined MGF file and it's the user's
543 * responsibility to create this file and put the appropriate
544 * stuff into it.
545 */
546 if (fa->nsargs < 1)
547 return(-1);
548 setmat(mod); /* only works if surfaces are void */
549 setobj(id);
550 for (cp = fa->sarg[0]; *cp; cp++) /* construct MGF file name */
551 if (*cp == '/')
552 start = cp+1;
553 else if (*cp == '.')
554 end = cp;
555 if (start == NULL)
556 start = fa->sarg[0];
557 if (end == NULL || start >= end)
558 end = cp;
559 fputs("i ", stdout); /* print include entity */
560 for (cp = start; cp < end; cp++)
561 putchar(*cp);
562 fputs(".mgf", stdout); /* add MGF suffix */
563 for (i = 1; i < fa->nsargs; i++) { /* add transform */
564 putchar(' ');
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
574 o_illum(mod, typ, id, fa) /* convert an illum material */
575 char *mod, *typ, *id;
576 FUNARGS *fa;
577 {
578 if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) {
579 newmat(id, fa->sarg[0]); /* just create alias */
580 return(0);
581 }
582 /* else create invisible material */
583 newmat(id, NULL);
584 puts("\tts 1 0");
585 return(0);
586 }
587
588
589 int
590 o_plastic(mod, typ, id, fa) /* convert a plastic material */
591 char *mod, *typ, *id;
592 register FUNARGS *fa;
593 {
594 COLOR cxyz, rrgb;
595 double d;
596
597 if (fa->nfargs != (typ[7]=='2' ? 6 : 5))
598 return(-1);
599 newmat(id, NULL);
600 rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
601 rgb_cie(cxyz, rrgb);
602 puts("\tc"); /* put diffuse component */
603 d = cxyz[0] + cxyz[1] + cxyz[2];
604 if (d > FTINY)
605 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
606 printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
607 if (fa->farg[3] > FTINY) { /* put specular component */
608 puts("\tc");
609 printf("\trs %.4f %.4f\n", fa->farg[3],
610 typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
611 fa->farg[4]);
612 }
613 return(0);
614 }
615
616
617 int
618 o_metal(mod, typ, id, fa) /* convert a metal material */
619 char *mod, *typ, *id;
620 register FUNARGS *fa;
621 {
622 COLOR cxyz, rrgb;
623 double d;
624
625 if (fa->nfargs != (typ[5]=='2' ? 6 : 5))
626 return(-1);
627 newmat(id, NULL);
628 rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
629 rgb_cie(cxyz, rrgb);
630 puts("\tc"); /* put diffuse component */
631 d = cxyz[0] + cxyz[1] + cxyz[2];
632 if (d > FTINY)
633 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
634 printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
635 /* put specular component */
636 printf("\trs %.4f %.4f\n", cxyz[1]*fa->farg[3],
637 typ[5]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
638 fa->farg[4]);
639 return(0);
640 }
641
642
643 int
644 o_glass(mod, typ, id, fa) /* convert a glass material */
645 char *mod, *typ, *id;
646 register FUNARGS *fa;
647 {
648 COLOR cxyz, rrgb, trgb;
649 double nrfr = 1.52, F, d;
650 register int i;
651
652 if (fa->nfargs != 3 && fa->nfargs != 4)
653 return(-1);
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++) {
661 trgb[i] = fa->farg[i] * (1. - F)*(1. - F) /
662 (1. - F*F*fa->farg[i]*fa->farg[i]);
663 rrgb[i] = F * (1. + (1. - 2.*F)*fa->farg[i]) /
664 (1. - F*F*fa->farg[i]*fa->farg[i]);
665 }
666 rgb_cie(cxyz, rrgb); /* put reflected component */
667 puts("\tc");
668 d = cxyz[0] + cxyz[1] + cxyz[2];
669 if (d > FTINY)
670 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
671 printf("\trs %.4f 0\n", cxyz[1]);
672 rgb_cie(cxyz, trgb); /* put transmitted component */
673 puts("\tc");
674 d = cxyz[0] + cxyz[1] + cxyz[2];
675 if (d > FTINY)
676 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
677 printf("\tts %.4f 0\n", cxyz[1]);
678 return(0);
679 }
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;
716 {
717 COLOR cxyz, rrgb;
718 double d;
719
720 if (fa->nsargs == 1) { /* use alternate material */
721 newmat(id, fa->sarg[0]);
722 return(0);
723 }
724 if (fa->nfargs != 3)
725 return(-1);
726 newmat(id, NULL);
727 rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
728 rgb_cie(cxyz, rrgb);
729 puts("\tc"); /* put specular component */
730 d = cxyz[0] + cxyz[1] + cxyz[2];
731 if (d > FTINY)
732 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
733 printf("\trs %.4f 0\n", cxyz[1]);
734 return(0);
735 }
736
737
738 int
739 o_trans(mod, typ, id, fa) /* convert a trans material */
740 char *mod, *typ, *id;
741 register FUNARGS *fa;
742 {
743 COLOR cxyz, rrgb;
744 double rough, trans, tspec, d;
745
746 if (typ[4] == '2') { /* trans2 */
747 if (fa->nfargs != 8)
748 return(-1);
749 rough = .5*(fa->farg[4] + fa->farg[5]);
750 trans = fa->farg[6];
751 tspec = fa->farg[7];
752 } else { /* trans */
753 if (fa->nfargs != 7)
754 return(-1);
755 rough = fa->farg[4];
756 trans = fa->farg[5];
757 tspec = fa->farg[6];
758 }
759 newmat(id, NULL);
760 rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
761 rgb_cie(cxyz, rrgb);
762 puts("\tc"); /* put transmitted diffuse */
763 d = cxyz[0] + cxyz[1] + cxyz[2];
764 if (d > FTINY)
765 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
766 printf("\ttd %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec));
767 /* put transmitted specular */
768 printf("\tts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
769 /* put reflected diffuse */
770 printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans));
771 puts("\tc"); /* put reflected specular */
772 printf("\trs %.4f %.4f\n", fa->farg[3], rough);
773 return(0);
774 }
775
776
777 int
778 o_light(mod, typ, id, fa) /* convert a light type */
779 char *mod, *typ, *id;
780 register FUNARGS *fa;
781 {
782 COLOR cxyz, rrgb;
783 double d;
784
785 if (fa->nfargs < 3)
786 return(-1);
787 newmat(id, NULL);
788 rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
789 rgb_cie(cxyz, rrgb);
790 d = cxyz[0] + cxyz[1] + cxyz[2];
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]*(PI*WHTEFFICACY));
795 return(0);
796 }