rework label drawable and text drawable's API
This commit is contained in:
@@ -61,11 +61,7 @@ namespace OpenVulkano
|
||||
m_nodesPool.resize(N);
|
||||
m_drawablesPool.reserve(N);
|
||||
|
||||
static TextDrawable textDrawable(sdfMetadataInfo);
|
||||
auto& sh = TextDrawable::GetSdfDefaultShader();
|
||||
sh.depthWrite = false;
|
||||
sh.depthCompareOp = CompareOp::LESS_OR_EQUAL;
|
||||
textDrawable.SetShader(&sh);
|
||||
TextDrawable textDrawable(sdfMetadataInfo);
|
||||
const std::vector<std::string> texts = { "Hello", "1", "XYZ" };
|
||||
BillboardControlBlock billboardSettings;
|
||||
LabelDrawableSettings labelSettings;
|
||||
@@ -75,14 +71,14 @@ namespace OpenVulkano
|
||||
//labelSettings.hasRoundedCorners = (i % 2 == 0 ? 1 : 0);
|
||||
labelSettings.hasArrow = (i % 2 == 0 ? 1 : 0);
|
||||
bool isBillboard = (i % 2 == 0 ? 1 : 0);
|
||||
LabelDrawable& label = m_drawablesPool.emplace_back(labelSettings, isBillboard);
|
||||
LabelDrawable& label = m_drawablesPool.emplace_back(textDrawable.GetAtlasData(), labelSettings, isBillboard);
|
||||
label.SetBillboardSettings(billboardSettings);
|
||||
label.AddText(&textDrawable, texts[i]);
|
||||
label.AddText(texts[i]);
|
||||
if (i == 2)
|
||||
{
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
label.AddText(&textDrawable, "Additional text" + std::to_string(j));
|
||||
label.AddText("Additional text" + std::to_string(j));
|
||||
}
|
||||
}
|
||||
m_scene.GetRoot()->AddChild(&m_nodesPool[i]);
|
||||
|
||||
@@ -74,6 +74,8 @@ namespace OpenVulkano
|
||||
msdf_atlas::Charset charset = SdfFontAtlasGenerator::LoadAllGlyphs(fontPath);
|
||||
m_atlasGenerator.GenerateAtlas(fontPath, charset);
|
||||
m_msdfAtlasGenerator.GenerateAtlas(fontPath, charset);
|
||||
m_atlasGenerator.SaveAtlasMetadataInfo("sdf_atlas.png");
|
||||
m_msdfAtlasGenerator.SaveAtlasMetadataInfo("msdf_atlas");
|
||||
#else
|
||||
auto sdfMetadataInfo = resourceLoader.GetResource("sdf_atlas_packed.png");
|
||||
auto msdfMetadataInfo = resourceLoader.GetResource("msdf_atlas_packed.png");
|
||||
@@ -86,12 +88,12 @@ namespace OpenVulkano
|
||||
#if defined(MSDFGEN_AVAILABLE) && defined(CREATE_NEW_ATLAS)
|
||||
if (i < texts.size())
|
||||
{
|
||||
t = new TextDrawable(&m_atlasGenerator, texts[textIdx].second);
|
||||
t = new TextDrawable(m_atlasGenerator.GetAtlasData(), texts[textIdx].second);
|
||||
t->SetShader(&TextDrawable::GetSdfDefaultShader());
|
||||
}
|
||||
else
|
||||
{
|
||||
t = new TextDrawable(&m_msdfAtlasGenerator, texts[textIdx].second);
|
||||
t = new TextDrawable(m_msdfAtlasGenerator.GetAtlasData(), texts[textIdx].second);
|
||||
t->SetShader(&TextDrawable::GetMsdfDefaultShader());
|
||||
}
|
||||
#else
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 1012 KiB After Width: | Height: | Size: 1012 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 631 KiB After Width: | Height: | Size: 631 KiB |
63
openVulkanoCpp/Scene/AtlasData.hpp
Normal file
63
openVulkanoCpp/Scene/AtlasData.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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 "Math/Math.hpp"
|
||||
#include "Image/Image.hpp"
|
||||
#include "Scene/Texture.hpp"
|
||||
#include <string_view>
|
||||
#include <map>
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
struct GlyphInfo
|
||||
{
|
||||
//GlyphGeometry geometry;
|
||||
//GlyphBox glyphBox;
|
||||
Math::Vector3f_SIMD xyz[4] = {};
|
||||
Math::Vector2f_SIMD uv[4] = {};
|
||||
double advance = 0;
|
||||
};
|
||||
|
||||
class FontAtlasType
|
||||
{
|
||||
public:
|
||||
enum Type : int16_t
|
||||
{
|
||||
SDF = 0,
|
||||
MSDF,
|
||||
UNKNOWN
|
||||
};
|
||||
static constexpr std::string_view DEFAULT_FG_SHADERS[] = { "Shader/text", "Shader/msdfText" };
|
||||
|
||||
public:
|
||||
FontAtlasType(Type type) : m_type(type) {}
|
||||
Type GetType() const { return m_type; }
|
||||
const std::string_view& GetDefaultFragmentShader() const
|
||||
{
|
||||
return DEFAULT_FG_SHADERS[static_cast<int>(m_type)];
|
||||
}
|
||||
private:
|
||||
Type m_type;
|
||||
};
|
||||
|
||||
struct AtlasMetadata
|
||||
{
|
||||
// vertical difference between baselines
|
||||
double lineHeight = 0;
|
||||
int16_t atlasType = FontAtlasType::UNKNOWN;
|
||||
};
|
||||
|
||||
struct AtlasData
|
||||
{
|
||||
std::map<uint32_t, GlyphInfo> glyphs;
|
||||
AtlasMetadata meta;
|
||||
std::unique_ptr<Image::Image> img;
|
||||
Texture texture;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* 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 "Math/Math.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
struct GlyphInfo
|
||||
{
|
||||
//GlyphGeometry geometry;
|
||||
//GlyphBox glyphBox;
|
||||
Math::Vector3f_SIMD xyz[4] = {};
|
||||
Math::Vector2f_SIMD uv[4] = {};
|
||||
double advance = 0;
|
||||
};
|
||||
|
||||
struct AtlasMetadata
|
||||
{
|
||||
// vertical difference between baselines
|
||||
double lineHeight = 0;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#include "FontAtlasGenerator.hpp"
|
||||
#include "Base/Logger.hpp"
|
||||
#include "Scene/AtlasMetadata.hpp"
|
||||
#include "Scene/AtlasData.hpp"
|
||||
#include <msdfgen.h>
|
||||
#include <msdfgen-ext.h>
|
||||
#include <msdf-atlas-gen/msdf-atlas-gen.h>
|
||||
@@ -120,7 +120,7 @@ namespace OpenVulkano::Scene
|
||||
void FontAtlasGenerator<Channels>::SaveAtlasMetadataInfo(const std::string& outputFile,
|
||||
bool packIntoSingleFile) const
|
||||
{
|
||||
if (m_symbols.empty())
|
||||
if (m_atlasData->glyphs.empty())
|
||||
{
|
||||
Logger::DATA->info("No glyphs loaded. Nothing to save.");
|
||||
return;
|
||||
@@ -134,9 +134,9 @@ namespace OpenVulkano::Scene
|
||||
SavePng(fileName);
|
||||
}
|
||||
std::fstream fs(fileName.c_str(), std::ios_base::out | std::ios_base::binary | (packedFlag ? std::ios_base::app : std::ios_base::trunc));
|
||||
fs.write(reinterpret_cast<const char*>(&m_meta), sizeof(AtlasMetadata));
|
||||
fs.write(reinterpret_cast<const char*>(&m_atlasData->meta), sizeof(AtlasMetadata));
|
||||
uint64_t metadataBytes = sizeof(AtlasMetadata);
|
||||
for (const auto& [key, val] : m_symbols)
|
||||
for (const auto& [key, val]: m_atlasData->glyphs)
|
||||
{
|
||||
fs.write(reinterpret_cast<const char*>(&key), sizeof(uint32_t));
|
||||
fs.write(reinterpret_cast<const char*>(&val), sizeof(GlyphInfo));
|
||||
@@ -181,7 +181,8 @@ namespace OpenVulkano::Scene
|
||||
void FontAtlasGenerator<Channels>::Generate(FreetypeHandle* ft, FontHandle* font, const Charset& chset,
|
||||
const std::optional<std::string>& pngOutput)
|
||||
{
|
||||
m_symbols.clear();
|
||||
m_atlasData.reset(new AtlasData);
|
||||
|
||||
std::vector<GlyphGeometry> glyphsGeometry;
|
||||
// FontGeometry is a helper class that loads a set of glyphs from a single font.
|
||||
FontGeometry fontGeometry(&glyphsGeometry);
|
||||
@@ -219,9 +220,12 @@ namespace OpenVulkano::Scene
|
||||
if constexpr (Channels == 3)
|
||||
{
|
||||
// store RGB as RGBA
|
||||
m_atlasData->img = std::make_unique<Image::Image>();
|
||||
m_atlasData->img->data = Array<uint8_t>(width * height * 4);
|
||||
m_atlasData->img->resolution = Math::Vector3ui(width, height, 1);
|
||||
m_atlasData->img->dataFormat = OpenVulkano::DataFormat::R8G8B8A8_UNORM;
|
||||
const BitmapConstRef<msdfgen::byte, 3> storage = generator.atlasStorage();
|
||||
msdfgen::Bitmap<msdfgen::byte, 4> bitmap(width, height);
|
||||
msdfgen::byte* data = static_cast<msdfgen::byte*>(bitmap);
|
||||
msdfgen::byte* data = static_cast<msdfgen::byte*>(m_atlasData->img->data.Data());
|
||||
for (size_t srcPos = 0, dstPos = 0; srcPos < width * height * 3; srcPos += 3, dstPos += 4)
|
||||
{
|
||||
data[dstPos] = storage.pixels[srcPos];
|
||||
@@ -229,19 +233,23 @@ namespace OpenVulkano::Scene
|
||||
data[dstPos + 2] = storage.pixels[srcPos + 2];
|
||||
data[dstPos + 3] = 255;
|
||||
}
|
||||
m_atlasStorage = std::move(bitmap);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_atlasStorage = generator.atlasStorage();
|
||||
m_atlasData->img = std::make_unique<Image::Image>();
|
||||
m_atlasData->img->data = Array<uint8_t>(width * height);
|
||||
m_atlasData->img->resolution = Math::Vector3ui(width, height, 1);
|
||||
m_atlasData->img->dataFormat = OpenVulkano::DataFormat::R8_UNORM;
|
||||
const msdfgen::BitmapConstRef<msdfgen::byte, 1>& storage = generator.atlasStorage();
|
||||
memcpy(m_atlasData->img->data.Data(), storage.pixels, width * height);
|
||||
}
|
||||
|
||||
m_atlasTex.resolution = Math::Vector3ui(m_atlasStorage.width(), m_atlasStorage.height(), 1);
|
||||
m_atlasTex.textureBuffer = (msdfgen::byte*) m_atlasStorage;
|
||||
m_atlasTex.format = (channelsCount == 1 ? OpenVulkano::DataFormat::R8_UNORM : OpenVulkano::DataFormat::R8G8B8A8_UNORM);
|
||||
m_atlasTex.size = m_atlasStorage.width() * m_atlasStorage.height() * channelsCount;
|
||||
|
||||
m_meta.lineHeight = fontGeometry.getMetrics().lineHeight;
|
||||
m_atlasData->texture.resolution = m_atlasData->img->resolution;
|
||||
m_atlasData->texture.textureBuffer = m_atlasData->img->data.Data();
|
||||
m_atlasData->texture.format = m_atlasData->img->dataFormat;
|
||||
m_atlasData->texture.size = width * height * channelsCount;
|
||||
m_atlasData->meta.lineHeight = fontGeometry.getMetrics().lineHeight;
|
||||
m_atlasData->meta.atlasType = channelsCount == 1 ? FontAtlasType::SDF : FontAtlasType::MSDF;
|
||||
|
||||
struct Bbox
|
||||
{
|
||||
@@ -250,7 +258,7 @@ namespace OpenVulkano::Scene
|
||||
|
||||
for (const auto& glyph: glyphsGeometry)
|
||||
{
|
||||
GlyphInfo& info = m_symbols[glyph.getCodepoint()];
|
||||
GlyphInfo& info = m_atlasData->glyphs[glyph.getCodepoint()];
|
||||
const GlyphBox& glyphBox = generator.getLayout()[idx++];
|
||||
|
||||
Bbox glyphBaselineBbox, glyphAtlasBbox;
|
||||
@@ -269,26 +277,26 @@ namespace OpenVulkano::Scene
|
||||
info.xyz[0].x = bearingX;
|
||||
info.xyz[0].y = h - bearingY;
|
||||
info.xyz[0].z = 0;
|
||||
info.uv[0].x = l / m_atlasTex.resolution.x;
|
||||
info.uv[0].y = b / m_atlasTex.resolution.y;
|
||||
info.uv[0].x = l / m_atlasData->texture.resolution.x;
|
||||
info.uv[0].y = b / m_atlasData->texture.resolution.y;
|
||||
|
||||
info.xyz[1].x = bearingX + w;
|
||||
info.xyz[1].y = h - bearingY;
|
||||
info.xyz[1].z = 0;
|
||||
info.uv[1].x = r / m_atlasTex.resolution.x;
|
||||
info.uv[1].y = b / m_atlasTex.resolution.y;
|
||||
info.uv[1].x = r / m_atlasData->texture.resolution.x;
|
||||
info.uv[1].y = b / m_atlasData->texture.resolution.y;
|
||||
|
||||
info.xyz[2].x = bearingX + w;
|
||||
info.xyz[2].y = bearingY; //h - bearingY + h;
|
||||
info.xyz[2].z = 0;
|
||||
info.uv[2].x = r / m_atlasTex.resolution.x;
|
||||
info.uv[2].y = t / m_atlasTex.resolution.y;
|
||||
info.uv[2].x = r / m_atlasData->texture.resolution.x;
|
||||
info.uv[2].y = t / m_atlasData->texture.resolution.y;
|
||||
|
||||
info.xyz[3].x = bearingX;
|
||||
info.xyz[3].y = bearingY;
|
||||
info.xyz[3].z = 0;
|
||||
info.uv[3].x = l / m_atlasTex.resolution.x;
|
||||
info.uv[3].y = t / m_atlasTex.resolution.y;
|
||||
info.uv[3].x = l / m_atlasData->texture.resolution.x;
|
||||
info.uv[3].y = t / m_atlasData->texture.resolution.y;
|
||||
|
||||
info.advance = glyphBox.advance;
|
||||
}
|
||||
@@ -304,13 +312,13 @@ namespace OpenVulkano::Scene
|
||||
stbi_flip_vertically_on_write(1);
|
||||
if (std::filesystem::path(output).extension() == ".png")
|
||||
{
|
||||
stbi_write_png(output.c_str(), m_atlasStorage.width(), m_atlasStorage.height(), channelsCount,
|
||||
m_atlasStorage.operator const msdfgen::byte*(), channelsCount * m_atlasStorage.width());
|
||||
stbi_write_png(output.c_str(), m_atlasData->img->resolution.x, m_atlasData->img->resolution.y,
|
||||
channelsCount, m_atlasData->img->data.Data(), channelsCount * m_atlasData->img->resolution.x);
|
||||
}
|
||||
else
|
||||
{
|
||||
stbi_write_png((output + ".png").c_str(), m_atlasStorage.width(), m_atlasStorage.height(), channelsCount,
|
||||
m_atlasStorage.operator const msdfgen::byte*(), channelsCount * m_atlasStorage.width());
|
||||
stbi_write_png((output + ".png").c_str(), m_atlasData->img->resolution.x, m_atlasData->img->resolution.y,
|
||||
channelsCount, m_atlasData->img->data.Data(), channelsCount * m_atlasData->img->resolution.x);
|
||||
}
|
||||
}
|
||||
template class FontAtlasGenerator<1>;
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#if __has_include("msdfgen.h")
|
||||
|
||||
#include "Scene/AtlasMetadata.hpp"
|
||||
#include "Scene/AtlasData.hpp"
|
||||
#include "IFontAtlasGenerator.hpp"
|
||||
#include "Scene/Texture.hpp"
|
||||
#include <msdfgen.h>
|
||||
@@ -41,7 +41,6 @@ namespace OpenVulkano::Scene
|
||||
msdf_atlas::BitmapAtlasStorage<msdfgen::byte, 3>>;
|
||||
public:
|
||||
using Generator = std::conditional<Channels == 1, SdfGenerator, MsdfGenerator>::type;
|
||||
using AtlasData = std::conditional<Channels == 1, msdfgen::Bitmap<msdfgen::byte, 1>, msdfgen::Bitmap<msdfgen::byte, 4>>::type;
|
||||
using Config = FontAtlasGeneratorConfig;
|
||||
static constexpr int channelsCount = (Channels == 1 ? 1 : 4);
|
||||
static msdf_atlas::Charset LoadAllGlyphs(const std::variant<std::string, Array<char>>& data);
|
||||
@@ -57,9 +56,7 @@ namespace OpenVulkano::Scene
|
||||
const std::optional<std::string>& pngOutput = std::nullopt);
|
||||
void SaveAtlasMetadataInfo(const std::string& outputFile, bool packIntoSingleFile = true) const override;
|
||||
void SetGeneratorConfig(const Config& config) { m_config = config; }
|
||||
const Texture& GetAtlas() const override { return m_atlasTex; }
|
||||
std::map<uint32_t, GlyphInfo>& GetGlyphsInfo() override { return m_symbols; }
|
||||
AtlasMetadata& GetAtlasMetadata() override { return m_meta; }
|
||||
std::shared_ptr<AtlasData> GetAtlasData() const { return m_atlasData; }
|
||||
Config& GetGeneratorConfig() { return m_config; }
|
||||
private:
|
||||
void InitFreetypeFromFile(msdfgen::FreetypeHandle*& ft, msdfgen::FontHandle*& font, const std::string& file);
|
||||
@@ -70,11 +67,8 @@ namespace OpenVulkano::Scene
|
||||
void SavePng(const std::string& output) const;
|
||||
|
||||
private:
|
||||
Texture m_atlasTex;
|
||||
AtlasMetadata m_meta;
|
||||
Config m_config;
|
||||
std::map<uint32_t, GlyphInfo> m_symbols;
|
||||
AtlasData m_atlasStorage;
|
||||
std::shared_ptr<AtlasData> m_atlasData;
|
||||
};
|
||||
using SdfFontAtlasGenerator = FontAtlasGenerator<1>;
|
||||
using MsdfFontAtlasGenerator = FontAtlasGenerator<3>;
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Scene/AtlasMetadata.hpp"
|
||||
#include "Scene/Texture.hpp"
|
||||
#include "Scene/AtlasData.hpp"
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <map>
|
||||
@@ -25,8 +24,6 @@ namespace OpenVulkano::Scene
|
||||
virtual void GenerateAtlas(const Array<char>& fontData, int length, const std::set<uint32_t>& charset,
|
||||
const std::optional<std::string>& pngOutput = std::nullopt) = 0;
|
||||
virtual void SaveAtlasMetadataInfo(const std::string& outputFile, bool packIntoSingleFile = true) const = 0;
|
||||
virtual const Texture& GetAtlas() const = 0;
|
||||
virtual std::map<uint32_t, GlyphInfo>& GetGlyphsInfo() = 0;
|
||||
virtual AtlasMetadata& GetAtlasMetadata() = 0;
|
||||
virtual std::shared_ptr<AtlasData> GetAtlasData() const = 0;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -15,9 +15,15 @@ namespace OpenVulkano::Scene
|
||||
{
|
||||
using namespace Math;
|
||||
|
||||
LabelDrawable::LabelDrawable(const LabelDrawableSettings& settings, bool isBillboard)
|
||||
LabelDrawable::LabelDrawable(const std::shared_ptr<AtlasData>& atlasData, const LabelDrawableSettings& settings,
|
||||
bool isBillboard)
|
||||
: Drawable(DrawEncoder::GetDrawEncoder<LabelDrawable>(), DrawPhase::MAIN)
|
||||
{
|
||||
if (atlasData->glyphs.empty() || !atlasData->texture.size)
|
||||
{
|
||||
throw std::runtime_error("Can't create label drawable. Either glyphs or texture is empty");
|
||||
}
|
||||
m_atlasData = atlasData;
|
||||
m_isBillboard = isBillboard;
|
||||
SetLabelSettings(settings);
|
||||
SetupShaders();
|
||||
@@ -34,40 +40,19 @@ namespace OpenVulkano::Scene
|
||||
m_labelData.arrowLength = settings.arrowLength;
|
||||
}
|
||||
|
||||
void LabelDrawable::AddText(TextDrawable* td, const std::string& text)
|
||||
void LabelDrawable::AddText(const std::string& text, const TextConfig& config)
|
||||
{
|
||||
if (!td || text.empty())
|
||||
if (text.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!td->GetShader())
|
||||
{
|
||||
Logger::RENDER->error("Text drawable shader is null, cannot add text to label drawable\n");
|
||||
return;
|
||||
}
|
||||
|
||||
TextDrawable& textDrawable = m_texts.emplace_back(td->GetConfig());
|
||||
// do not render glyph's background
|
||||
TextDrawable& textDrawable = m_texts.emplace_back(m_atlasData, config);
|
||||
textDrawable.GetConfig().backgroundColor.a = 0;
|
||||
if (m_isBillboard)
|
||||
{
|
||||
textDrawable.SetShader(GetBillboardTextShader(td->GetShader()));
|
||||
}
|
||||
else
|
||||
{
|
||||
textDrawable.SetShader(td->GetShader());
|
||||
}
|
||||
double lineHeight;
|
||||
if (textDrawable.GetFontAtlasGenerator())
|
||||
{
|
||||
textDrawable.SetFontAtlasGenerator(td->GetFontAtlasGenerator());
|
||||
lineHeight = td->GetFontAtlasGenerator()->GetAtlasMetadata().lineHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
textDrawable.SetAtlasData(td->GetAtlasData());
|
||||
lineHeight = td->GetAtlasData()->meta.lineHeight;
|
||||
}
|
||||
textDrawable.SetShader(&m_textShader);
|
||||
double lineHeight = m_atlasData->meta.lineHeight;
|
||||
|
||||
textDrawable.GenerateText(text, m_position);
|
||||
RecalculateBbox(textDrawable.GetBoundingBox());
|
||||
// update position for next text entry
|
||||
@@ -163,59 +148,6 @@ namespace OpenVulkano::Scene
|
||||
}
|
||||
}
|
||||
|
||||
// if we have want our label to be billboard, then we'll create new shader and set it's vertex shader to billboard.vert
|
||||
// to avoid modifying input shader
|
||||
Shader* LabelDrawable::GetBillboardTextShader(Shader* src)
|
||||
{
|
||||
auto GetFragmentShaderName = [](const Shader& shaderToCheck) -> std::optional<std::string>
|
||||
{
|
||||
const auto& shaderPrograms = shaderToCheck.shaderPrograms;
|
||||
auto pos = std::find_if(shaderPrograms.begin(), shaderPrograms.end(),
|
||||
[](const ShaderProgram& program)
|
||||
{ return program.type == ShaderProgramType::FRAGMENT; });
|
||||
return pos != shaderPrograms.end() ? std::optional<std::string>(pos->name) : std::nullopt;
|
||||
};
|
||||
|
||||
std::optional<std::string> inputFragmentName = GetFragmentShaderName(*src);
|
||||
if (!inputFragmentName)
|
||||
{
|
||||
throw std::runtime_error("Shader doesn't have fragment program");
|
||||
}
|
||||
|
||||
bool exists = false;
|
||||
int shaderIdx = 0;
|
||||
for (; shaderIdx < m_billboardTextShaders.size(); shaderIdx++)
|
||||
{
|
||||
std::optional<std::string> name = GetFragmentShaderName(m_billboardTextShaders[shaderIdx]);
|
||||
if (name.value() == inputFragmentName.value())
|
||||
{
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!exists)
|
||||
{
|
||||
Shader& shader = m_billboardTextShaders.emplace_back();
|
||||
shaderIdx = m_billboardTextShaders.size() - 1;
|
||||
shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/billboard");
|
||||
shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, inputFragmentName.value());
|
||||
shader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
|
||||
shader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING);
|
||||
DescriptorSetLayoutBinding binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
|
||||
binding.stageFlags = ShaderProgramType::Type::VERTEX;
|
||||
DescriptorSetLayoutBinding binding2 = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
|
||||
binding2.stageFlags = ShaderProgramType::FRAGMENT;
|
||||
shader.AddDescriptorSetLayoutBinding(binding2, 3);
|
||||
shader.AddDescriptorSetLayoutBinding(binding, 4);
|
||||
shader.depthWrite = false;
|
||||
shader.depthCompareOp = CompareOp::LESS_OR_EQUAL;
|
||||
shader.alphaBlend = true;
|
||||
shader.cullMode = CullMode::NONE;
|
||||
}
|
||||
return &m_billboardTextShaders[shaderIdx];
|
||||
}
|
||||
|
||||
void LabelDrawable::SetupShaders()
|
||||
{
|
||||
DescriptorSetLayoutBinding binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
|
||||
@@ -237,6 +169,31 @@ namespace OpenVulkano::Scene
|
||||
m_backgroundShader.AddDescriptorSetLayoutBinding(binding, 5);
|
||||
m_backgroundShader.cullMode = CullMode::NONE;
|
||||
SetShader(&m_backgroundShader);
|
||||
|
||||
FontAtlasType fontAtlasType(static_cast<FontAtlasType::Type>(m_atlasData->meta.atlasType));
|
||||
if (!m_isBillboard)
|
||||
{
|
||||
m_textShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/text");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_textShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/billboard");
|
||||
DescriptorSetLayoutBinding billboardUniformBinding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
|
||||
billboardUniformBinding.stageFlags = ShaderProgramType::Type::VERTEX;
|
||||
m_textShader.AddDescriptorSetLayoutBinding(billboardUniformBinding, 4);
|
||||
}
|
||||
|
||||
DescriptorSetLayoutBinding textUniformBinding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
|
||||
textUniformBinding.stageFlags = ShaderProgramType::FRAGMENT;
|
||||
m_textShader.AddDescriptorSetLayoutBinding(textUniformBinding, 3);
|
||||
m_textShader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT,
|
||||
std::string(fontAtlasType.GetDefaultFragmentShader()));
|
||||
m_textShader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
|
||||
m_textShader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING, 2);
|
||||
m_textShader.alphaBlend = true;
|
||||
m_textShader.cullMode = CullMode::NONE;
|
||||
m_textShader.depthWrite = false;
|
||||
m_textShader.depthCompareOp = CompareOp::LESS_OR_EQUAL;
|
||||
}
|
||||
|
||||
void LabelDrawable::SetupBuffers()
|
||||
|
||||
@@ -46,8 +46,9 @@ namespace OpenVulkano::Scene
|
||||
class LabelDrawable final : public Drawable
|
||||
{
|
||||
public:
|
||||
LabelDrawable(const LabelDrawableSettings& settings = LabelDrawableSettings(), bool isBillboard = false);
|
||||
void AddText(TextDrawable* td, const std::string& text);
|
||||
LabelDrawable(const std::shared_ptr<AtlasData>& atlasData,
|
||||
const LabelDrawableSettings& settings = LabelDrawableSettings(), bool isBillboard = false);
|
||||
void AddText(const std::string& text, const TextConfig& config = TextConfig());
|
||||
void SetLabelSettings(const LabelDrawableSettings& settings);
|
||||
void SetBillboardSettings(const BillboardControlBlock& settings);
|
||||
void SetPosition(const Math::Vector3f& pos) { m_position = pos; }
|
||||
@@ -62,7 +63,6 @@ namespace OpenVulkano::Scene
|
||||
const Math::AABB& GetBoundingBox() const { return m_bbox; }
|
||||
private:
|
||||
void RecalculateBbox(const Math::AABB& other);
|
||||
Shader* GetBillboardTextShader(Shader* src);
|
||||
void SetupShaders();
|
||||
void SetupBuffers();
|
||||
private:
|
||||
@@ -73,8 +73,10 @@ namespace OpenVulkano::Scene
|
||||
std::vector<Shader> m_billboardTextShaders;
|
||||
// list over vector to prevent memory reallocation and crash
|
||||
std::list<TextDrawable> m_texts;
|
||||
Shader m_textShader;
|
||||
LabelDrawableSettings m_settings;
|
||||
LabelUniformData m_labelData;
|
||||
std::shared_ptr<AtlasData> m_atlasData;
|
||||
BillboardControlBlock m_billboardSettings;
|
||||
Math::Vector3f m_position = { 0, 0, 0 };
|
||||
Math::AABB m_bbox;
|
||||
|
||||
@@ -139,23 +139,11 @@ namespace OpenVulkano::Scene
|
||||
throw std::runtime_error("Cannot initialize text drawable with empty atlas data");
|
||||
}
|
||||
m_atlasData = atlasData;
|
||||
m_material.texture = &atlasData->texture;
|
||||
m_cfg = config;
|
||||
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
|
||||
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
|
||||
}
|
||||
|
||||
TextDrawable::TextDrawable(IFontAtlasGenerator* fontAtlasGenerator, const TextConfig& config)
|
||||
{
|
||||
if (!fontAtlasGenerator) { throw std::runtime_error("FontAtlasGenerator is nullptr"); }
|
||||
if (fontAtlasGenerator->GetGlyphsInfo().empty()) { throw std::runtime_error("Glyphs are not loaded"); }
|
||||
m_fontAtlasGenerator = fontAtlasGenerator;
|
||||
m_cfg = config;
|
||||
m_material.texture = const_cast<Texture*>(&m_fontAtlasGenerator->GetAtlas());
|
||||
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
|
||||
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
|
||||
}
|
||||
|
||||
void TextDrawable::GenerateText(const std::string& text, const Math::Vector3f& pos)
|
||||
{
|
||||
if (text.empty())
|
||||
@@ -180,26 +168,12 @@ namespace OpenVulkano::Scene
|
||||
size_t len = GetActualLength();
|
||||
m_geometry.Close();
|
||||
m_geometry.Init(len * 4, len * 6);
|
||||
|
||||
// TODO: better implementation to decide what to use: data from atlas generator or data read from file
|
||||
// we have msdf but loaded glyphs metadata from file before
|
||||
AtlasMetadata* meta;
|
||||
std::map<uint32_t, GlyphInfo>* symbols;
|
||||
if (m_fontAtlasGenerator)
|
||||
{
|
||||
// just in case if FontAtlasGenerator changed it's atlas
|
||||
m_material.texture = const_cast<Texture*>(&m_fontAtlasGenerator->GetAtlas());
|
||||
symbols = &m_fontAtlasGenerator->GetGlyphsInfo();
|
||||
meta = &m_fontAtlasGenerator->GetAtlasMetadata();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_material.texture = &m_atlasData->texture;
|
||||
symbols = &m_atlasData->glyphs;
|
||||
meta = &m_atlasData->meta;
|
||||
}
|
||||
|
||||
const Texture& atlasTex = *m_material.texture;
|
||||
double cursorX = pos.x;
|
||||
auto begin = text.begin();
|
||||
auto end = text.end();
|
||||
@@ -285,14 +259,4 @@ namespace OpenVulkano::Scene
|
||||
}
|
||||
m_atlasData = atlasData;
|
||||
}
|
||||
|
||||
void TextDrawable::SetFontAtlasGenerator(IFontAtlasGenerator* fontAtlasGenerator)
|
||||
{
|
||||
if (!fontAtlasGenerator || fontAtlasGenerator->GetGlyphsInfo().empty())
|
||||
{
|
||||
Logger::RENDER->error("FontAtlasGenerator is either nullptr or doesn't contain glyphs info");
|
||||
return;
|
||||
}
|
||||
m_fontAtlasGenerator = fontAtlasGenerator;
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
#include "Material.hpp"
|
||||
#include "Geometry.hpp"
|
||||
#include "UniformBuffer.hpp"
|
||||
#include "AtlasMetadata.hpp"
|
||||
#include "AtlasData.hpp"
|
||||
#include "Image/Image.hpp"
|
||||
#include <map>
|
||||
#include <optional>
|
||||
@@ -33,14 +33,6 @@ namespace OpenVulkano::Scene
|
||||
//bool sdfMultiChannel = false;
|
||||
};
|
||||
|
||||
struct AtlasData
|
||||
{
|
||||
std::map<uint32_t, GlyphInfo> glyphs;
|
||||
AtlasMetadata meta;
|
||||
std::unique_ptr<Image::Image> img;
|
||||
Texture texture;
|
||||
};
|
||||
|
||||
class TextDrawable : public SimpleDrawable
|
||||
{
|
||||
public:
|
||||
@@ -52,7 +44,6 @@ namespace OpenVulkano::Scene
|
||||
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::shared_ptr<AtlasData>& atlasData, const TextConfig& config = TextConfig());
|
||||
TextDrawable(IFontAtlasGenerator* fontAtlasGenerator, const TextConfig& config = TextConfig());
|
||||
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; }
|
||||
@@ -61,15 +52,12 @@ namespace OpenVulkano::Scene
|
||||
TextConfig& GetConfig() { return m_cfg; }
|
||||
Shader* GetShader() { return m_shader; }
|
||||
std::shared_ptr<AtlasData> GetAtlasData() { return m_atlasData; }
|
||||
void SetFontAtlasGenerator(IFontAtlasGenerator* fontAtlasGenerator);
|
||||
IFontAtlasGenerator* GetFontAtlasGenerator() { return m_fontAtlasGenerator; }
|
||||
private:
|
||||
Geometry m_geometry;
|
||||
Material m_material;
|
||||
UniformBuffer m_uniBuffer;
|
||||
std::shared_ptr<AtlasData> m_atlasData;
|
||||
Math::AABB m_bbox;
|
||||
IFontAtlasGenerator* m_fontAtlasGenerator = nullptr;
|
||||
Shader* m_shader = nullptr;
|
||||
TextConfig m_cfg;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user