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.22 by schorsch, Sat Nov 15 16:29:11 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 50 | Line 49 | struct vert {
49  
50   LUTAB   vertab = LU_SINIT(free,NULL);   /* our vertex lookup table */
51  
52 < static void rad2mgf(char *inp);
53 < static void cvtprim(char *inp, char *mod, char *typ, char *id, FUNARGS *fa);
54 < static void newmat(char *id, char *alias);
55 < static void setmat(char *id);
56 < static void setobj(char *id);
57 < static void init(void);
58 < static void uninit(void);
59 < static void clrverts(void);
60 < static void add2dispatch(char *name, int (*func)());
61 < static char *getvertid(char *vname, FVECT vp);
62 < static int o_unsupported(char *mod, char *typ, char *id, FUNARGS *fa);
63 < static int o_face(char *mod, char *typ, char *id, FUNARGS *fa);
64 < static int o_cone(char *mod, char *typ, char *id, FUNARGS *fa);
65 < static int o_sphere(char *mod, char *typ, char *id, FUNARGS *fa);
66 < static int o_cylinder(char *mod, char *typ, char *id, FUNARGS *fa);
67 < static int o_ring(char *mod, char *typ, char *id, FUNARGS *fa);
68 < static int o_instance(char *mod, char *typ, char *id, FUNARGS *fa);
69 < static int o_illum(char *mod, char *typ, char *id, FUNARGS *fa);
70 < static int o_plastic(char *mod, char *typ, char *id, FUNARGS *fa);
71 < static int o_metal(char *mod, char *typ, char *id, FUNARGS *fa);
72 < static int o_glass(char *mod, char *typ, char *id, FUNARGS *fa);
73 < static int o_dielectric(char *mod, char *typ, char *id, FUNARGS *fa);
74 < static int o_mirror(char *mod, char *typ, char *id, FUNARGS *fa);
75 < static int o_trans(char *mod, char *typ, char *id, FUNARGS *fa);
76 < static int o_light(char *mod, char *typ, char *id, FUNARGS *fa);
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
# Line 127 | Line 127 | rad2mgf(               /* convert a Radiance file to MGF */
127          char    *inp
128   )
129   {
130 < #define mod     buf
131 < #define typ     (buf+128)
132 < #define id      (buf+256)
133 < #define alias   (buf+384)
134 <        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 170 | Line 167 | rad2mgf(               /* convert a Radiance file to MGF */
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 196 | Line 198 | rad2mgf(               /* convert a Radiance file to MGF */
198                  pclose(fp);
199          else
200                  fclose(fp);
199 #undef mod
200 #undef typ
201 #undef id
202 #undef alias
201   }
202  
203  
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,
# Line 217 | Line 228 | cvtprim(       /* process Radiance primitive */
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", "rat2mgf", typ, id);
231 >                        fprintf(stderr, "%s: bad %s \"%s\"\n", "rad2mgf", typ, id);
232                          exit(1);
233                  }
234          } else {                                        /* unsupported */
# Line 677 | Line 688 | o_plastic(     /* convert a plastic material */
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 708 | Line 719 | o_metal(       /* convert a metal material */
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 748 | Line 759 | o_glass(       /* convert a glass material */
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  
# Line 782 | Line 793 | o_dielectric(  /* convert a dielectric material */
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  
# Line 816 | Line 827 | o_mirror(      /* convert a mirror material */
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  
# Line 833 | Line 844 | o_trans(       /* convert a trans material */
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 852 | Line 863 | o_trans(       /* convert a trans material */
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  
# Line 883 | Line 894 | o_light(               /* convert a light type */
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