implement text rendering without msdfgen library
This commit is contained in:
@@ -11,12 +11,16 @@
|
||||
#include "Scene/UniformBuffer.hpp"
|
||||
#include "Scene/FontAtlasGenerator.hpp"
|
||||
#include "Base/Logger.hpp"
|
||||
#include "Host/ResourceLoader.hpp"
|
||||
#include <fstream>
|
||||
#include <utf8.h>
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
#ifdef MSDFGEN_AVAILABLE
|
||||
using namespace msdfgen;
|
||||
using namespace msdf_atlas;
|
||||
#endif
|
||||
|
||||
Shader& TextDrawable::GetDefaultShader()
|
||||
{
|
||||
@@ -36,6 +40,42 @@ namespace OpenVulkano::Scene
|
||||
return textDefaultShader;
|
||||
}
|
||||
|
||||
std::map<uint32_t, GlyphInfo> TextDrawable::ReadAtlasMetadataFile(const std::string& atlasMetadataFile)
|
||||
{
|
||||
OpenVulkano::Array<char> data = OpenVulkano::Utils::ReadFile(atlasMetadataFile);
|
||||
return ReadAtlasMetadata(data);
|
||||
}
|
||||
|
||||
std::map<uint32_t, GlyphInfo> TextDrawable::ReadAtlasMetadata(const Array<char>& atlasMetadata)
|
||||
{
|
||||
size_t len = atlasMetadata.Size();
|
||||
size_t read_bytes = 0;
|
||||
uint32_t unicode = 0;
|
||||
GlyphInfo info;
|
||||
std::map<uint32_t, GlyphInfo> glyphs;
|
||||
while (read_bytes < len)
|
||||
{
|
||||
std::memcpy(&unicode, atlasMetadata.Data() + read_bytes, sizeof(uint32_t));
|
||||
read_bytes += sizeof(uint32_t);
|
||||
std::memcpy(&info, atlasMetadata.Data() + read_bytes, sizeof(GlyphInfo));
|
||||
read_bytes += sizeof(GlyphInfo);
|
||||
glyphs[unicode] = std::move(info);
|
||||
}
|
||||
return glyphs;
|
||||
}
|
||||
|
||||
TextDrawable::TextDrawable(const std::map<uint32_t, GlyphInfo>& glyphData, Texture* atlasTex,
|
||||
const TextConfig& config)
|
||||
{
|
||||
if (!atlasTex) { throw std::runtime_error("Atlas texture is nullptr"); }
|
||||
if (glyphData.empty()) { throw std::runtime_error("Glyphs are not loaded"); }
|
||||
m_material.texture = atlasTex;
|
||||
m_glyphs = glyphData;
|
||||
m_cfg = config;
|
||||
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
|
||||
}
|
||||
|
||||
#ifdef MSDFGEN_AVAILABLE
|
||||
TextDrawable::TextDrawable(FontAtlasGenerator* fontAtlasGenerator, const TextConfig& config)
|
||||
{
|
||||
if (!fontAtlasGenerator) { throw std::runtime_error("FontAtlasGenerator is nullptr"); }
|
||||
@@ -45,80 +85,77 @@ namespace OpenVulkano::Scene
|
||||
m_material.texture = const_cast<Texture*>(&m_fontAtlasGenerator->GetAtlas());
|
||||
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
|
||||
}
|
||||
#endif
|
||||
|
||||
void TextDrawable::GenerateText(const std::string& text, const Math::Vector3f& pos)
|
||||
void TextDrawable::GenerateText(const std::string& text, const Math::Vector3f& pos)
|
||||
{
|
||||
if (text.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
// just in case if FontAtlasGenerator changed it's atlas
|
||||
m_material.texture = const_cast<Texture*>(&m_fontAtlasGenerator->GetAtlas());
|
||||
std::map<unicode_t, GlyphInfo>& symbols = m_fontAtlasGenerator->GetAtlasInfo();
|
||||
|
||||
m_geometry.Close();
|
||||
m_geometry.Init(text.size() * 4, text.size() * 6);
|
||||
const Texture& atlasTex = *m_material.texture;
|
||||
|
||||
struct Bbox
|
||||
#ifdef MSDFGEN_AVAILABLE
|
||||
// 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
|
||||
std::optional<std::reference_wrapper<std::map<unicode_t, GlyphInfo>>> glyphData;
|
||||
if (m_fontAtlasGenerator->GetAtlasInfo().empty() && !m_glyphs.empty())
|
||||
{
|
||||
double l = 0, r = 0, t = 0, b = 0;
|
||||
};
|
||||
|
||||
// texture is set in ctor
|
||||
glyphData = m_glyphs;
|
||||
}
|
||||
else
|
||||
{
|
||||
// just in case if FontAtlasGenerator changed it's atlas
|
||||
m_material.texture = const_cast<Texture*>(&m_fontAtlasGenerator->GetAtlas());
|
||||
glyphData = m_fontAtlasGenerator->GetAtlasInfo();
|
||||
}
|
||||
std::map<unicode_t, GlyphInfo>& symbols = glyphData.value();
|
||||
#else
|
||||
std::map<unicode_t, GlyphInfo>& symbols = m_glyphs;
|
||||
#endif
|
||||
const Texture& atlasTex = *m_material.texture;
|
||||
double cursorX = pos.x;
|
||||
auto begin = text.begin();
|
||||
auto end = text.end();
|
||||
for (size_t i = 0; begin != end; i++)
|
||||
{
|
||||
unicode_t c = utf8::next(begin, end);
|
||||
uint32_t c = utf8::next(begin, end);
|
||||
if (symbols.find(c) != symbols.end())
|
||||
{
|
||||
Bbox glyphBaselineBbox, glyphAtlasBbox;
|
||||
uint32_t vIdx = i * 4;
|
||||
uint32_t indices[] = { 1 + vIdx, 2 + vIdx, 3 + vIdx, 1 + vIdx, 3 + vIdx, 0 + vIdx };
|
||||
GlyphInfo& info = symbols.at(c);
|
||||
info.geometry.getQuadPlaneBounds(glyphBaselineBbox.l, glyphBaselineBbox.b, glyphBaselineBbox.r,
|
||||
glyphBaselineBbox.t);
|
||||
info.geometry.getQuadAtlasBounds(glyphAtlasBbox.l, glyphAtlasBbox.b, glyphAtlasBbox.r,
|
||||
glyphAtlasBbox.t);
|
||||
double bearingX = info.glyphBox.bounds.l;
|
||||
double bearingY = info.glyphBox.bounds.t;
|
||||
double w = glyphBaselineBbox.r - glyphBaselineBbox.l;
|
||||
double h = glyphBaselineBbox.t - glyphBaselineBbox.b;
|
||||
double l = glyphAtlasBbox.l;
|
||||
double r = glyphAtlasBbox.r;
|
||||
double t = glyphAtlasBbox.t;
|
||||
double b = glyphAtlasBbox.b;
|
||||
double ax = cursorX + bearingX;
|
||||
double ay = pos.y - (h - bearingY);
|
||||
|
||||
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;
|
||||
|
||||
// left bottom
|
||||
m_geometry.vertices[vIdx].position.x = info.xyz[0].x + cursorX;
|
||||
m_geometry.vertices[vIdx].position.y = pos.y - info.xyz[0].y;
|
||||
m_geometry.vertices[vIdx].position.z = info.xyz[0].z;
|
||||
m_geometry.vertices[vIdx].textureCoordinates = Math::Vector3f(info.uv[0], 0);
|
||||
|
||||
// right bottom
|
||||
m_geometry.vertices[vIdx + 1].position.x = info.xyz[1].x + cursorX;
|
||||
m_geometry.vertices[vIdx + 1].position.y = pos.y - info.xyz[1].y;
|
||||
m_geometry.vertices[vIdx + 1].position.z = info.xyz[1].z;
|
||||
m_geometry.vertices[vIdx + 1].textureCoordinates = Math::Vector3f(info.uv[1], 0);
|
||||
|
||||
// top right
|
||||
m_geometry.vertices[vIdx + 2].position.x = info.xyz[2].x + cursorX;
|
||||
m_geometry.vertices[vIdx + 2].position.y = pos.y + info.xyz[2].y;
|
||||
m_geometry.vertices[vIdx + 2].position.z = info.xyz[2].z;
|
||||
m_geometry.vertices[vIdx + 2].textureCoordinates = Math::Vector3f(info.uv[2], 0);
|
||||
|
||||
// top left
|
||||
m_geometry.vertices[vIdx + 3].position.x = info.xyz[3].x + cursorX;
|
||||
m_geometry.vertices[vIdx + 3].position.y = pos.y + info.xyz[3].y;
|
||||
m_geometry.vertices[vIdx + 3].position.z = info.xyz[3].z;
|
||||
m_geometry.vertices[vIdx + 3].textureCoordinates = Math::Vector3f(info.uv[3], 0);
|
||||
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;
|
||||
cursorX += info.advance + 0.08;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user