Files
OpenVulkano/openVulkanoCpp/Scene/TextDrawable.hpp
2024-08-06 10:33:28 +03:00

87 lines
2.7 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "UpdateFrequency.hpp"
#include "Base/ICloseable.hpp"
#include "Math/Math.hpp"
#include "DataFormat.hpp"
#include "SimpleDrawable.hpp"
#include "FontAtlasGenerator.hpp"
#include "Texture.hpp"
#include "Material.hpp"
#include "Geometry.hpp"
#include "UniformBuffer.hpp"
#include "Base/Logger.hpp"
#include "AtlasMetadata.hpp"
#if __has_include("msdfgen.h")
#include "msdfgen.h"
#include "msdfgen-ext.h"
#include "msdf-atlas-gen/msdf-atlas-gen.h"
#define MSDFGEN_AVAILABLE 1
#endif
namespace OpenVulkano::Scene
{
#ifdef MSDFGEN_AVAILABLE
using namespace msdfgen;
using namespace msdf_atlas;
#endif // MSDFGEN_AVAILABLE
struct TextConfig
{
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;
uint32_t applyBorder = false;
//bool sdfMultiChannel = false;
};
class TextDrawable : public SimpleDrawable
{
public:
static Shader& GetDefaultShader();
TextDrawable(const std::string& atlasMetadataFile, Texture* atlasTex, const TextConfig& config = TextConfig());
TextDrawable(const Array<char>& atlasMetadata, Texture* atlasTex, const TextConfig& config = TextConfig());
TextDrawable(const std::map<uint32_t, GlyphInfo>& glyphData, Texture* atlasTex, const TextConfig& config = TextConfig());
#ifdef MSDFGEN_AVAILABLE
TextDrawable(FontAtlasGenerator* fontAtlasGenerator, const TextConfig& config = TextConfig());
#endif
void GenerateText(const std::string& text, const Math::Vector3f& pos = Math::Vector3f(0.f));
void SetConfig(const TextConfig& cfg) { m_cfg = cfg; }
void SetShader(Shader* shader) { m_shader = shader; }
TextConfig& GetConfig() { return m_cfg; }
Shader* GetShader() { return m_shader; }
#ifdef MSDFGEN_AVAILABLE
void SetFontAtlasGenerator(FontAtlasGenerator* fontAtlasGenerator)
{
if (!fontAtlasGenerator || fontAtlasGenerator->GetGlyphsInfo().empty())
{
Logger::RENDER->error("FontAtlasGenerator is either nullptr or doesn't contain glyphs info");
return;
}
m_fontAtlasGenerator = fontAtlasGenerator;
}
FontAtlasGenerator* GetFontAtlasGenerator() { return m_fontAtlasGenerator; }
#endif
private:
Geometry m_geometry;
Material m_material;
UniformBuffer m_uniBuffer;
std::map<uint32_t, GlyphInfo> m_glyphs;
AtlasMetadata m_meta;
#ifdef MSDFGEN_AVAILABLE
FontAtlasGenerator* m_fontAtlasGenerator = nullptr;
#endif
Shader* m_shader = &GetDefaultShader();
TextConfig m_cfg;
};
}