-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAssetReaders.cpp
More file actions
310 lines (266 loc) · 6.55 KB
/
Copy pathAssetReaders.cpp
File metadata and controls
310 lines (266 loc) · 6.55 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "AssetReaders.hpp"
#include <fstream>
#include <sstream>
#include <iostream>
BEGIN_XE_NAMESPACE
bool startsWith(std::string s, std::string prefix)
{
if (s.size() < prefix.size())
return false;
auto start = s.substr(0, prefix.size());
return start == prefix;
}
void appendFromObjLine3f(std::vector<float> &target, std::istringstream &iss)
{
float x, y, z;
iss >> x >> y >> z;
target.push_back(x);
target.push_back(y);
target.push_back(z);
}
void appendFromObjLine2f(std::vector<float> &target, std::istringstream &iss)
{
float x, y;
iss >> x >> y;
target.push_back(x);
target.push_back(y);
}
void appendVertexFlat(
ModelData &data,
std::vector<float> &rawPositions,
std::vector<float> &rawUvs,
std::vector<float> &rawNormals,
unsigned int vp,
unsigned int vuv,
unsigned int vn)
{
auto pOffset = (vp - 1) * 3;
auto uvOffset = (vuv - 1) * 2;
auto nOffset = (vn - 1) * 3;
data.positions.push_back(rawPositions[pOffset + 0]);
data.positions.push_back(rawPositions[pOffset + 1]);
data.positions.push_back(rawPositions[pOffset + 2]);
data.uvs.push_back(rawUvs[uvOffset + 0]);
data.uvs.push_back(rawUvs[uvOffset + 1]);
data.normals.push_back(rawNormals[nOffset + 0]);
data.normals.push_back(rawNormals[nOffset + 1]);
data.normals.push_back(rawNormals[nOffset + 2]);
}
void appendVertexSmooth(
ModelData &data,
std::vector<float> &rawPositions,
std::vector<float> &rawUvs,
std::vector<float> &rawNormals,
unsigned int vp,
unsigned int vuv,
unsigned int vn)
{
auto uvSource = (vuv - 1) * 2;
auto uvDest = (vp - 1) * 2;
auto nSource = (vn - 1) * 3;
auto nDest = (vp - 1) * 3;
data.uvs[uvDest + 0] = rawUvs[uvSource + 0];
data.uvs[uvDest + 1] = rawUvs[uvSource + 1];
data.normals[nDest + 0] = rawNormals[nSource + 0];
data.normals[nDest + 1] = rawNormals[nSource + 1];
data.normals[nDest + 2] = rawNormals[nSource + 2];
}
void appendFaceFromObjLineFlat(
ModelData &data,
std::vector<float> &rawPositions,
std::vector<float> &rawUvs,
std::vector<float> &rawNormals,
std::istringstream &iss)
{
char _;
unsigned int v1p, v1uv, v1n, v2p, v2uv, v2n, v3p, v3uv, v3n;
iss >> v1p >> _ >> v1uv >> _ >> v1n >> v2p >> _ >> v2uv >> _ >> v2n >> v3p
>> _ >> v3uv >> _ >> v3n;
appendVertexFlat(data, rawPositions, rawUvs, rawNormals, v1p, v1uv, v1n);
appendVertexFlat(data, rawPositions, rawUvs, rawNormals, v2p, v2uv, v2n);
appendVertexFlat(data, rawPositions, rawUvs, rawNormals, v3p, v3uv, v3n);
auto faceIndex = data.indices.size();
data.indices.push_back(faceIndex + 0);
data.indices.push_back(faceIndex + 1);
data.indices.push_back(faceIndex + 2);
}
void appendFaceFromObjLineSmooth(
ModelData &data,
std::vector<float> &rawPositions,
std::vector<float> &rawUvs,
std::vector<float> &rawNormals,
std::istringstream &iss)
{
char _;
unsigned int v1p, v1uv, v1n, v2p, v2uv, v2n, v3p, v3uv, v3n;
iss >> v1p >> _ >> v1uv >> _ >> v1n >> v2p >> _ >> v2uv >> _ >> v2n >> v3p
>> _ >> v3uv >> _ >> v3n;
appendVertexSmooth(data, rawPositions, rawUvs, rawNormals, v1p, v1uv, v1n);
appendVertexSmooth(data, rawPositions, rawUvs, rawNormals, v2p, v2uv, v2n);
appendVertexSmooth(data, rawPositions, rawUvs, rawNormals, v3p, v3uv, v3n);
data.indices.push_back(v1p);
data.indices.push_back(v2p);
data.indices.push_back(v3p);
}
ModelData readObjFileSmooth(std::string path)
{
ModelData data;
std::ifstream infile(path);
std::string line;
std::vector<float> rawPositions;
std::vector<float> rawNormals;
std::vector<float> rawUvs;
while (std::getline(infile, line))
{
std::istringstream iss(line);
std::string prefix;
iss >> prefix;
if (prefix == "v")
{
appendFromObjLine3f(rawPositions, iss);
}
else if (prefix == "vt")
{
appendFromObjLine2f(rawUvs, iss);
}
else if (prefix == "vn")
{
appendFromObjLine3f(rawNormals, iss);
}
else if (prefix == "f")
{
if (data.positions.size() == 0)
{
data.positions = rawPositions;
if (rawNormals.size() != 0)
{
data.normals = std::vector<float>(rawPositions.size());
}
if (rawUvs.size() != 0)
{
data.uvs =
std::vector<float>((rawPositions.size() / 3) * 2);
}
}
appendFaceFromObjLineSmooth(
data, rawPositions, rawUvs, rawNormals, iss);
}
}
return data;
}
ModelData readObjFileFlat(std::string path)
{
ModelData data;
std::ifstream infile(path);
std::string line;
std::vector<float> rawPositions;
std::vector<float> rawNormals;
std::vector<float> rawUvs;
while (std::getline(infile, line))
{
std::istringstream iss(line);
std::string prefix;
iss >> prefix;
if (prefix == "v")
{
appendFromObjLine3f(rawPositions, iss);
}
else if (prefix == "vt")
{
appendFromObjLine2f(rawUvs, iss);
}
else if (prefix == "vn")
{
appendFromObjLine3f(rawNormals, iss);
}
else if (prefix == "f")
{
appendFaceFromObjLineFlat(
data, rawPositions, rawUvs, rawNormals, iss);
}
}
return data;
}
unsigned getPlyVertCount(std::ifstream &infile)
{
std::string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
std::string element;
iss >> element;
if (element == "element")
{
std::string type;
iss >> type;
if (type == "vertex")
{
unsigned count;
iss >> count;
return count;
}
}
}
return 0;
}
void skipRemainingPlyHeader(std::ifstream &infile)
{
std::string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
std::string word;
iss >> word;
if (word == "end_header")
{
break;
}
else
{
std::cout << word << std::endl;
}
}
}
ModelData readPlyFile(std::string path)
{
ModelData data;
std::ifstream infile(path);
// find vertex count in header
auto vertCount = getPlyVertCount(infile);
// read until end of header
skipRemainingPlyHeader(infile);
// read vertex info
std::string line;
for (unsigned i = 0; i < vertCount && std::getline(infile, line); i++)
{
std::istringstream iss(line);
float x, y, z, nx, ny, nz, r, g, b;
iss >> x >> y >> z >> nx >> ny >> nz >> r >> g >> b;
data.positions.push_back(x);
data.positions.push_back(z);
data.positions.push_back(y);
data.normals.push_back(nx);
data.normals.push_back(nz);
data.normals.push_back(ny);
data.colors.push_back(r / 255.0f);
data.colors.push_back(g / 255.0f);
data.colors.push_back(b / 255.0f);
}
// read index info (faces)
while (std::getline(infile, line))
{
std::istringstream iss(line);
unsigned vertsInFace, a, b, c;
iss >> vertsInFace;
if (vertsInFace == 3)
{
// triangle
iss >> a >> b >> c;
data.indices.push_back(a);
data.indices.push_back(c);
data.indices.push_back(b);
}
}
return data;
}
END_XE_NAMESPACE