From d20e8e8e7acd686e8c0e64d198f41d7785656466 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 1 Jun 2023 10:10:34 -0700 Subject: [PATCH] Testing Font shader setup for arrays --- src/dawn/scene/components/ui/UILabelNew.cpp | 34 ++++++++++++++----- .../display/shader/shaders/FontShader.cpp | 9 +++-- .../display/shader/shaders/FontShader.hpp | 5 +-- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/dawn/scene/components/ui/UILabelNew.cpp b/src/dawn/scene/components/ui/UILabelNew.cpp index 24680cab..aeab4143 100644 --- a/src/dawn/scene/components/ui/UILabelNew.cpp +++ b/src/dawn/scene/components/ui/UILabelNew.cpp @@ -14,12 +14,32 @@ UILabelNew::UILabelNew(SceneItem *item) : UIComponentRenderable(item) { void UILabelNew::onStart() { std::cout << "Hello new Label" << std::endl; - QuadMesh::initQuadMesh(&this->mesh, - glm::vec2(0, 0), glm::vec2(0, 0), - glm::vec2(32, 32), glm::vec2(1, 1), - 0.0f - ); + this->shaderBuffer.init(); + + struct FontShaderBufferData fontData; + + int32_t n = 4 * 4; + this->mesh.createBuffers(n * QUAD_VERTICE_COUNT, n * QUAD_INDICE_COUNT); + for(int32_t i = 0; i < n; i++) { + glm::vec2 size = glm::vec2(32, 32); + glm::vec2 pos = glm::vec2(size.x * i, 0) + glm::vec2(2 * i, 0); + QuadMesh::bufferQuadMeshWithZ(&this->mesh, + pos, glm::vec2(0, 0), + pos + size, glm::vec2(1, 1), + 0.0f, i * QUAD_VERTICE_COUNT, i * QUAD_INDICE_COUNT + ); + fontData.fontQuadParts[i] = i / 4; + } + + fontData.fontParts[0].color = COLOR_RED; + fontData.fontParts[1].color = COLOR_GREEN; + fontData.fontParts[2].color = COLOR_BLUE; + fontData.fontParts[3].color = COLOR_MAGENTA; + fontData.fontParts[4].color = COLOR_BLACK; + fontData.fontParts[5].color = COLOR_WHITE; + + shaderBuffer.buffer(&fontData); } std::vector UILabelNew::getUIRenderPasses() { @@ -33,10 +53,6 @@ std::vector UILabelNew::getUIRenderPasses() { item.parameterBuffers[shader->bufferUiCanvas] = &canvas->shaderBuffer; item.parameterBuffers[shader->bufferFont] = &this->shaderBuffer; - struct FontShaderBufferData fontData; - fontData.fontParts[9].color = COLOR_BLUE; - shaderBuffer.buffer(&fontData); - return { item }; } diff --git a/src/dawnopengl/display/shader/shaders/FontShader.cpp b/src/dawnopengl/display/shader/shaders/FontShader.cpp index 2e47535a..3c992fc9 100644 --- a/src/dawnopengl/display/shader/shaders/FontShader.cpp +++ b/src/dawnopengl/display/shader/shaders/FontShader.cpp @@ -4,6 +4,7 @@ // https://opensource.org/licenses/MIT #include "FontShader.hpp" +#include "display/mesh/QuadMesh.hpp" using namespace Dawn; @@ -30,7 +31,7 @@ void FontShader::compile() { "layout (std140) uniform ub_Font {\n" "FontShaderPart u_FontParts[" MACRO_STRINGIFY(FONT_SHADER_PARTS_MAX) "];\n" - "int u_FontPartsCount;\n" + "int u_FontQuadParts[" MACRO_STRINGIFY(FONT_SHADER_QUADS_MAX) "];\n" "};\n" "uniform mat4 u_Model;\n" @@ -38,9 +39,11 @@ void FontShader::compile() { "out vec4 o_VertColor;\n" "void main() {\n" - "gl_Position = u_Projection * u_View * u_Model * vec4(aPos, 1.0);\n" + "gl_Position = u_Projection * u_View * u_Model * vec4(aPos.xy, 0, 1.0);\n" "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" - "o_VertColor = u_FontParts[9].color;\n" + "int quadIndex = gl_VertexID / " MACRO_STRINGIFY(QUAD_VERTICE_COUNT) ";\n" + "int partIndex = u_FontQuadParts[quadIndex];\n" + "o_VertColor = u_FontParts[partIndex].color;\n" "}", // Fragment Shader diff --git a/src/dawnopengl/display/shader/shaders/FontShader.hpp b/src/dawnopengl/display/shader/shaders/FontShader.hpp index efb95bb7..def716ed 100644 --- a/src/dawnopengl/display/shader/shaders/FontShader.hpp +++ b/src/dawnopengl/display/shader/shaders/FontShader.hpp @@ -7,7 +7,8 @@ #include "UIShader.hpp" #include "util/macro.hpp" -#define FONT_SHADER_PARTS_MAX 32 +#define FONT_SHADER_PARTS_MAX 16 +#define FONT_SHADER_QUADS_MAX 16 namespace Dawn { struct FontShaderPart { @@ -16,7 +17,7 @@ namespace Dawn { struct FontShaderBufferData { struct FontShaderPart fontParts[FONT_SHADER_PARTS_MAX]; - int32_t fontPartsCount; + int32_t fontQuadParts[FONT_SHADER_QUADS_MAX]; }; class FontShaderBuffer : public ShaderParameterBuffer {