Skip to main content

OpenGLGlyphTextRenderer Class

Renders and manages text using OpenGL and FreeType. More...

Declaration

class helios::ext::opengl::rendering::OpenGLGlyphTextRenderer { ... }

Base class

classTextRenderer

Abstract interface for text rendering implementations. More...

Friends Index

classOpenGLDevice

Public Destructor Index

~OpenGLGlyphTextRenderer () override

Destroys the OpenGLGlyphTextRenderer and releases all OpenGL resources. More...

Public Member Functions Index

voidrender (const helios::rendering::text::TextRenderCommand &command, const helios::rendering::shader::UniformValueMap &frameUniformValues) noexcept override

Renders text using the specified render command. More...

Private Member Functions Index

voidinit ()

Initializes OpenGL resources (VAO and VBO) for text rendering. More...

voidrenderText (const std::string_view text, const helios::engine::modules::ui::widgets::types::FontId fontId, std::span< helios::math::vec4f > vertices, helios::rendering::text::FontResourceProvider *fontResourceProvider) noexcept

Renders text at a specified position using a specified font and scale. More...

voidbeginRenderPass (helios::rendering::RenderPass &renderPass) const noexcept

Resets cached rendering state at the beginning of a render pass. More...

Private Member Attributes Index

unsigned intvao_ {}

OpenGL Vertex Array Object for glyph rendering. More...

unsigned intvbo_ {}

OpenGL Vertex Buffer Object for glyph quad vertices. More...

const helios::ext::opengl::rendering::shader::OpenGLShader *lastShader_ = nullptr

Cached pointer to the last used shader for state optimization. More...

unsigned intlastTexture_ = 0

Cached texture ID for state optimization. More...

unsigned intlastVao_ = 0

Cached VAO ID for state optimization. More...

Public Static Functions Index

static inttextTextureUnit ()

Returns the texture unit used for text rendering. More...

Description

Renders and manages text using OpenGL and FreeType.

This class is responsible for rendering text using cached glyph textures and managing OpenGL resources for efficient rendering. It leverages FreeType to rasterize font glyphs and OpenGL to render them onto the screen. Text rendering is performed by positioning quads (one per character) with texture-mapped glyphs.

The class provides methods for initializing OpenGL resources, loading fonts into a cache, and rendering text at specified positions and scales.

Inherits from the TextRenderer class in the helios rendering system.

Definition at line 55 of file OpenGLGlyphTextRenderer.ixx.

Friends

OpenGLDevice

friend class OpenGLDevice

Definition at line 59 of file OpenGLGlyphTextRenderer.ixx.

59 friend class OpenGLDevice;

Reference OpenGLDevice.

Referenced by OpenGLDevice.

Public Destructor

~OpenGLGlyphTextRenderer()

helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::~OpenGLGlyphTextRenderer ()
inline

Destroys the OpenGLGlyphTextRenderer and releases all OpenGL resources.

Cleans up the Vertex Array Object (VAO), Vertex Buffer Object (VBO), and all cached glyph textures for every loaded font.

info

Must be called in a valid OpenGL context.

Definition at line 197 of file OpenGLGlyphTextRenderer.ixx.

198
199 if (vao_ != 0) {
200 glDeleteVertexArrays(1, &vao_);
201 }
202 if (vbo_ != 0) {
203 glDeleteBuffers(1, &vbo_);
204 }
205
206 /**
207 *@todo glDeleteTextures()
208 */
209
210 glBindVertexArray(0);
211 glBindTexture(GL_TEXTURE_2D, 0);
212 }

Public Member Functions

render()

void helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::render (const helios::rendering::text::TextRenderCommand & command, const helios::rendering::shader::UniformValueMap & frameUniformValues)
inline noexcept virtual

Renders text using the specified render command.

Processes the `TextRenderCommand`, activates the associated shader, applies uniform values, and renders the text string.

Parameters
command

The render command containing text and rendering properties.

frameUniformValues

Frame-level uniforms (e.g., projection matrix).

info

Requires a valid OpenGL context and initialized renderer.

Definition at line 234 of file OpenGLGlyphTextRenderer.ixx.

234 void render(
236 const helios::rendering::shader::UniformValueMap& frameUniformValues) noexcept override {
237
238 if (auto* textPrototype = command.textRenderPrototype()) {
239 const auto& baseShader = textPrototype->shader();
240
241 const auto* shader = static_cast<const helios::ext::opengl::rendering::shader::OpenGLShader*>(&baseShader);
242 assert(shader && "Unexpected failure when casting to OpenGLShader.");
243
244 if (lastShader_ != shader) {
245 shader->use();
246 lastShader_ = shader;
247 }
248
249 shader->applyUniformValues(frameUniformValues);
250 shader->applyUniformValues(command.objectUniformValues());;
251 shader->applyUniformValues(command.materialUniformValues());;
252
253 const auto* textMesh = command.textMesh();
254
255 renderText(
256 textMesh->text(),
257 textMesh->fontId(),
258 textMesh->vertices(textPrototype->fontResourceProvider()),
259 &textPrototype->fontResourceProvider()
260 );
261 }
262
263 };

Private Member Functions

beginRenderPass()

void helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::beginRenderPass (helios::rendering::RenderPass & renderPass)
inline noexcept

Resets cached rendering state at the beginning of a render pass.

