diff options
author | github-classroom[bot] <66690702+github-classroom[bot]@users.noreply.github.com> | 2024-03-19 02:01:17 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-19 02:01:17 +0000 |
commit | 0f8d0e3cfdbd9b11b2357ed3e1a11375e7af8e80 (patch) | |
tree | 48b88b3b3b3a522a90c38b2178363a163a32f2ee /src/graphics/meshloader.cpp |
Initial commit
Diffstat (limited to 'src/graphics/meshloader.cpp')
-rw-r--r-- | src/graphics/meshloader.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/graphics/meshloader.cpp b/src/graphics/meshloader.cpp new file mode 100644 index 0000000..fc95f6f --- /dev/null +++ b/src/graphics/meshloader.cpp @@ -0,0 +1,65 @@ +#include "graphics/meshloader.h" + +#define TINYOBJLOADER_IMPLEMENTATION +#include "util/tiny_obj_loader.h" + +#include <iostream> + +#include <QString> +#include <QFile> +#include <QTextStream> +#include <QRegularExpression> +#include <QFileInfo> +#include <iostream> +#include <set> + +using namespace std; +using namespace Eigen; + +MeshLoader::MeshLoader() {} + +bool MeshLoader::loadTriMesh(const string &filePath, vector<Vector3f> &vertices, vector<Vector3i> &faces) +{ + tinyobj::attrib_t attrib; + vector<tinyobj::shape_t> shapes; + vector<tinyobj::material_t> materials; + + QFileInfo info(QString(filePath.c_str())); + string err; + bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, + info.absoluteFilePath().toStdString().c_str(), (info.absolutePath().toStdString() + "/").c_str(), true); + if (!err.empty()) { + cerr << err << endl; + } + + if (!ret) { + cerr << "Failed to load/parse .obj file" << endl; + return false; + } + + for (size_t s = 0; s < shapes.size(); s++) { + size_t index_offset = 0; + for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { + unsigned int fv = shapes[s].mesh.num_face_vertices[f]; + + Vector3i face; + for (size_t v = 0; v < fv; v++) { + tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v]; + + face[v] = idx.vertex_index; + + } + faces.push_back(face); + + index_offset += fv; + } + } + + for (size_t i = 0; i < attrib.vertices.size(); i += 3) { + vertices.emplace_back(attrib.vertices[i], attrib.vertices[i + 1], attrib.vertices[i + 2]); + } + + cout << "Loaded " << faces.size() << " faces and " << vertices.size() << " vertices" << endl; + + return true; +} |