Text Rendering

This commit is contained in:
Isuru Samarathunga
2025-10-20 23:39:54 +05:30
parent d4e93b047c
commit b10aacaee7
40 changed files with 1295 additions and 297 deletions

View File

@ -19,6 +19,7 @@
#include <Renderer/EmbeddedShader.hpp>
#include <Renderer/Renderer.hpp>
#include <FontManager.hpp>
#include <ResourceManager.hpp>
#include <WorldManager.hpp>
@ -45,6 +46,7 @@ namespace ia::iae
Pipeline *Renderer::s_postprocessPipeline{};
Renderer::Geometry *Renderer::s_quadGeometry{};
Renderer::Geometry *Renderer::s_circleGeometry{};
Vector<Renderer::DrawEntry> Renderer::s_drawEntries;
@ -123,6 +125,125 @@ namespace ia::iae
{glm::vec3{0, 0, 0}, glm::vec2{0, 0}, glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}},
},
{0, 1, 2, 2, 3, 0});
s_circleGeometry = CreateGeometry(
{
{{0.0000000f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.0000000f},
{0.5000000f, 0.5000000f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 0 Center
{{1.0000000f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.0000000f},
{1.0000000f, 0.5000000f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 1
{{0.9848078f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.1736482f},
{0.9924039f, 0.5868241f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 2
{{0.9396926f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.3420201f},
{0.9698463f, 0.6710101f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 3
{{0.8660254f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.5000000f},
{0.9330127f, 0.7500000f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 4
{{0.7660444f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.6427876f},
{0.8830222f, 0.8213938f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 5
{{0.6427876f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.7660444f},
{0.8213938f, 0.8830222f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 6
{{0.5000000f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.8660254f},
{0.7500000f, 0.9330127f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 7
{{0.3420201f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.9396926f},
{0.6710101f, 0.9698463f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 8
{{0.1736482f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.9848078f},
{0.5868241f, 0.9924039f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 9
{{0.0000000f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 1.0000000f},
{0.5000000f, 1.0000000f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 10
{{-0.1736482f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.9848078f},
{0.4131759f, 0.9924039f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 11
{{-0.3420201f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.9396926f},
{0.3289899f, 0.9698463f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 12
{{-0.5000000f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.8660254f},
{0.2500000f, 0.9330127f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 13
{{-0.6427876f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.7660444f},
{0.1786062f, 0.8830222f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 14
{{-0.7660444f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.6427876f},
{0.1169778f, 0.8213938f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 15
{{-0.8660254f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.5000000f},
{0.0669873f, 0.7500000f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 16
{{-0.9396926f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.3420201f},
{0.0301537f, 0.6710101f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 17
{{-0.9848078f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.1736482f},
{0.0075961f, 0.5868241f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 18
{{-1.0000000f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * 0.0000000f},
{0.0000000f, 0.5000000f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 19
{{-0.9848078f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.1736482f},
{0.0075961f, 0.4131759f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 20
{{-0.9396926f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.3420201f},
{0.0301537f, 0.3289899f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 21
{{-0.8660254f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.5000000f},
{0.0669873f, 0.2500000f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 22
{{-0.7660444f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.6427876f},
{0.1169778f, 0.1786062f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 23
{{-0.6427876f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.7660444f},
{0.1786062f, 0.1169778f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 24
{{-0.5000000f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.8660254f},
{0.2500000f, 0.0669873f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 25
{{-0.3420201f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.9396926f},
{0.3289899f, 0.0301537f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 26
{{-0.1736482f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.9848078f},
{0.4131759f, 0.0075961f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 27
{{-0.0000000f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -1.0000000f},
{0.5000000f, 0.0000000f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 28
{{0.1736482f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.9848078f},
{0.5868241f, 0.0075961f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 29
{{0.3420201f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.9396926f},
{0.6710101f, 0.0301537f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 30
{{0.5000000f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.8660254f},
{0.7500000f, 0.0669873f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 31
{{0.6427876f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.7660444f},
{0.8213938f, 0.1169778f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 32
{{0.7660444f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.6427876f},
{0.8830222f, 0.1786062f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 33
{{0.8660254f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.5000000f},
{0.9330127f, 0.2500000f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 34
{{0.9396926f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.3420201f},
{0.9698463f, 0.3289899f},
{1.0f, 1.0f, 1.0f, 1.0f}}, // 35
{{0.9848078f, (1.0f / Engine::GetDisplayAspectRatio()) * 0.75f * -0.1736482f},
{0.9924039f, 0.4131759f},
{1.0f, 1.0f, 1.0f, 1.0f}} // 36
},
{0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 7, 0, 7, 8, 0, 8, 9, 0, 9, 10,
0, 10, 11, 0, 11, 12, 0, 12, 13, 0, 13, 14, 0, 14, 15, 0, 15, 16, 0, 16, 17, 0, 17, 18, 0, 18, 19,
0, 19, 20, 0, 20, 21, 0, 21, 22, 0, 22, 23, 0, 23, 24, 0, 24, 25, 0, 25, 26, 0, 26, 27, 0, 27, 28,
0, 28, 29, 0, 29, 30, 0, 30, 31, 0, 31, 32, 0, 32, 33, 0, 33, 34, 0, 34, 35, 0, 35, 36, 0, 36, 1});
}
VOID Renderer::Terminate()
@ -130,6 +251,7 @@ namespace ia::iae
SDL_WaitForGPUIdle(s_gpuDevice);
DestroyGeometry(s_quadGeometry);
DestroyGeometry(s_circleGeometry);
DebugDraw::Terminate();
@ -321,6 +443,36 @@ namespace ia::iae
DrawEntry{.Layer = layer, .SortIndex = t, .DrawState = s_state, .GeometryHandle = handle});
}
VOID Renderer::DrawText(IN CONST String &text, IN Vec2 position, IN FLOAT32 scale, IN FLOAT32 rotation,
IN UINT8 layer, IN UINT16 sortIndex)
{
const auto &font = FontManager::GetFont("Roboto");
const auto t = (UINT16) (sortIndex + ((UINT16) s_state.PositionY * s_state.YSortingEnabled));
Vec3 p{};
Vec3 s{};
for (const auto &c : text)
{
if (!c)
break;
const auto glyph = font.Chars[(UINT8) c];
p = Vec3(position.x, position.y, 0.0f) +
Vec3(glyph.Bearing.x, glyph.Size.y - glyph.Bearing.y, 0.0f) * scale;
p.y += (16 - glyph.Size.y) * scale;
s = Vec3(scale * glyph.Size.x, scale * glyph.Size.y, 1.0f);
s_state.ActiveTexture = glyph.Texture;
s_state.PositionY = position.y;
s_state.ModelMatrix = glm::translate(glm::mat4(1.0f), p);
s_state.ModelMatrix = glm::rotate(s_state.ModelMatrix, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
s_state.ModelMatrix = glm::scale(s_state.ModelMatrix, s);
s_drawEntries.pushBack(
DrawEntry{.Layer = layer, .SortIndex = t, .DrawState = s_state, .GeometryHandle = s_quadGeometry});
position.x += glyph.Advance * scale;
}
}
VOID Renderer::WaitForGPUIdle()
{
SDL_WaitForGPUIdle(s_gpuDevice);
@ -334,6 +486,11 @@ namespace ia::iae
return IVec2(Renderer::s_screenWidth, Renderer::s_screenHeight);
}
FLOAT32 Engine::GetDisplayAspectRatio()
{
return (FLOAT32) Renderer::s_screenHeight / (FLOAT32) Renderer::s_screenWidth;
}
VOID Engine::SetActiveCamera(IN CameraComponent *cameraComponent)
{
Renderer::s_activeCamera = cameraComponent;
@ -365,6 +522,12 @@ namespace ia::iae
Renderer::DrawGeometry((Renderer::Geometry *) handle, layer, sortIndex);
}
VOID Engine::DrawText(IN CONST String &text, IN Vec2 position, IN FLOAT32 scale, IN FLOAT32 rotation,
IN UINT8 layer, IN UINT16 sortIndex)
{
Renderer::DrawText(text, position, scale, rotation, layer, sortIndex);
}
VOID Engine::SetRenderState_Scissor(IN IVec4 rect)
{
Renderer::s_scissor = rect.z ? SDL_Rect{0, 0, Renderer::s_screenWidth, Renderer::s_screenHeight}
@ -431,4 +594,9 @@ namespace ia::iae
{
return (Handle) Renderer::s_quadGeometry;
}
Handle Engine::GetGeometry_Circle()
{
return (Handle) Renderer::s_circleGeometry;
}
} // namespace ia::iae