rework API for text rendering

This commit is contained in:
ohyzha
2024-08-04 00:10:59 +03:00
parent dcf6e72f96
commit 232ad0a938
8 changed files with 122 additions and 129 deletions

View File

@@ -11,51 +11,51 @@
#include "Scene/UniformBuffer.hpp"
#include "Scene/FontAtlasGenerator.hpp"
#include "Base/Logger.hpp"
#include "utf8.h"
#include "fmt/core.h"
#include <utf8.h>
namespace OpenVulkano::Scene
{
using namespace msdfgen;
using namespace msdf_atlas;
TextDrawable::~TextDrawable()
{
delete m_mesh;
delete m_material;
Shader& TextDrawable::GetDefaultShader()
{
static bool once = true;
static Shader textDefaultShader;
if (once)
{
textDefaultShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/text");
textDefaultShader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/text");
textDefaultShader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
textDefaultShader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING);
textDefaultShader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING);
textDefaultShader.alphaBlend = true;
textDefaultShader.cullMode = CullMode::NONE;
once = false;
}
return textDefaultShader;
}
TextDrawable::TextDrawable(FontAtlasGenerator* fontAtlasGenerator, const TextConfig& config)
{
if (!fontAtlasGenerator) { throw std::runtime_error("FontAtlasGenerator is nullptr"); }
if (fontAtlasGenerator->GetAtlasInfo().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);
}
void TextDrawable::GenerateText(const std::string& text, const Math::Vector3f& pos)
{
if (!m_fontAtlasGenerator)
{
Logger::RENDER->error("Can't draw text. FontAtlasGenerator is nullptr");
return;
}
if (m_mesh)
{
delete m_mesh;
m_mesh = nullptr;
}
if (m_material)
{
delete m_material;
m_material = nullptr;
}
if (text.empty())
{
return;
}
std::map<unicode_t, GlyphInfo>& symbols = m_fontAtlasGenerator->GetAtlasInfo();
if (symbols.empty())
{
throw std::runtime_error("Glyphs are not loaded");
}
m_mesh = new Geometry();
m_material = new Material();
m_mesh->freeAfterUpload = false;
m_mesh->Init(text.size() * 4, text.size() * 6);
m_geometry.Close();
m_geometry.Init(text.size() * 4, text.size() * 6);
const Texture& atlasTex = *m_material.texture;
struct Bbox
{
@@ -89,34 +89,31 @@ namespace OpenVulkano::Scene
double ax = cursorX + bearingX;
double ay = pos.y - (h - bearingY);
const Texture& atlasTex = m_fontAtlasGenerator->GetAtlas();
m_material->texture = const_cast<Texture*>(&atlasTex);
m_mesh->vertices[vIdx].position.x = ax;
m_mesh->vertices[vIdx].position.y = ay;
m_mesh->vertices[vIdx].position.z = 1;
m_mesh->vertices[vIdx].textureCoordinates.x = l / atlasTex.resolution.x;
m_mesh->vertices[vIdx].textureCoordinates.y = b / atlasTex.resolution.y;
m_mesh->vertices[vIdx + 1].position.x = ax + w;
m_mesh->vertices[vIdx + 1].position.y = ay;
m_mesh->vertices[vIdx + 1].position.z = 1;
m_mesh->vertices[vIdx + 1].textureCoordinates.x = r / atlasTex.resolution.x;
m_mesh->vertices[vIdx + 1].textureCoordinates.y = b / atlasTex.resolution.y;
m_mesh->vertices[vIdx + 2].position.x = ax + w;
m_mesh->vertices[vIdx + 2].position.y = ay + h;
m_mesh->vertices[vIdx + 2].position.z = 1;
m_mesh->vertices[vIdx + 2].textureCoordinates.x = r / atlasTex.resolution.x;
m_mesh->vertices[vIdx + 2].textureCoordinates.y = t / atlasTex.resolution.y;
m_mesh->vertices[vIdx + 3].position.x = ax;
m_mesh->vertices[vIdx + 3].position.y = ay + h;
m_mesh->vertices[vIdx + 3].position.z = 1;
m_mesh->vertices[vIdx + 3].textureCoordinates.x = l / atlasTex.resolution.x;
m_mesh->vertices[vIdx + 3].textureCoordinates.y = t / atlasTex.resolution.y;
m_mesh->SetIndices(indices, 6, 6 * i);
m_geometry.vertices[vIdx].position.x = ax;
m_geometry.vertices[vIdx].position.y = ay;
m_geometry.vertices[vIdx].position.z = 1;
m_geometry.vertices[vIdx].textureCoordinates.x = l / atlasTex.resolution.x;
m_geometry.vertices[vIdx].textureCoordinates.y = b / atlasTex.resolution.y;
m_geometry.vertices[vIdx + 1].position.x = ax + w;
m_geometry.vertices[vIdx + 1].position.y = ay;
m_geometry.vertices[vIdx + 1].position.z = 1;
m_geometry.vertices[vIdx + 1].textureCoordinates.x = r / atlasTex.resolution.x;
m_geometry.vertices[vIdx + 1].textureCoordinates.y = b / atlasTex.resolution.y;
m_geometry.vertices[vIdx + 2].position.x = ax + w;
m_geometry.vertices[vIdx + 2].position.y = ay + h;
m_geometry.vertices[vIdx + 2].position.z = 1;
m_geometry.vertices[vIdx + 2].textureCoordinates.x = r / atlasTex.resolution.x;
m_geometry.vertices[vIdx + 2].textureCoordinates.y = t / atlasTex.resolution.y;
m_geometry.vertices[vIdx + 3].position.x = ax;
m_geometry.vertices[vIdx + 3].position.y = ay + h;
m_geometry.vertices[vIdx + 3].position.z = 1;
m_geometry.vertices[vIdx + 3].textureCoordinates.x = l / atlasTex.resolution.x;
m_geometry.vertices[vIdx + 3].textureCoordinates.y = t / atlasTex.resolution.y;
m_geometry.SetIndices(indices, 6, 6 * i);
// TODO: change to lower value(or ideally remove completely) to avoid overlapping and make less space between symbols
// when setting for depth comparison operator will be available( <= )
cursorX += info.glyphBox.advance + 0.08;
@@ -127,5 +124,6 @@ namespace OpenVulkano::Scene
Logger::RENDER->error("Could not find glyph for character {}", c);
}
}
SimpleDrawable::Init(m_shader, &m_geometry, &m_material, &m_uniBuffer);
}
}