diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-03-14 19:16:57 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-03-14 19:18:30 +0000 |
commit | 3fea21a2d8f2aa67fd212837fe7e09e4f29ad515 (patch) | |
tree | 2c7a212745ddda5d55caf0cfd3c85b2cb39c15b5 /assetFactory | |
parent | Add postLoad support to persistence (diff) | |
download | ilt-3fea21a2d8f2aa67fd212837fe7e09e4f29ad515.tar.bz2 ilt-3fea21a2d8f2aa67fd212837fe7e09e4f29ad515.tar.xz ilt-3fea21a2d8f2aa67fd212837fe7e09e4f29ad515.zip |
Support creating a super texture from fragments
Currently makes wild assumptions about vertices and doesn't actually populate the texture, it's just grey
Diffstat (limited to 'assetFactory')
-rw-r--r-- | assetFactory/asset.cpp | 10 | ||||
-rw-r--r-- | assetFactory/asset.h | 6 | ||||
-rw-r--r-- | assetFactory/assetFactory.cpp | 62 | ||||
-rw-r--r-- | assetFactory/assetFactory.h | 10 | ||||
-rw-r--r-- | assetFactory/factoryMesh.cpp | 6 | ||||
-rw-r--r-- | assetFactory/modelFactoryMesh.h | 4 | ||||
-rw-r--r-- | assetFactory/style.cpp | 17 |
7 files changed, 107 insertions, 8 deletions
diff --git a/assetFactory/asset.cpp b/assetFactory/asset.cpp index 3ab2f1c..e3f5feb 100644 --- a/assetFactory/asset.cpp +++ b/assetFactory/asset.cpp @@ -1,4 +1,5 @@ #include "asset.h" +#include "assetFactory.h" bool Asset::persist(Persistence::PersistenceStore & store) @@ -6,6 +7,15 @@ Asset::persist(Persistence::PersistenceStore & store) return STORE_MEMBER(id) && STORE_MEMBER(name); } +Asset::TexturePtr +Asset::getTexture() const +{ + if (auto mf = Persistence::ParseBase::getShared<const AssetFactory>("assetFactory")) { + return mf->getTexture(); + } + return nullptr; +} + Asset::MeshConstruct::MeshConstruct(Mesh::Ptr & m) : Persistence::SelectionPtrBase<FactoryMesh::Ptr> {fmesh}, out {m} { } diff --git a/assetFactory/asset.h b/assetFactory/asset.h index e3318e4..30f40cd 100644 --- a/assetFactory/asset.h +++ b/assetFactory/asset.h @@ -4,12 +4,18 @@ #include "persistence.h" #include <stdTypeDefs.hpp> +class Texture; + class Asset : public Persistence::Persistable, public StdTypeDefs<Asset> { public: + using TexturePtr = std::shared_ptr<Texture>; + std::string id; std::string name; protected: + TexturePtr getTexture() const; + struct MeshConstruct : public Persistence::SelectionPtrBase<FactoryMesh::Ptr> { using Persistence::SelectionPtrBase<FactoryMesh::Ptr>::setValue; diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index de13579..276c258 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -2,11 +2,16 @@ #include "collections.hpp" #include "cuboid.h" #include "cylinder.h" +#include "filesystem.h" +#include "gfx/image.h" +#include "gfx/models/texture.h" #include "modelFactoryMesh_fwd.h" #include "object.h" #include "plane.h" +#include "resource.h" #include "saxParse-persistence.h" -#include <filesystem.h> +#include "texturePacker.h" +#include <stb/stb_image.h> AssetFactory::AssetFactory() : shapes { @@ -76,6 +81,61 @@ AssetFactory::parseColour(std::string_view in) const throw std::runtime_error("No such asset factory colour"); } +AssetFactory::TextureFragmentCoords +AssetFactory::getTextureCoords(std::string_view id) const +{ + createTexutre(); + const auto & fragmentUV = textureFragmentPositions.at(id); + return { + fragmentUV.xy(), + fragmentUV.zy(), + fragmentUV.zw(), + fragmentUV.xw(), + }; +} + +Asset::TexturePtr +AssetFactory::getTexture() const +{ + createTexutre(); + return texture; +} + +void +AssetFactory::createTexutre() const +{ + if (!textureFragments.empty() && (!texture || textureFragmentPositions.empty())) { + // * load images + std::vector<std::unique_ptr<Image>> images; + std::transform( + textureFragments.begin(), textureFragments.end(), std::back_inserter(images), [](const auto & tf) { + return std::make_unique<Image>(Resource::mapPath(tf.second->path), STBI_rgb_alpha); + }); + // * layout images + std::vector<TexturePacker::Image> imageSizes; + std::transform(images.begin(), images.end(), std::back_inserter(imageSizes), [](const auto & image) { + return TexturePacker::Image {image->width, image->height}; + }); + const auto [layout, outSize] = TexturePacker {imageSizes}.pack(); + // * create texture + std::vector<glm::u8vec4> textureData(TexturePacker::area(outSize), {127, 127, 127, 255}); + std::transform(textureFragments.begin(), textureFragments.end(), + std::inserter(textureFragmentPositions, textureFragmentPositions.end()), + [position = layout.begin(), size = imageSizes.begin(), outSize = outSize](const auto & tf) mutable { + const glm::vec4 positionFraction { + static_cast<float>(position->x) / static_cast<float>(outSize.x), + static_cast<float>(position->y) / static_cast<float>(outSize.y), + static_cast<float>(position->x + size->x) / static_cast<float>(outSize.x), + static_cast<float>(position->y + size->y) / static_cast<float>(outSize.y), + }; + position++; + size++; + return decltype(textureFragmentPositions)::value_type {tf.first, positionFraction}; + }); + texture = std::make_shared<Texture>(outSize.x, outSize.y, textureData.data()); + } +} + bool AssetFactory::persist(Persistence::PersistenceStore & store) { diff --git a/assetFactory/assetFactory.h b/assetFactory/assetFactory.h index 3d52f7f..52692c4 100644 --- a/assetFactory/assetFactory.h +++ b/assetFactory/assetFactory.h @@ -6,6 +6,8 @@ #include "textureFragment.h" #include <filesystem> +class Texture; + class AssetFactory : public Persistence::Persistable { public: using Shapes = std::map<std::string, Shape::Ptr, std::less<>>; @@ -14,10 +16,13 @@ public: using Colour = glm::vec3; using ColourAlpha = glm::vec4; using Colours = std::map<std::string, Colour, std::less<>>; + using TextureFragmentCoords = std::array<glm::vec2, 4>; AssetFactory(); [[nodiscard]] static std::shared_ptr<AssetFactory> loadXML(const std::filesystem::path &); [[nodiscard]] ColourAlpha parseColour(std::string_view) const; + [[nodiscard]] TextureFragmentCoords getTextureCoords(std::string_view) const; + [[nodiscard]] Asset::TexturePtr getTexture() const; Shapes shapes; Assets assets; @@ -30,4 +35,9 @@ public: private: friend Persistence::SelectionPtrBase<std::shared_ptr<AssetFactory>>; bool persist(Persistence::PersistenceStore & store) override; + + void createTexutre() const; + + mutable Asset::TexturePtr texture; + mutable std::map<std::string_view, glm::vec4, std::less<>> textureFragmentPositions; }; diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index 1665b90..c47e80e 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -2,13 +2,10 @@ #include "collections.hpp" #include "gfx/models/vertex.hpp" #include "modelFactoryMesh.h" -#include <glm/ext/matrix_transform.hpp> Mesh::Ptr FactoryMesh::createMesh() const { - constexpr glm::vec2 NullUV {}; - ModelFactoryMesh mesh; for (const auto & use : uses) { use->createMesh(mesh, 1); @@ -23,7 +20,8 @@ FactoryMesh::createMesh() const const auto smooth = mesh.property(mesh.smoothFaceProperty, face); const auto colour = mesh.color(face); for (const auto & vertex : mesh.fv_range(face)) { - vertices.emplace_back(mesh.point(vertex), NullUV, + const auto textureUV = mesh.texcoord2D(vertex); + vertices.emplace_back(mesh.point(vertex), textureUV, smooth ? mesh.property(mesh.vertex_normals_pph(), vertex) : mesh.property(mesh.face_normals_pph(), face), colour); diff --git a/assetFactory/modelFactoryMesh.h b/assetFactory/modelFactoryMesh.h index 149ff1a..493ce1b 100644 --- a/assetFactory/modelFactoryMesh.h +++ b/assetFactory/modelFactoryMesh.h @@ -4,6 +4,7 @@ #include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh> #include <OpenMesh/Core/Mesh/Traits.hh> #include <glm/geometric.hpp> +#include <glm/vec2.hpp> #include <glm/vec3.hpp> #include <glm/vec4.hpp> @@ -34,10 +35,11 @@ namespace OpenMesh { struct ModelFactoryTraits : public OpenMesh::DefaultTraits { FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status | OpenMesh::Attributes::Color); EdgeAttributes(OpenMesh::Attributes::Status); - VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status | OpenMesh::Attributes::TexCoord2D); using Point = glm::vec3; using Normal = glm::vec3; using Color = glm::vec4; + using TexCoord2D = glm::vec2; }; struct ModelFactoryMesh : public OpenMesh::PolyMesh_ArrayKernelT<ModelFactoryTraits> { diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp index b5ddcf1..8aecc71 100644 --- a/assetFactory/style.cpp +++ b/assetFactory/style.cpp @@ -19,8 +19,21 @@ void Style::applyStyle( ModelFactoryMesh & mesh, const ModelFactoryMesh::FaceHandle & face, EffectiveColour effectiveColour) const { - if (effectiveColour.has_value()) { - mesh.set_color(face, effectiveColour->get()); + if (texture.empty()) { + if (effectiveColour.has_value()) { + mesh.set_color(face, effectiveColour->get()); + } + } + else { + mesh.set_color(face, {}); + if (auto mf = Persistence::ParseBase::getShared<const AssetFactory>("assetFactory")) { + auto coords = mf->getTextureCoords(texture); + auto coord = coords.begin(); + // Wild assumption that face is a quad and the texture should apply linearly + for (const auto & vh : mesh.fv_range(face)) { + mesh.set_texcoord2D(vh, *coord++); + } + } } } |