6 |
|
*/ |
7 |
|
|
8 |
|
#include <ctype.h> |
9 |
– |
#include <string.h> |
10 |
– |
#include <stdio.h> |
9 |
|
|
10 |
|
#include "platform.h" |
11 |
|
#include "rtmath.h" |
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 |
127 |
|
char *inp |
128 |
|
) |
129 |
|
{ |
130 |
< |
#define mod buf |
131 |
< |
#define typ (buf+128) |
133 |
< |
#define id (buf+256) |
134 |
< |
#define alias (buf+384) |
135 |
< |
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; |
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)) { |
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 |
201 |
< |
#undef typ |
202 |
< |
#undef id |
203 |
< |
#undef alias |
203 |
> |
printf("# End conversion from: %s\n", inp); |
204 |
|
} |
205 |
|
|
206 |
|
|
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, |
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", "rat2mgf", typ, id); |
234 |
> |
fprintf(stderr, "%s: bad %s \"%s\"\n", "rad2mgf", typ, id); |
235 |
|
exit(1); |
236 |
|
} |
237 |
|
} else { /* unsupported */ |
691 |
|
puts("\tc"); /* put diffuse component */ |
692 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
693 |
|
if (d > FTINY) |
694 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
695 |
< |
printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])); |
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 %.4f %.4f\n", fa->farg[3], |
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 |
|
} |
722 |
|
puts("\tc"); /* put diffuse component */ |
723 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
724 |
|
if (d > FTINY) |
725 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
726 |
< |
printf("\trd %.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("\trs %.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); |
762 |
|
puts("\tc"); |
763 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
764 |
|
if (d > FTINY) |
765 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
766 |
< |
printf("\trs %.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("\tc"); |
769 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
770 |
|
if (d > FTINY) |
771 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
772 |
< |
printf("\tts %.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 |
|
|
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 %.4f 0\n", F); |
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 %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
805 |
< |
printf("\tts %.4f 0\n", cxyz[1]); |
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 |
|
|
830 |
|
puts("\tc"); /* put specular component */ |
831 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
832 |
|
if (d > FTINY) |
833 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
834 |
< |
printf("\trs %.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 |
|
|
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]); |
866 |
|
puts("\tc"); /* put transmitted diffuse */ |
867 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
868 |
|
if (d > FTINY) |
869 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
870 |
< |
printf("\ttd %.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("\tts %.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("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans)); |
874 |
> |
printf("\trd %.6f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans)); |
875 |
|
puts("\tc"); /* put reflected specular */ |
876 |
< |
printf("\trs %.4f %.4f\n", fa->farg[3], rough); |
876 |
> |
printf("\trs %.6f %.6f\n", fa->farg[3], rough); |
877 |
|
return(0); |
878 |
|
} |
879 |
|
|
897 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
898 |
|
puts("\tc"); |
899 |
|
if (d > FTINY) |
900 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
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 |
|
} |