-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseConvert.cpp
262 lines (252 loc) · 8.1 KB
/
ParseConvert.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "ParseConvert.h"
struct ParseConvert::XMFLOAT3 {
float x;
float y;
float z;
XMFLOAT3() {}
XMFLOAT3(float x, float y, float z) :x(x), y(y), z(z) {}
bool operator==(const XMFLOAT3 &xmlfloat3) const {
return (this->x == xmlfloat3.x) && (this->y == xmlfloat3.y) && (this->z == xmlfloat3.z);
}
};
struct ParseConvert::Vertex {
XMFLOAT3 pos;
XMFLOAT3 textCoord;
XMFLOAT3 normals;
Vertex() {}
Vertex(XMFLOAT3 &pos, XMFLOAT3 &normals, XMFLOAT3 &textCoord) :pos(pos), normals(normals), textCoord(textCoord) {}
bool operator==(const Vertex &vertex)const {
return (vertex.normals == this->normals) && (vertex.pos == this->pos) && (vertex.textCoord == this->textCoord);
}
};
struct ParseConvert::Materials {
bool hasTexture;
bool hasNormMap;
float range;
float transparancy;
std::wstring diffuseMap;
std::wstring normalMap;
XMFLOAT3 ambient;
XMFLOAT3 diffuse;
XMFLOAT3 defaultÑolor;
XMFLOAT3 transparencyFactor;
Materials() {}
Materials(float &range, float &transparancy, XMFLOAT3 &ambient, XMFLOAT3 &diffuse, XMFLOAT3 &defaultÑolor, XMFLOAT3 &transparencyFactor):
range(range),
transparancy(transparancy),
ambient(ambient),
diffuse(diffuse),
defaultÑolor(defaultÑolor),
transparencyFactor(transparencyFactor),
hasTexture(false),
hasNormMap(false){}
};
void ParseConvert::SplitFacesOBJ(std::wstring &data, int *bufferArray) {
auto first_position = data.find_first_of('/');
auto second_position = data.find_last_of('/');
bufferArray[0] = stoi(data.substr(0, first_position));
if (first_position == std::wstring::npos) {
bufferArray[1] = -1;
bufferArray[2] = -1;
}
else if (second_position != first_position && second_position != (first_position + 1)) {
bufferArray[1] = stoi(data.substr(first_position + 1, second_position - first_position - 1));
bufferArray[2] = stoi(data.substr(second_position + 1, std::wstring::npos));
}
else if (second_position != first_position && second_position == (first_position + 1)) {
bufferArray[1] = stoi(data.substr(second_position + 1, std::wstring::npos));
bufferArray[2] = -1;
}
else {
bufferArray[1] = -1;
bufferArray[2] = stoi(data.substr(second_position + 1, std::wstring::npos));
}
}
ParseConvert::ParseConvert()
{
}
bool ParseConvert::LoadOBJ(const std::wstring &filename) {
std::wifstream input(filename);
std::wstring buffer;
std::wstringstream strstream;
float x, y, z;
int x_buf, y_buf, z_buf;
if (input) {
while (input) {
input >> buffer;
if (buffer == L"#") {
getline(input, buffer);
}
else if (buffer == L"g") {//äîäåëàòü
this->meshStartPosition.push_back(indecis.size());
}
else if (buffer == L"mtllib") { //check mtllib
input >> buffer;
this->mtlName = buffer;
}
else if (buffer == L"v") { //search vertecis
input >> x >> y >> z;
this->verteñis.push_back(XMFLOAT3(x, y, z));
}
else if (buffer == L"vn") { // search normals
input >> x >> y >> z;
this->normals.push_back(XMFLOAT3(x, y, z));
}
else if (buffer == L"vt") { // search text coordinates
input >> x >> y >> z;
this->textÑoords.push_back(XMFLOAT3(x, y, z));// äîðàáîòàòü è óáðàòü z
}
else if (buffer == L"usemtl") {
input >> buffer;
this->useMtl.push_back(buffer);
}
else if (buffer == L"f") { // calculate normals
for (size_t i(0); i < 3; i++) { //read each Vertex for triangle
input >> buffer;
strstream.str(buffer);
getline(strstream, buffer, L'/'); // vertecis
x_buf = std::stoi(buffer);
if (x_buf < 0) {
x_buf *= (-1);
}
getline(strstream, buffer, L'/'); // texture coordinates
y_buf = std::stoi(buffer);
if (y_buf < 0) {
y_buf *= (-1);
}
getline(strstream, buffer, L'/'); // normals
z_buf = std::stoi(buffer);
if (z_buf < 0) {
z_buf *= (-1);
}
;
auto findResult = find(vertex.begin(), vertex.end(), Vertex (verteñis[x_buf - 1], normals[z_buf - 1], textÑoords[y_buf - 1]));
if (findResult == vertex.end()) { //have not such vertex
this->vertex.push_back(Vertex(verteñis[x_buf - 1], normals[z_buf - 1], textÑoords[y_buf - 1]));// add new triangle
this->indecis.push_back(vertex.size() - 1);
}
else {//already have vertex
this->indecis.push_back(findResult - vertex.begin());
}
strstream.clear();
strstream.ignore(strstream.rdbuf()->in_avail());
}
}
}
if (!this->LoadMTL()) {
std::cerr << "MTL wasn't loaded";
}
return true;
}
else {
return false;
}
}
bool ParseConvert::LoadMTL() {
std::wifstream input(this->mtlName);
std::wstring buffer;
std::map <std::wstring, std::unique_ptr<Materials>>::iterator matIter(materials.end());
if (input) {
while (input) {
input >> buffer;
if (buffer == L"#") {
getline(input, buffer);
}
else if (buffer == L"newmtl") {
input >> buffer;
this->materials.insert(std::pair<std::wstring, std::unique_ptr<Materials>>(buffer, std::make_unique<Materials>()));
matIter = this->materials.find(buffer);
}
else if (buffer == L"Ns") {
if (matIter != materials.end()) {
input >> matIter->second->range;
}
}
else if (buffer == L"Tr") {
if (matIter != materials.end()) {
input >> matIter->second->transparancy;
}
}
else if (buffer == L"Tf") {
if (matIter != materials.end()) {
input >> matIter->second->transparencyFactor.x >> matIter->second->transparencyFactor.y >> matIter->second->transparencyFactor.z;
}
}
else if (buffer == L"Ka") {
if (matIter != materials.end()) {
input >> matIter->second->ambient.x >> matIter->second->ambient.y >> matIter->second->ambient.z;
}
}
else if (buffer == L"Kd") {
if (matIter != materials.end()) {
input >> matIter->second->diffuse.x >> matIter->second->diffuse.y >> matIter->second->diffuse.z;
}
}
else if (buffer == L"Ks") {
if (matIter != materials.end()) {
input >> matIter->second->defaultÑolor.x >> matIter->second->defaultÑolor.y >> matIter->second->defaultÑolor.z;
}
}
else if (buffer == L"map_Kd") {
if (matIter != materials.end()) {
input >> matIter->second->diffuseMap;
matIter->second->hasTexture = true;
}
else if (buffer == L"map_bump" || buffer == L"bump") {
if (matIter != materials.end()) {
input >> matIter->second->normalMap;
matIter->second->hasNormMap = true;
}
}
}
}
return true;
}
else {
return false;
}
}
void ParseConvert::Out() {
std::ofstream out("check.txt");
for (const auto &element : this->vertex) {
out << element.pos.x << ',' << element.pos.y << ',' << element.pos.z << ',';
out << element.normals.x << ',' << element.normals.y << ',' << element.normals.z << ',';
out << element.textCoord.x << ',' << element.textCoord.y << ',' << element.textCoord.z << '\n';
}
for (const auto &element : this->materials) {
out << element.second->range << '\n';
out << element.second->transparancy << '\n';
out << element.second->transparencyFactor.x << ',' << element.second->transparencyFactor.y << ',' << element.second->transparencyFactor.z << '\n';
out << element.second->ambient.x << ',' << element.second->ambient.y << ',' << element.second->ambient.z << '\n';
out << element.second->diffuse.x << ',' << element.second->diffuse.y << ',' << element.second->diffuse.z << '\n';
out << element.second->defaultÑolor.x << ',' << element.second->defaultÑolor.y << ',' << element.second->defaultÑolor.z << '\n';
}
size_t count(0);
for (const auto &element : this->indecis) {
if (count == 3) {
out << '\n';
count = 0;
}
out << element << ' ';
count++;
}
out << '\n';
std::wcout << useMtl[0] << L'\n';
out.close();
}
ParseConvert::~ParseConvert()
{
verteñis.clear();
verteñis.shrink_to_fit();
textÑoords.clear();
textÑoords.shrink_to_fit();
indecis.clear();
indecis.shrink_to_fit();
normals.clear();
normals.shrink_to_fit();
useMtl.clear();
useMtl.shrink_to_fit();
vertex.clear();
vertex.shrink_to_fit();
materials.clear();
}