-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjReader.cpp
More file actions
325 lines (231 loc) · 8.67 KB
/
ObjReader.cpp
File metadata and controls
325 lines (231 loc) · 8.67 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "ObjReader.h"
#include <sstream>
#include <iostream>
#include <fstream>
#include <cfloat>
#define DEBUG_OBJ 1
#if DEBUG_OBJ
#define DBG(x) std::cout << x << std::endl
#else
#define DBG(x)
#endif
int meshID = 0;
ObjReader::ObjReader()
{
}
ObjReader::~ObjReader()
{
}
void ObjReader::SetReadParam(ObjData param, bool value)
{
m_ObjParam[(int)param] = value;
}
std::unordered_map<std::string, ObjMaterial> ObjReader::ReadMaterial(const std::string &path)
{
std::unordered_map<std::string, ObjMaterial> objMaterials;
if(path.substr(path.find_last_of(".") + 1) != "mtl"){
std::cout << "ERROR: UNSUPPORTED FILE FORMAT. ONLY ACCEPTS .mtl";
return objMaterials;
}
std::ifstream stream(path);
if (!stream.is_open()) {
std::cout << "FAILED TO OPEN FILE: " << path << std::endl;
return objMaterials;
}
std::string line;
std::string currentMaterial;
std::cout << "reading object: " << path << std::endl;
while (getline(stream, line)){
if(line.empty() || line.size() < 2)
continue;
if (line.rfind("newmtl", 0) == 0) {
std::stringstream ss(line);
std::string newmtl;
ss >> newmtl >> currentMaterial;
std::cout << currentMaterial << std::endl;
ObjMaterial mat = {};
objMaterials[currentMaterial] = mat;
std::cout << "Current material: " << currentMaterial << std::endl;
}
if(line.rfind("Kd", 0) == 0)
{
if(!currentMaterial.empty()){
std::stringstream ss(line.substr(3));
float x, y, z;
ss >> x >> y >> z;
objMaterials[currentMaterial].hasColor = true;
objMaterials[currentMaterial].color = {x, y, z, 1.0f};
}
}
//TODO implement ka, ks etc
else if (line[0] == '#'){
std::cout << line << std::endl;
}
}
return objMaterials;
}
Mesh ObjReader::ReadObject(const std::string &path)
{
if(path.substr(path.find_last_of(".") + 1) != "obj"){
std::cout << "ERROR: UNSUPPORTED FILE FORMAT. ONLY ACCEPTS .OBJ";
return Mesh();
}
//open the material file if exist
std::string mtlPath = path.substr(0, path.find_last_of('.')) + ".mtl";
std::unordered_map<std::string, ObjMaterial> objMaterials = ReadMaterial(mtlPath);
std::ifstream stream(path);
if (!stream.is_open()) {
std::cout << "FAILED TO OPEN FILE: " << path << std::endl;
}
std::string line;
std::vector<glm::vec3> o_vertex;
std::vector<glm::vec3> o_vnormal;
std::vector<glm::vec2> o_texcord;
std::cout << "reading object: " << path << std::endl;
Mesh mesh;
std::unordered_map<ObjFace, uint32_t> FaceIndexMap;
bool hasNormal = false;
std::string currentMaterial;
while (getline(stream, line)){
if(line.empty() || line.size() < 2)
continue;
if(line[0] == 'o')
{
std::cout << line.substr(2) << path << std::endl;
}
if(line[0] == 'v')
{
if (line[1] == ' ' && m_ObjParam[ObjData::VERTEX])
{
std::stringstream ss(line.substr(2));
float x, y, z;
ss >> x >> y >> z;
o_vertex.push_back({x, y, z});
//Read Vertex data
}
else if (line[1] == 'n' && m_ObjParam[ObjData::NORMAL])
{
hasNormal = true;
//Read Vertex normal
std::stringstream ss(line.substr(2));
float x, y, z;
ss >> x >> y >> z;
o_vnormal.push_back({x, y, z});
}
else if (line[1] == 't' && m_ObjParam[ObjData::TEXCORD])
{
//Read text cord
std::stringstream ss(line.substr(2));
float x, y;
ss >> x >> y;
o_texcord.push_back({x, y});
}
}
else if (line[0] == 'f')
{
std::vector<uint32_t> faceIndices;
std::vector<ObjFace> f = ReadFaceData(line);
for (size_t i = 0; i < f.size(); i++)
{
//If this face already exist, no need to dublicate it
auto idx = FaceIndexMap.find(f[i]);
if(idx != FaceIndexMap.end()){
faceIndices.push_back(idx->second); // Push the index to the idx buffer
}
else{
Vertex v = f[i].GenerateVertex(o_vertex, o_vnormal, o_texcord);
uint32_t n_idx = mesh.vertices.size();
mesh.vertices.push_back(v);
FaceIndexMap[f[i]] = n_idx;
faceIndices.push_back(n_idx);
}
}
//The provided basic triangulation and process the face
for (size_t i = 1; i < faceIndices.size()-1; i++)
{
mesh.indices.push_back(faceIndices[0]);
mesh.indices.push_back(faceIndices[i]);
mesh.indices.push_back(faceIndices[i + 1]);
//Recalculate the normal if not exist
if(!hasNormal){
glm::vec3 A = mesh.vertices[faceIndices[0]].pos;
glm::vec3 B = mesh.vertices[faceIndices[i]].pos;
glm::vec3 C = mesh.vertices[faceIndices[i+1]].pos;
// //Calculate the normal if none is given using right handed rule
glm::vec3 n = glm::normalize(glm::cross(B-A, C-A));
mesh.vertices[faceIndices[0]].normal = n;
mesh.vertices[faceIndices[i]].normal = n;
mesh.vertices[faceIndices[i+1]].normal = n;
}
//Fetch material
if(objMaterials.find(currentMaterial) != objMaterials.end() && objMaterials[currentMaterial].hasColor){
ObjMaterial mat = objMaterials[currentMaterial];
mesh.vertices[faceIndices[0]].color = mat.color;
mesh.vertices[faceIndices[i]].color = mat.color;
mesh.vertices[faceIndices[i+1]].color = mat.color;
} else { //Just for fun, add the normal as color
//mesh.vertices[faceIndices[0]].color = glm::vec4(mesh.vertices[faceIndices[0]].normal, 1.0f);
//mesh.vertices[faceIndices[i]].color = glm::vec4(mesh.vertices[faceIndices[i]].normal, 1.0f);
//mesh.vertices[faceIndices[i+1]].color = glm::vec4(mesh.vertices[faceIndices[i+1]].normal, 1.0f);
}
} }
else if (line[0] == '#'){
std::cout << line << std::endl;
currentMaterial = "";
}
if (line.rfind("usemtl", 0) == 0) {
std::stringstream ss(line);
std::string usemtl;
ss >> usemtl >> currentMaterial;
}
}
std::cout << "Finished reading object" << std::endl;
NormalizeMesh(mesh);
mesh.id = meshID++;
return mesh;
}
void ObjReader::NormalizeMesh(Mesh& mesh){
//Find max and min values for all vertex
glm::vec3 min( FLT_MAX);
glm::vec3 max(-FLT_MAX);
for (size_t i = 0; i < mesh.vertices.size(); i++)
{
min.x = std::min(min.x, mesh.vertices[i].pos.x);
min.y = std::min(min.y, mesh.vertices[i].pos.y);
min.z = std::min(min.z, mesh.vertices[i].pos.z);
max.x = std::max(max.x, mesh.vertices[i].pos.x);
max.y = std::max(max.y, mesh.vertices[i].pos.y);
max.z = std::max(max.z, mesh.vertices[i].pos.z);
}
//Normalize the vertex
for (size_t i = 0; i < mesh.vertices.size(); i++)
{
mesh.vertices[i].pos = 2.0f*( (mesh.vertices[i].pos-min) / (max - min) ) -1.0f;
}
}
std::vector<ObjFace> ObjReader::ReadFaceData(std::string& line)
{
std::vector<ObjFace> faces;
std::stringstream ss(line.substr(2));
std::string ref;
while (ss >> ref)
{
int part = 0;
std::string temp;
std::stringstream s2(ref);
ObjFace face;
//Bcs we need to support v/vt/vn and v//vt etc... so delim it
while(getline(s2, temp,'/'))
{
if(!temp.empty())
{
if(part == 0) face.v_idx = std::stoi(temp);
else if (part == 1) face.vt_idx = std::stoi(temp);
else if (part == 2)face.vn_idx = std::stoi(temp);
}
part++;
}
faces.push_back(face);
}
return faces;
}