From ffe2764d295ed44e4a4f81d44c6cc16b09a89112 Mon Sep 17 00:00:00 2001 From: Kevin Trogant Date: Fri, 16 Dec 2022 11:46:17 +0100 Subject: [PATCH] Fixed font rectangles --- app/src/main/cpp/Renderer.cpp | 65 ++++++++++++++++------------------ app/src/main/cpp/Texture.cpp | 1 + app/src/main/cpp/win32_kde.cpp | 26 ++++++++++++++ 3 files changed, 58 insertions(+), 34 deletions(-) diff --git a/app/src/main/cpp/Renderer.cpp b/app/src/main/cpp/Renderer.cpp index d34ff8b..d656028 100644 --- a/app/src/main/cpp/Renderer.cpp +++ b/app/src/main/cpp/Renderer.cpp @@ -115,11 +115,11 @@ Renderer::Renderer() glBindBuffer(GL_ARRAY_BUFFER, m_vbo); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)0); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)(2 * sizeof(float))); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)(4 * sizeof(float))); - glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)(6 * sizeof(float))); - glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)(8 * sizeof(float))); - glVertexAttribPointer(5, 1, GL_INT, GL_FALSE, sizeof(Rect), (GLvoid*)(12 * sizeof(float))); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)offsetof(Rect, dst_p1_x)); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)offsetof(Rect, src_p0_x)); + glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)offsetof(Rect, src_p1_x)); + glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)offsetof(Rect, r)); + glVertexAttribIPointer(5, 1, GL_INT, sizeof(Rect), (GLvoid*)offsetof(Rect, expand_r)); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); @@ -225,35 +225,35 @@ void Renderer::addRect(float x, } void Renderer::addFontRect(float x, - float y, - float w, - float h, - float char_height, - StringHandle font, - StringHandle id, - float src_x0, - float src_y0, - float src_x1, - float src_y1) + float y, + float w, + float h, + float char_height, + StringHandle font, + StringHandle id, + float src_x0, + float src_y0, + float src_x1, + float src_y1) { addFontRect(x, y, w, h, 0.f, 0.f, 0.f, 1.f, char_height, font, id, src_x0, src_y0, src_x1, src_y1); } void Renderer::addFontRect(float x, - float y, - float w, - float h, - float r, - float g, - float b, - float a, - float char_height, - StringHandle font, - StringHandle id, - float src_x0, - float src_y0, - float src_x1, - float src_y1) + float y, + float w, + float h, + float r, + float g, + float b, + float a, + float char_height, + StringHandle font, + StringHandle id, + float src_x0, + float src_y0, + float src_x1, + float src_y1) { if (m_textures.find(id) == m_textures.end()) { Texture tex; @@ -287,7 +287,7 @@ void Renderer::renderFrame(float width, float height) assert(m_rects.size() == m_draw_textures.size()); glViewport(0, 0, static_cast(width), static_cast(height)); - //glClearColor(0.8f, 0.3f, 0.3f, 1.f); + // glClearColor(0.8f, 0.3f, 0.3f, 1.f); glClearColor(0.0f, 0.0f, 0.0f, 1.f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -346,10 +346,7 @@ void Renderer::renderFrame(float width, float height) m_draw_textures.clear(); } -void Renderer::getTextureSize( - StringHandle texture, - unsigned int* w, - unsigned int* h) +void Renderer::getTextureSize(StringHandle texture, unsigned int* w, unsigned int* h) { if (m_textures.find(texture) == m_textures.end()) { Texture tex; diff --git a/app/src/main/cpp/Texture.cpp b/app/src/main/cpp/Texture.cpp index c481d25..bc61416 100644 --- a/app/src/main/cpp/Texture.cpp +++ b/app/src/main/cpp/Texture.cpp @@ -38,6 +38,7 @@ Texture::Texture(unsigned int width, unsigned int height, const void* data, GLin GL_RED, GL_UNSIGNED_BYTE, data); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); diff --git a/app/src/main/cpp/win32_kde.cpp b/app/src/main/cpp/win32_kde.cpp index 2e32a63..62eb3b0 100644 --- a/app/src/main/cpp/win32_kde.cpp +++ b/app/src/main/cpp/win32_kde.cpp @@ -90,6 +90,9 @@ int main() // BEISPIELCODE Position smiley_pos = {512, 384}; StringHandle smiley = StringRepository::global->internString("smiley_PNG42.png"); + StringHandle ttf = StringRepository::global->internString("Milky Honey.ttf"); + FontData fd; + AssetManager::ptr->loadFontData("Milky Honey.ttf", 24.f, &fd); // ENDE BEISPIELCODE glfwSwapInterval(1); @@ -178,6 +181,29 @@ int main() Renderer::ptr->addRect(100, 100, 500, 500, 0.3f, 0.3f, 0.3f, 1.f); Renderer::ptr->addRect(smiley_pos.x, smiley_pos.y, 500, 500, 0.f, x * x, 1.f - x * x, 1.f, smiley); + + const char* text = "Hello, Sailor!"; + float tx = 100, ty = 200; + while (*text) { + if (*text >= 32 && *text < 128) { + stbtt_aligned_quad q; + stbtt_GetBakedQuad(fd.char_data, 1024, 1024, *text - 32, &tx, &ty, &q, 1); + float w = q.x1 - q.x0, h = q.y1 - q.y0; + Renderer::ptr->addFontRect(q.x0, + q.y0, + w, + h, + fd.char_height, + ttf, + StringRepository::global->internString("1"), + q.s0, + q.t0, + q.s1, + q.t1); + } + ++text; + } + // ENDE BEISPIELCODE Renderer::ptr->renderFrame(static_cast(w), static_cast(h));