-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.cpp
272 lines (216 loc) · 7.4 KB
/
mesh.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
/**
* @file mesh.cpp
* @brief Mesh class and OBJ loader.
*
* @author Eric Butler (edbutler)
* @author Zeyang Li (zeyangl)
*/
#include "mesh.hpp"
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
namespace _462 {
struct TriIndex
{
int vertex;
int normal;
int tcoord;
bool operator<( const TriIndex& rhs ) const {
if ( vertex == rhs.vertex ) {
if ( normal == rhs.normal ) {
return tcoord < rhs.tcoord;
} else {
return normal < rhs.normal;
}
} else {
return vertex < rhs.vertex;
}
}
};
struct Face
{
TriIndex v[3];
};
enum ObjFormat
{
VERTEX_ONLY = 1 << 0,
VERTEX_UV = 1 << 1,
VERTEX_NORMAL = 1 << 2,
VERTEX_UV_NORMAL = 1 << 3
};
Mesh::Mesh() { }
Mesh::~Mesh() { }
bool Mesh::load()
{
std::cout << "Loading mesh from '" << filename << "'..." << std::endl;
std::string line;
std::ifstream file( filename.c_str() );
typedef std::vector< Vector3 > PositionList;
typedef std::vector< Vector3 > NormalList;
typedef std::vector< Vector2 > UVList;
typedef std::vector< Face > FaceList;
static const char* scan_vertex = "%d";
static const char* scan_vertex_uv = "%d/%d";
static const char* scan_vertex_normal = "%d//%d";
static const char* scan_vertex_uv_normal = "%d/%d/%d";
size_t num_vertex;
TriIndex tri[4];
FaceList face_list;
PositionList position_list;
NormalList normal_list;
UVList uv_list;
int line_num = 0;
std::string token;
triangles.clear();
ObjFormat format = VERTEX_ONLY;
typedef std::map< TriIndex, unsigned int > VertexMap;
VertexMap vertex_map;
if ( !file.is_open() ) {
std::cout << "Error opening file '" << filename << "' for mesh loading.\n";
return false;
}
while ( getline( file, line ) )
{
std::stringstream stream( line );
stream >> token;
line_num++;
if ( token == "v" ) {
Vector3 position;
stream >> position.x >> position.y >> position.z;
if ( stream.fail() ) {
std::cerr << "position syntax error on line " << line_num << std::endl;
return false;
}
position_list.push_back( position );
} else if ( token == "vn" ) {
Vector3 normal;
stream >> normal.x >> normal.y >> normal.z;
if( stream.fail() ) {
std::cerr << "normal syntax error on line " << line_num << std::endl;
return false;
}
normal_list.push_back( normal );
} else if ( token == "vt" ) {
Vector2 uv;
stream >> uv.x >> uv.y;
if ( stream.fail() ) {
std::cerr << "uv syntax error on line " << line_num << std::endl;
return false;
}
uv_list.push_back( uv );
} else if ( token == "f" ) {
std::vector< std::string > face_tokens;
std::string vert;
while ( true ) {
stream >> vert;
if( stream.fail() )
break;
face_tokens.push_back( vert );
}
// if it's the first time parsing a face, figure out the face format
if ( face_list.size() == 0 ) {
std::string token = face_tokens[0];
if ( token.find( "//" ) != std::string::npos ) {
format = VERTEX_NORMAL;
} else if ( token.find( '/' ) == std::string::npos ) {
format = VERTEX_ONLY;
} else {
size_t p1 = token.find( '/' );
size_t p2 = token.rfind( '/' );
if ( p1 == p2 ) {
format = VERTEX_UV;
} else {
format = VERTEX_UV_NORMAL;
}
}
}
num_vertex = face_tokens.size();
if ( num_vertex > 4 || num_vertex < 3 ) {
std::cerr << "Syntax error at line " << line_num
<< ", face has incorrect number of vertices" << std::endl;
return false;
}
for ( size_t i = 0; i < num_vertex; ++i ) {
switch ( format )
{
case VERTEX_ONLY:
sscanf( face_tokens[i].c_str(),
scan_vertex,
&tri[i].vertex );
tri[i].normal = 0;
tri[i].tcoord = 0;
break;
case VERTEX_UV:
sscanf( face_tokens[i].c_str(),
scan_vertex_uv,
&tri[i].vertex,
&tri[i].tcoord );
tri[i].normal = 0;
break;
case VERTEX_NORMAL:
sscanf( face_tokens[i].c_str(),
scan_vertex_normal,
&tri[i].vertex,
&tri[i].normal );
tri[i].tcoord = 0;
break;
case VERTEX_UV_NORMAL:
sscanf( face_tokens[i].c_str(),
scan_vertex_uv_normal,
&tri[i].vertex,
&tri[i].tcoord,
&tri[i].normal );
break;
default:
std::cerr << "Syntax error, unrecongnized face format at line "
<< line_num << std::endl;
break;
}
}
for ( size_t i = 0; i < num_vertex; ++i ) {
tri[i].vertex--;
tri[i].normal--;
tri[i].tcoord--;
}
Face f1 = { { tri[0], tri[1], tri[2] } };
face_list.push_back( f1 );
if ( num_vertex == 4 ) {
Face f2 = { { tri[2], tri[3], tri[0] } };
face_list.push_back( f2 );
}
} else if ( token == " " ) {
} else {
//std::cerr << "Unknown token on line " << line_num << std::endl;
}
token.clear();
line.clear();
}
// build vertex list using map for shared vertices
triangles.reserve( face_list.size() );
vertices.reserve( face_list.size() * 2 );
// current vertex index, for creating new vertices
unsigned int vert_idx_counter = 0;
for ( size_t i = 0; i < face_list.size(); ++i ) {
const Face& face = face_list[i];
MeshTriangle tri;
for ( size_t j = 0; j < 3; ++j ) {
// two vertices are only actually the same one if the vertex,
// normal, and tcoord are all the same. use the map to check this.
std::pair< VertexMap::iterator, bool > rv = vertex_map.insert( std::make_pair( face.v[j], vert_idx_counter ) );
if ( rv.second ) {
MeshVertex v;
v.position = position_list[face.v[j].vertex];
vertices.push_back( v );
vert_idx_counter++;
}
tri.vertices[j] = rv.first->second;
}
triangles.push_back( tri );
}
std::cout << "Successfully loaded mesh '" << filename << "'.\n";
return true;
}
} /* _462 */