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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines