working version of text rendering

This commit is contained in:
ohyzha
2024-08-02 09:52:26 +03:00
parent 9b58ba5f55
commit e69a553b18
13 changed files with 491 additions and 102 deletions

View File

@@ -22,6 +22,7 @@
#include "Base/EngineConfiguration.hpp"
#include "Controller/FreeCamCameraController.hpp"
#include "Image/ImageLoaderPng.hpp"
#include "Scene/FontAtlasGenerator.hpp"
#ifdef _WIN32
#undef TRANSPARENT
@@ -49,19 +50,13 @@ namespace OpenVulkano
m_scene.SetCamera(&m_cam);
m_cfg.applyBorder = true;
//m_cfg.smoothing = 1 / 16.f;
const std::string symbols = "Ak?";
const int N = symbols.size();
const std::string font = (OpenVulkano::Utils::GetFontsDirectory() / "arial.ttf").string();
Image::ImageLoaderPng pngLoader;
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);
m_materials.resize(N);
m_geos.resize(N);
m_nodesPool.resize(N);
m_drawablesPool.resize(N);
m_textures.resize(N);
m_sdfs.resize(N);
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/text");
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/text");
@@ -71,59 +66,27 @@ namespace OpenVulkano
m_shader.alphaBlend = true;
m_shader.cullMode = CullMode::NONE;
static float vertices[] = {
// positions // texture coords
-0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 1.0f, 1.0f
};
uint32_t indices[] = { 1, 2, 3, 1, 3, 0 };
m_atlasGenerator.GenerateAtlas(font, (OpenVulkano::Utils::GetBuildDirectory() / "atlas.png").string());
Text text = Text(m_cfg);
for (int i = 0; i < N; i++)
for (int i = 0; i < texts.size(); i++)
{
m_drawablesPool[i].SetDrawPhase(OpenVulkano::Scene::DrawPhase::BACKGROUND);
const std::string fileName = std::string("output") + std::to_string(i + 1) + ".png";
const std::string pngOutput = (OpenVulkano::Utils::GetBuildDirectory() / fileName).string();
auto& drawable = m_drawablesPool[i];
auto& mat = m_materials[i];
auto& tex = m_textures[i];
auto& geo = m_geos[i];
drawable.Init(&m_shader, &geo, &mat, &m_uniBuffer);
text.Init(font, symbols[i], pngOutput);
m_sdfs[i] = pngLoader.loadFromFile(pngOutput);
auto& sdf = m_sdfs[i];
tex.resolution = sdf->resolution;
tex.textureBuffer = sdf->data.Data();
tex.format = sdf->dataFormat;
tex.size = sdf->data.Size();
mat.texture = &tex;
geo.Init(4, 6);
for (int j = 0; j < geo.vertexCount; j++)
{
geo.vertices[j].position.x = vertices[j * 4];
geo.vertices[j].position.y = vertices[j * 4 + 1];
geo.vertices[j].position.z = 0.f;
geo.vertices[j].textureCoordinates.x = vertices[j * 4 + 2];
geo.vertices[j].textureCoordinates.y = vertices[j * 4 + 3];
}
geo.SetIndices(indices, 6);
Text* t = new Text();
t->SetFontAtlasGenerator(&m_atlasGenerator);
t->SetConfig(m_cfg);
t->SetUniformBuffer(&m_uniBuffer);
t->SetShader(&m_shader);
t->GenerateText(texts[i]);
m_drawablesPool[i] = t;
m_nodesPool[i].Init();
m_nodesPool[i].SetMatrix(Math::Utils::translate(
glm::mat4x4(1.f), Vector3f(-3 + std::rand() % 3, -2 + std::rand() % 2, 2)));
m_nodesPool[i].AddDrawable(&drawable);
m_nodesPool[i].SetMatrix(
Math::Utils::translate(glm::mat4x4(1.f), Vector3f(-3 + std::rand() % 3, -2 + std::rand() % 4, 2)));
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({ -2, -1, 5 });
m_camController.SetPosition({ 0, 0, 5 });
m_camController.SetBoostFactor(5);
std::shared_ptr<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo =
@@ -139,6 +102,10 @@ namespace OpenVulkano
void Close() override
{
for (SimpleDrawable* d: m_drawablesPool)
{
delete d;
}
}
private:
@@ -148,12 +115,9 @@ namespace OpenVulkano
OpenVulkano::FreeCamCameraController m_camController;
Shader m_shader;
TextConfig m_cfg;
std::vector<Material> m_materials;
std::vector<SimpleDrawable> m_drawablesPool;
std::vector<Geometry> m_geos;
FontAtlasGenerator m_atlasGenerator;
std::vector<SimpleDrawable*> m_drawablesPool;
std::vector<Node> m_nodesPool;
std::vector<Texture> m_textures;
std::vector<std::unique_ptr<Image::Image>> m_sdfs;
Vector3f_SIMD m_position = { 0, 0, -10 };
OpenVulkano::Scene::UI::SimpleUi m_ui;
std::shared_ptr<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo;