support msdf

This commit is contained in:
ohyzha
2024-08-13 10:35:46 +03:00
parent f975581390
commit 1f418fa673
13 changed files with 327 additions and 163 deletions

View File

@@ -24,7 +24,7 @@
#include "Controller/FreeCamCameraController.hpp"
#include "Image/ImageLoaderPng.hpp"
#include "Scene/FontAtlasGenerator.hpp"
#include "Scene/MsdfFontAtlasGenerator.hpp"
#include "Scene/IFontAtlasGenerator.hpp"
#include <filesystem>
#ifdef _WIN32
@@ -57,7 +57,7 @@ namespace OpenVulkano
texts.push_back(std::make_pair("ABab?.^{}_cdFGETG123)(", TextConfig()));
texts.push_back(std::make_pair("Hello, World!", TextConfig()));
texts.push_back(std::make_pair("\u0410\u0411\u0412\u041F", TextConfig()));
texts.push_back(std::make_pair("Text with unsupported glyphs \u1E30\u1E31 is coming", TextConfig()));
texts.push_back(std::make_pair("Unsupported glyphs \u1E30\u1E31 are coming", TextConfig()));
texts.push_back(std::make_pair("This is first line\nSecond gg line\nThird G line", TextConfig()));
texts[0].second.applyBorder = true;
texts[1].second.backgroundColor.a = 1;
@@ -65,27 +65,44 @@ namespace OpenVulkano
const int N = texts.size();
auto& resourceLoader = ResourceLoader::GetInstance();
const std::string fontPath = resourceLoader.GetResourcePath("Roboto-Regular.ttf");
m_nodesPool.resize(N);
m_drawablesPool.resize(N);
m_nodesPool.resize(N * 2);
m_drawablesPool.resize(N * 2);
#ifdef MSDFGEN_AVAILABLE
msdf_atlas::Charset charset = MsdfFontAtlasGenerator::LoadAllGlyphs(fontPath);
//msdf_atlas::Charset charset = msdf_atlas::Charset::ASCII;
//for (msdf_atlas::unicode_t c = 0x0410; c <= 0x041F; c++)
//{
// // some unicode values
// charset.add(c);
//}
msdf_atlas::Charset charset = SdfFontAtlasGenerator::LoadAllGlyphs(fontPath);
m_atlasGenerator.GenerateAtlas(fontPath, charset);
m_msdfAtlasGenerator.GenerateAtlas(fontPath, charset);
#else
auto sdfMetadataInfo = resourceLoader.GetResource("sdf_atlas_packed");
auto msdfMetadataInfo = resourceLoader.GetResource("msdf_atlas_packed.png");
#endif
for (int i = 0; i < texts.size(); i++)
for (int i = 0; i < texts.size() * 2; i++)
{
int textIdx = i % texts.size();
TextDrawable* t = nullptr;
#ifdef MSDFGEN_AVAILABLE
TextDrawable* t = new TextDrawable(&m_atlasGenerator, texts[i].second);
if (i < texts.size())
{
t = new TextDrawable(&m_atlasGenerator, texts[textIdx].second);
t->SetShader(&TextDrawable::GetSdfDefaultShader());
}
else
{
t = new TextDrawable(&m_msdfAtlasGenerator, texts[textIdx].second);
t->SetShader(&TextDrawable::GetMsdfDefaultShader());
}
#else
auto metadataInfo = resourceLoader.GetResource("full_atlas_metadata_packed");
TextDrawable* t = new TextDrawable(metadataInfo, texts[i].second);
if (i < texts.size())
{
t = new TextDrawable(sdfMetadataInfo, texts[textIdx].second);
t->SetShader(&TextDrawable::GetSdfDefaultShader());
}
else
{
t = new TextDrawable(msdfMetadataInfo, texts[textIdx].second);
t->SetShader(&TextDrawable::GetMsdfDefaultShader());
}
// OR use separate texture + metadata file
//auto metadataInfo = resourceLoader.GetResource("atlas_metadata");
//auto data = resourceLoader.GetResource("roboto-regular-atlas.png");
@@ -98,17 +115,17 @@ namespace OpenVulkano
//tex.size = image->data.Size(); // 1 channel
//TextDrawable* t = new TextDrawable(metadataInfo, &tex, texts[i].second);
#endif // MSDFGEN_AVAILABLE
t->GenerateText(texts[i].first);
t->GenerateText(texts[textIdx].first);
m_drawablesPool[i] = t;
m_nodesPool[i].Init();
m_nodesPool[i].SetMatrix(Math::Utils::translate(glm::mat4x4(1.f), Vector3f(-5, 2 - i * 2, 0)));
m_nodesPool[i].SetMatrix(Math::Utils::translate(glm::mat4x4(1.f), Vector3f((i < texts.size() ? -5 : 15), 2 - textIdx * 2, 0)));
m_nodesPool[i].AddDrawable(m_drawablesPool[i]);
m_scene.GetRoot()->AddChild(&m_nodesPool[i]);
}
GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene);
m_camController.Init(&m_cam);
m_camController.SetDefaultKeybindings();
m_camController.SetPosition({ 0, 0, 10 });
m_camController.SetPosition({ 10, 0, 15 });
m_camController.SetBoostFactor(5);
std::shared_ptr<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo =
@@ -126,6 +143,7 @@ namespace OpenVulkano
{
for (SimpleDrawable* d: m_drawablesPool)
{
d->Close();
delete d;
}
}
@@ -135,7 +153,8 @@ namespace OpenVulkano
PerspectiveCamera m_cam;
OpenVulkano::FreeCamCameraController m_camController;
#ifdef MSDFGEN_AVAILABLE
MsdfFontAtlasGenerator m_atlasGenerator;
SdfFontAtlasGenerator m_atlasGenerator;
MsdfFontAtlasGenerator m_msdfAtlasGenerator;
#endif
std::vector<SimpleDrawable*> m_drawablesPool;
std::vector<Node> m_nodesPool;

Binary file not shown.

After

Width:  |  Height:  |  Size: 1012 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 163 KiB

View File

Before

Width:  |  Height:  |  Size: 449 KiB

After

Width:  |  Height:  |  Size: 449 KiB