-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.cpp
179 lines (175 loc) · 5.86 KB
/
parse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <map>
#include <sstream>
#include <string>
#include "boolean.h"
#include "cylinder.h"
#include "flat_shader.h"
#include "phong_shader.h"
#include "plane.h"
#include "point_light.h"
#include "reflective_shader.h"
#include "render_world.h"
#include "sphere.h"
void Parse(Render_World& world,int& width,int& height,const char* test_file)
{
FILE* F = fopen(test_file,"r");
if(!F)
{
printf("Failed to open file '%s'\n",test_file);
exit(EXIT_FAILURE);
}
double f0;
char buff[1000];
vec3 u,v,w;
std::string name,s0,s1,s2;
std::map<std::string,vec3> colors;
std::map<std::string,Object*> objects;
std::map<std::string,Shader*> shaders;
while(fgets(buff, sizeof(buff), F))
{
std::stringstream ss(buff);
std::string item,name;
if(!(ss>>item) || !item.size() || item[0]=='#') continue;
if(item=="size")
{
ss>>width>>height;
assert(ss);
}
else if(item=="color")
{
ss>>name>>u;
assert(ss);
colors[name]=u;
}
else if(item=="plane")
{
ss>>name>>u>>v>>s0;
assert(ss);
Object* o=new Plane(u,v);
std::map<std::string,Shader*>::const_iterator sh=shaders.find(s0);
assert(sh!=shaders.end());
o->material_shader=sh->second;
if(name=="-") world.objects.push_back(o);
else objects[name]=o;
}
else if(item=="sphere")
{
ss>>name>>u>>f0>>s0;
assert(ss);
Object* o=new Sphere(u,f0);
std::map<std::string,Shader*>::const_iterator sh=shaders.find(s0);
assert(sh!=shaders.end());
o->material_shader=sh->second;
if(name=="-") world.objects.push_back(o);
else objects[name]=o;
}
else if(item=="cylinder")
{
ss>>name>>u>>v>>f0>>s0;
assert(ss);
Object* o=new Cylinder(u,v,f0);
std::map<std::string,Shader*>::const_iterator sh=shaders.find(s0);
assert(sh!=shaders.end());
o->material_shader=sh->second;
if(name=="-") world.objects.push_back(o);
else objects[name]=o;
}
else if(item=="intersection" || item=="union" || item=="difference")
{
ss>>name>>s0>>s1>>s2;
assert(ss);
std::map<std::string,Object*>::const_iterator o0=objects.find(s0);
std::map<std::string,Object*>::const_iterator o1=objects.find(s1);
assert(o0!=objects.end());
assert(o1!=objects.end());
Boolean::Type type;
if(item=="intersection") type=Boolean::type_intersection;
else if(item=="union") type=Boolean::type_union;
else type=Boolean::type_difference;
Object* o=new Boolean(o0->second,o1->second,type);
std::map<std::string,Shader*>::const_iterator sh=shaders.find(s2);
assert(sh!=shaders.end());
o->material_shader=sh->second;
if(name=="-") world.objects.push_back(o);
else objects[name]=o;
}
else if(item=="flat_shader")
{
ss>>name>>s0;
assert(ss);
std::map<std::string,vec3>::const_iterator c0=colors.find(s0);
assert(c0!=colors.end());
shaders[name]=new Flat_Shader(world,c0->second);
}
else if(item=="phong_shader")
{
ss>>name>>s0>>s1>>s2>>f0;
assert(ss);
std::map<std::string,vec3>::const_iterator c0=colors.find(s0);
std::map<std::string,vec3>::const_iterator c1=colors.find(s1);
std::map<std::string,vec3>::const_iterator c2=colors.find(s2);
assert(c0!=colors.end());
assert(c1!=colors.end());
assert(c2!=colors.end());
shaders[name]=new Phong_Shader(world,c0->second,c1->second,c2->second,f0);
}
else if(item=="reflective_shader")
{
ss>>name>>s0>>f0;
assert(ss);
std::map<std::string,Shader*>::const_iterator sh=shaders.find(s0);
assert(sh!=shaders.end());
shaders[name]=new Reflective_Shader(world,sh->second,f0);
}
else if(item=="point_light")
{
ss>>u>>s0>>f0;
assert(ss);
std::map<std::string,vec3>::const_iterator c0=colors.find(s0);
assert(c0!=colors.end());
world.lights.push_back(new Point_Light(u,c0->second,f0));
}
else if(item=="ambient_light")
{
ss>>s0>>f0;
assert(ss);
std::map<std::string,vec3>::const_iterator c0=colors.find(s0);
assert(c0!=colors.end());
world.ambient_color=c0->second;
world.ambient_intensity=f0;
}
else if(item=="camera")
{
ss>>u>>v>>w>>f0;
assert(ss);
world.camera.Position_And_Aim_Camera(u,v,w);
world.camera.Focus_Camera(1,(double)width/height,f0*(M_PI/180));
}
else if(item=="background")
{
ss>>s0;
assert(ss);
std::map<std::string,Shader*>::const_iterator sh=shaders.find(s0);
assert(sh!=shaders.end());
world.background_shader=sh->second;
}
else if(item=="enable_shadows")
{
ss>>world.enable_shadows;
assert(ss);
}
else if(item=="recursion_depth_limit")
{
ss>>world.recursion_depth_limit;
assert(ss);
}
else
{
std::cout<<"Failed to parse: "<<buff<<std::endl;
exit(EXIT_FAILURE);
}
}
if(!world.background_shader)
world.background_shader=new Flat_Shader(world,vec3());
world.camera.film.Set_Resolution(width,height);
}