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.21 by schorsch, Mon Oct 27 10:26:45 2003 UTC vs.
Revision 2.28 by greg, Sat Dec 28 18:05:14 2019 UTC

# Line 5 | Line 5 | static const char      RCSid[] = "$Id$";
5   * Convert Radiance scene description to MGF
6   */
7  
8 #include "platform.h"
9 #include "standard.h"
8   #include <ctype.h>
11 #include <string.h>
12 #include <stdio.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"
# Line 18 | Line 17 | static const char      RCSid[] = "$Id$";
17  
18   #define C_1SIDEDTHICK   0.005
19  
21 int     o_face(), o_cone(), o_sphere(), o_ring(), o_cylinder();
22 int     o_instance(), o_illum();
23 int     o_plastic(), o_metal(), o_glass(), o_dielectric(),
24        o_mirror(), o_trans(), o_light();
20  
21   LUTAB   rmats = LU_SINIT(free,NULL);            /* defined material table */
22  
# Line 54 | Line 49 | struct vert {
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 < main(argc, argv)
80 < int     argc;
81 < char    **argv;
79 >
80 > int
81 > main(
82 >        int     argc,
83 >        char    **argv
84 > )
85   {
86          int     i;
87  
# Line 98 | 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)
106 < #define id      (buf+256)
107 < #define alias   (buf+384)
108 <        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;
# Line 144 | 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 170 | Line 198 | char   *inp;
198                  pclose(fp);
199          else
200                  fclose(fp);
173 #undef mod
174 #undef typ
175 #undef id
176 #undef alias
201   }
202  
203  
204 < cvtprim(inp, mod, typ, id, fa)  /* process Radiance primitive */
205 < char    *inp, *mod, *typ, *id;
206 < FUNARGS *fa;
204 > void
205 > unspace(        /* replace spaces with underscores in s */
206 >        char *s
207 > )
208   {
209 +        while (*s) {
210 +                if (isspace(*s))
211 +                        *s = '_';
212 +                ++s;
213 +        }
214 + }
215 +
216 +
217 + void
218 + cvtprim(        /* process Radiance primitive */
219 +        char    *inp,
220 +        char    *mod,
221 +        char    *typ,
222 +        char    *id,
223 +        FUNARGS *fa
224 + )
225 + {
226          int     (*df)();
227  
228          df = (int (*)())lu_find(&rdispatch, typ)->data;
229          if (df != NULL) {                               /* convert */
230                  if ((*df)(mod, typ, id, fa) < 0) {
231 <                        fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id);
231 >                        fprintf(stderr, "%s: bad %s \"%s\"\n", "rad2mgf", typ, id);
232                          exit(1);
233                  }
234          } else {                                        /* unsupported */
# Line 197 | Line 239 | FUNARGS        *fa;
239   }
240  
241  
242 < newmat(id, alias)               /* add a modifier to the alias list */
243 < char    *id;
244 < char    *alias;
242 > void
243 > newmat(         /* add a modifier to the alias list */
244 >        char    *id,
245 >        char    *alias
246 > )
247   {
248          register LUENT  *lp, *lpa;
249  
# Line 233 | Line 277 | memerr:
277   }
278  
279  
280 < setmat(id)                      /* set material to this one */
281 < char    *id;
280 > void
281 > setmat(                 /* set material to this one */
282 >        char    *id
283 > )
284   {
285          if (!strcmp(id, curmat))        /* already set? */
286                  return;
# Line 245 | Line 291 | char   *id;
291   }
292  
293  
294 < setobj(id)                      /* set object name to this one */
295 < char    *id;
294 > void
295 > setobj(                 /* set object name to this one */
296 >        char    *id
297 > )
298   {
299          register char   *cp, *cp2;
300          char    *end = NULL;
# Line 276 | Line 324 | char   *id;
324   }
325  
326  
327 < init()                  /* initialize dispatch table and output */
327 > void
328 > init(void)                      /* initialize dispatch table and output */
329   {
330          lu_init(&vertab, NVERTS);
331          lu_init(&rdispatch, 22);
# Line 310 | Line 359 | init()                 /* initialize dispatch table and output */
359   }
360  
361  
362 < uninit()                        /* mark end of MGF file */
362 > void
363 > uninit(void)                    /* mark end of MGF file */
364   {
365          puts("o");
366          if (hasmult)
# Line 322 | Line 372 | uninit()                       /* mark end of MGF file */
372   }
373  
374  
375 < clrverts()                      /* clear vertex table */
375 > void
376 > clrverts(void)                  /* clear vertex table */
377   {
378          register int    i;
379  
# Line 333 | Line 384 | clrverts()                     /* clear vertex table */
384   }
385  
386  
387 < add2dispatch(name, func)        /* add function to dispatch table */
388 < char    *name;
389 < int     (*func)();
387 > void
388 > add2dispatch(   /* add function to dispatch table */
389 >        char    *name,
390 >        int     (*func)()
391 > )
392   {
393          register LUENT  *lp;
394  
# Line 351 | Line 404 | int    (*func)();
404  
405  
406   char *
407 < getvertid(vname, vp)            /* get/set vertex ID for this point */
408 < char    *vname;
409 < FVECT   vp;
407 > getvertid(              /* get/set vertex ID for this point */
408 >        char    *vname,
409 >        FVECT   vp
410 > )
411   {
412          static char     vkey[VKLEN];
413          register LUENT  *lp;
# Line 395 | Line 449 | memerr:
449  
450  
451   int
452 < o_unsupported(mod, typ, id, fa)         /* mark unsupported primitive */
453 < char    *mod, *typ, *id;
454 < FUNARGS *fa;
452 > o_unsupported(          /* mark unsupported primitive */
453 >        char    *mod,
454 >        char    *typ,
455 >        char    *id,
456 >        FUNARGS *fa
457 > )
458   {
459          register int    i;
460  
# Line 422 | Line 479 | FUNARGS        *fa;
479  
480  
481   int
482 < o_face(mod, typ, id, fa)                /* print out a polygon */
483 < char    *mod, *typ, *id;
484 < FUNARGS *fa;
482 > o_face(         /* print out a polygon */
483 >        char    *mod,
484 >        char    *typ,
485 >        char    *id,
486 >        FUNARGS *fa
487 > )
488   {
489          char    entbuf[2048], *linestart;
490          register char   *cp;
# Line 453 | Line 513 | FUNARGS        *fa;
513  
514  
515   int
516 < o_cone(mod, typ, id, fa)        /* print out a cone */
517 < char    *mod, *typ, *id;
518 < register FUNARGS        *fa;
516 > o_cone( /* print out a cone */
517 >        char    *mod,
518 >        char    *typ,
519 >        char    *id,
520 >        register FUNARGS        *fa
521 > )
522   {
523          char    v1[6], v2[6];
524  
# Line 476 | Line 539 | register FUNARGS       *fa;
539  
540  
541   int
542 < o_sphere(mod, typ, id, fa)      /* print out a sphere */
543 < char    *mod, *typ, *id;
544 < register FUNARGS        *fa;
542 > o_sphere(       /* print out a sphere */
543 >        char    *mod,
544 >        char    *typ,
545 >        char    *id,
546 >        register FUNARGS        *fa
547 > )
548   {
549          char    cent[6];
550  
# Line 493 | Line 559 | register FUNARGS       *fa;
559  
560  
561   int
562 < o_cylinder(mod, typ, id, fa)    /* print out a cylinder */
563 < char    *mod, *typ, *id;
564 < register FUNARGS        *fa;
562 > o_cylinder(     /* print out a cylinder */
563 >        char    *mod,
564 >        char    *typ,
565 >        char    *id,
566 >        register FUNARGS        *fa
567 > )
568   {
569          char    v1[6], v2[6];
570  
# Line 512 | Line 581 | register FUNARGS       *fa;
581  
582  
583   int
584 < o_ring(mod, typ, id, fa)        /* print out a ring */
585 < char    *mod, *typ, *id;
586 < register FUNARGS        *fa;
584 > o_ring( /* print out a ring */
585 >        char    *mod,
586 >        char    *typ,
587 >        char    *id,
588 >        register FUNARGS        *fa
589 > )
590   {
591          if (fa->nfargs != 8)
592                  return(-1);
# Line 535 | Line 607 | register FUNARGS       *fa;
607  
608  
609   int
610 < o_instance(mod, typ, id, fa)    /* convert an instance (or mesh) */
611 < char    *mod, *typ, *id;
612 < FUNARGS *fa;
610 > o_instance(     /* convert an instance (or mesh) */
611 >        char    *mod,
612 >        char    *typ,
613 >        char    *id,
614 >        FUNARGS *fa
615 > )
616   {
617          register int    i;
618          register char   *cp;
# Line 576 | Line 651 | FUNARGS        *fa;
651  
652  
653   int
654 < o_illum(mod, typ, id, fa)       /* convert an illum material */
655 < char    *mod, *typ, *id;
656 < FUNARGS *fa;
654 > o_illum(        /* convert an illum material */
655 >        char    *mod,
656 >        char    *typ,
657 >        char    *id,
658 >        FUNARGS *fa
659 > )
660   {
661          if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) {
662                  newmat(id, fa->sarg[0]);        /* just create alias */
# Line 592 | Line 670 | FUNARGS        *fa;
670  
671  
672   int
673 < o_plastic(mod, typ, id, fa)     /* convert a plastic material */
674 < char    *mod, *typ, *id;
675 < register FUNARGS        *fa;
673 > o_plastic(      /* convert a plastic material */
674 >        char    *mod,
675 >        char    *typ,
676 >        char    *id,
677 >        register FUNARGS        *fa
678 > )
679   {
680          COLOR   cxyz, rrgb;
681          double  d;
# Line 607 | Line 688 | register FUNARGS       *fa;
688          puts("\tc");                            /* put diffuse component */
689          d = cxyz[0] + cxyz[1] + cxyz[2];
690          if (d > FTINY)
691 <                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
692 <        printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
691 >                printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d);
692 >        printf("\trd %.6f\n", cxyz[1]*(1. - fa->farg[3]));
693          if (fa->farg[3] > FTINY) {              /* put specular component */
694                  puts("\tc");
695 <                printf("\trs %.4f %.4f\n", fa->farg[3],
695 >                printf("\trs %.6f %.6f\n", fa->farg[3],
696                                  typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
697                                                  fa->farg[4]);
698          }
# Line 620 | Line 701 | register FUNARGS       *fa;
701  
702  
703   int
704 < o_metal(mod, typ, id, fa)       /* convert a metal material */
705 < char    *mod, *typ, *id;
706 < register FUNARGS        *fa;
704 > o_metal(        /* convert a metal material */
705 >        char    *mod,
706 >        char    *typ,
707 >        char    *id,
708 >        register FUNARGS        *fa
709 > )
710   {
711          COLOR   cxyz, rrgb;
712          double  d;
# Line 635 | Line 719 | register FUNARGS       *fa;
719          puts("\tc");                            /* put diffuse component */
720          d = cxyz[0] + cxyz[1] + cxyz[2];
721          if (d > FTINY)
722 <                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
723 <        printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
722 >                printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d);
723 >        printf("\trd %.6f\n", cxyz[1]*(1. - fa->farg[3]));
724                                                  /* put specular component */
725 <        printf("\trs %.4f %.4f\n", cxyz[1]*fa->farg[3],
725 >        printf("\trs %.6f %.6f\n", cxyz[1]*fa->farg[3],
726                          typ[5]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
727                                          fa->farg[4]);
728          return(0);
# Line 646 | Line 730 | register FUNARGS       *fa;
730  
731  
732   int
733 < o_glass(mod, typ, id, fa)       /* convert a glass material */
734 < char    *mod, *typ, *id;
735 < register FUNARGS        *fa;
733 > o_glass(        /* convert a glass material */
734 >        char    *mod,
735 >        char    *typ,
736 >        char    *id,
737 >        register FUNARGS        *fa
738 > )
739   {
740          COLOR   cxyz, rrgb, trgb;
741          double  nrfr = 1.52, F, d;
# Line 672 | Line 759 | register FUNARGS       *fa;
759          puts("\tc");
760          d = cxyz[0] + cxyz[1] + cxyz[2];
761          if (d > FTINY)
762 <                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
763 <        printf("\trs %.4f 0\n", cxyz[1]);
762 >                printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d);
763 >        printf("\trs %.6f 0\n", cxyz[1]);
764          rgb_cie(cxyz, trgb);                    /* put transmitted component */
765          puts("\tc");
766          d = cxyz[0] + cxyz[1] + cxyz[2];
767          if (d > FTINY)
768 <                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
769 <        printf("\tts %.4f 0\n", cxyz[1]);
768 >                printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d);
769 >        printf("\tts %.6f 0\n", cxyz[1]);
770          return(0);
771   }
772  
773  
774   int
775 < o_dielectric(mod, typ, id, fa)  /* convert a dielectric material */
776 < char    *mod, *typ, *id;
777 < register FUNARGS        *fa;
775 > o_dielectric(   /* convert a dielectric material */
776 >        char    *mod,
777 >        char    *typ,
778 >        char    *id,
779 >        register FUNARGS        *fa
780 > )
781   {
782          COLOR   cxyz, trgb;
783          double  F, d;
# Line 703 | Line 793 | register FUNARGS       *fa;
793          printf("\tir %f 0\n", fa->farg[3]);     /* put index of refraction */
794          printf("\tsides 1\n");
795          puts("\tc");                            /* put reflected component */
796 <        printf("\trs %.4f 0\n", F);
796 >        printf("\trs %.6f 0\n", F);
797          rgb_cie(cxyz, trgb);                    /* put transmitted component */
798          puts("\tc");
799          d = cxyz[0] + cxyz[1] + cxyz[2];
800          if (d > FTINY)
801 <                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
802 <        printf("\tts %.4f 0\n", cxyz[1]);
801 >                printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d);
802 >        printf("\tts %.6f 0\n", cxyz[1]);
803          return(0);
804   }
805  
806  
807   int
808 < o_mirror(mod, typ, id, fa)      /* convert a mirror material */
809 < char    *mod, *typ, *id;
810 < register FUNARGS        *fa;
808 > o_mirror(       /* convert a mirror material */
809 >        char    *mod,
810 >        char    *typ,
811 >        char    *id,
812 >        register FUNARGS        *fa
813 > )
814   {
815          COLOR   cxyz, rrgb;
816          double  d;
# Line 734 | Line 827 | register FUNARGS       *fa;
827          puts("\tc");                            /* put specular component */
828          d = cxyz[0] + cxyz[1] + cxyz[2];
829          if (d > FTINY)
830 <                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
831 <        printf("\trs %.4f 0\n", cxyz[1]);
830 >                printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d);
831 >        printf("\trs %.6f 0\n", cxyz[1]);
832          return(0);
833   }
834  
835  
836   int
837 < o_trans(mod, typ, id, fa)       /* convert a trans material */
838 < char    *mod, *typ, *id;
839 < register FUNARGS        *fa;
837 > o_trans(        /* convert a trans material */
838 >        char    *mod,
839 >        char    *typ,
840 >        char    *id,
841 >        register FUNARGS        *fa
842 > )
843   {
844          COLOR   cxyz, rrgb;
845          double  rough, trans, tspec, d;
846  
847 <        if (typ[4] == '2') {            /* trans2 */
847 >        if (typ[5] == '2') {            /* trans2 */
848                  if (fa->nfargs != 8)
849                          return(-1);
850                  rough = .5*(fa->farg[4] + fa->farg[5]);
# Line 767 | Line 863 | register FUNARGS       *fa;
863          puts("\tc");                            /* put transmitted diffuse */
864          d = cxyz[0] + cxyz[1] + cxyz[2];
865          if (d > FTINY)
866 <                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
867 <        printf("\ttd %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec));
866 >                printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d);
867 >        printf("\ttd %.6f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec));
868                                                  /* put transmitted specular */
869 <        printf("\tts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
869 >        printf("\tts %.6f %.6f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
870                                                  /* put reflected diffuse */
871 <        printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans));
871 >        printf("\trd %.6f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans));
872          puts("\tc");                            /* put reflected specular */
873 <        printf("\trs %.4f %.4f\n", fa->farg[3], rough);
873 >        printf("\trs %.6f %.6f\n", fa->farg[3], rough);
874          return(0);
875   }
876  
877  
878   int
879 < o_light(mod, typ, id, fa)               /* convert a light type */
880 < char    *mod, *typ, *id;
881 < register FUNARGS        *fa;
879 > o_light(                /* convert a light type */
880 >        char    *mod,
881 >        char    *typ,
882 >        char    *id,
883 >        register FUNARGS        *fa
884 > )
885   {
886          COLOR   cxyz, rrgb;
887          double  d;
# Line 795 | Line 894 | register FUNARGS       *fa;
894          d = cxyz[0] + cxyz[1] + cxyz[2];
895          puts("\tc");
896          if (d > FTINY)
897 <                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
897 >                printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d);
898          printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY));
899          return(0);
900   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines