From 88c9d66352c2f4856682efd48482b5b72aca18bf Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 12 Mar 2023 00:54:24 +0000 Subject: Support loading references to texture fragments --- assetFactory/assetFactory.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'assetFactory/assetFactory.cpp') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index f5fc2b3..de13579 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -75,11 +75,14 @@ AssetFactory::parseColour(std::string_view in) const } throw std::runtime_error("No such asset factory colour"); } + bool AssetFactory::persist(Persistence::PersistenceStore & store) { using MapObjects = Persistence::MapByMember>; using MapAssets = Persistence::MapByMember; + using MapTextureFragments = Persistence::MapByMember; return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects) + && STORE_NAME_HELPER("textureFragment", textureFragments, MapTextureFragments) && STORE_NAME_HELPER("asset", assets, MapAssets); } -- cgit v1.2.3 From 3fea21a2d8f2aa67fd212837fe7e09e4f29ad515 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 14 Mar 2023 19:16:57 +0000 Subject: Support creating a super texture from fragments Currently makes wild assumptions about vertices and doesn't actually populate the texture, it's just grey --- assetFactory/assetFactory.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) (limited to 'assetFactory/assetFactory.cpp') 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 +#include "texturePacker.h" +#include 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> images; + std::transform( + textureFragments.begin(), textureFragments.end(), std::back_inserter(images), [](const auto & tf) { + return std::make_unique(Resource::mapPath(tf.second->path), STBI_rgb_alpha); + }); + // * layout images + std::vector 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 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(position->x) / static_cast(outSize.x), + static_cast(position->y) / static_cast(outSize.y), + static_cast(position->x + size->x) / static_cast(outSize.x), + static_cast(position->y + size->y) / static_cast(outSize.y), + }; + position++; + size++; + return decltype(textureFragmentPositions)::value_type {tf.first, positionFraction}; + }); + texture = std::make_shared(outSize.x, outSize.y, textureData.data()); + } +} + bool AssetFactory::persist(Persistence::PersistenceStore & store) { -- cgit v1.2.3 From ac90f8e60b1c971c342cdd98769388563e157557 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 15 Mar 2023 02:10:31 +0000 Subject: Populate super texture with fragments This is kinda buggy due to TexturePacker sorting its input array and the client having no idea the order of the results. --- assetFactory/assetFactory.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'assetFactory/assetFactory.cpp') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 276c258..77dd554 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -118,21 +118,25 @@ AssetFactory::createTexutre() const }); const auto [layout, outSize] = TexturePacker {imageSizes}.pack(); // * create texture - std::vector textureData(TexturePacker::area(outSize), {127, 127, 127, 255}); + texture = std::make_shared(outSize.x, outSize.y, TextureOptions {.wrap = GL_CLAMP_TO_EDGE}); 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(position->x) / static_cast(outSize.x), - static_cast(position->y) / static_cast(outSize.y), - static_cast(position->x + size->x) / static_cast(outSize.x), - static_cast(position->y + size->y) / static_cast(outSize.y), + [position = layout.begin(), image = images.begin(), size = imageSizes.begin(), + outSize = glm::vec2 {outSize}](const auto & tf) mutable { + glm::vec4 positionFraction { + static_cast(position->x) / outSize.x, + static_cast(position->y) / outSize.y, + static_cast(position->x + size->x) / outSize.x, + static_cast(position->y + size->y) / outSize.y, }; + glTexSubImage2D(GL_TEXTURE_2D, 0, static_cast(position->x), static_cast(position->y), + static_cast(size->x), static_cast(size->y), GL_RGBA, GL_UNSIGNED_BYTE, + image->get()->data.data()); position++; + image++; size++; return decltype(textureFragmentPositions)::value_type {tf.first, positionFraction}; }); - texture = std::make_shared(outSize.x, outSize.y, textureData.data()); } } -- cgit v1.2.3 From 07dc690837f6a665490e370e8a07410e970384d7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 16 Mar 2023 23:23:04 +0000 Subject: Simplify calculation of texture position fraction --- assetFactory/assetFactory.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'assetFactory/assetFactory.cpp') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 77dd554..ed1af58 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -123,12 +123,7 @@ AssetFactory::createTexutre() const std::inserter(textureFragmentPositions, textureFragmentPositions.end()), [position = layout.begin(), image = images.begin(), size = imageSizes.begin(), outSize = glm::vec2 {outSize}](const auto & tf) mutable { - glm::vec4 positionFraction { - static_cast(position->x) / outSize.x, - static_cast(position->y) / outSize.y, - static_cast(position->x + size->x) / outSize.x, - static_cast(position->y + size->y) / outSize.y, - }; + const auto positionFraction = glm::vec4 {*position, *position + *size} / outSize.xyxy(); glTexSubImage2D(GL_TEXTURE_2D, 0, static_cast(position->x), static_cast(position->y), static_cast(size->x), static_cast(size->y), GL_RGBA, GL_UNSIGNED_BYTE, image->get()->data.data()); -- cgit v1.2.3