Add scale for label

This commit is contained in:
Georg Hagen
2025-02-04 23:21:13 +01:00
parent 4a15c06c6f
commit 0d2bcbbdf6
5 changed files with 22 additions and 21 deletions

View File

@@ -84,9 +84,9 @@ namespace OpenVulkano::Scene
m_labelData.color = settings.backgroundColor; m_labelData.color = settings.backgroundColor;
m_labelData.hasRoundedCorners = settings.hasRoundedCorners; m_labelData.hasRoundedCorners = settings.hasRoundedCorners;
m_labelData.hasArrow = settings.hasArrow; m_labelData.hasArrow = settings.hasArrow;
m_labelData.cornerRadius = settings.cornerRadius * settings.cornerRadius; m_labelData.cornerRadius = settings.cornerRadius * settings.cornerRadius * settings.scale;
m_labelData.arrowLength = settings.arrowLength; m_labelData.arrowLength = settings.arrowLength * settings.scale;
m_labelData.arrowWidth = settings.arrowWidth; m_labelData.arrowWidth = settings.arrowWidth * settings.scale;
} }
void LabelDrawable::AddText(const std::string& text, const TextConfig& config) void LabelDrawable::AddText(const std::string& text, const TextConfig& config)
@@ -96,18 +96,18 @@ namespace OpenVulkano::Scene
TextDrawable& textDrawable = m_texts.emplace_back(m_atlasData, config); TextDrawable& textDrawable = m_texts.emplace_back(m_atlasData, config);
textDrawable.GetConfig().backgroundColor.a = 0; // do not render glyph's background textDrawable.GetConfig().backgroundColor.a = 0; // do not render glyph's background
double lineHeight = m_atlasData->GetLineHeight(); double lineHeight = m_atlasData->GetLineHeight();
textDrawable.GenerateText(text, m_position); textDrawable.GenerateText(text, m_position, m_settings.scale);
textDrawable.SetShader(GetTextShader(m_atlasData->GetAtlasType(), IsBillboard())); textDrawable.SetShader(GetTextShader(m_atlasData->GetAtlasType(), IsBillboard()));
m_bbox.Grow(textDrawable.GetBoundingBox()); m_bbox.Grow(textDrawable.GetBoundingBox());
// update position for next text entry // update position for next text entry
m_position.y = m_bbox.GetMin().y - lineHeight; m_position.y = m_bbox.GetMin().y - lineHeight * m_settings.scale;
Math::Vector2f padding = m_settings.padding * 2; Math::Vector2f padding = m_settings.padding * 2 * m_settings.scale;
if (m_settings.hasArrow) padding.y += m_settings.arrowLength; if (m_settings.hasArrow) padding.y += m_settings.arrowLength * m_settings.scale;
m_labelData.textSize = m_bbox.GetSize() + padding; m_labelData.textSize = m_bbox.GetSize() + padding;
m_labelData.bboxCenter = m_bbox.GetCenter(); m_labelData.bboxCenter = m_bbox.GetCenter();
if (m_settings.hasArrow) m_labelData.bboxCenter.y -= m_settings.arrowLength * 0.5f; if (m_settings.hasArrow) m_labelData.bboxCenter.y -= m_settings.arrowLength * 0.5f * m_settings.scale;
} }
std::optional<RayHit> LabelDrawable::Intersect(const Ray& ray) const std::optional<RayHit> LabelDrawable::Intersect(const Ray& ray) const

View File

@@ -23,6 +23,7 @@ namespace OpenVulkano::Scene
float cornerRadius = 0.05f; float cornerRadius = 0.05f;
float arrowLength = 0.5f; float arrowLength = 0.5f;
float arrowWidth = 0.2f; float arrowWidth = 0.2f;
float scale = 1.0f;
bool hasRoundedCorners = false; bool hasRoundedCorners = false;
bool hasArrow = false; bool hasArrow = false;
bool isBillboard = false; bool isBillboard = false;

View File

