-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjLoader.cpp
310 lines (253 loc) · 9.64 KB
/
ObjLoader.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
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 "ObjLoader.h"
#include <iostream>
#include <fstream>
ObjLoader::ObjLoader(std::string filepath)
{
std::ifstream file;
file.open(filepath.c_str(), std::ios::in);
for(std::string line; std::getline(file, line);)
{
std::stringstream ss(line);
std::string identifier;
ss >> identifier;
if(identifier.compare("#") ==0)
{
std::cout << "Found comment" << std::endl;
}
else if(identifier.compare("v") == 0)
{
std::vector<float> vec3 = loadNumbers(ss);
vertices.push_back(Vertex(vec3[0], vec3[1], vec3[2]));
}
else if(identifier.compare("vn") == 0)
{
std::vector<float> vec3 = loadNumbers(ss);
normals.push_back(triplet<float, float, float>(vec3[0], vec3[1], vec3[2]));
}
else if(identifier.compare("vt") == 0)
{
//std::cout << "tex coord found" << std::endl;
std::pair<float, float> texCoord = loadTexCoords(ss);
texCoords.push_back(texCoord);
}
else if(identifier.compare("f") == 0)
{
std::string face;
ss >> face;
Face newFace;
while(!ss.fail())
{
std::vector<std::pair<int, bool> > vec3 = loadFace(face);
newFace.addVertex(vec3[0].first);
if(vec3[1].second)
{
newFace.addTexCoord(vec3[1].first);
}
if(vec3[2].second)
{
newFace.addNormal(vec3[2].first);
}
ss >> face;
}
for(int i = 0; i < 3; i ++)
{
int vertexIdx = newFace.getVertex(i);
Vertex vert = vertices[vertexIdx];
triplet<float, float, float> normal = normals[newFace.getNormal(i)];
vert.addNormal(normal.first, normal.second, normal.third);
vertices[vertexIdx] = vert;
}
faces.push_back(newFace);
}
}
/*for(std::vector<Face>::iterator it = faces.begin(); it != faces.end(); ++it)
{
Face face = *it;
std::cout << "Face:" << std::endl;
std::cout << "<" << vertices[face.getVertex(0)].first << "," << vertices[face.getVertex(0)].second << "," << vertices[face.getVertex(0)].third << ">" << std::endl;
std::cout << "<" << vertices[face.getVertex(1)].first << "," << vertices[face.getVertex(1)].second << "," << vertices[face.getVertex(1)].third << ">" << std::endl;
std::cout << "<" << vertices[face.getVertex(2)].first << "," << vertices[face.getVertex(2)].second << "," << vertices[face.getVertex(2)].third << ">" << std::endl;
}*/
file.close();
}
ObjLoader::~ObjLoader()
{
//dtor
}
std::vector<float> ObjLoader::loadNumbers(std::stringstream &ss)
{
std::vector<float> vec3;
float number;
ss >> number;
do
{
vec3.push_back(number);
ss >> number;
}
while(!ss.fail());
return vec3;
}
std::pair<float, float> ObjLoader::loadTexCoords(std::stringstream &ss)
{
std::pair<float, float> texCoord;
float number;
ss >> number;
texCoord.first = number;
ss >> number;
texCoord.second = 1.0f - number;
return texCoord;
}
std::vector<std::pair<int, bool> > ObjLoader::loadFace(std::string&ss)
{
std::vector<std::pair<int, bool> > face(3);
std::string::iterator start = ss.begin();
std::string::iterator end = ss.begin();
while((end != ss.end()) && (*end != '/'))
{
++end;
}
{
std::string num(start, end);
std::istringstream convert(num);
int vertex;
convert >> vertex;
face.insert(face.begin(), std::pair<int, bool>((vertex - 1), true));
}
start = end + 1;
end = end + 1;
while((end != ss.end()) && (*end != '/'))
{
++end;
}
if(start != end)
{
std::string texCoord(start, end);
std::istringstream convert(texCoord);
int textureCoord;
convert >> textureCoord;
face.insert(face.begin() + 1, std::pair<int, bool>((textureCoord - 1), true));
}
start = end + 1;
end = end + 1;
while((end != ss.end()))
{
++end;
}
if(start != end)
{
std::string normalNum(start, end);
std::istringstream convert(normalNum);
int normal;
convert >> normal;
face.insert(face.begin() + 2, std::pair<int, bool>((normal - 1), true));
}
return face;
}
float *ObjLoader::getVertices()
{
int count = faces.size() * 3 * 3;
float *vertices = new float[faces.size() * 3 * 3];
int i = 0;
for(std::vector<Face>::iterator it = faces.begin(); it != faces.end(); ++it, i += 9)
{
Face face = *it;
triplet<float, float, float> vertex1= (this->vertices[face.getVertex(2)]).getXyz();
vertices[i + 0] = vertex1.first;
vertices[i + 1] = vertex1.second;
vertices[i + 2] = vertex1.third;
triplet<float, float, float> vertex2 = (this->vertices[face.getVertex(1)]).getXyz();
vertices[i + 3] = vertex2.first;
vertices[i + 4] = vertex2.second;
vertices[i + 5] = vertex2.third;
triplet<float, float, float> vertex3 = (this->vertices[face.getVertex(0)]).getXyz();
vertices[i + 6] = vertex3.first;
vertices[i + 7] = vertex3.second;
vertices[i + 8] = vertex3.third;
}
return vertices;
}
size_t ObjLoader::vsize()
{
return sizeof(float) * faces.size() * 3 * 3;
}
size_t ObjLoader::nsize()
{
return sizeof(float) * faces.size() * 3 * 3;
}
size_t ObjLoader::tsize()
{
return sizeof(float) * faces.size() * 3 * 2;
}
float *ObjLoader::getNormals()
{
float *normals = new float[faces.size() * 3 * 3];
int i = 0;
for(std::vector<Face>::iterator it = faces.begin(); it != faces.end(); ++it)
{
Face face = *it;
for(std::vector<int>::iterator itNormals = face.beginNormals(); itNormals != face.endNormals(); ++itNormals, i += 3)
{
int normalIndex = *itNormals;
triplet<float, float, float> normal = this->normals[normalIndex];
normals[i + 0] = normal.first;
normals[i + 1] = normal.second;
normals[i + 2] = normal.third;
}
}
return normals;
}
float *ObjLoader::getAvgNormals()
{
float *normals = new float[faces.size() * 3 * 3];
int i = 0;
for(std::vector<Face>::iterator it = faces.begin(); it != faces.end(); ++it)
{
Face face = *it;
//Reverse normals because the vertices are in the wrong order so we had to reverse them
//so we have to reverse the order of normals as well!
for(int j = 2; j >= 0; j--, i += 3)
{
int vertIdx = face.getVertex(j);
Vertex vert = vertices[vertIdx];
triplet<float, float, float> normal = vert.getNormal();
normals[i + 0] = normal.first;
normals[i + 1] = normal.second;
normals[i + 2] = normal.third;
}
}
return normals;
}
float *ObjLoader::getTexCoords()
{
float *textureCoords = new float[faces.size() * 3 * 2];
int i = 0;
for(std::vector<Face>::iterator it = faces.begin(); it != faces.end(); ++it, i += 6)
{
Face face = *it;
std::pair<float, float> coord1 = texCoords[face.getTexCoord(2)];
textureCoords[i + 0] = coord1.first;
textureCoords[i + 1] = coord1.second;
std::pair<float, float> coord2 = texCoords[face.getTexCoord(1)];
textureCoords[i + 2] = coord2.first;
textureCoords[i + 3] = coord2.second;
std::pair<float, float> coord3 = texCoords[face.getTexCoord(0)];
textureCoords[i + 4] = coord3.first;
textureCoords[i + 5] = coord3.second;
}
return textureCoords;
}
float *ObjLoader::getColours()
{
float *colours = new float[faces.size() * 3 * 3];
for(int i = 0; i < faces.size() * 3 * 3; i += 3)
{
colours[i + 0] = 1.0f;
colours[i + 1] = 0.0f;
colours[i + 2] = 0.0f;
}
return colours;
}
size_t ObjLoader::csize()
{
return sizeof(float) * faces.size() * 3 * 3;
}