This ensures proper shader and VAO binding even when the render queue contents change between frames. Called internally by `OpenGLDevice` before processing the render queue.

Parameters
renderPass

The render pass being started (currently unused, reserved for future use).

Definition at line 179 of file OpenGLGlyphTextRenderer.ixx.

179 void beginRenderPass(helios::rendering::RenderPass& renderPass) const noexcept {
180 // Reset cached state at the beginning of each render pass
181 // to ensure proper shader and VAO binding even when render queue contents change
182 lastShader_ = nullptr;
183 lastVao_ = 0;
184 }

init()

void helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::init ()
inline

Initializes OpenGL resources (VAO and VBO) for text rendering.

Creates a vertex array object and vertex buffer for rendering glyph quads. The VBO is configured for dynamic updates (one quad per character).

Must be called before any rendering operations.

Definition at line 69 of file OpenGLGlyphTextRenderer.ixx.

69 void init() {
70 glGenVertexArrays(1, &vao_);
71 glGenBuffers(1, &vbo_);
72 glBindVertexArray(vao_);
73 glBindBuffer(GL_ARRAY_BUFFER, vbo_);
74 // 2D quad for a texture requires 6 vertices with 4 floats each => 6*4
75 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6*4, nullptr, GL_DYNAMIC_DRAW);
76 glEnableVertexAttribArray(0);
77 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
78 glBindBuffer(GL_ARRAY_BUFFER, 0);
79 glBindVertexArray(0);
80 }

renderText()

void helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::renderText (const std::string_view text, const helios::engine::modules::ui::widgets::types::FontId fontId, std::span< helios::math::vec4f > vertices, helios::rendering::text::FontResourceProvider * fontResourceProvider)
inline noexcept

Renders text at a specified position using a specified font and scale.

This method uses OpenGL to render text, character by character, by updating vertex buffer objects (VBOs) and binding glyph textures. Each glyph is positioned relative to the given starting position, scaled according to the provided scale factor, and rendered using stored glyph information from a font cache.

Parameters
text

The text string to render.

position

The starting (x, y) position for the text, in screen space coordinates.

scale

The scale factor to apply to the size of each glyph.

fontId

The font identifier used to select the font from the font cache.

Definition at line 130 of file OpenGLGlyphTextRenderer.ixx.

130 void renderText(
131 const std::string_view text,
133 std::span<helios::math::vec4f> vertices,
135 ) noexcept {
136
137 glActiveTexture(GL_TEXTURE0);
138
139 if (vao_ != lastVao_) {
140 glBindVertexArray(vao_);
141 lastVao_ = vao_;
142 }
143
144 int offset = 0;
145 int stride = 6;
146 for (auto ch : text) {
147 auto vertex = vertices.subspan(offset, stride);
148 auto glyph = fontResourceProvider->glyph(ch, fontId);
149
150 // update the VBO for _each_ character (multiple drawcalls!)
151
152 // render glyph texture over quad
153 glBindTexture(GL_TEXTURE_2D, glyph.textureId);
154 // update content of vbo memory
155
156 glBindBuffer(GL_ARRAY_BUFFER, vbo_);
157
158 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(helios::math::vec4f) * stride, vertex.data());
159 glBindBuffer(GL_ARRAY_BUFFER, 0);
160 //render quad
161 glDrawArrays(GL_TRIANGLES, 0, 6);
162 // advance cursors for next glyph (1/64 pixels)
163
164 offset += stride;
165 }
166
167 }

Private Member Attributes

lastShader_

const helios::ext::opengl::rendering::shader::OpenGLShader* helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::lastShader_ = nullptr
mutable

Cached pointer to the last used shader for state optimization.

Used to avoid redundant shader program activations. Reset at the beginning of each render pass.

Definition at line 101 of file OpenGLGlyphTextRenderer.ixx.

101 mutable const helios::ext::opengl::rendering::shader::OpenGLShader* lastShader_ = nullptr;

lastTexture_

unsigned int helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::lastTexture_ = 0
mutable

Cached texture ID for state optimization.

Used to avoid redundant texture bindings.

Definition at line 108 of file OpenGLGlyphTextRenderer.ixx.

108 mutable unsigned int lastTexture_ = 0;

lastVao_

unsigned int helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::lastVao_ = 0
mutable

Cached VAO ID for state optimization.

Used to avoid redundant VAO bindings. Reset at the beginning of each render pass.

Definition at line 115 of file OpenGLGlyphTextRenderer.ixx.

115 mutable unsigned int lastVao_ = 0;

vao_

unsigned int helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::vao_ {}

OpenGL Vertex Array Object for glyph rendering.

Definition at line 88 of file OpenGLGlyphTextRenderer.ixx.

88 unsigned int vao_{};

vbo_

unsigned int helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::vbo_ {}

OpenGL Vertex Buffer Object for glyph quad vertices.

Definition at line 93 of file OpenGLGlyphTextRenderer.ixx.

93 unsigned int vbo_{};

Public Static Functions

textTextureUnit()

int helios::ext::opengl::rendering::OpenGLGlyphTextRenderer::textTextureUnit ()
inline static

Returns the texture unit used for text rendering.

Returns

The texture unit index (always 0).

Definition at line 219 of file OpenGLGlyphTextRenderer.ixx.

219 static inline int textTextureUnit() {
220 return 0;
221 }

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.