This repository was archived by the owner on Jun 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLLoader.cpp
More file actions
229 lines (179 loc) · 8.43 KB
/
XMLLoader.cpp
File metadata and controls
229 lines (179 loc) · 8.43 KB
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
221
222
223
224
225
226
227
228
229
#include "XMLLoader.h"
//used the following reference to understand xml parsing: https://www.setnode.com/blog/quick-notes-on-how-to-use-rapidxml/
void XMLLoader::InitCameras(std::vector<Camera*>& cameraList, int windowWidth, int windowHeight) {
std::vector<rapidxml::xml_document<>*> documents = GetAllXMLDocumentsInDir("data/cameras");
for (int i = 0; i < documents.size(); i++) {
std::string cameraType;
XMFLOAT3 eye;
XMFLOAT3 at;
XMFLOAT3 up;
FLOAT nearPlane;
FLOAT farPlane;
//get all node data
rapidxml::xml_node<>* currentNode = documents[i]->first_node("camera");
rapidxml::xml_attribute<>* currentAttribute = currentNode->first_attribute("type");
cameraType = currentAttribute->value();
currentNode = currentNode->first_node("eye");
eye = GetFloat3InXml(currentNode);
currentNode = currentNode->next_sibling("at");
at = GetFloat3InXml(currentNode);
currentNode = currentNode->next_sibling("up");
up = GetFloat3InXml(currentNode);
currentNode = currentNode->next_sibling("clip");
currentAttribute = currentNode->first_attribute("near");
nearPlane = std::stof(currentAttribute->value());
currentAttribute = currentAttribute->next_attribute("far");
farPlane = std::stof(currentAttribute->value());
//construct a camera based on the data
Camera* newCamera = nullptr;
if (cameraType == "to") {
newCamera = new ToCam(eye, at, up, windowWidth, windowHeight, nearPlane, farPlane);
}
else if (cameraType == "at") {
newCamera = new AtCam(eye, at, up, windowWidth, windowHeight, nearPlane, farPlane);
}
else if (cameraType == "path") {
newCamera = new PathCam(eye, at, up, windowWidth, windowHeight, nearPlane, farPlane);
}
if (newCamera != nullptr) cameraList.push_back(newCamera);
//destroy document data.
documents[i]->clear();
}
documents.clear();
}
void XMLLoader::InitLights(std::vector<LightData>& lightList) {
std::vector<rapidxml::xml_document<>*> documents = GetAllXMLDocumentsInDir("data/lights");
for (int i = 0; i < documents.size(); i++) {
std::string lightType;
LightData light;
rapidxml::xml_node<>* subNode;
//type
rapidxml::xml_node<>* currentNode = documents[i]->first_node("light");
rapidxml::xml_attribute<>* currentAttribute = currentNode->first_attribute("type");
lightType = currentAttribute->value();
//ambient
currentNode = currentNode->first_node("ambient");
light.ambientMat = GetColorInXml(currentNode);
subNode = currentNode->first_node("strength");
light.ambientStr = GetColorInXml(subNode);
//diffuse
currentNode = currentNode->next_sibling("diffuse");
light.diffuseMat = GetColorInXml(currentNode);
subNode = currentNode->first_node("strength");
light.diffuseStr = GetColorInXml(subNode);
//specular
currentNode = currentNode->next_sibling("specular");
light.specularMat = GetColorInXml(currentNode);
subNode = currentNode->first_node("strength");
light.specularStr = GetColorInXml(subNode);
subNode = subNode->next_sibling("power");
currentAttribute = subNode->first_attribute("value");
light.specularPow = std::stof(currentAttribute->value());
//positioning
currentNode = currentNode->next_sibling("direction");
light.dir = GetFloat3InXml(currentNode);
//generate light object.
lightList.push_back(light);
//destroy document data.
documents[i]->clear();
}
documents.clear();
}
void XMLLoader::InitGameObjects(std::vector<GameObject*>& objectList, ID3D11Device* d3dDevice) {
std::vector<rapidxml::xml_document<>*> documents = GetAllXMLDocumentsInDir("data/gameobjects");
for (int i = 0; i < documents.size(); i++) {
std::string meshLoc;
std::string texLoc;
Transform newTransform;
rapidxml::xml_node<>* currentNode = documents[i]->first_node("gameobject");
//mesh
currentNode = currentNode->first_node("mesh");
rapidxml::xml_attribute<>* currentAttribute = currentNode->first_attribute("path");
meshLoc = currentAttribute->value();
//color
currentNode = currentNode->next_sibling("texture");
currentAttribute = currentNode->first_attribute("path");
texLoc = currentAttribute->value();
//transform
currentNode = currentNode->next_sibling("transform");
currentNode = currentNode->first_node("position");
newTransform.position = GetFloat3InXml(currentNode);
currentNode = currentNode->next_sibling("rotation");
newTransform.rotation = GetFloat3InXml(currentNode);
currentNode = currentNode->next_sibling("scale");
newTransform.scale = GetFloat3InXml(currentNode);
//construct object
//solution for wide string found at https://stackoverflow.com/questions/6691555/converting-narrow-string-to-wide-string
//get file names
std::wstring longName;
longName.assign(texLoc.begin(), texLoc.end());
GameObject* nextObject = new GameObject(d3dDevice, longName.c_str(), (char*)meshLoc.c_str());
nextObject->transform = newTransform;
objectList.push_back(nextObject);
//destroy document data.
documents[i]->clear();
}
documents.clear();
}
FogSettings XMLLoader::LoadFogSettings(std::string fileName) {
FogSettings result;
rapidxml::xml_document<>* doc = GetSingleXMLDocument(fileName);
rapidxml::xml_node<>* node = doc->first_node("fog");
//color
node = node->first_node("color");
result.fogColor = GetColorInXml(node);
//density
node = node->next_sibling("start");
rapidxml::xml_attribute<>* attrib = node->first_attribute("value");
result.fogStart = std::stof(attrib->value());
node = node->next_sibling("range");
attrib = node->first_attribute("value");
result.fogRange = std::stof(attrib->value());
doc->clear();
return result;
}
std::vector<rapidxml::xml_document<>*> XMLLoader::GetAllXMLDocumentsInDir(std::string dirName) {
std::vector<std::string> filenames;
std::vector<rapidxml::xml_document<>*> documents;
//utilize new c++17 features to find a list of files to ensure we process them all dynamically, without caring about how many.
//reference to this technique: https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c
for (const auto& entry : std::filesystem::directory_iterator(dirName))
filenames.push_back(entry.path().string());
//now for every file found, parse xml into heap.
for (int i = 0; i < filenames.size(); i++) {
documents.push_back(GetSingleXMLDocument(filenames[i]));
}
return documents;
}
rapidxml::xml_document<>* XMLLoader::GetSingleXMLDocument(std::string fileName) {
//use pointer instead of stack so the string memory remains available when the loop scope is exited.
//we don't care this pointer goes missing: doc has its own copies.
rapidxml::file<>* docFile = new rapidxml::file<>(fileName.c_str());
rapidxml::xml_document<>* doc = new rapidxml::xml_document<>();
doc->parse<0>(docFile->data());
return doc;
}
XMFLOAT3 XMLLoader::GetFloat3InXml(rapidxml::xml_node<>* node) {
XMFLOAT3 result;
rapidxml::xml_attribute<>* currentAttribute;
currentAttribute = node->first_attribute("x");
result.x = std::stof(currentAttribute->value());
currentAttribute = currentAttribute->next_attribute("y");
result.y = std::stof(currentAttribute->value());
currentAttribute = currentAttribute->next_attribute("z");
result.z = std::stof(currentAttribute->value());
return result;
}
XMFLOAT4 XMLLoader::GetColorInXml(rapidxml::xml_node<>* node) {
XMFLOAT4 result;
rapidxml::xml_attribute<>* currentAttribute;
currentAttribute = node->first_attribute("r");
result.x = std::stof(currentAttribute->value());
currentAttribute = currentAttribute->next_attribute("g");
result.y = std::stof(currentAttribute->value());
currentAttribute = currentAttribute->next_attribute("b");
result.z = std::stof(currentAttribute->value());
currentAttribute = currentAttribute->next_attribute("a");
result.w = std::stof(currentAttribute->value());
return result;
}