-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wavefront.cpp
270 lines (207 loc) · 9.04 KB
/
Wavefront.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
#include "Wavefront.h"
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <fstream>
#include <list>
glsh::Mesh* LoadWavefrontOBJ(const std::string& path)
{
std::cout << "Loading '" << path << "'" << std::endl;
// open the file for reading
std::ifstream file(path.c_str());
// make sure the file opened correctly
if (!file) {
std::cerr << "ERROR: Failed to open " << path << std::endl;
return NULL;
}
std::string line; // storage for a line of text
std::vector<std::string> lineTok; // storage for the tokens that make up a line
std::vector<std::string> vertexTok; // storage for the components of a vertex definition (v/vt/vn)
int lineno = 0; // tracks the line number that we're on (for desciptive error messages, mostly)
std::vector<glm::vec3> vertexPositions;
std::vector<glm::vec3> vertexNormals;
std::vector<glm::vec2> textureCoordinates;
bool withTextureCoords = true;
std::vector<glsh::VertexPositionNormalTexture> textureVerts;
std::vector<glsh::VertexPositionNormal> noTextureVerts;
// go through the file one line at a time until the end
for (;;) {
// read a line of text and check status
// - if the read failed due to end of file, then we're done reading and break out of the loop
// - otherwise, there was an error, so bail out
if (!std::getline(file, line)) {
if (file.eof()) {
break; // done reading file, proceed with vertex buffer creation
} else {
std::cerr << "ERROR: Failed to read from " << path << " at line " << lineno << std::endl;
return NULL;
}
}
// increment the line counter
++lineno;
// break the line up into whitespace-separated tokens
lineTok = glsh::Tokenize(line);
// if it's an empty line, move on to the next line
if (lineTok.empty()) {
continue;
}
// if it's a comment, move on to the next line
char firstChar = lineTok[0][0];
if (firstChar == '#') {
continue;
}
if (lineTok[0] == "v") {
// it's a vertex position
// make sure we have three coordinates following the "v"
if (lineTok.size() != 4) {
std::cerr << "ERROR: Incorrect number of vertex position coordinates on line " << lineno << std::endl;
return NULL;
}
// convert the coordinates to floats
float x = glsh::FromString<float>(lineTok[1]);
float y = glsh::FromString<float>(lineTok[2]);
float z = glsh::FromString<float>(lineTok[3]);
//std::cout << "Position: " << x << ", " << y << ", " << z << std::endl;
vertexPositions.push_back(glm::vec3(x, y, z));
} else if (lineTok[0] == "vn") {
// it's a vertex normal
// make sure we have three coordinates following the "vn"
if (lineTok.size() != 4) {
std::cerr << "ERROR: Incorrect number of normal coordinates on line " << lineno << std::endl;
return NULL;
}
// convert the coordinates to floats
float nx = glsh::FromString<float>(lineTok[1]);
float ny = glsh::FromString<float>(lineTok[2]);
float nz = glsh::FromString<float>(lineTok[3]);
//std::cout << "Normal: " << nx << ", " << ny << ", " << nz << std::endl;
glm::vec3 normal = glm::vec3(nx, ny, nz);
if (nx && ny && nz) {
normal = glm::normalize(normal);
}
vertexNormals.push_back(normal);
} else if (lineTok[0] == "vt") {
// it's a texture coordinate
// make sure we have at least two coordinates following the "vt"
if (lineTok.size() < 3) {
std::cerr << "ERROR: Incorrect number of uv coordinates on line " << lineno << std::endl;
return NULL;
}
// convert the coordinates to floats
float u = glsh::FromString<float>(lineTok[1]);
float v = glsh::FromString<float>(lineTok[2]);
//std::cout << "Texcoord: " << u << ", " << v << std::endl;
textureCoordinates.push_back(glm::vec2(u, v));
} else if (lineTok[0] == "f") {
// it's a face
std::vector<glsh::VertexPositionNormalTexture> verticesPNT;
std::vector<glsh::VertexPositionNormal> verticesPN;
// need at least 3 vertices per face
if (lineTok.size() < 4) {
std::cerr << "ERROR: Insufficient number of face elements on line " << lineno << std::endl;
return NULL;
}
//std::cout << "Face with " << lineTok.size() - 1 << " vertices" << std::endl;
// process each vertex definition for this face
for (unsigned i = 1; i < lineTok .size(); i++) {
// split into three parts (v/vt/vn)
vertexTok = glsh::Split(lineTok[i], '/');
// make sure there are three parts
if (vertexTok.size() != 3) {
std::cerr << "ERROR: Incorrect number of vertex tokens for vertex " << i << " on line " << lineno << std::endl;
return NULL;
}
// convert the index strings to ints
int positionIndex = 0;
int normalIndex = 0;
int texcoordIndex = 0;
// get position index (required)
if (!vertexTok[0].empty()) {
positionIndex = glsh::FromString<int>(vertexTok[0]);
} else {
std::cerr << "ERROR: Vertex position index not given for vertex " << i << " on line " << lineno << std::endl;
return NULL;
}
// get normal index (required)
if (!vertexTok[2].empty()) {
normalIndex = glsh::FromString<int>(vertexTok[2]);
} else {
std::cerr << "ERROR: Vertex normal index not given for vertex " << i << " on line " << lineno << std::endl;
return NULL;
}
// get texcoord index (optional)
if (!vertexTok[1].empty()) {
texcoordIndex = glsh::FromString<int>(vertexTok[1]);
if (!withTextureCoords) {
withTextureCoords = true;
}
} else if (withTextureCoords){
withTextureCoords = false;
// use a default texcoord of (0, 0)
// ...or create a mesh without texcoords
}
//std::cout << " Vertex pos at index " << positionIndex << ", normal at index " << normalIndex << ", texcoord at index " << texcoordIndex << std::endl;
if (withTextureCoords) {
glsh::VertexPositionNormalTexture vertex = glsh::VertexPositionNormalTexture(
vertexPositions[positionIndex - 1].x, vertexPositions[positionIndex - 1].y, vertexPositions[positionIndex - 1].z,
vertexNormals[normalIndex - 1].x, vertexNormals[normalIndex - 1].y, vertexNormals[normalIndex - 1].z,
textureCoordinates[texcoordIndex - 1].x, textureCoordinates[texcoordIndex - 1].y);
verticesPNT.push_back(vertex);
}
else {
glsh::VertexPositionNormal vertex = glsh::VertexPositionNormal(
vertexPositions[positionIndex - 1].x, vertexPositions[positionIndex - 1].y, vertexPositions[positionIndex - 1].z,
vertexNormals[normalIndex - 1].x, vertexNormals[normalIndex - 1].y, vertexNormals[normalIndex - 1].z);
verticesPN.push_back(vertex);
}
}
switch (withTextureCoords) {
case true:
if (verticesPNT.size() > 3) {
triangulate(verticesPNT);
}
for (unsigned int i = 0; i < verticesPNT.size(); i++) {
textureVerts.push_back(verticesPNT[i]);
}
case false:
if (verticesPN.size() > 3) {
triangulate(verticesPN);
}
for (unsigned int i = 0; i < verticesPN.size(); i++) {
noTextureVerts.push_back(verticesPN[i]);
}
}
}
}
if (withTextureCoords) {
return glsh::CreateMesh(GL_TRIANGLES, textureVerts);
}
else {
return glsh::CreateMesh(GL_TRIANGLES, noTextureVerts);
}
}
void triangulate(std::vector<glsh::VertexPositionNormalTexture>& _vertices) {
glsh::VertexPositionNormalTexture firstVertex = *(_vertices.begin());
glsh::VertexPositionNormalTexture previousVertex;
for (unsigned int i = 0; i < _vertices.size(); i++) {
if (i && i % 3 == 0) {
_vertices.insert(_vertices.begin() + i, firstVertex);
_vertices.insert(_vertices.begin() + i + 1, previousVertex);
i += 2;
}
previousVertex = _vertices[i];
}
}
void triangulate(std::vector<glsh::VertexPositionNormal>& _vertices) {
glsh::VertexPositionNormal firstVertex = *(_vertices.begin());
glsh::VertexPositionNormal previousVertex;
for (unsigned int i = 0; i < _vertices.size(); i++) {
if (i && i % 3 == 0) {
_vertices.insert(_vertices.begin() + i, firstVertex);
_vertices.insert(_vertices.begin() + i + 1, previousVertex);
i += 2;
}
previousVertex = _vertices[i];
}
}