ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/rad2mgf.c
(Generate patch)

Comparing ray/src/cv/rad2mgf.c (file contents):
Revision 2.1 by greg, Thu Jul 7 17:30:33 1994 UTC vs.
Revision 2.25 by greg, Wed Dec 28 18:39:07 2005 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Convert Radiance scene description to MGF
6   */
7  
8 < #include <stdio.h>
8 > #include <ctype.h>
9   #include <string.h>
10 < #include "fvect.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 < 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();
20 > #define C_1SIDEDTHICK   0.005
21  
22 extern void     free();
23 extern char     *malloc();
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 */
27 > char    curmat[80];                             /* current material */
28 > char    curobj[128] = "Untitled";               /* current object name */
29  
30 < double  unit_mult = 1.;         /* units multiplier */
30 > double  unit_mult = 1.;                         /* units multiplier */
31  
32 + #define hasmult         (unit_mult < .999 || unit_mult > 1.001)
33  
34 < main(argc, argv)
35 < int     argc;
36 < char    **argv;
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  
# Line 57 | Line 107 | char   **argv;
107                                  goto unkopt;
108                          }
109                          break;
110 +                default:
111 +                        goto unkopt;
112                  }
113          init();
114          if (i >= argc)
# Line 72 | Line 124 | unkopt:
124   }
125  
126  
127 < rad2mgf(inp)            /* convert a Radiance file to MGF */
128 < char    *inp;
127 > void
128 > rad2mgf(                /* convert a Radiance file to MGF */
129 >        char    *inp
130 > )
131   {
132 < #define mod     buf
133 < #define typ     (buf+128)
80 < #define id      (buf+256)
81 < #define alias   (buf+384)
82 <        char    buf[512];
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 = "the standard input";
139 >                inp = "standard input";
140                  fp = stdin;
141          } else if (inp[0] == '!') {
142                  if ((fp = popen(inp+1, "r")) == NULL) {
# Line 118 | Line 169 | char   *inp;
169                          break;
170                  default:                /* Radiance primitive */
171                          ungetc(c, fp);
172 <                        if (fscanf(fp, "%s %s %s", mod, typ, id) != 3) {
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 <                                fscanf(fp, "%s", alias);
183 >                                fgetword(alias, sizeof(alias), fp);
184 >                                unspace(alias);
185                                  newmat(id, alias);
186                          } else {
187                                  if (!readfargs(&fa, fp)) {
# Line 144 | Line 200 | char   *inp;
200                  pclose(fp);
201          else
202                  fclose(fp);
147 #undef mod
148 #undef typ
149 #undef id
150 #undef alias
203   }
204  
205  
206 < cvtprim(inp, mod, typ, id, fa)  /* process Radiance primitive */
207 < char    *inp, *mod, *typ, *id;
208 < FUNARGS *fa;
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", typ, id);
233 >                        fprintf(stderr, "%s: bad %s \"%s\"\n", "rad2mgf", typ, id);
234                          exit(1);
235                  }
236 <        } else if (lu_find(&rmats, mod)->data != NULL)  /* make alias */
237 <                newmat(id, mod);
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 < newmat(id, alias)               /* add a modifier to the alias list */
245 < char    *id;
246 < char    *alias;
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  
# Line 204 | Line 279 | memerr:
279   }
280  
281  
282 < setmat(id)                      /* set material to this one */
283 < char    *id;
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 < init()                  /* initialize dispatch table and output */
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);
# Line 226 | Line 340 | init()                 /* initialize dispatch table and output */
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);
# Line 238 | Line 354 | init()                 /* initialize dispatch table and output */
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 (unit_mult < .999 || unit_mult > 1.001)
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 < uninit()                        /* mark end of MGF file */
364 > void
365 > uninit(void)                    /* mark end of MGF file */
366   {
367 <        if (unit_mult < .999 || unit_mult > 1.001)
367 >        puts("o");
368 >        if (hasmult)
369                  puts("xf");
370 <        puts("# End of data converted from Radiance scene input");
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 < add2dispatch(name, func)        /* add function to dispatch table */
378 < char    *name;
259 < int     (*func)();
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);
# Line 271 | Line 405 | int    (*func)();
405   }
406  
407  
274 char    VKFMT[] = "%+1.9e %+1.9e %+1.9e";
275 #define VKLEN           64
276
277 #define mkvkey(k,v)     sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2])
278
279 #define NVERTS          256
280
281 long    clock;          /* incremented at each vertex request */
282
283 struct vert {
284        long    lused;          /* when last used (0 if unassigned) */
285        FVECT   p;              /* track point position only */
286 } vert[NVERTS];
287
288 LUTAB   vertab = LU_SINIT(free,NULL);   /* our vertex lookup table */
289
290
408   char *
409 < getvertid(vp)                   /* get/set vertex ID for this point */
410 < FVECT   vp;
409 > getvertid(              /* get/set vertex ID for this point */
410 >        char    *vname,
411 >        FVECT   vp
412 > )
413   {
414 <        static char     vname[6];
296 <        char    vkey[VKLEN];
414 >        static char     vkey[VKLEN];
415          register LUENT  *lp;
416          register int    i, vndx;
417  
418 <        if (!vertab.tsiz && !lu_init(&vertab, NVERTS))
301 <                goto memerr;
302 <        clock++;                        /* increment counter */
418 >        vclock++;                       /* increment counter */
419          mkvkey(vkey, vp);
420          if ((lp = lu_find(&vertab, vkey)) == NULL)
421                  goto memerr;
# Line 319 | Line 435 | FVECT  vp;
435                          mkvkey(vkey, vert[vndx].p);
436                          lu_delete(&vertab, vkey);
437                  }
438 <                vert[vndx].lused = clock;                       /* assign it */
439 <                VCOPY(vert[vndx].p, vp);
324 <                printf("v v%d =\np %.15g %.15g %.15g\n",        /* print it */
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:
# Line 335 | Line 451 | memerr:
451  
452  
453   int
454 < o_face(mod, typ, id, fa)                /* print out a polygon */
455 < char    *mod, *typ, *id;
456 < FUNARGS *fa;
454 > o_unsupported(          /* mark unsupported primitive */
455 >        char    *mod,
456 >        char    *typ,
457 >        char    *id,
458 >        FUNARGS *fa
459 > )
460   {
342        char    entbuf[512];
343        register char   *cp1, *cp2;
461          register int    i;
462  
463 <        if (fa->nfargs < 9 | fa->nfargs % 3)
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 <        printf("o %s\n", id);
499 <        cp1 = entbuf;
500 <        *cp1++ = 'f';
498 >        setobj(id);
499 >        cp = linestart = entbuf;
500 >        *cp++ = 'f';
501          for (i = 0; i < fa->nfargs; i += 3) {
502 <                cp2 = getvertid(fa->farg + i);
503 <                *cp1++ = ' ';
504 <                while ((*cp1 = *cp2++))
505 <                        cp1++;
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);
359        puts("o");
513          return(0);
514   }
515  
516  
517   int
518 < o_cone(mod, typ, id, fa)        /* print out a cone */
519 < char    *mod, *typ, *id;
520 < register FUNARGS        *fa;
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 <        printf("o %s\n", id);
531 <        printf("v cv1 =\np %.12g %.12g %.12g\n",
532 <                        fa->farg[0], fa->farg[1], fa->farg[2]);
375 <        printf("v cv2 =\np %.12g %.12g %.12g\n",
376 <                        fa->farg[3], fa->farg[4], fa->farg[5]);
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 cv1 %.12g cv2 %.12g\n",
535 <                                -fa->farg[6], -fa->farg[7]);
534 >                printf("cone %s %.12g %s %.12g\n",
535 >                                v1, -fa->farg[6], v2, -fa->farg[7]);
536          else
537 <                printf("cone cv1 %.12g cv2 %.12g\n",
538 <                                fa->farg[6], fa->farg[7]);
383 <        puts("o");
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(mod, typ, id, fa)      /* print out a sphere */
545 < char    *mod, *typ, *id;
546 < register FUNARGS        *fa;
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 <        printf("o %s\n", id);
557 <        printf("v cent =\np %.12g %.12g %.12g\n",
558 <                        fa->farg[0], fa->farg[1], fa->farg[2]);
399 <        printf("sph cent %.12g\n", typ[0]=='b' ? -fa->farg[3] : fa->farg[3]);
400 <        puts("o");
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(mod, typ, id, fa)    /* print out a cylinder */
565 < char    *mod, *typ, *id;
566 < register FUNARGS        *fa;
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 <        printf("o %s\n", id);
577 <        printf("v cv1 =\np %.12g %.12g %.12g\n",
578 <                        fa->farg[0], fa->farg[1], fa->farg[2]);
579 <        printf("v cv2 =\np %.12g %.12g %.12g\n",
580 <                        fa->farg[3], fa->farg[4], fa->farg[5]);
418 <        printf("cyl cv1 %.12g cv2\n",
419 <                        typ[0]=='t' ? -fa->farg[6] : fa->farg[6]);
420 <        puts("o");
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(mod, typ, id, fa)        /* print out a ring */
587 < char    *mod, *typ, *id;
588 < register FUNARGS        *fa;
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 <        printf("o %s\n", id);
597 <        printf("v cent =\np %.12g %.12g %.12g\n",
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("n %.12g %.12g %.12g\n",
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",
# Line 441 | Line 604 | register FUNARGS       *fa;
604          else
605                  printf("ring cent %.12g %.12g\n",
606                                  fa->farg[7], fa->farg[6]);
444        puts("o");
607          return(0);
608   }
609  
610  
611   int
612 < o_instance(mod, typ, id, fa)    /* convert an instance */
613 < char    *mod, *typ, *id;
614 < FUNARGS *fa;
612 > o_instance(     /* convert an instance (or mesh) */
613 >        char    *mod,
614 >        char    *typ,
615 >        char    *id,
616 >        FUNARGS *fa
617 > )
618   {
619 <        return(0);              /* this is too damned difficult! */
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_source(mod, typ, id, fa)      /* convert a source */
657 < char    *mod, *typ, *id;
658 < FUNARGS *fa;
656 > o_illum(        /* convert an illum material */
657 >        char    *mod,
658 >        char    *typ,
659 >        char    *id,
660 >        FUNARGS *fa
661 > )
662   {
463        return(0);              /* there is no MGF equivalent! */
464 }
465
466
467 int
468 o_illum(mod, typ, id, fa)       /* convert an illum material */
469 char    *mod, *typ, *id;
470 FUNARGS *fa;
471 {
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("ts 1 0");
669 >        puts("\tts 1 0");
670          return(0);
671   }
672  
673  
674   int
675 < o_plastic(mod, typ, id, fa)     /* convert a plastic material */
676 < char    *mod, *typ, *id;
677 < register FUNARGS        *fa;
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;
# Line 493 | Line 687 | register FUNARGS       *fa;
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("c");                              /* put diffuse component */
690 >        puts("\tc");                            /* put diffuse component */
691          d = cxyz[0] + cxyz[1] + cxyz[2];
692          if (d > FTINY)
693 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
694 <        printf("rd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
695 <        puts("c");                              /* put specular component */
696 <        printf("rs %.4f %.4f\n", fa->farg[3],
697 <                        typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
698 <                                        fa->farg[4]);
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(mod, typ, id, fa)       /* convert a metal material */
707 < char    *mod, *typ, *id;
708 < register FUNARGS        *fa;
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;
# Line 519 | Line 718 | register FUNARGS       *fa;
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("c");                              /* put diffuse component */
721 >        puts("\tc");                            /* put diffuse component */
722          d = cxyz[0] + cxyz[1] + cxyz[2];
723          if (d > FTINY)
724 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
725 <        printf("rd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
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("rs %.4f %.4f\n", cxyz[1]*fa->farg[3],
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);
# Line 533 | Line 732 | register FUNARGS       *fa;
732  
733  
734   int
735 < o_glass(mod, typ, id, fa)       /* convert a glass material */
736 < char    *mod, *typ, *id;
737 < register FUNARGS        *fa;
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;
# Line 546 | Line 748 | register FUNARGS       *fa;
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 <                rrgb[i] = (1. - F)*(1. - F)/(1. - F*F*fa->farg[i]*fa->farg[i]);
553 <                trgb[i] = F * (1. + (1. - 2.*F)*fa->farg[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("c");
761 >        puts("\tc");
762          d = cxyz[0] + cxyz[1] + cxyz[2];
763          if (d > FTINY)
764 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
765 <        printf("rs %.4f 0\n", cxyz[1]);
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("c");
767 >        puts("\tc");
768          d = cxyz[0] + cxyz[1] + cxyz[2];
769          if (d > FTINY)
770 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
771 <        printf("ts %.4f 0\n", cxyz[1]);
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_mirror(mod, typ, id, fa)      /* convert a mirror material */
778 < char    *mod, *typ, *id;
779 < register FUNARGS        *fa;
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  
# Line 586 | Line 826 | register FUNARGS       *fa;
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("c");                              /* put specular component */
829 >        puts("\tc");                            /* put specular component */
830          d = cxyz[0] + cxyz[1] + cxyz[2];
831          if (d > FTINY)
832 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
833 <        printf("rs %.4f 0\n", cxyz[1]);
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(mod, typ, id, fa)       /* convert a trans material */
840 < char    *mod, *typ, *id;
841 < register FUNARGS        *fa;
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;
# Line 619 | Line 862 | register FUNARGS       *fa;
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("c");                              /* put transmitted diffuse */
865 >        puts("\tc");                            /* put transmitted diffuse */
866          d = cxyz[0] + cxyz[1] + cxyz[2];
867          if (d > FTINY)
868 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
869 <        printf("td %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec));
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("ts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
871 >        printf("\tts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
872                                                  /* put reflected diffuse */
873 <        printf("rd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans));
874 <        puts("c");                              /* put reflected specular */
875 <        printf("rs %.4f %.4f\n", fa->farg[3], rough);
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(mod, typ, id, fa)               /* convert a light type */
882 < char    *mod, *typ, *id;
883 < register FUNARGS        *fa;
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;
# Line 648 | Line 894 | register FUNARGS       *fa;
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("c");
897 >        puts("\tc");
898          if (d > FTINY)
899 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
900 <        printf("ed %.4g\n", cxyz[1]);
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines