diff --git a/3rdParty/msdf/CMakeLists.txt b/3rdParty/msdf/CMakeLists.txt index f4729ce..c95b35e 100644 --- a/3rdParty/msdf/CMakeLists.txt +++ b/3rdParty/msdf/CMakeLists.txt @@ -1,11 +1,5 @@ include(FetchContent) -unset(ASSIMP_FOUND) -find_package(ASSIMP QUIET) -if (NOT ASSIMP_FOUND) - message(FATAL_ERROR "Assimp package is required to build msdfgen") -endif() - if(NOT DEFINED MSDFGEN_REPO) set(MSDFGEN_REPO https://github.com/Chlumsky/msdfgen.git) endif () diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b53063..0a66998 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ include(cmake/SetupVulkan.cmake) include(cmake/Filter.cmake) include(cmake/AppleHelper.cmake) include(cmake/SetShaderDependency.cmake) +include(cmake/CopyResources.cmake) set(DEPENDENCY_MIRROR_FILE "DependencyMirrors.txt" CACHE STRING "Dependency mirror") VarsFromFile("${DEPENDENCY_MIRROR_FILE}") # Load mirror list (for CICD) @@ -107,6 +108,7 @@ if (WIN32) endif () SetupVulkan(openVulkanoCpp) +CopyResources(openVulkanoCpp "${CMAKE_CURRENT_SOURCE_DIR}/fonts" ".ttf") SetShaderDependency(openVulkanoCpp ${CMAKE_CURRENT_SOURCE_DIR}/openVulkanoCpp/Shader ${SHADER_OUTPUT_DEST}) diff --git a/cmake/CopyResources.cmake b/cmake/CopyResources.cmake new file mode 100644 index 0000000..a431490 --- /dev/null +++ b/cmake/CopyResources.cmake @@ -0,0 +1,23 @@ +function(CopyResources TARGET FROM EXTENSIONS) + file(GLOB RESOURCES "${FROM}/*") + set(RESOURCES_TO_COPY "") + + if (${EXTENSIONS} STREQUAL "*") + set(RESOURCES_TO_COPY ${RESOURCES}) + else() + foreach(RESOURCE ${RESOURCES}) + get_filename_component(EXT "${RESOURCE}" EXT) + list(FIND EXTENSIONS ${EXT} EXT_FOUND) + if(NOT EXT_FOUND EQUAL -1) + list(APPEND RESOURCES_TO_COPY "${RESOURCE}") + endif() + endforeach() + endif() + + foreach(RESOURCE ${RESOURCES_TO_COPY}) + add_custom_command(TARGET ${TARGET} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${RESOURCE} $ + ) + endforeach() +endfunction() \ No newline at end of file diff --git a/examples/ExampleApps/TextExampleApp.cpp b/examples/ExampleApps/TextExampleApp.cpp index 68f0058..88f25f7 100644 --- a/examples/ExampleApps/TextExampleApp.cpp +++ b/examples/ExampleApps/TextExampleApp.cpp @@ -18,11 +18,13 @@ #include "Input/InputManager.hpp" #include "Host/GraphicsAppManager.hpp" #include "Host/GLFW/WindowGLFW.hpp" +#include "Host/ResourceLoader.hpp" #include "Math/Math.hpp" #include "Base/EngineConfiguration.hpp" #include "Controller/FreeCamCameraController.hpp" #include "Image/ImageLoaderPng.hpp" #include "Scene/FontAtlasGenerator.hpp" +#include #ifdef _WIN32 #undef TRANSPARENT @@ -33,6 +35,7 @@ namespace OpenVulkano using namespace Scene; using namespace Input; using namespace Math; + namespace fs = std::filesystem; class TextExampleAppImpl final : public TextExampleApp { @@ -49,14 +52,26 @@ namespace OpenVulkano m_cam.Init(70, 16, 9, 0.1f, 100); m_scene.SetCamera(&m_cam); - m_cfg.applyBorder = true; + static std::vector> texts; + texts.push_back(std::make_pair("ABab?.^{}_cdFGETG123)(", TextConfig())); + texts.push_back(std::make_pair("Hello, World!", TextConfig())); + texts[0].second.applyBorder = true; + texts[1].second.backgroundColor.a = 1; - const std::array texts = { "ABab?.^{}_cdFGETG123)(", "Hello, World!" }; const int N = texts.size(); - const std::string font = (OpenVulkano::Utils::GetFontsDirectory() / "Roboto-Regular.ttf").string(); - m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3); + std::string fontPath; + auto& resourceLoader = ResourceLoader::GetInstance(); + resourceLoader.GetResource("Roboto-Regular.ttf", fontPath); + const std::string atlasPath = (fs::path(fontPath).parent_path() / "roboto-regular-atlas.png").string(); + m_nodesPool.resize(N); m_drawablesPool.resize(N); + m_uniBuffers.resize(N); + + for (int i = 0; i < N; i++) + { + m_uniBuffers[i].Init(sizeof(TextConfig), &texts[i].second, 3); + } m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/text"); m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/text"); @@ -66,16 +81,16 @@ namespace OpenVulkano m_shader.alphaBlend = true; m_shader.cullMode = CullMode::NONE; - m_atlasGenerator.GenerateAtlas(font, (OpenVulkano::Utils::GetBuildDirectory() / "atlas.png").string()); - + m_atlasGenerator.GenerateAtlas(fontPath, atlasPath); + for (int i = 0; i < texts.size(); i++) { Text* t = new Text(); t->SetFontAtlasGenerator(&m_atlasGenerator); - t->SetConfig(m_cfg); - t->SetUniformBuffer(&m_uniBuffer); + t->SetConfig(texts[i].second); + t->SetUniformBuffer(&m_uniBuffers[i]); t->SetShader(&m_shader); - t->GenerateText(texts[i]); + t->GenerateText(texts[i].first); m_drawablesPool[i] = t; m_nodesPool[i].Init(); m_nodesPool[i].SetMatrix( @@ -111,10 +126,9 @@ namespace OpenVulkano private: OpenVulkano::Scene::Scene m_scene; PerspectiveCamera m_cam; - UniformBuffer m_uniBuffer; + std::vector m_uniBuffers; OpenVulkano::FreeCamCameraController m_camController; Shader m_shader; - TextConfig m_cfg; FontAtlasGenerator m_atlasGenerator; std::vector m_drawablesPool; std::vector m_nodesPool; diff --git a/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.cpp b/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.cpp index c650231..e0ac8f3 100644 --- a/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.cpp +++ b/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.cpp @@ -40,8 +40,9 @@ namespace OpenVulkano } } - Array ResourceLoaderAppDirLinux::GetResource(const std::string& resourceName) + Array ResourceLoaderAppDirLinux::GetResource(const std::string& resourceName, std::string& resourcePath) { - return Utils::ReadFile(GetAppDir() + resourceName, true); + resourcePath = GetAppDir() + resourceName; + return Utils::ReadFile(resourcePath, true); } } \ No newline at end of file diff --git a/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.hpp b/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.hpp index f8351e4..b827e99 100644 --- a/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.hpp +++ b/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.hpp @@ -13,6 +13,6 @@ namespace OpenVulkano class ResourceLoaderAppDirLinux final : public ResourceLoader { public: - Array GetResource(const std::string& resourceName) override; + Array GetResource(const std::string& resourceName, std::string& resourcePath) override; }; } \ No newline at end of file diff --git a/openVulkanoCpp/Host/ResourceLoader.cpp b/openVulkanoCpp/Host/ResourceLoader.cpp index 229f368..508a1d1 100644 --- a/openVulkanoCpp/Host/ResourceLoader.cpp +++ b/openVulkanoCpp/Host/ResourceLoader.cpp @@ -15,13 +15,13 @@ namespace OpenVulkano std::vector> m_loaders; public: - Array GetResource(const std::string& resourceName) override + Array GetResource(const std::string& resourceName, std::string& resourcePath) override { for(auto& loader : m_loaders) { try { - auto res = loader->GetResource(resourceName); + auto res = loader->GetResource(resourceName, resourcePath); if (!res.Empty()) return res; } catch (const std::exception& e) diff --git a/openVulkanoCpp/Host/ResourceLoader.hpp b/openVulkanoCpp/Host/ResourceLoader.hpp index fb0360b..fe13dbf 100644 --- a/openVulkanoCpp/Host/ResourceLoader.hpp +++ b/openVulkanoCpp/Host/ResourceLoader.hpp @@ -15,7 +15,7 @@ namespace OpenVulkano public: virtual ~ResourceLoader() = default; - virtual Array GetResource(const std::string& resourceName) = 0; + virtual Array GetResource(const std::string& resourceName, std::string& resourcePath) = 0; static ResourceLoader& GetInstance(); diff --git a/openVulkanoCpp/Host/WebResourceLoader.cpp b/openVulkanoCpp/Host/WebResourceLoader.cpp index 419f87e..bfb0ff2 100644 --- a/openVulkanoCpp/Host/WebResourceLoader.cpp +++ b/openVulkanoCpp/Host/WebResourceLoader.cpp @@ -90,11 +90,12 @@ namespace OpenVulkano return Array(buffer); } - Array WebResourceLoader::GetResource(const std::string& resourceName) + Array WebResourceLoader::GetResource(const std::string& resourceName, std::string& resourcePath) { if (IsUrl(resourceName)) { std::filesystem::path cacheFilePath = GetCacheFilePath(resourceName); + resourcePath = resourceName; if (std::filesystem::exists(cacheFilePath)) { return Utils::ReadFile(cacheFilePath.string()); } else { return DownloadResource(resourceName); } } diff --git a/openVulkanoCpp/Host/WebResourceLoader.hpp b/openVulkanoCpp/Host/WebResourceLoader.hpp index 5159e85..7b8208b 100644 --- a/openVulkanoCpp/Host/WebResourceLoader.hpp +++ b/openVulkanoCpp/Host/WebResourceLoader.hpp @@ -24,6 +24,6 @@ namespace OpenVulkano WebResourceLoader(); ~WebResourceLoader(); - Array GetResource(const std::string& resourceName) override; + Array GetResource(const std::string& resourceName, std::string& resourcePath) override; }; } \ No newline at end of file diff --git a/openVulkanoCpp/Host/Windows/ResourceLoaderAppDirWindows.cpp b/openVulkanoCpp/Host/Windows/ResourceLoaderAppDirWindows.cpp index 1d6ca35..6a0b588 100644 --- a/openVulkanoCpp/Host/Windows/ResourceLoaderAppDirWindows.cpp +++ b/openVulkanoCpp/Host/Windows/ResourceLoaderAppDirWindows.cpp @@ -38,8 +38,9 @@ namespace OpenVulkano } } - Array ResourceLoaderAppDirWindows::GetResource(const std::string& resourceName) + Array ResourceLoaderAppDirWindows::GetResource(const std::string& resourceName, std::string& resourcePath) { - return Utils::ReadFile(GetAppDir() + resourceName); + resourcePath = GetAppDir() + resourceName; + return Utils::ReadFile(resourcePath); } } \ No newline at end of file diff --git a/openVulkanoCpp/Host/Windows/ResourceLoaderAppDirWindows.hpp b/openVulkanoCpp/Host/Windows/ResourceLoaderAppDirWindows.hpp index fa0ac97..22ea019 100644 --- a/openVulkanoCpp/Host/Windows/ResourceLoaderAppDirWindows.hpp +++ b/openVulkanoCpp/Host/Windows/ResourceLoaderAppDirWindows.hpp @@ -13,6 +13,6 @@ namespace OpenVulkano class ResourceLoaderAppDirWindows final : public ResourceLoader { public: - Array GetResource(const std::string& resourceName) override; + Array GetResource(const std::string& resourceName, std::string& resourcePath) override; }; } \ No newline at end of file diff --git a/openVulkanoCpp/Image/ImageLoaderJpeg.cpp b/openVulkanoCpp/Image/ImageLoaderJpeg.cpp index 41915d6..a5e0a21 100644 --- a/openVulkanoCpp/Image/ImageLoaderJpeg.cpp +++ b/openVulkanoCpp/Image/ImageLoaderJpeg.cpp @@ -5,6 +5,7 @@ */ #include "ImageLoaderJpeg.hpp" +#include "Base/Utils.hpp" #include #include #include @@ -20,10 +21,8 @@ namespace OpenVulkano::Image { std::unique_ptr ImageLoaderJpeg::loadFromFile(const std::string& filePath) { - std::ifstream file(filePath, std::ios::binary); - if (!file) { throw std::runtime_error("Could not open file: " + filePath); } - std::vector buffer((std::istreambuf_iterator(file)), std::istreambuf_iterator()); - return loadJpeg(buffer.data(), buffer.size()); + Array buffer = OpenVulkano::Utils::ReadFile(filePath); + return loadJpeg(reinterpret_cast(buffer.Data()), buffer.Size()); } std::unique_ptr ImageLoaderJpeg::loadFromMemory(const std::vector& buffer) @@ -59,7 +58,7 @@ namespace OpenVulkano::Image } #else { - return loadData(data, static_cast(size), 3); + return loadData(data, static_cast(size)); } #endif } diff --git a/openVulkanoCpp/Image/ImageLoaderPng.cpp b/openVulkanoCpp/Image/ImageLoaderPng.cpp index ee37dd1..0968e4f 100644 --- a/openVulkanoCpp/Image/ImageLoaderPng.cpp +++ b/openVulkanoCpp/Image/ImageLoaderPng.cpp @@ -5,6 +5,7 @@ */ #include "ImageLoaderPng.hpp" +#include "Base/Utils.hpp" #include #include #include @@ -13,10 +14,8 @@ namespace OpenVulkano::Image { std::unique_ptr ImageLoaderPng::loadFromFile(const std::string& filePath) { - std::ifstream file(filePath, std::ios::binary); - if (!file) { throw std::runtime_error("Could not open file: " + filePath); } - std::vector buffer((std::istreambuf_iterator(file)), std::istreambuf_iterator()); - return loadData(buffer.data(), static_cast(buffer.size())); + Array buffer = OpenVulkano::Utils::ReadFile(filePath); + return loadData(reinterpret_cast(buffer.Data()), static_cast(buffer.Size())); } std::unique_ptr ImageLoaderPng::loadFromMemory(const std::vector& buffer) diff --git a/openVulkanoCpp/Scene/Drawable.hpp b/openVulkanoCpp/Scene/Drawable.hpp index 3e0ccf5..5f58614 100644 --- a/openVulkanoCpp/Scene/Drawable.hpp +++ b/openVulkanoCpp/Scene/Drawable.hpp @@ -32,7 +32,7 @@ namespace OpenVulkano::Scene Scene* m_scene = nullptr; Shader* m_shader = nullptr; const DrawEncoder m_encoder; - DrawPhase m_drawPhase; + const DrawPhase m_drawPhase; public: explicit Drawable(const DrawEncoder& encoder, @@ -45,8 +45,6 @@ namespace OpenVulkano::Scene void SetShader(Shader* shader) { m_shader = shader; } - void SetDrawPhase(DrawPhase phase) { m_drawPhase = phase; } - [[nodiscard]] Scene* GetScene() const { return m_scene; } [[nodiscard]] const auto& GetNodes() const { return m_nodes; } diff --git a/openVulkanoCpp/Scene/Geometry.cpp b/openVulkanoCpp/Scene/Geometry.cpp index 5dff1db..24dab8d 100644 --- a/openVulkanoCpp/Scene/Geometry.cpp +++ b/openVulkanoCpp/Scene/Geometry.cpp @@ -17,10 +17,6 @@ #endif #include -#include "msdfgen.h" -#include "msdfgen-ext.h" -#include "msdf-atlas-gen/msdf-atlas-gen.h" - namespace OpenVulkano::Scene { Geometry::Geometry(const Geometry& other) diff --git a/openVulkanoCpp/Scene/Text.hpp b/openVulkanoCpp/Scene/Text.hpp index 00a9076..7df40b6 100644 --- a/openVulkanoCpp/Scene/Text.hpp +++ b/openVulkanoCpp/Scene/Text.hpp @@ -24,12 +24,13 @@ namespace OpenVulkano::Scene struct TextConfig { - Math::Vector4f textColor = { 1, 1, 1, 0 }; // vec4 to match paddding (multiple of 16) - Math::Vector3f borderColor = { 1, 0, 0 }; + Math::Vector4f textColor = { 1, 1, 1, 1 }; + Math::Vector4f borderColor = { 1, 0, 0, 1 }; + Math::Vector4f backgroundColor = { 0, 1, 0, 0 }; float threshold = 0.4f; float borderSize = 0.05f; float smoothing = 1.f/32.f; - bool applyBorder = false; + uint32_t applyBorder = false; //bool sdfMultiChannel = false; }; diff --git a/openVulkanoCpp/Shader/text.frag b/openVulkanoCpp/Shader/text.frag index a8d852b..b874c24 100644 --- a/openVulkanoCpp/Shader/text.frag +++ b/openVulkanoCpp/Shader/text.frag @@ -8,8 +8,9 @@ layout(set = 2, binding = 0) uniform sampler2D texSampler; layout(set = 3, binding = 0) uniform TextConfig { - vec3 textColor; - vec3 borderColor; + vec4 textColor; + vec4 borderColor; + vec4 backgroundColor; float threshold; float borderSize; float smoothing; @@ -24,10 +25,15 @@ void main() { float border = smoothstep(textConfig.threshold + textConfig.borderSize - textConfig.smoothing, textConfig.threshold + textConfig.borderSize + textConfig.smoothing, distance); - outColor = vec4(mix(textConfig.borderColor, textConfig.textColor, border), 1) * alpha; + outColor = mix(textConfig.borderColor, textConfig.textColor, border) * alpha; } else { - outColor = vec4(textConfig.textColor, 1) * alpha; + outColor = vec4(textConfig.textColor) * alpha; + } + + if (textConfig.backgroundColor.a != 0) + { + outColor = mix(textConfig.backgroundColor, outColor, alpha); } }