diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-02-22 23:40:46 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-02-22 23:40:46 +0000 |
commit | df2a078c51cee464905c6fb1d1c7c4aa7873f6a1 (patch) | |
tree | 209467479c61ab42bf39510f32fd375027115dcd | |
parent | Fixup MapByMember to work with shared or unique ptr (diff) | |
download | ilt-df2a078c51cee464905c6fb1d1c7c4aa7873f6a1.tar.bz2 ilt-df2a078c51cee464905c6fb1d1c7c4aa7873f6a1.tar.xz ilt-df2a078c51cee464905c6fb1d1c7c4aa7873f6a1.zip |
Implement loading asset, mesh and face definitions
-rw-r--r-- | assetFactory/assetFactory.cpp | 11 | ||||
-rw-r--r-- | assetFactory/assetFactory.h | 18 | ||||
-rw-r--r-- | assetFactory/faceController.cpp | 10 | ||||
-rw-r--r-- | assetFactory/faceController.h | 14 | ||||
-rw-r--r-- | assetFactory/factoryMesh.cpp | 7 | ||||
-rw-r--r-- | assetFactory/factoryMesh.h | 6 | ||||
-rw-r--r-- | assetFactory/use.cpp | 5 | ||||
-rw-r--r-- | assetFactory/use.h | 2 | ||||
-rw-r--r-- | res/brush47.xml | 24 | ||||
-rw-r--r-- | 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<Shapes, std::shared_ptr<Object>>; - return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); + using MapAssets = Persistence::MapByMember<Assets>; + 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<FactoryMesh::Collection>); } 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 <filesystem> +#include <stdTypeDefs.hpp> + +class Asset : public Persistence::Persistable, public StdTypeDefs<Asset> { +public: + std::string id; + std::string name; + + FactoryMesh::Collection meshes; + +private: + friend Persistence::SelectionPtrBase<std::shared_ptr<Asset>, true>; + bool persist(Persistence::PersistenceStore & store) override; +}; class AssetFactory : public Persistence::Persistable { public: - using Shapes = std::map<std::string, Shape::CPtr, std::less<>>; + using Shapes = std::map<std::string, Shape::Ptr, std::less<>>; + using Assets = std::map<std::string, Asset::Ptr, std::less<>>; AssetFactory(); [[nodiscard]] static std::shared_ptr<AssetFactory> loadXML(const std::filesystem::path &); Shapes shapes; + Assets assets; private: friend Persistence::SelectionPtrBase<std::shared_ptr<AssetFactory>, 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<FaceControllers>); +} 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 <map> #include <string> -class FaceController : public Mutation { +class FaceController : public Mutation, public Persistence::Persistable { public: - using FaceControllers = std::map<std::string, FaceController>; + using FaceControllers = std::map<std::string, std::unique_ptr<FaceController>>; 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<std::unique_ptr<FaceController>, 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<Mesh>(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<Use::Collection>); +} 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<FactoryMesh> { +class FactoryMesh : public Persistence::Persistable, public StdTypeDefs<FactoryMesh> { public: Mesh::Ptr createMesh() const; std::string id; glm::vec3 size; Use::Collection uses; + +private: + friend Persistence::SelectionPtrBase<std::shared_ptr<FactoryMesh>, 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<FaceControllers>); } 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<Use>, public Mutation, public Persistence::Persistable { public: - using FaceControllers = std::map<std::string, FaceController>; + using FaceControllers = std::map<std::string, std::unique_ptr<FaceController>>; 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 @@ <?xml version="1.0"?> <ilt p.id="assetFactory"> <object id="wheel"> - <use type="cylinder" position="0,0,0.571" scale="1.142,1.142,0.07" rotation="0,0,90"/> + <use type="cylinder" position="0,0,0.571" scale="1.142,1.142,0.07" rotation="0,0,1.5708"/> </object> <object id="axel"> <use type="wheel" position="0.717,0,0"/> - <use type="wheel" position="-0.717,0,0" rotation="0,90,0"/> + <use type="wheel" position="-0.717,0,0" rotation="0,3.14159,0"/> </object> - <object id="bogey"> + <object id="bogie"> <use type="axel" position="0,0,0"/> <use type="axel" position="0,2,0"/> <use type="axel" position="0,-2,0"/> </object> + <asset id="brush-47" name="Brush 47"> + <mesh id="body" size="2.69,19.38,3.9"> + <use type="cuboid" position="0,0,1.2" scale="2.69,19.38,1.5" colour="#1111dd"> + <face id="bottom" colour="#2C3539"/> + <face id="top" type="extrude" scale="1,0.95,1" position="0,0,1.0"> + <face id="top" type="extrude" scale="0.6,0.9,0" position="0,0,0.2"/> + <!--smooth="true"--> + </face> + </use> + <use type="cuboid" position="0,0,0.2" scale="2.6,4.5,1" colour="#2C3539"/> + </mesh> + <mesh id="bogie1"> + <use type="bogie" position="0,6,0"/> + </mesh> + <mesh id="bogie2"> + <use type="bogie" position="0,-6,0" rotation="0,3.14159,0"/> + </mesh> + </asset> </ilt> 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<Object>("bogey"); + auto bogie = std::make_shared<Object>("bogie"); for (float y : {-2.f, 0.f, 2.f}) { - auto axel = bogey->uses.emplace_back(std::make_shared<Use>()); + auto axel = bogie->uses.emplace_back(std::make_shared<Use>()); 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<FactoryMesh>()); - bogey->id = "bogey" + std::to_string(b); - auto bogeyUse = bogey->uses.emplace_back(std::make_shared<Use>()); - 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<FactoryMesh>()); + bogie->id = "bogie" + std::to_string(b); + auto bogieUse = bogie->uses.emplace_back(std::make_shared<Use>()); + 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<FactoryMesh>()); body->id = "body"; body->size = {2.69f, 19.38f, 3.9f}; - { - auto bodyLower = body->uses.emplace_back(std::make_shared<Use>()); - 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<Use>()); + 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<FaceController>(); + bottom->colour = "#2C3539"; + auto & bodyUpper = bodyLower->faceControllers["top"]; + bodyUpper = std::make_unique<FaceController>(); + bodyUpper->type = "extrude"; + bodyUpper->scale = {1, .95f, 1}; + bodyUpper->position = {0, 0, 1.0}; + auto & roof = bodyUpper->faceControllers["top"]; + roof = std::make_unique<FaceController>(); + 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<Use>()); 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<const Object>(bogey); - BOOST_CHECK_EQUAL(3, bogeyObj->uses.size()); + auto bogie = mf->shapes.at("bogie"); + BOOST_REQUIRE(bogie); + auto bogieObj = std::dynamic_pointer_cast<const Object>(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(); }); |