From df2a078c51cee464905c6fb1d1c7c4aa7873f6a1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 22 Feb 2023 23:40:46 +0000 Subject: Implement loading asset, mesh and face definitions --- assetFactory/assetFactory.cpp | 11 ++++++- assetFactory/assetFactory.h | 18 +++++++++- assetFactory/faceController.cpp | 10 +++++- assetFactory/faceController.h | 14 ++++++-- assetFactory/factoryMesh.cpp | 7 ++++ assetFactory/factoryMesh.h | 6 +++- assetFactory/use.cpp | 5 +-- assetFactory/use.h | 2 +- res/brush47.xml | 24 ++++++++++++-- test/test-assetFactory.cpp | 73 +++++++++++++++++++++++------------------ 10 files changed, 126 insertions(+), 44 deletions(-) diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 470eacf..564ea28 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -27,5 +27,14 @@ bool AssetFactory::persist(Persistence::PersistenceStore & store) { using MapObjects = Persistence::MapByMember>; - return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); + using MapAssets = Persistence::MapByMember; + return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects) + && STORE_NAME_HELPER("asset", assets, MapAssets); +} + +bool +Asset::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(name) + && STORE_NAME_HELPER("mesh", meshes, Persistence::Appender); } diff --git a/assetFactory/assetFactory.h b/assetFactory/assetFactory.h index 5cf90dd..9d79827 100644 --- a/assetFactory/assetFactory.h +++ b/assetFactory/assetFactory.h @@ -1,17 +1,33 @@ #pragma once +#include "factoryMesh.h" #include "persistence.h" #include "shape.h" #include +#include + +class Asset : public Persistence::Persistable, public StdTypeDefs { +public: + std::string id; + std::string name; + + FactoryMesh::Collection meshes; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; +}; class AssetFactory : public Persistence::Persistable { public: - using Shapes = std::map>; + using Shapes = std::map>; + using Assets = std::map>; AssetFactory(); [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); Shapes shapes; + Assets assets; private: friend Persistence::SelectionPtrBase, true>; diff --git a/assetFactory/faceController.cpp b/assetFactory/faceController.cpp index 7ec7820..499f7e4 100644 --- a/assetFactory/faceController.cpp +++ b/assetFactory/faceController.cpp @@ -52,10 +52,18 @@ FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape:: } } for (const auto & [name, faceController] : faceControllers) { - faceController.apply(mesh, name, newFaces); + faceController->apply(mesh, name, newFaces); } faces.merge(std::move(newFaces)); } } } } + +bool +FaceController::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(colour) && STORE_MEMBER(type) && STORE_MEMBER(smooth) + && STORE_MEMBER(scale) && STORE_MEMBER(position) && STORE_MEMBER(rotation) + && STORE_NAME_HELPER("face", faceControllers, Persistence::MapByMember); +} diff --git a/assetFactory/faceController.h b/assetFactory/faceController.h index 9974caf..296210d 100644 --- a/assetFactory/faceController.h +++ b/assetFactory/faceController.h @@ -2,13 +2,14 @@ #include "modelFactoryMesh_fwd.h" #include "mutation.h" +#include "persistence.h" #include "shape.h" #include #include -class FaceController : public Mutation { +class FaceController : public Mutation, public Persistence::Persistable { public: - using FaceControllers = std::map; + using FaceControllers = std::map>; void apply(ModelFactoryMesh & mesh, const std::string & name, Shape::CreatedFaces & faces) const; @@ -17,4 +18,13 @@ public: std::string type; bool smooth {false}; FaceControllers faceControllers; + +private: + friend Persistence::SelectionPtrBase, false>; + bool persist(Persistence::PersistenceStore & store) override; + std::string + getId() const override + { + return {}; + }; }; diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index baa031d..0fb72ad 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -29,3 +29,10 @@ FactoryMesh::createMesh() const } return std::make_shared(vertices, vectorOfN(vertices.size())); } + +bool +FactoryMesh::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(size) + && STORE_NAME_HELPER("use", uses, Persistence::Appender); +} diff --git a/assetFactory/factoryMesh.h b/assetFactory/factoryMesh.h index f850893..4b6d3e5 100644 --- a/assetFactory/factoryMesh.h +++ b/assetFactory/factoryMesh.h @@ -4,11 +4,15 @@ #include "stdTypeDefs.hpp" #include "use.h" -class FactoryMesh : public StdTypeDefs { +class FactoryMesh : public Persistence::Persistable, public StdTypeDefs { public: Mesh::Ptr createMesh() const; std::string id; glm::vec3 size; Use::Collection uses; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; }; diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp index 3b574c3..1f28332 100644 --- a/assetFactory/use.cpp +++ b/assetFactory/use.cpp @@ -6,7 +6,7 @@ Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) cons { auto faces = type->createMesh(mesh, mutation * getMatrix()); for (const auto & [name, faceController] : faceControllers) { - faceController.apply(mesh, name, faces); + faceController->apply(mesh, name, faces); } return faces; } @@ -27,5 +27,6 @@ bool Use::persist(Persistence::PersistenceStore & store) { return STORE_TYPE && STORE_HELPER(type, Lookup) && STORE_MEMBER(position) && STORE_MEMBER(scale) - && STORE_MEMBER(rotation); + && STORE_MEMBER(rotation) && STORE_MEMBER(colour) + && STORE_NAME_HELPER("face", faceControllers, Persistence::MapByMember); } diff --git a/assetFactory/use.h b/assetFactory/use.h index 96f07f6..853af23 100644 --- a/assetFactory/use.h +++ b/assetFactory/use.h @@ -8,7 +8,7 @@ class Use : public StdTypeDefs, public Mutation, public Persistence::Persistable { public: - using FaceControllers = std::map; + using FaceControllers = std::map>; Shape::CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const; diff --git a/res/brush47.xml b/res/brush47.xml index 3c6705a..9cd654f 100644 --- a/res/brush47.xml +++ b/res/brush47.xml @@ -1,15 +1,33 @@ - + - + - + + + + + + + + + + + + + + + + + + + diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp index 64d6a62..fd7a41d 100644 --- a/test/test-assetFactory.cpp +++ b/test/test-assetFactory.cpp @@ -90,24 +90,24 @@ BOOST_AUTO_TEST_CASE(brush47) assetFactory.shapes.emplace(axel->id, axel); } { - auto bogey = std::make_shared("bogey"); + auto bogie = std::make_shared("bogie"); for (float y : {-2.f, 0.f, 2.f}) { - auto axel = bogey->uses.emplace_back(std::make_shared()); + auto axel = bogie->uses.emplace_back(std::make_shared()); axel->type = assetFactory.shapes.at("axel"); axel->position = {0, y, 0}; } - assetFactory.shapes.emplace(bogey->id, bogey); + assetFactory.shapes.emplace(bogie->id, bogie); } FactoryMesh::Collection factoryMeshes; { unsigned short b {0}; for (float y : {-6.f, 6.f}) { - auto bogey = factoryMeshes.emplace_back(std::make_shared()); - bogey->id = "bogey" + std::to_string(b); - auto bogeyUse = bogey->uses.emplace_back(std::make_shared()); - bogeyUse->type = assetFactory.shapes.at("bogey"); - bogeyUse->position = {0, y, 0}; - bogeyUse->rotation = {0, b * pi, 0}; + auto bogie = factoryMeshes.emplace_back(std::make_shared()); + bogie->id = "bogie" + std::to_string(b); + auto bogieUse = bogie->uses.emplace_back(std::make_shared()); + bogieUse->type = assetFactory.shapes.at("bogie"); + bogieUse->position = {0, y, 0}; + bogieUse->rotation = {0, b * pi, 0}; b++; } } @@ -115,23 +115,25 @@ BOOST_AUTO_TEST_CASE(brush47) auto body = factoryMeshes.emplace_back(std::make_shared()); body->id = "body"; body->size = {2.69f, 19.38f, 3.9f}; - { - auto bodyLower = body->uses.emplace_back(std::make_shared()); - bodyLower->type = assetFactory.shapes.at("cuboid"); - bodyLower->position = {0, 0, 1.2}; - bodyLower->scale = {2.69, 19.38, 1.5}; - bodyLower->colour = "#1111DD"; - bodyLower->faceControllers["bottom"].colour = "#2C3539"; - auto & bodyUpper = bodyLower->faceControllers["top"]; - bodyUpper.type = "extrude"; - bodyUpper.scale = {1, .95f, 1}; - bodyUpper.position = {0, 0, 1.0}; - auto & roof = bodyUpper.faceControllers["top"]; - roof.type = "extrude"; - roof.scale = {.6f, .9f, 0}; - roof.position = {0, 0, 0.2}; - roof.smooth = true; - } + auto bodyLower = body->uses.emplace_back(std::make_shared()); + bodyLower->type = assetFactory.shapes.at("cuboid"); + bodyLower->position = {0, 0, 1.2}; + bodyLower->scale = {2.69, 19.38, 1.5}; + bodyLower->colour = "#1111DD"; + auto & bottom = bodyLower->faceControllers["bottom"]; + bottom = std::make_unique(); + bottom->colour = "#2C3539"; + auto & bodyUpper = bodyLower->faceControllers["top"]; + bodyUpper = std::make_unique(); + bodyUpper->type = "extrude"; + bodyUpper->scale = {1, .95f, 1}; + bodyUpper->position = {0, 0, 1.0}; + auto & roof = bodyUpper->faceControllers["top"]; + roof = std::make_unique(); + roof->type = "extrude"; + roof->scale = {.6f, .9f, 0}; + roof->position = {0, 0, 0.2}; + roof->smooth = true; { auto batteryBox = body->uses.emplace_back(std::make_shared()); batteryBox->type = assetFactory.shapes.at("cuboid"); @@ -157,13 +159,20 @@ BOOST_AUTO_TEST_CASE(brush47xml) BOOST_CHECK(mf->shapes.at("cuboid")); BOOST_CHECK(mf->shapes.at("wheel")); BOOST_CHECK(mf->shapes.at("axel")); - auto bogey = mf->shapes.at("bogey"); - BOOST_REQUIRE(bogey); - auto bogeyObj = std::dynamic_pointer_cast(bogey); - BOOST_CHECK_EQUAL(3, bogeyObj->uses.size()); + auto bogie = mf->shapes.at("bogie"); + BOOST_REQUIRE(bogie); + auto bogieObj = std::dynamic_pointer_cast(bogie); + BOOST_CHECK_EQUAL(3, bogieObj->uses.size()); + BOOST_CHECK_EQUAL(1, mf->assets.size()); + auto brush47 = mf->assets.at("brush-47"); + BOOST_REQUIRE(brush47); + BOOST_CHECK_EQUAL(3, brush47->meshes.size()); + auto body = brush47->meshes.at(0); + BOOST_REQUIRE(body); + BOOST_CHECK_EQUAL("body", body->id); + BOOST_CHECK_EQUAL(2, body->uses.size()); - FactoryMesh::Collection factoryMeshes; - std::transform(factoryMeshes.begin(), factoryMeshes.end(), std::back_inserter(meshes.objects), + std::transform(brush47->meshes.begin(), brush47->meshes.end(), std::back_inserter(meshes.objects), [](const FactoryMesh::CPtr & factoryMesh) -> Mesh::Ptr { return factoryMesh->createMesh(); }); -- cgit v1.2.3