From 5e561d390dc82b08c20532de0952f428e7b14283 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 21 Feb 2023 19:34:16 +0000 Subject: Rename ModelFactory to AssetFactory --- assetFactory/assetFactory.cpp | 31 ++++++++ assetFactory/assetFactory.h | 19 +++++ assetFactory/modelFactory.cpp | 31 -------- assetFactory/modelFactory.h | 19 ----- assetFactory/use.cpp | 4 +- res/brush47.xml | 2 +- test/Jamfile.jam | 2 +- test/test-assetFactory.cpp | 173 ++++++++++++++++++++++++++++++++++++++++++ test/test-modelFactory.cpp | 173 ------------------------------------------ 9 files changed, 227 insertions(+), 227 deletions(-) create mode 100644 assetFactory/assetFactory.cpp create mode 100644 assetFactory/assetFactory.h delete mode 100644 assetFactory/modelFactory.cpp delete mode 100644 assetFactory/modelFactory.h create mode 100644 test/test-assetFactory.cpp delete mode 100644 test/test-modelFactory.cpp diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp new file mode 100644 index 0000000..0ee1f94 --- /dev/null +++ b/assetFactory/assetFactory.cpp @@ -0,0 +1,31 @@ +#include "assetFactory.h" +#include "cuboid.h" +#include "cylinder.h" +#include "modelFactoryMesh_fwd.h" +#include "object.h" +#include "plane.h" +#include "saxParse-persistence.h" +#include + +AssetFactory::AssetFactory() : + shapes { + {"plane", std::make_shared()}, + {"cuboid", std::make_shared()}, + {"cylinder", std::make_shared()}, + } +{ +} + +std::shared_ptr +AssetFactory::loadXML(const std::filesystem::path & filename) +{ + filesystem::FileStar file {filename.c_str(), "r"}; + return Persistence::SAXParsePersistence {}.loadState>(file); +} + +bool +AssetFactory::persist(Persistence::PersistenceStore & store) +{ + using MapObjects = Persistence::MapByMember; + return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); +} diff --git a/assetFactory/assetFactory.h b/assetFactory/assetFactory.h new file mode 100644 index 0000000..5cf90dd --- /dev/null +++ b/assetFactory/assetFactory.h @@ -0,0 +1,19 @@ +#pragma once + +#include "persistence.h" +#include "shape.h" +#include + +class AssetFactory : public Persistence::Persistable { +public: + using Shapes = std::map>; + + AssetFactory(); + [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); + + Shapes shapes; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; +}; diff --git a/assetFactory/modelFactory.cpp b/assetFactory/modelFactory.cpp deleted file mode 100644 index 2642900..0000000 --- a/assetFactory/modelFactory.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "modelFactory.h" -#include "cuboid.h" -#include "cylinder.h" -#include "modelFactoryMesh_fwd.h" -#include "object.h" -#include "plane.h" -#include "saxParse-persistence.h" -#include - -ModelFactory::ModelFactory() : - shapes { - {"plane", std::make_shared()}, - {"cuboid", std::make_shared()}, - {"cylinder", std::make_shared()}, - } -{ -} - -std::shared_ptr -ModelFactory::loadXML(const std::filesystem::path & filename) -{ - filesystem::FileStar file {filename.c_str(), "r"}; - return Persistence::SAXParsePersistence {}.loadState>(file); -} - -bool -ModelFactory::persist(Persistence::PersistenceStore & store) -{ - using MapObjects = Persistence::MapByMember; - return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); -} diff --git a/assetFactory/modelFactory.h b/assetFactory/modelFactory.h deleted file mode 100644 index 94db055..0000000 --- a/assetFactory/modelFactory.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "persistence.h" -#include "shape.h" -#include - -class ModelFactory : public Persistence::Persistable { -public: - using Shapes = std::map>; - - ModelFactory(); - [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); - - Shapes shapes; - -private: - friend Persistence::SelectionPtrBase, true>; - bool persist(Persistence::PersistenceStore & store) override; -}; diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp index 21e26f3..3b574c3 100644 --- a/assetFactory/use.cpp +++ b/assetFactory/use.cpp @@ -1,5 +1,5 @@ #include "use.h" -#include "modelFactory.h" +#include "assetFactory.h" Shape::CreatedFaces Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const @@ -17,7 +17,7 @@ struct Lookup : public Persistence::SelectionV { void setValue(std::string && str) override { - if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("modelFactory"))) { + if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { v = mf->shapes.at(str); } } diff --git a/res/brush47.xml b/res/brush47.xml index 8f1d934..3c6705a 100644 --- a/res/brush47.xml +++ b/res/brush47.xml @@ -1,5 +1,5 @@ - + diff --git a/test/Jamfile.jam b/test/Jamfile.jam index 027b880..06f907a 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -52,6 +52,6 @@ run test-text.cpp ; run test-enumDetails.cpp ; run test-render.cpp : : : test ; run test-glContextBhvr.cpp ; -run test-modelFactory.cpp : -- : ../res/brush47.xml : test ; +run test-assetFactory.cpp : -- : ../res/brush47.xml : test ; compile test-static-enumDetails.cpp ; compile test-static-stream_support.cpp ; diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp new file mode 100644 index 0000000..64d6a62 --- /dev/null +++ b/test/test-assetFactory.cpp @@ -0,0 +1,173 @@ +#define BOOST_TEST_MODULE test_asset_factory + +#include "testHelpers.h" +#include "testRenderOutput.h" +#include +#include + +#include "assetFactory/assetFactory.h" +#include "assetFactory/factoryMesh.h" +#include "assetFactory/object.h" +#include "gfx/gl/sceneRenderer.h" +#include "lib/collection.hpp" +#include "lib/location.hpp" +#include "testMainWindow.h" +#include "ui/applicationBase.h" + +BOOST_GLOBAL_FIXTURE(ApplicationBase); +BOOST_GLOBAL_FIXTURE(TestMainWindow); + +const std::filesystem::path TMP {"/tmp"}; +class FactoryFixture : public TestRenderOutputSize, public SceneProvider { +public: + FactoryFixture() : sceneRenderer {size, output} { } + ~FactoryFixture() + { + glDisable(GL_DEBUG_OUTPUT); + auto outpath = (TMP / boost::unit_test::framework::current_test_case().full_name()).replace_extension(".tga"); + std::filesystem::create_directories(outpath.parent_path()); + Texture::save(outImage, size, outpath.c_str()); + } + void + content(const SceneShader & shader) const override + { + shader.basic.use(Location {{0, 0, 0}, {0, 0, 0}}); + meshes.apply(&Mesh::Draw); + } + void + lights(const SceneShader & shader) const override + { + shader.pointLight.add({-3, 1, 5}, {1, 1, 1}, .1F); + } + void + environment(const SceneShader &, const SceneRenderer & sceneRenderer) const override + { + sceneRenderer.setAmbientLight({.2, .2, .2}); + sceneRenderer.setDirectionalLight({.3, .3, .3}, east + south + south + down, *this); + } + void + shadows(const ShadowMapper & mapper) const override + { + mapper.dynamicPoint.use(Location {{0, 0, 0}, {0, 0, 0}}); + meshes.apply(&Mesh::Draw); + } + void + render(float dist = 10.f) + { + sceneRenderer.camera.setView({dist, dist, dist}, south + west + down); + sceneRenderer.render(*this); + } + Collection meshes; + +private: + SceneRenderer sceneRenderer; +}; + +BOOST_FIXTURE_TEST_SUITE(m, FactoryFixture); +BOOST_AUTO_TEST_CASE(brush47) +{ + AssetFactory assetFactory; + { + auto wheel = std::make_shared("wheel"); + { + auto wheelCylinder = wheel->uses.emplace_back(std::make_shared()); + wheelCylinder->type = assetFactory.shapes.at("cylinder"); + wheelCylinder->position = {0, 0, 0.571}; + wheelCylinder->scale = {1.142, 1.142, 0.07}; + wheelCylinder->rotation = {0, 0, half_pi}; + wheelCylinder->colour = "#2C3539"; + } + assetFactory.shapes.emplace(wheel->id, wheel); + } + { + auto axel = std::make_shared("axel"); + for (float x : {-1.f, 1.f}) { + auto wheel = axel->uses.emplace_back(std::make_shared()); + wheel->type = assetFactory.shapes.at("wheel"); + wheel->position = {x * 0.717f, 0, 0}; + wheel->rotation = {0, x == 1.f ? pi : 0.f, 0}; + } + assetFactory.shapes.emplace(axel->id, axel); + } + { + auto bogey = std::make_shared("bogey"); + for (float y : {-2.f, 0.f, 2.f}) { + auto axel = bogey->uses.emplace_back(std::make_shared()); + axel->type = assetFactory.shapes.at("axel"); + axel->position = {0, y, 0}; + } + assetFactory.shapes.emplace(bogey->id, bogey); + } + 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}; + b++; + } + } + { + 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 batteryBox = body->uses.emplace_back(std::make_shared()); + batteryBox->type = assetFactory.shapes.at("cuboid"); + batteryBox->position = {0, 0, .2}; + batteryBox->scale = {2.6, 4.5, 1}; + batteryBox->colour = "#2C3539"; + } + } + std::transform(factoryMeshes.begin(), factoryMeshes.end(), std::back_inserter(meshes.objects), + [](const FactoryMesh::CPtr & factoryMesh) -> Mesh::Ptr { + return factoryMesh->createMesh(); + }); + + render(20); +} +BOOST_AUTO_TEST_CASE(brush47xml) +{ + auto mf = AssetFactory::loadXML(RESDIR "/brush47.xml"); + BOOST_REQUIRE(mf); + BOOST_REQUIRE_EQUAL(6, mf->shapes.size()); + BOOST_CHECK(mf->shapes.at("plane")); + BOOST_CHECK(mf->shapes.at("cylinder")); + 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()); + + FactoryMesh::Collection factoryMeshes; + std::transform(factoryMeshes.begin(), factoryMeshes.end(), std::back_inserter(meshes.objects), + [](const FactoryMesh::CPtr & factoryMesh) -> Mesh::Ptr { + return factoryMesh->createMesh(); + }); + + render(20); +} +BOOST_AUTO_TEST_SUITE_END(); diff --git a/test/test-modelFactory.cpp b/test/test-modelFactory.cpp deleted file mode 100644 index 8f2d56c..0000000 --- a/test/test-modelFactory.cpp +++ /dev/null @@ -1,173 +0,0 @@ -#define BOOST_TEST_MODULE test_model_factory - -#include "testHelpers.h" -#include "testRenderOutput.h" -#include -#include - -#include "assetFactory/factoryMesh.h" -#include "assetFactory/modelFactory.h" -#include "assetFactory/object.h" -#include "gfx/gl/sceneRenderer.h" -#include "lib/collection.hpp" -#include "lib/location.hpp" -#include "testMainWindow.h" -#include "ui/applicationBase.h" - -BOOST_GLOBAL_FIXTURE(ApplicationBase); -BOOST_GLOBAL_FIXTURE(TestMainWindow); - -const std::filesystem::path TMP {"/tmp"}; -class FactoryFixture : public TestRenderOutputSize, public SceneProvider { -public: - FactoryFixture() : sceneRenderer {size, output} { } - ~FactoryFixture() - { - glDisable(GL_DEBUG_OUTPUT); - auto outpath = (TMP / boost::unit_test::framework::current_test_case().full_name()).replace_extension(".tga"); - std::filesystem::create_directories(outpath.parent_path()); - Texture::save(outImage, size, outpath.c_str()); - } - void - content(const SceneShader & shader) const override - { - shader.basic.use(Location {{0, 0, 0}, {0, 0, 0}}); - meshes.apply(&Mesh::Draw); - } - void - lights(const SceneShader & shader) const override - { - shader.pointLight.add({-3, 1, 5}, {1, 1, 1}, .1F); - } - void - environment(const SceneShader &, const SceneRenderer & sceneRenderer) const override - { - sceneRenderer.setAmbientLight({.2, .2, .2}); - sceneRenderer.setDirectionalLight({.3, .3, .3}, east + south + south + down, *this); - } - void - shadows(const ShadowMapper & mapper) const override - { - mapper.dynamicPoint.use(Location {{0, 0, 0}, {0, 0, 0}}); - meshes.apply(&Mesh::Draw); - } - void - render(float dist = 10.f) - { - sceneRenderer.camera.setView({dist, dist, dist}, south + west + down); - sceneRenderer.render(*this); - } - Collection meshes; - -private: - SceneRenderer sceneRenderer; -}; - -BOOST_FIXTURE_TEST_SUITE(m, FactoryFixture); -BOOST_AUTO_TEST_CASE(brush47) -{ - ModelFactory modelFactory; - { - auto wheel = std::make_shared("wheel"); - { - auto wheelCylinder = wheel->uses.emplace_back(std::make_shared()); - wheelCylinder->type = modelFactory.shapes.at("cylinder"); - wheelCylinder->position = {0, 0, 0.571}; - wheelCylinder->scale = {1.142, 1.142, 0.07}; - wheelCylinder->rotation = {0, 0, half_pi}; - wheelCylinder->colour = "#2C3539"; - } - modelFactory.shapes.emplace(wheel->id, wheel); - } - { - auto axel = std::make_shared("axel"); - for (float x : {-1.f, 1.f}) { - auto wheel = axel->uses.emplace_back(std::make_shared()); - wheel->type = modelFactory.shapes.at("wheel"); - wheel->position = {x * 0.717f, 0, 0}; - wheel->rotation = {0, x == 1.f ? pi : 0.f, 0}; - } - modelFactory.shapes.emplace(axel->id, axel); - } - { - auto bogey = std::make_shared("bogey"); - for (float y : {-2.f, 0.f, 2.f}) { - auto axel = bogey->uses.emplace_back(std::make_shared()); - axel->type = modelFactory.shapes.at("axel"); - axel->position = {0, y, 0}; - } - modelFactory.shapes.emplace(bogey->id, bogey); - } - 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 = modelFactory.shapes.at("bogey"); - bogeyUse->position = {0, y, 0}; - bogeyUse->rotation = {0, b * pi, 0}; - b++; - } - } - { - 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 = modelFactory.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 batteryBox = body->uses.emplace_back(std::make_shared()); - batteryBox->type = modelFactory.shapes.at("cuboid"); - batteryBox->position = {0, 0, .2}; - batteryBox->scale = {2.6, 4.5, 1}; - batteryBox->colour = "#2C3539"; - } - } - std::transform(factoryMeshes.begin(), factoryMeshes.end(), std::back_inserter(meshes.objects), - [](const FactoryMesh::CPtr & factoryMesh) -> Mesh::Ptr { - return factoryMesh->createMesh(); - }); - - render(20); -} -BOOST_AUTO_TEST_CASE(brush47xml) -{ - auto mf = ModelFactory::loadXML(RESDIR "/brush47.xml"); - BOOST_REQUIRE(mf); - BOOST_REQUIRE_EQUAL(6, mf->shapes.size()); - BOOST_CHECK(mf->shapes.at("plane")); - BOOST_CHECK(mf->shapes.at("cylinder")); - 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()); - - FactoryMesh::Collection factoryMeshes; - std::transform(factoryMeshes.begin(), factoryMeshes.end(), std::back_inserter(meshes.objects), - [](const FactoryMesh::CPtr & factoryMesh) -> Mesh::Ptr { - return factoryMesh->createMesh(); - }); - - render(20); -} -BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3