-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobjloader.cpp
More file actions
255 lines (212 loc) · 6.87 KB
/
objloader.cpp
File metadata and controls
255 lines (212 loc) · 6.87 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
#include "objloader.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstring>
#include <assert.h>
#include <map>
using namespace std;
using namespace sp;
float& Vec3::operator[](int idx)
{
assert(idx > 0);
assert(idx < 3);
return *(&x + idx);
}
float& TexCoord::operator[](int idx)
{
assert(idx > 0);
assert(idx < 3);
return *(&u + idx);
}
struct FaceVert
{
FaceVert() : vert(-1), norm(-1), coord(-1) {}
int vert;
int norm;
int coord;
};
struct vert_less {
bool operator() (const FaceVert& lhs, const FaceVert& rhs) const
{
// handle any size mesh
if (lhs.vert != rhs.vert) return (lhs.vert<rhs.vert);
if (lhs.norm != rhs.norm) return (lhs.norm<rhs.norm);
if (lhs.coord!=rhs.coord) return (lhs.coord<rhs.coord);
return false;
// the following breaks down on very large meshes
// const unsigned long prime1 = 73856093;
// const unsigned long prime2 = 19349663;
// const unsigned long prime3 = 83492791;
// unsigned long lh = (lhs.vert * prime1) ^ (lhs.norm * prime2) ^ (lhs.coord * prime3);
// unsigned long rh = (rhs.vert * prime1) ^ (rhs.norm * prime2) ^ (rhs.coord * prime3);
// return lh < rh;
}
};
ObjLoader::ObjLoader()
: TexCoordLayers(1)
{
}
void ObjLoader::load(char* filename)
{
ifstream inf;
inf.open(filename, ios_base::in);
if (!inf.is_open()) {
cerr << "[!] Failed to load file: " << filename << endl;
}
Positions.clear();
Normals.clear();
TexCoords.clear();
Faces.clear();
char *delims = " \n\r";
const unsigned int CHARACTER_COUNT = 500;
char line[CHARACTER_COUNT] = {0};
std::vector<Vec3> verts;
std::vector<Vec3> norms;
std::vector<TexCoord> texcoords;
std::map<FaceVert, int, vert_less> uniqueverts;
unsigned int vert_count = 0;
while (inf.good()) {
memset( (void*)line, 0, CHARACTER_COUNT);
inf.getline(line, CHARACTER_COUNT);
if (inf.eof()) break;
char *token = strtok(line, delims);
if (token == NULL || token[0] == '#' || token[0] == '$')
continue;
// verts look like:
// v float float float
if (strcmp(token, "v") == 0) {
float x=0, y=0, z=0, w=1;
sscanf(line+2, "%f %f %f %f", &x, &y, &z, &w);
verts.push_back( Vec3(x/w,y/w,z/w) );
}
// normals:
// nv float float float
else if (strcmp(token, "vn") == 0) {
float x=0, y=0, z=0;
sscanf(line+3, "%f %f %f", &x, &y, &z);
norms.push_back( Vec3(x,y,z) );
}
// texcoords:
// vt float float
else if (strcmp(token, "vt") == 0) {
float x=0, y=0, z=0;
sscanf(line+3, "%f %f %f", &x, &y, &z);
texcoords.push_back( TexCoord(x, y) );
}
// keep track of smoothing groups
// s [number|off]
else if (strcmp(token, "s") == 0) {
}
// faces start with:
// f
else if (strcmp(token, "f") == 0) {
std::vector<int> vindices;
std::vector<int> nindices;
std::vector<int> tindices;
// fill out a triangle from the line, it could have 3 or 4 edges
char *lineptr = line + 2;
while (lineptr[0] != 0) {
while (lineptr[0] == ' ') ++lineptr;
int vi=0, ni=0, ti=0;
if (sscanf(lineptr, "%d/%d/%d", &vi, &ni, &ti) == 3) {
vindices.push_back(vi-1);
nindices.push_back(ni-1);
tindices.push_back(ti-1);
}
else
if (sscanf(lineptr, "%d//%d", &vi, &ni) == 2) {
vindices.push_back(vi-1);
nindices.push_back(ni-1);
}
else
if (sscanf(lineptr, "%d/%d", &vi, &ti) == 2) {
vindices.push_back(vi-1);
tindices.push_back(ti-1);
}
else
if (sscanf(lineptr, "%d", &vi) == 1) {
vindices.push_back(vi-1);
}
while(lineptr[0] != ' ' && lineptr[0] != 0) ++lineptr;
}
// being that some exporters can export either 3 or 4 sided polygon's
// convert what ever was exported into triangles
for (size_t i=1; i<vindices.size()-1; ++i) {
Face face;
FaceVert tri;
tri.vert = vindices[0];
if (!nindices.empty())
tri.norm = nindices[0];
if (!tindices.empty())
tri.norm = tindices[0];
if (uniqueverts.count(tri) == 0)
uniqueverts[tri] = vert_count++;
face.a = uniqueverts[tri];
tri.vert = vindices[i];
if (!nindices.empty())
tri.norm = nindices[i];
if (!tindices.empty())
tri.norm = tindices[i];
if (uniqueverts.count(tri) == 0)
uniqueverts[tri] = vert_count++;
face.b = uniqueverts[tri];
tri.vert = vindices[i+1];
if (!nindices.empty())
tri.norm = nindices[i+1];
if (!tindices.empty())
tri.norm = tindices[i+1];
if (uniqueverts.count(tri) == 0)
uniqueverts[tri] = vert_count++;
face.c = uniqueverts[tri];
Faces.push_back(face);
}
}
}
inf.close();
// use resize instead of reserve because we'll be indexing in random locations.
Positions.resize(vert_count);
if (norms.size() > 0)
Normals.resize(vert_count);
if (texcoords.size() > 0)
TexCoords.resize(vert_count);
std::map<FaceVert, int, vert_less>::iterator iter;
for (iter = uniqueverts.begin(); iter != uniqueverts.end(); ++iter) {
Positions[iter->second] = verts[iter->first.vert];
if ( norms.size() > 0 ) {
Normals[iter->second] = norms[iter->first.norm];
}
if ( texcoords.size() > 0) {
TexCoords[iter->second] = texcoords[iter->first.coord];
}
}
}
int ObjLoader::getIndexCount()
{
return (int)Faces.size() * 3;
}
int ObjLoader::getVertCount()
{
return (int)Positions.size();
}
const unsigned int* ObjLoader::getFaces()
{
return (const unsigned int*)&Faces[0];
}
const float* ObjLoader::getPositions()
{
return (const float*)&Positions[0];
}
const float* ObjLoader::getNormals()
{
return (const float*)&Normals[0];
}
int ObjLoader::getTexCoordLayers()
{
return TexCoordLayers;
}
const float* ObjLoader::getTexCoords(int multiTexCoordLayer)
{
assert(multiTexCoordLayer < TexCoordLayers);
return (const float*)&TexCoords[0];
}