Skip to content

Commit 94b7d55

Browse files
Add FreeType-based font and text rendering support
Integrates FreeType as a submodule and adds Roboto-Regular.ttf as a default font asset. Introduces GfxFont and GfxTextRenderer classes for font loading and text rendering, including glyph texture generation and per-character rendering logic. Updates build scripts and headers to include FreeType, and adds necessary shader/material handling for text rendering.
1 parent c1b0c0f commit 94b7d55

File tree

9 files changed

+343
-6
lines changed

9 files changed

+343
-6
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
[submodule "ext/FastNoiseLite"]
1414
path = ext/FastNoiseLite
1515
url = https://github.com/OutputGames/FastNoiseLite.git
16+
[submodule "ext/freetype"]
17+
path = ext/freetype
18+
url = https://gitlab.freedesktop.org/freetype/freetype.git

default_assets/Roboto-Regular.ttf

143 KB
Binary file not shown.

include/borealis/gfx/buffer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ namespace brl
107107
bool setupAttributesForInstancing = false;
108108
GfxBuffer* instanceBuffer;
109109

110-
}; // <-- Add a semicolon here to terminate the struct definition
110+
};
111111

112112

113113
} // namespace brl

include/borealis/gfx/shader.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <memory>
99
#include <vector>
1010

11+
#include "ui.hpp"
12+
1113
namespace brl
1214
{
1315
struct GfxShaderProgram;
@@ -309,6 +311,18 @@ namespace brl
309311
setOverride(override);
310312
}
311313

314+
void setTexture(std::string name, const GfxTexture* value)
315+
{
316+
if (!shader->getUniform(name))
317+
return;
318+
319+
auto val = std::make_shared<GfxShaderValue>();
320+
//val->txValue = const_cast<GfxTexture*>(value);
321+
322+
auto override = GfxShaderBinding(shader->getUniform(name), val);
323+
setOverride(override);
324+
}
325+
312326
GfxShaderValue* getUniform(std::string name)
313327
{
314328
if (!shader->getUniform(name))
@@ -334,8 +348,6 @@ namespace brl
334348
void drawInstanced(std::vector<glm::mat4> transforms, GfxAttribBuffer* gfxBuffer,
335349
GfxUniformList runtimeOverrides = {});
336350

337-
338-
339351
private:
340352
friend struct GfxMaterialMgr;
341353
GfxShaderProgram* shader;

include/borealis/gfx/texture.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ namespace brl
1515
~GfxTexture();
1616

1717

18-
private:
18+
protected:
1919
friend struct GfxMaterial;
2020
friend struct GfxShader;
2121
friend struct GfxSprite;
2222
friend struct GfxTexture2d;
2323
friend struct GfxTexture2dArray;
2424
friend struct GfxFramebuffer;
25+
friend struct GfxTextRenderer;
26+
friend struct TextMaterialKey;
2527

2628
unsigned id = UINT_MAX;
2729
int width, height;

include/borealis/gfx/ui.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define UI_HPP
33

44

5+
#include "buffer.hpp"
6+
#include "ui.hpp"
57
#include "borealis/ecs/entity.hpp"
68
#include "borealis/gfx/camera.hpp"
79
#include "borealis/gfx/shader.hpp"
@@ -37,6 +39,7 @@ namespace brl
3739
private:
3840
friend struct GfxImage;
3941
friend struct GfxUIElement;
42+
friend struct GfxTextRenderer;
4043
friend GfxEngine;
4144

4245
static GfxCanvas* mainCanvas;
@@ -122,6 +125,53 @@ namespace brl
122125
GfxMaterial* material;
123126
};
124127

128+
struct GfxFont
129+
{
130+
struct GfxFontCharacter : GfxTexture
131+
{
132+
GfxFontCharacter(unsigned int id, glm::ivec2 s, glm::ivec2 b, unsigned int adv);
133+
glm::ivec2 size; // Size of glyph
134+
glm::ivec2 bearing; // Offset from baseline to left/top of glyph
135+
unsigned int advanceOffset; // Offset to advance to next glyph
136+
};
137+
138+
std::map<char, GfxFontCharacter> characters;
139+
int fontSize = 48;
140+
141+
GfxFont(std::string path);
142+
143+
};
144+
145+
struct GfxTextRenderer : GfxUIElement
146+
{
147+
GfxTextRenderer(GfxCanvas* c);
148+
149+
GfxFont* font = nullptr;
150+
151+
std::string text;
152+
153+
void start() override;
154+
void lateUpdate() override;
155+
156+
private:
157+
GfxAttribBuffer* charVAO = nullptr;
158+
GfxBuffer* charVBO = nullptr;
159+
160+
// Material pool for text rendering with different colors
161+
struct TextMaterialKey
162+
{
163+
glm::vec3 color;
164+
GfxFont::GfxFontCharacter textureID;
165+
166+
bool operator<(const TextMaterialKey& other) const;
167+
};
168+
std::map<TextMaterialKey, GfxMaterial*> materialCache;
169+
// Helper to get or create text material with specific color and texture
170+
GfxMaterial* getMaterial(glm::vec3 color, const GfxFont::GfxFontCharacter& textureID);
171+
// Helper to create quad geometry for a single character
172+
void createCharacterQuad(const GfxFont::GfxFontCharacter& ch, glm::vec2 position, float scale, float vertices[6][4]);
173+
};
174+
125175
} // namespace brl
126176

127177

projects.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ project "borealis"
88
cppdialect "C++23"
99
targetdir "lib"
1010
objdir "build"
11-
dependson {"glad", "tinygltf", "resource_packer", "glfw"}
11+
dependson {"glad", "tinygltf", "resource_packer", "glfw", "freetype"}
1212

1313
files {
1414
"src/**.cpp",
@@ -31,6 +31,7 @@ project "borealis"
3131
"ext/glfw/include",
3232
"ext/assimp/include",
3333
"ext/tinygltf",
34+
"ext/freetype/include",
3435
"ext"
3536
}
3637

@@ -95,3 +96,4 @@ include("ext/glad/premake5.lua")
9596
include("ext/glfw/premake5.lua")
9697
include("ext/stb/premake5.lua")
9798
include("ext/glm/premake5.lua")
99+
include("ext/freetype/premake5.lua")

src/brl/gfx/engine.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ void brl::GfxEngine::initialize(int width, int height, std::string windowName)
264264

265265
InputMgr::init(static_cast<GLFWwindow*>(mainWindow->window));
266266

267+
auto defaultFont = new GfxFont("D:/Roboto-Regular.ttf");
267268

268269
// To start a frame capture, call StartFrameCapture.
269270
// You can specify NULL, NULL for the device to capture on if you have only one device and

0 commit comments

Comments
 (0)