-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
220 lines (192 loc) · 7.15 KB
/
parser.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "parser.h"
#include "tinyxml2.h"
#include <sstream>
#include <stdexcept>
using namespace std;
void parser::Scene::loadFromXml(const std::string &filepath)
{
tinyxml2::XMLDocument file;
std::stringstream stream;
auto res = file.LoadFile(filepath.c_str());
if (res)
{
throw std::runtime_error("Error: The xml file cannot be loaded.");
}
auto root = file.FirstChild();
if (!root)
{
throw std::runtime_error("Error: Root is not found.");
}
//Get BackgroundColor
auto element = root->FirstChildElement("BackgroundColor");
if (element)
{
stream << element->GetText() << std::endl;
}
else
{
stream << "0 0 0" << std::endl;
}
stream >> background_color.x >> background_color.y >> background_color.z;
//Get ShadowRayEpsilon
element = root->FirstChildElement("ShadowRayEpsilon");
if (element)
{
stream << element->GetText() << std::endl;
}
else
{
stream << "0.001" << std::endl;
}
stream >> shadow_ray_epsilon;
//Get MaxRecursionDepth
element = root->FirstChildElement("MaxRecursionDepth");
if (element)
{
stream << element->GetText() << std::endl;
}
else
{
stream << "0" << std::endl;
}
stream >> max_recursion_depth;
//Get Cameras
element = root->FirstChildElement("Cameras");
element = element->FirstChildElement("Camera");
Camera camera;
while (element)
{
auto child = element->FirstChildElement("Position");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("Gaze");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("Up");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("NearPlane");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("NearDistance");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("ImageResolution");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("ImageName");
stream << child->GetText() << std::endl;
stream >> camera.position.x >> camera.position.y >> camera.position.z;
stream >> camera.gaze.x >> camera.gaze.y >> camera.gaze.z;
stream >> camera.up.x >> camera.up.y >> camera.up.z;
stream >> camera.near_plane.x >> camera.near_plane.y >> camera.near_plane.z >> camera.near_plane.w;
stream >> camera.near_distance;
stream >> camera.image_width >> camera.image_height;
stream >> camera.image_name;
cameras.push_back(camera);
element = element->NextSiblingElement("Camera");
}
//Get Lights
element = root->FirstChildElement("Lights");
auto child = element->FirstChildElement("AmbientLight");
stream << child->GetText() << std::endl;
stream >> ambient_light.x >> ambient_light.y >> ambient_light.z;
element = element->FirstChildElement("PointLight");
PointLight point_light;
while (element)
{
child = element->FirstChildElement("Position");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("Intensity");
stream << child->GetText() << std::endl;
stream >> point_light.position.x >> point_light.position.y >> point_light.position.z;
stream >> point_light.intensity.x >> point_light.intensity.y >> point_light.intensity.z;
point_lights.push_back(point_light);
element = element->NextSiblingElement("PointLight");
}
//Get Materials
element = root->FirstChildElement("Materials");
element = element->FirstChildElement("Material");
Material material;
while (element)
{
material.is_mirror = (element->Attribute("type", "mirror") != NULL);
child = element->FirstChildElement("AmbientReflectance");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("DiffuseReflectance");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("SpecularReflectance");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("MirrorReflectance");
stream << child->GetText() << std::endl;
child = element->FirstChildElement("PhongExponent");
stream << child->GetText() << std::endl;
stream >> material.ambient.x >> material.ambient.y >> material.ambient.z;
stream >> material.diffuse.x >> material.diffuse.y >> material.diffuse.z;
stream >> material.specular.x >> material.specular.y >> material.specular.z;
stream >> material.mirror.x >> material.mirror.y >> material.mirror.z;
stream >> material.phong_exponent;
materials.push_back(material);
element = element->NextSiblingElement("Material");
}
//Get VertexData
element = root->FirstChildElement("VertexData");
stream << element->GetText() << std::endl;
Vec3f vertex;
while (!(stream >> vertex.x).eof())
{
stream >> vertex.y >> vertex.z;
vertex_data.push_back(vertex);
}
stream.clear();
//Get Meshes
element = root->FirstChildElement("Objects");
element = element->FirstChildElement("Mesh");
Mesh mesh;
while (element)
{
child = element->FirstChildElement("Material");
stream << child->GetText() << std::endl;
stream >> mesh.material_id;
child = element->FirstChildElement("Faces");
stream << child->GetText() << std::endl;
Face face;
while (!(stream >> face.v0_id).eof())
{
stream >> face.v1_id >> face.v2_id;
mesh.faces.push_back(face);
}
stream.clear();
meshes.push_back(mesh);
mesh.faces.clear();
element = element->NextSiblingElement("Mesh");
}
stream.clear();
//Get Triangles
element = root->FirstChildElement("Objects");
element = element->FirstChildElement("Triangle");
Triangle triangle;
while (element)
{
child = element->FirstChildElement("Material");
stream << child->GetText() << std::endl;
stream >> triangle.material_id;
child = element->FirstChildElement("Indices");
stream << child->GetText() << std::endl;
stream >> triangle.indices.v0_id >> triangle.indices.v1_id >> triangle.indices.v2_id;
triangles.push_back(triangle);
element = element->NextSiblingElement("Triangle");
}
//Get Spheres
element = root->FirstChildElement("Objects");
element = element->FirstChildElement("Sphere");
Sphere sphere;
while (element)
{
child = element->FirstChildElement("Material");
stream << child->GetText() << std::endl;
stream >> sphere.material_id;
child = element->FirstChildElement("Center");
stream << child->GetText() << std::endl;
stream >> sphere.center_vertex_id;
child = element->FirstChildElement("Radius");
stream << child->GetText() << std::endl;
stream >> sphere.radius;
spheres.push_back(sphere);
element = element->NextSiblingElement("Sphere");
}
}