From 30f58cc3832bd1b5a5cadf4b0a4a9d47d024f9f7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 26 Jan 2024 17:48:11 +0000 Subject: Remove the static font cache Each thing can own/share a font rendering itself --- ui/text.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'ui/text.cpp') diff --git a/ui/text.cpp b/ui/text.cpp index b776b90..7cb7d30 100644 --- a/ui/text.cpp +++ b/ui/text.cpp @@ -4,19 +4,13 @@ #include "uiComponent.h" #include #include -#include #include #include -#include -#include #include -const std::filesystem::path font {"/usr/share/fonts/hack/Hack-Regular.ttf"}; - -Text::Text(std::string_view s, Position pos, glm::vec3 c) : UIComponent {pos}, colour {c} +Text::Text(std::string_view s, const Font & font, Position pos, glm::vec3 c) : UIComponent {pos}, colour {c} { - for (const auto & textureQuads : - Font::cachedFontRenderings.get(font, static_cast(pos.size.y))->render(s)) { + for (const auto & textureQuads : font.render(s)) { auto & rendering = models.emplace_back(textureQuads.first, static_cast(6 * textureQuads.second.size())); glBindVertexArray(rendering.vao); -- cgit v1.2.3 From 14d82ff67eab5d94b69fb38ea6a0bc634674245c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 26 Jan 2024 17:56:52 +0000 Subject: Remove the generic cache completely --- ui/text.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'ui/text.cpp') diff --git a/ui/text.cpp b/ui/text.cpp index 7cb7d30..5b9e591 100644 --- a/ui/text.cpp +++ b/ui/text.cpp @@ -3,7 +3,6 @@ #include "gfx/gl/uiShader.h" #include "uiComponent.h" #include -#include #include #include #include -- cgit v1.2.3 From 264640420882ceeafd9b1149eba9472cf8835e7b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 27 Jan 2024 21:47:04 +0000 Subject: Render text in N draw calls Creates a single buffer per required texture and draws the whole buffer in one go. It does introduce the use of deprecated GL_QUADS primitive, but it's the easiest way to go without needing indices, repeated vertices etc --- ui/text.cpp | 63 +++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 27 deletions(-) (limited to 'ui/text.cpp') diff --git a/ui/text.cpp b/ui/text.cpp index 5b9e591..5675061 100644 --- a/ui/text.cpp +++ b/ui/text.cpp @@ -1,37 +1,48 @@ #include "text.h" #include "font.h" #include "gfx/gl/uiShader.h" +#include "gfx/gl/vertexArrayObject.h" #include "uiComponent.h" #include +#include #include #include +#include +#include #include -Text::Text(std::string_view s, const Font & font, Position pos, glm::vec3 c) : UIComponent {pos}, colour {c} +Text::Text(std::string_view s, const Font & font, Position pos, glm::vec3 c) : + UIComponent {pos}, colour {c}, font {font} { - for (const auto & textureQuads : font.render(s)) { - auto & rendering - = models.emplace_back(textureQuads.first, static_cast(6 * textureQuads.second.size())); - glBindVertexArray(rendering.vao); - - glBindBuffer(GL_ARRAY_BUFFER, rendering.vbo); - std::vector vertices; - vertices.reserve(6 * textureQuads.second.size()); - for (const auto & quad : textureQuads.second) { - for (auto offset = 0U; offset < 3; offset += 2) { - for (auto vertex = 0U; vertex < 3; vertex += 1) { - vertices.emplace_back(quad[(vertex + offset) % 4] + glm::vec4 {position.origin, 0, 0}); - } - } - }; - glBufferData(GL_ARRAY_BUFFER, static_cast(sizeof(glm::vec4)) * rendering.count, - glm::value_ptr(vertices.front()), GL_STATIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), nullptr); + VertexArrayObject {vao}.addAttribs(quads.bufferName(), 0); + operator=(s); +} - glBindVertexArray(0); +Text & +Text::operator=(const std::string_view s) +{ + auto tquads = font.render(s); + models.resize(tquads.size()); + const auto glyphCount = std::accumulate(tquads.begin(), tquads.end(), size_t {}, [](auto && init, const auto & q) { + return init += q.second.size(); + }); + quads.resize(glyphCount); + GLint current = 0; + auto model = models.begin(); + auto quad = quads.begin(); + for (const auto & [texture, fquads] : tquads) { + model->first = texture; + model->second = {fquads.size() * 4, current * 4}; + current += static_cast(fquads.size()); + model++; + quad = std::transform(fquads.begin(), fquads.end(), quad, [this](const Font::Quad & q) { + return q * [this](const glm::vec4 & corner) { + return corner + glm::vec4 {this->position.origin, 0, 0}; + }; + }); } + quads.unmap(); + return *this; } void @@ -39,10 +50,10 @@ Text::render(const UIShader & shader, const Position &) const { shader.text.use(colour); glActiveTexture(GL_TEXTURE0); + glBindVertexArray(vao); for (const auto & m : models) { - glBindTexture(GL_TEXTURE_2D, m.texture); - glBindVertexArray(m.vao); - glDrawArrays(GL_TRIANGLES, 0, m.count); + glBindTexture(GL_TEXTURE_2D, m.first); + glDrawArrays(GL_QUADS, m.second.second, m.second.first); } glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -53,5 +64,3 @@ Text::handleInput(const SDL_Event &, const Position &) { return false; } - -Text::Model::Model(GLuint t, GLsizei c) : texture {t}, count {c} { } -- cgit v1.2.3