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" |
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) |
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; |
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: warning - bad exit status\n", inp); |
199 |
> |
} else |
200 |
|
fclose(fp); |
201 |
< |
#undef mod |
200 |
< |
#undef typ |
201 |
< |
#undef id |
202 |
< |
#undef alias |
201 |
> |
printf("# End conversion from: %s\n", inp); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
void |
206 |
+ |
unspace( /* replace spaces with underscores in s */ |
207 |
+ |
char *s |
208 |
+ |
) |
209 |
+ |
{ |
210 |
+ |
while (*s) { |
211 |
+ |
if (isspace(*s)) |
212 |
+ |
*s = '_'; |
213 |
+ |
++s; |
214 |
+ |
} |
215 |
+ |
} |
216 |
+ |
|
217 |
+ |
|
218 |
+ |
void |
219 |
|
cvtprim( /* process Radiance primitive */ |
220 |
|
char *inp, |
221 |
|
char *mod, |
229 |
|
df = (int (*)())lu_find(&rdispatch, typ)->data; |
230 |
|
if (df != NULL) { /* convert */ |
231 |
|
if ((*df)(mod, typ, id, fa) < 0) { |
232 |
< |
fprintf(stderr, "%s: bad %s \"%s\"\n", "rat2mgf", typ, id); |
232 |
> |
fprintf(stderr, "%s: bad %s \"%s\"\n", "rad2mgf", typ, id); |
233 |
|
exit(1); |
234 |
|
} |
235 |
|
} else { /* unsupported */ |
689 |
|
puts("\tc"); /* put diffuse component */ |
690 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
691 |
|
if (d > FTINY) |
692 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
693 |
< |
printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])); |
692 |
> |
printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); |
693 |
> |
printf("\trd %.6f\n", cxyz[1]*(1. - fa->farg[3])); |
694 |
|
if (fa->farg[3] > FTINY) { /* put specular component */ |
695 |
|
puts("\tc"); |
696 |
< |
printf("\trs %.4f %.4f\n", fa->farg[3], |
696 |
> |
printf("\trs %.6f %.6f\n", fa->farg[3], |
697 |
|
typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) : |
698 |
|
fa->farg[4]); |
699 |
|
} |
720 |
|
puts("\tc"); /* put diffuse component */ |
721 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
722 |
|
if (d > FTINY) |
723 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
724 |
< |
printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])); |
723 |
> |
printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); |
724 |
> |
printf("\trd %.6f\n", cxyz[1]*(1. - fa->farg[3])); |
725 |
|
/* put specular component */ |
726 |
< |
printf("\trs %.4f %.4f\n", cxyz[1]*fa->farg[3], |
726 |
> |
printf("\trs %.6f %.6f\n", cxyz[1]*fa->farg[3], |
727 |
|
typ[5]=='2' ? .5*(fa->farg[4] + fa->farg[5]) : |
728 |
|
fa->farg[4]); |
729 |
|
return(0); |
760 |
|
puts("\tc"); |
761 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
762 |
|
if (d > FTINY) |
763 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
764 |
< |
printf("\trs %.4f 0\n", cxyz[1]); |
763 |
> |
printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); |
764 |
> |
printf("\trs %.6f 0\n", cxyz[1]); |
765 |
|
rgb_cie(cxyz, trgb); /* put transmitted component */ |
766 |
|
puts("\tc"); |
767 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
768 |
|
if (d > FTINY) |
769 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
770 |
< |
printf("\tts %.4f 0\n", cxyz[1]); |
769 |
> |
printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); |
770 |
> |
printf("\tts %.6f 0\n", cxyz[1]); |
771 |
|
return(0); |
772 |
|
} |
773 |
|
|
794 |
|
printf("\tir %f 0\n", fa->farg[3]); /* put index of refraction */ |
795 |
|
printf("\tsides 1\n"); |
796 |
|
puts("\tc"); /* put reflected component */ |
797 |
< |
printf("\trs %.4f 0\n", F); |
797 |
> |
printf("\trs %.6f 0\n", F); |
798 |
|
rgb_cie(cxyz, trgb); /* put transmitted component */ |
799 |
|
puts("\tc"); |
800 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
801 |
|
if (d > FTINY) |
802 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
803 |
< |
printf("\tts %.4f 0\n", cxyz[1]); |
802 |
> |
printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); |
803 |
> |
printf("\tts %.6f 0\n", cxyz[1]); |
804 |
|
return(0); |
805 |
|
} |
806 |
|
|
828 |
|
puts("\tc"); /* put specular component */ |
829 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
830 |
|
if (d > FTINY) |
831 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
832 |
< |
printf("\trs %.4f 0\n", cxyz[1]); |
831 |
> |
printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); |
832 |
> |
printf("\trs %.6f 0\n", cxyz[1]); |
833 |
|
return(0); |
834 |
|
} |
835 |
|
|
845 |
|
COLOR cxyz, rrgb; |
846 |
|
double rough, trans, tspec, d; |
847 |
|
|
848 |
< |
if (typ[4] == '2') { /* trans2 */ |
848 |
> |
if (typ[5] == '2') { /* trans2 */ |
849 |
|
if (fa->nfargs != 8) |
850 |
|
return(-1); |
851 |
|
rough = .5*(fa->farg[4] + fa->farg[5]); |
864 |
|
puts("\tc"); /* put transmitted diffuse */ |
865 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
866 |
|
if (d > FTINY) |
867 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
868 |
< |
printf("\ttd %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec)); |
867 |
> |
printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); |
868 |
> |
printf("\ttd %.6f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec)); |
869 |
|
/* put transmitted specular */ |
870 |
< |
printf("\tts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough); |
870 |
> |
printf("\tts %.6f %.6f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough); |
871 |
|
/* put reflected diffuse */ |
872 |
< |
printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans)); |
872 |
> |
printf("\trd %.6f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans)); |
873 |
|
puts("\tc"); /* put reflected specular */ |
874 |
< |
printf("\trs %.4f %.4f\n", fa->farg[3], rough); |
874 |
> |
printf("\trs %.6f %.6f\n", fa->farg[3], rough); |
875 |
|
return(0); |
876 |
|
} |
877 |
|
|
895 |
|
d = cxyz[0] + cxyz[1] + cxyz[2]; |
896 |
|
puts("\tc"); |
897 |
|
if (d > FTINY) |
898 |
< |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
898 |
> |
printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); |
899 |
|
printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY)); |
900 |
|
return(0); |
901 |
|
} |