diff options
-rw-r--r-- | gfx/models/obj.h | 3 | ||||
-rw-r--r-- | gfx/models/obj.impl.cpp | 19 | ||||
-rw-r--r-- | test/test-obj.cpp | 10 |
3 files changed, 26 insertions, 6 deletions
diff --git a/gfx/models/obj.h b/gfx/models/obj.h index 2921e34..71733fe 100644 --- a/gfx/models/obj.h +++ b/gfx/models/obj.h @@ -12,6 +12,7 @@ #include <vector> class Mesh; +class Vertex; class ObjParser : yyFlexLexer { public: @@ -38,6 +39,8 @@ public: std::vector<Object> objects; glm::length_t axis {0}; + using NamedMeshData = std::pair<std::string, std::pair<std::vector<Vertex>, std::vector<unsigned int>>>; + [[nodiscard]] std::vector<NamedMeshData> createMeshData() const; using NamedMesh = std::pair<std::string, std::shared_ptr<const Mesh>>; [[nodiscard]] std::vector<NamedMesh> createMeshes() const; }; diff --git a/gfx/models/obj.impl.cpp b/gfx/models/obj.impl.cpp index 0c1303a..5ac15ac 100644 --- a/gfx/models/obj.impl.cpp +++ b/gfx/models/obj.impl.cpp @@ -1,6 +1,6 @@ #include "obj.h" #include <algorithm> -#include <gfx/models/mesh.h> +#include <gfx/models/mesh.h> // IWYU pragma: keep #include <gfx/models/vertex.hpp> #include <glm/glm.hpp> #include <iterator> @@ -12,8 +12,19 @@ std::vector<ObjParser::NamedMesh> ObjParser::createMeshes() const { std::vector<ObjParser::NamedMesh> out; + const auto data {createMeshData()}; + std::transform(data.begin(), data.end(), std::back_inserter(out), [](auto && obj) { + return std::make_pair(obj.first, std::make_shared<Mesh>(obj.second.first, obj.second.second)); + }); + return out; +} + +std::vector<ObjParser::NamedMeshData> +ObjParser::createMeshData() const +{ + std::vector<ObjParser::NamedMeshData> out; out.reserve(objects.size()); - for (const auto & obj : objects) { + std::transform(objects.begin(), objects.end(), std::back_inserter(out), [this](auto && obj) { std::vector<Vertex> overtices; std::vector<ObjParser::FaceElement> vertexOrder; std::vector<unsigned int> indices; @@ -36,7 +47,7 @@ ObjParser::createMeshes() const f(idx - 1); } } - out.emplace_back(obj.first, std::make_shared<Mesh>(overtices, indices)); - } + return std::make_pair(obj.first, std::make_pair(overtices, indices)); + }); return out; } diff --git a/test/test-obj.cpp b/test/test-obj.cpp index 0d53691..4adc961 100644 --- a/test/test-obj.cpp +++ b/test/test-obj.cpp @@ -3,6 +3,8 @@ #include <boost/test/unit_test.hpp> #include <gfx/models/obj.h> +#include <gfx/models/vertex.hpp> +#include <glm/glm.hpp> #include <memory> #include <utility> #include <vector> @@ -10,7 +12,6 @@ BOOST_AUTO_TEST_CASE(objparse) { ObjParser op {"/home/randomdan/dev/game/res/brush47.obj"}; - BOOST_REQUIRE_EQUAL(0, op.yylex()); BOOST_CHECK_EQUAL(48, op.vertices.size()); BOOST_CHECK_EQUAL(104, op.texCoords.size()); BOOST_CHECK_EQUAL(25, op.normals.size()); @@ -26,6 +27,11 @@ BOOST_AUTO_TEST_CASE(objparse) BOOST_AUTO_TEST_CASE(create_meshes) { ObjParser op {"/home/randomdan/dev/game/res/brush47.obj"}; - const auto ms = op.createMeshes(); + const auto ms = op.createMeshData(); BOOST_REQUIRE_EQUAL(3, ms.size()); + BOOST_REQUIRE_EQUAL("Body", ms.front().first); + BOOST_REQUIRE_EQUAL(76, ms.front().second.first.size()); + const auto & v = ms.front().second.first.front(); + BOOST_REQUIRE_CLOSE(1.345, v.pos.x, 1); + BOOST_REQUIRE_EQUAL(120, ms.front().second.second.size()); } |