Skip to content

Commit e2fb3b6

Browse files
Use tinyobj to load files
1 parent af3cd70 commit e2fb3b6

File tree

3 files changed

+3147
-35
lines changed

3 files changed

+3147
-35
lines changed

AOEmbree.cpp

Lines changed: 86 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
#include "AOEmbree.h"
77

8+
#define TINYOBJLOADER_IMPLEMENTATION
9+
#include "tiny_obj_loader.h"
10+
11+
# define M_PI 3.14159265358979323846
812

913
void errorFunction(void* userPtr, enum RTCError error, const char* str)
1014
{
@@ -204,7 +208,7 @@ void computeAOPerVert(float *verts, float *norms, int *tris, float *result,
204208
float x = radius * cos(theta);
205209
float y = radius * sin(theta);
206210
if (y > 0.0f)//Only keep the upper half of the sphere
207-
rayDir.push_back(vec3(x, y, z));
211+
rayDir.push_back(normalize(vec3(x, y, z)));
208212
}
209213

210214
float step = 1.0f / samplesAO;
@@ -220,10 +224,7 @@ void computeAOPerVert(float *verts, float *norms, int *tris, float *result,
220224
quat q = glm::rotation(oriVec, normal);
221225

222226
for (int s = 0; s < rayDir.size(); s++) {
223-
vec3 dir (
224-
rayDir[s].x + vertices[i * 3],
225-
rayDir[s].y + vertices[i * 3 + 1],
226-
rayDir[s].z + vertices[i * 3 + 2]);
227+
vec3 dir = rayDir[s];
227228

228229

229230
quat dirq = quat(dir.x, dir.y, dir.z, 0.0f);
@@ -257,44 +258,94 @@ int main(int argc, char **argv) {
257258
int samplesAO = 128;
258259
float maxDist = 10.0f;
259260

260-
std::ifstream infile(argv[1]);
261+
tinyobj::attrib_t attrib;
262+
std::vector<tinyobj::shape_t> shapes;
263+
std::vector<tinyobj::material_t> materials;
264+
265+
std::string warn;
266+
std::string err;
267+
268+
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, argv[1]);
269+
270+
if (!warn.empty()) {
271+
std::cout << warn << std::endl;
272+
}
273+
274+
if (!err.empty()) {
275+
std::cerr << err << std::endl;
276+
}
261277

262278
std::vector<float> verts;
263279
std::vector<float> norms;
264280
std::vector<int> ids;
265281

266-
std::string line;
267-
while (std::getline(infile, line))
268-
{
282+
verts = attrib.vertices;
283+
norms = attrib.normals;
284+
for (int f = 0; f < shapes[0].mesh.num_face_vertices.size(); f++) {
285+
size_t index_offset = 0;
286+
int fv = shapes[0].mesh.num_face_vertices[f];
269287

270-
if (line.size() > 0 && line[0] == 'v' && line[1] == ' ') {
271-
std::vector<std::string> words;
272-
split1(line.c_str(), words);
273-
float x = std::stof(words[1]);
274-
float y = std::stof(words[2]);
275-
float z = std::stof(words[3]);
276-
verts.push_back(x);
277-
verts.push_back(y);
278-
verts.push_back(z);
279-
}
280-
else if (line.size() > 0 && line[0] == 'f') {
281-
std::vector<std::string> words;
282-
split1(line.c_str(), words);
283-
ids.push_back(std::stoi(words[1]) - 1);
284-
ids.push_back(std::stoi(words[2]) - 1);
285-
ids.push_back(std::stoi(words[3]) - 1);
286-
}
287-
if (line.size() > 0 && line[0] == 'v' && line[1] == 'n') {
288-
std::vector<std::string> words;
289-
split1(line.c_str(), words);
290-
float x = std::stof(words[1]);
291-
float y = std::stof(words[2]);
292-
float z = std::stof(words[3]);
293-
norms.push_back(x);
294-
norms.push_back(y);
295-
norms.push_back(z);
288+
for (size_t v = 0; v < fv; v++) {
289+
tinyobj::index_t idx = shapes[0].mesh.indices[index_offset + v];
290+
ids.push_back(idx.vertex_index-1);
296291
}
297292
}
293+
294+
295+
//std::ifstream infile(argv[1]);
296+
297+
298+
299+
//std::string line;
300+
//while (std::getline(infile, line))
301+
//{
302+
303+
// if (line.size() > 0 && line[0] == 'v' && line[1] == ' ') {
304+
// std::vector<std::string> words;
305+
// split1(line.c_str(), words);
306+
// float x = std::stof(words[1]);
307+
// float y = std::stof(words[2]);
308+
// float z = std::stof(words[3]);
309+
// verts.push_back(x);
310+
// verts.push_back(y);
311+
// verts.push_back(z);
312+
// }
313+
// else if (line.size() > 0 && line[0] == 'f') {
314+
// std::vector<std::string> words;
315+
// split1(line.c_str(), words);
316+
// if (words[0].find("/") != std::string::npos) {
317+
// int a, b, c;
318+
// sscanf(words[0].c_str(), "%d/%d/%d",&a,&b,&c);
319+
// ids.push_back(a - 1);
320+
321+
322+
// sscanf(words[1].c_str(), "%d/%d/%d", &a, &b, &c);
323+
// ids.push_back(a - 1);
324+
325+
326+
// sscanf(words[2].c_str(), "%d/%d/%d", &a, &b, &c);
327+
// ids.push_back(a - 1);
328+
329+
330+
//
331+
// }
332+
// else {
333+
// ids.push_back(std::stoi(words[1]) - 1);
334+
// ids.push_back(std::stoi(words[2]) - 1);
335+
// ids.push_back(std::stoi(words[3]) - 1);
336+
// }
337+
// }
338+
// if (line.size() > 0 && line[0] == 'v' && line[1] == 'n') {
339+
// std::vector<std::string> words;
340+
// split1(line.c_str(), words);
341+
// float x = std::stof(words[1]);
342+
// float y = std::stof(words[2]);
343+
// float z = std::stof(words[3]);
344+
// norms.push_back(x);
345+
// norms.push_back(y);
346+
// norms.push_back(z);
347+
// }
348+
//}
298349
float *result = new float[verts.size() / 3];
299350
computeAOPerVert(verts.data(), norms.data(), ids.data(), result,
300351
verts.size() / 3, ids.size() / 3,

AOEmbree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <fstream>
55
#include <sstream>
66
#include <string>
7+
#include <iterator>
78

89
#include <iostream>
910

0 commit comments

Comments
 (0)