@@ -14,7 +14,7 @@
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
struct FontAtlas; class FontAtlas;
class IFontAtlasGenerator class IFontAtlasGenerator
{ {

View File

@@ -167,7 +167,7 @@ namespace OpenVulkano::Scene
return c == '\t' || c == '\n' || c == '\v'; return c == '\t' || c == '\n' || c == '\v';
} }
void TextDrawable::GenerateText(const std::string& text, const Math::Vector2f& pos) void TextDrawable::GenerateText(const std::string& text, const Math::Vector2f& pos, float scale)
{ {
if (text.empty()) return; if (text.empty()) return;
if (m_vertexBuffer.data) throw std::runtime_error("Text has already been initialized"); if (m_vertexBuffer.data) throw std::runtime_error("Text has already been initialized");
@@ -180,8 +180,7 @@ namespace OpenVulkano::Scene
TextGlyphVertex* vertices = m_vertexBuffer.Init<TextGlyphVertex>(len); TextGlyphVertex* vertices = m_vertexBuffer.Init<TextGlyphVertex>(len);
const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs(); const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs();
float cursorX = pos.x; Math::Vector2f cursor = pos / scale;
float cursorY = pos.y;
Math::Vector2f bmin(pos), bmax(pos); Math::Vector2f bmin(pos), bmax(pos);
float prevGlyphXBound = -INFINITY; float prevGlyphXBound = -INFINITY;
const float heightBetweenLines = GetHeightBetweenLines(text); const float heightBetweenLines = GetHeightBetweenLines(text);
@@ -191,7 +190,7 @@ namespace OpenVulkano::Scene
uint32_t c = utf8::next(begin, end); uint32_t c = utf8::next(begin, end);
if (IsSpecialCharacter(c)) if (IsSpecialCharacter(c))
{ {
HandleSpecialCharacter(symbols, c, heightBetweenLines, pos.x, cursorX, cursorY, prevGlyphXBound); HandleSpecialCharacter(symbols, c, heightBetweenLines, pos.x, cursor.x, cursor.y, prevGlyphXBound);
continue; continue;
} }
@@ -208,31 +207,32 @@ namespace OpenVulkano::Scene
const bool isBitmap = m_atlasData->GetAtlasType().IsBitmap(); const bool isBitmap = m_atlasData->GetAtlasType().IsBitmap();
if (prevGlyphXBound != -INFINITY && !isZeroLenGlyph && !isBitmap) if (prevGlyphXBound != -INFINITY && !isZeroLenGlyph && !isBitmap)
{ {
offset = prevGlyphXBound - (info.pos[0].x + cursorX); offset = prevGlyphXBound - (info.pos[0].x + cursor.x);
} }
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
vertices->position[i].x = info.pos[i].x + cursorX + offset; vertices->position[i].x = info.pos[i].x + cursor.x + offset;
vertices->uv[i] = info.uv[i]; vertices->uv[i] = info.uv[i];
if (i < 2) if (i < 2)
{ {
vertices->position[i].y = cursorY - info.pos[i].y; vertices->position[i].y = cursor.y - info.pos[i].y;
} }
else else
{ {
vertices->position[i].y = cursorY + info.pos[i].y; vertices->position[i].y = cursor.y + info.pos[i].y;
} }
vertices->color = m_cfg.textColor; vertices->color = m_cfg.textColor;
vertices->background = m_cfg.backgroundColor; vertices->background = m_cfg.backgroundColor;
vertices->position[i] *= scale;
} }
// somehow it's possible that cursorX can be less than prevGlyphXBound for sdf atlases // somehow it's possible that cursorX can be less than prevGlyphXBound for sdf atlases
// e.g. string `A, _` where space is not noticeable in the resulting output // e.g. string `A, _` where space is not noticeable in the resulting output
cursorX = std::max(cursorX, prevGlyphXBound); cursor.x = std::max(cursor.x, prevGlyphXBound);
// slight offset for bitmap atlas since it's tightly packed without any extra padding, while sdf has additional padding // slight offset for bitmap atlas since it's tightly packed without any extra padding, while sdf has additional padding
cursorX += info.advance + (isBitmap ? 0.05 : 0); cursor.x += info.advance + (isBitmap ? 0.05 : 0);
prevGlyphXBound = isZeroLenGlyph ? cursorX : vertices->position[2].x; prevGlyphXBound = isZeroLenGlyph ? cursor.x : vertices->position[2].x / scale;
if (!m_symbolCount) bmin.x = vertices->position[0].x; if (!m_symbolCount) bmin.x = vertices->position[0].x;
bmax.x = std::max(bmax.x, vertices->position[1].x); bmax.x = std::max(bmax.x, vertices->position[1].x);

View File

@@ -51,7 +51,7 @@ namespace OpenVulkano::Scene
TextDrawable(const Array<char>& atlasMetadata, const TextConfig& config = TextConfig()); TextDrawable(const Array<char>& atlasMetadata, const TextConfig& config = TextConfig());
TextDrawable(const std::shared_ptr<FontAtlas>& atlasData, const TextConfig& config = TextConfig()); TextDrawable(const std::shared_ptr<FontAtlas>& atlasData, const TextConfig& config = TextConfig());
void GenerateText(const std::string& text, const Math::Vector2f& pos = Math::Vector2f(0.f)); void GenerateText(const std::string& text, const Math::Vector2f& pos = Math::Vector2f(0.f), float scale = 1.0f);
void SetConfig(const TextConfig& cfg) { m_cfg = cfg; } void SetConfig(const TextConfig& cfg) { m_cfg = cfg; }
void SetAtlasData(const std::shared_ptr<FontAtlas>& atlasData); void SetAtlasData(const std::shared_ptr<FontAtlas>& atlasData);