From 970e98f49ae48758c4f8a8667c0f9448903705c3 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 31 May 2023 10:47:06 -0700 Subject: [PATCH] Fixed some bugs with buffers during font building --- src/dawn/display/Color.hpp | 55 ++++++++---------- src/dawn/scene/components/ui/CMakeLists.txt | 1 + src/dawn/scene/components/ui/UILabelNew.cpp | 58 +++++++++++++++++++ src/dawn/scene/components/ui/UILabelNew.hpp | 26 +++++++++ src/dawnliminal/game/LiminalGame.cpp | 4 +- src/dawnliminal/scenes/HelloWorldScene.hpp | 19 +++--- .../display/BackBufferRenderTarget.cpp | 3 +- .../display/TextureRenderTarget.cpp | 9 ++- src/dawnopengl/display/shader/Shader.cpp | 3 +- .../display/shader/ShaderParameterBuffer.hpp | 13 ++++- .../display/shader/shaders/FontShader.cpp | 35 ++++++----- .../display/shader/shaders/FontShader.hpp | 25 ++++++-- .../shader/shaders/SimpleTexturedShader.cpp | 2 - src/dawnshared/util/macro.hpp | 9 +++ 14 files changed, 186 insertions(+), 76 deletions(-) create mode 100644 src/dawn/scene/components/ui/UILabelNew.cpp create mode 100644 src/dawn/scene/components/ui/UILabelNew.hpp create mode 100644 src/dawnshared/util/macro.hpp diff --git a/src/dawn/display/Color.hpp b/src/dawn/display/Color.hpp index 466d8ca5..6c5b2b88 100644 --- a/src/dawn/display/Color.hpp +++ b/src/dawn/display/Color.hpp @@ -8,55 +8,46 @@ namespace Dawn { struct Color { - uint8_t r, g, b, a; - - glm::vec4 precision() { - return { - (float_t)r / 255.0f, - (float_t)g / 255.0f, - (float_t)b / 255.0f, - (float_t)a / 100.0f - }; - } + float_t r, g, b, a; struct Color operator * (const float_t &x) { return { - (uint8_t)(r * x), - (uint8_t)(g * x), - (uint8_t)(b * x), - (uint8_t)(a * x) + r * x, + g * x, + b * x, + a * x }; } struct Color operator - (const struct Color &color) { return { - (uint8_t)(r - color.r), - (uint8_t)(g - color.g), - (uint8_t)(b - color.b), - (uint8_t)(a - color.a) + r - color.r, + g - color.g, + b - color.b, + a - color.a }; } struct Color operator + (const struct Color &color) { return { - (uint8_t)(r + color.r), - (uint8_t)(g + color.g), - (uint8_t)(b + color.b), - (uint8_t)(a + color.a) + r + color.r, + g + color.g, + b + color.b, + a + color.a }; } }; - #define COLOR_WHITE { 255, 255, 255, 255 } - #define COLOR_RED { 255, 0, 0, 255 } - #define COLOR_GREEN { 0, 255, 0, 255 } - #define COLOR_BLUE { 0, 0, 255, 255 } - #define COLOR_BLACK { 0, 0, 0, 255 } - #define COLOR_MAGENTA { 255, 0, 255, 255 } - #define COLOR_DARK_GREY { 50, 50, 50, 255 } - #define COLOR_LIGHT_GREY { 204, 204, 204, 255 } - #define COLOR_CORNFLOWER_BLUE { 100, 149, 237, 255 } - #define COLOR_WHITE_TRANSPARENT { 255, 255, 255, 0 } + #define COLOR_WHITE { 1.0f, 1.0f, 1.0f, 1.0f } + #define COLOR_RED { 1.0f, 0, 0, 1.0f } + #define COLOR_GREEN { 0, 1.0f, 0, 1.0f } + #define COLOR_BLUE { 0, 0, 1.0f, 1.0f } + #define COLOR_BLACK { 0, 0, 0, 1.0f } + #define COLOR_MAGENTA { 1.0f, 0, 1.0f, 1.0f } + #define COLOR_DARK_GREY { 0.19607843137254901961f, 0.19607843137254901961f, 0.19607843137254901961f, 1.0f } + #define COLOR_LIGHT_GREY { 0.8f, 0.8f, 0.8f, 1.0f } + #define COLOR_CORNFLOWER_BLUE { 0.39215686274509803922f, 0.58431372549019607843f, 0.92941176470588235294f, 1.0f } + #define COLOR_WHITE_TRANSPARENT { 1.0f, 1.0f, 1.0f, 0 } #define COLOR_BLACK_TRANSPARENT { 0, 0, 0, 0 } #define COLOR_TRANSPARENT COLOR_BLACK_TRANSPARENT } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/CMakeLists.txt b/src/dawn/scene/components/ui/CMakeLists.txt index 4ff90deb..ac25b369 100644 --- a/src/dawn/scene/components/ui/CMakeLists.txt +++ b/src/dawn/scene/components/ui/CMakeLists.txt @@ -12,6 +12,7 @@ target_sources(${DAWN_TARGET_NAME} UILabel.cpp UIImage.cpp UIBorder.cpp + UILabelNew.cpp ) add_subdirectory(menu) \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UILabelNew.cpp b/src/dawn/scene/components/ui/UILabelNew.cpp new file mode 100644 index 00000000..24680cab --- /dev/null +++ b/src/dawn/scene/components/ui/UILabelNew.cpp @@ -0,0 +1,58 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "UILabelNew.hpp" +#include "game/DawnGame.hpp" + +using namespace Dawn; + +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(); +} + +std::vector UILabelNew::getUIRenderPasses() { + auto canvas = this->getCanvas(); + auto shader = getGame()->renderManager.fontShader; + + struct ShaderPassItem item; + item.shader = shader; + item.mesh = &mesh; + item.matrixValues[shader->paramModel] = transform->getWorldTransform(); + 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 }; +} + + +float_t UILabelNew::getWidth() { + return 0; +} + +float_t UILabelNew::getHeight() { + return 0; +} + +float_t UILabelNew::getContentWidth() { + return 0; +} + +float_t UILabelNew::getContentHeight() { + return 0; +} \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UILabelNew.hpp b/src/dawn/scene/components/ui/UILabelNew.hpp new file mode 100644 index 00000000..62c1d75e --- /dev/null +++ b/src/dawn/scene/components/ui/UILabelNew.hpp @@ -0,0 +1,26 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/components/ui/UIComponentRenderable.hpp" +#include "display/mesh/QuadMesh.hpp" + +namespace Dawn { + class UILabelNew : public UIComponentRenderable { + protected: + Mesh mesh; + FontShaderBuffer shaderBuffer; + + public: + UILabelNew(SceneItem *item); + + void onStart() override; + std::vector getUIRenderPasses() override; + float_t getWidth() override; + float_t getHeight() override; + float_t getContentWidth() override; + float_t getContentHeight() override; + }; +} \ No newline at end of file diff --git a/src/dawnliminal/game/LiminalGame.cpp b/src/dawnliminal/game/LiminalGame.cpp index 0b992f0f..3341dd8c 100644 --- a/src/dawnliminal/game/LiminalGame.cpp +++ b/src/dawnliminal/game/LiminalGame.cpp @@ -10,6 +10,6 @@ using namespace Dawn; Scene * Dawn::dawnGameGetInitialScene(DawnGame *game) { - // return new HelloWorldScene(game); - return new Scene1Prologue(game); + return new HelloWorldScene(game); + // return new Scene1Prologue(game); } \ No newline at end of file diff --git a/src/dawnliminal/scenes/HelloWorldScene.hpp b/src/dawnliminal/scenes/HelloWorldScene.hpp index 1637eb5d..953b607c 100644 --- a/src/dawnliminal/scenes/HelloWorldScene.hpp +++ b/src/dawnliminal/scenes/HelloWorldScene.hpp @@ -6,22 +6,14 @@ #pragma once #include "scene/Scene.hpp" #include "prefabs/SimpleSpinningCubePrefab.hpp" -#include "prefabs/ui/debug/FPSLabel.hpp" -#include "scene/components/ui/UIImage.hpp" -#include "display/font/BitmapFont.hpp" - -#include "display/font/NewTrueType.hpp" +#include "scene/components/display/Camera.hpp" +#include "scene/components/ui/UILabelNew.hpp" namespace Dawn { class HelloWorldScene : public Scene { protected: Camera *camera; SimpleSpinningCubePrefab *cube; - FT_Face face; - Texture texture; - std::map charStore; - - SceneItem *item; void stage() override { camera = Camera::create(this); @@ -29,6 +21,13 @@ namespace Dawn { cube = SimpleSpinningCubePrefab::create(this); + auto canvasItem = this->createSceneItem(); + auto canvas = canvasItem->addComponent(); + + auto newLabelItem = this->createSceneItem(); + newLabelItem->transform.setParent(canvas->transform); + auto newLabel = newLabelItem->addComponent(); + // item = this->createSceneItem(); // auto meshRenderer = item->addComponent(); // auto quadMeshHost = item->addComponent(); diff --git a/src/dawnopengl/display/BackBufferRenderTarget.cpp b/src/dawnopengl/display/BackBufferRenderTarget.cpp index 7d2caa92..68394c34 100644 --- a/src/dawnopengl/display/BackBufferRenderTarget.cpp +++ b/src/dawnopengl/display/BackBufferRenderTarget.cpp @@ -37,8 +37,7 @@ void BackBufferRenderTarget::setClearColor(struct Color color) { } void BackBufferRenderTarget::clear(flag8_t clearFlags) { - auto clear = this->clearColor.precision(); - glClearColor(clear.r, clear.g, clear.b, clear.a); + glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } diff --git a/src/dawnopengl/display/TextureRenderTarget.cpp b/src/dawnopengl/display/TextureRenderTarget.cpp index d04ee6e0..8b1d27ac 100644 --- a/src/dawnopengl/display/TextureRenderTarget.cpp +++ b/src/dawnopengl/display/TextureRenderTarget.cpp @@ -67,12 +67,11 @@ void TextureRenderTarget::setClearColor(struct Color color) { } void TextureRenderTarget::clear(flag8_t clearFlags) { - auto clear = this->clearColor.precision(); glClearColor( - clear.r, - clear.g, - clear.b, - clear.a + clearColor.r, + clearColor.g, + clearColor.b, + clearColor.a ); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } diff --git a/src/dawnopengl/display/shader/Shader.cpp b/src/dawnopengl/display/shader/Shader.cpp index 0cb90c99..581af531 100644 --- a/src/dawnopengl/display/shader/Shader.cpp +++ b/src/dawnopengl/display/shader/Shader.cpp @@ -105,8 +105,7 @@ void Shader::setBoolean(shaderparameter_t uni, bool value) { } void Shader::setColor(shaderparameter_t uniform, struct Color color) { - auto precise = color.precision(); - glUniform4f(uniform, precise.r, precise.g, precise.b, precise.a); + glUniform4f(uniform, color.r, color.g, color.b, color.a); } void Shader::setVector3(shaderparameter_t uniform, glm::vec3 vector) { diff --git a/src/dawnopengl/display/shader/ShaderParameterBuffer.hpp b/src/dawnopengl/display/shader/ShaderParameterBuffer.hpp index af76fcba..c3d9311c 100644 --- a/src/dawnopengl/display/shader/ShaderParameterBuffer.hpp +++ b/src/dawnopengl/display/shader/ShaderParameterBuffer.hpp @@ -29,6 +29,10 @@ namespace Dawn { glBufferData(GL_UNIFORM_BUFFER, this->size, NULL, GL_DYNAMIC_DRAW); } + bool_t isReady() { + return this->id != -1; + } + /** * Basic buffer method. Buffers the entire contents of the data struct to * this shader parameter buffer. @@ -36,11 +40,11 @@ namespace Dawn { * @param data Data to buffer to the parameter. */ void buffer(T *data) { - this->bind(0); this->bufferRaw((void*)data); } void bind(shaderbufferslot_t location) override { + assertTrue(this->isReady()); glBindBuffer(GL_UNIFORM_BUFFER, this->id); glBindBufferBase(GL_UNIFORM_BUFFER, location, this->id); } @@ -63,6 +67,7 @@ namespace Dawn { * @param length Length of the data to buffer. */ void bufferRaw(void *data, size_t start, size_t length) { + assertTrue(this->isReady()); glBindBuffer(GL_UNIFORM_BUFFER, this->id); glBufferSubData(GL_UNIFORM_BUFFER, start, length, (void*)((size_t)data + start)); } @@ -99,8 +104,10 @@ namespace Dawn { * Destroys this shader parameter buffer. */ ~ShaderParameterBuffer() { - glDeleteBuffers(1, &this->id); - this->id = -1; + if(this->id != -1) { + glDeleteBuffers(1, &this->id); + this->id = -1; + } } }; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/shaders/FontShader.cpp b/src/dawnopengl/display/shader/shaders/FontShader.cpp index 71f4e168..2e47535a 100644 --- a/src/dawnopengl/display/shader/shaders/FontShader.cpp +++ b/src/dawnopengl/display/shader/shaders/FontShader.cpp @@ -18,37 +18,46 @@ void FontShader::compile() { "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "layout (location = 1) in vec2 aTexCoord;\n" + + "layout (std140) uniform ub_UICanvas {\n" + "mat4 u_View;\n" + "mat4 u_Projection;\n" + "};" + + "struct FontShaderPart {\n" + "vec4 color;\n" + "};\n" + + "layout (std140) uniform ub_Font {\n" + "FontShaderPart u_FontParts[" MACRO_STRINGIFY(FONT_SHADER_PARTS_MAX) "];\n" + "int u_FontPartsCount;\n" + "};\n" - "uniform mat4 u_Proj;\n" - "uniform mat4 u_View;\n" "uniform mat4 u_Model;\n" - "out vec2 o_TextCoord;\n" + "out vec4 o_VertColor;\n" + "void main() {\n" - "gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" + "gl_Position = u_Projection * u_View * u_Model * vec4(aPos, 1.0);\n" "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" + "o_VertColor = u_FontParts[9].color;\n" "}", // Fragment Shader "#version 330 core\n" "in vec2 o_TextCoord;\n" + "in vec4 o_VertColor;\n" "out vec4 o_Color;\n" - "uniform vec4 u_Color;\n" - "uniform bool u_HasTexture;\n" - "uniform sampler2D u_Text;\n" "void main() {\n" - "o_Color = u_Color;\n" - "o_Color.a *= texture(u_Text, o_TextCoord).r;\n" + "o_Color = o_VertColor;\n" "}\n" ); #else #error Shader Type unknown #endif - this->paramProjection = this->getParameterByName("u_Proj"); - this->paramView = this->getParameterByName("u_View"); this->paramModel = this->getParameterByName("u_Model"); - this->paramColor = this->getParameterByName("u_Color"); - this->paramTexture = this->getParameterByName("u_Text"); + this->bufferUiCanvas = this->getBufferLocationByName("ub_UICanvas"); + this->bufferFont = this->getBufferLocationByName("ub_Font"); } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/shaders/FontShader.hpp b/src/dawnopengl/display/shader/shaders/FontShader.hpp index 5501c9d7..efb95bb7 100644 --- a/src/dawnopengl/display/shader/shaders/FontShader.hpp +++ b/src/dawnopengl/display/shader/shaders/FontShader.hpp @@ -2,17 +2,32 @@ // // This software is released under the MIT License. // https://opensource.org/licenses/MIT + #pragma once -#include "display/shader/Shader.hpp" +#include "UIShader.hpp" +#include "util/macro.hpp" + +#define FONT_SHADER_PARTS_MAX 32 namespace Dawn { + struct FontShaderPart { + struct Color color; + }; + + struct FontShaderBufferData { + struct FontShaderPart fontParts[FONT_SHADER_PARTS_MAX]; + int32_t fontPartsCount; + }; + + class FontShaderBuffer : public ShaderParameterBuffer { + + }; + class FontShader : public Shader { public: - shaderparameter_t paramProjection; - shaderparameter_t paramView; shaderparameter_t paramModel; - shaderparameter_t paramColor; - shaderparameter_t paramTexture; + shaderbufferlocation_t bufferUiCanvas; + shaderbufferlocation_t bufferFont; void compile() override; }; diff --git a/src/dawnopengl/display/shader/shaders/SimpleTexturedShader.cpp b/src/dawnopengl/display/shader/shaders/SimpleTexturedShader.cpp index 283e1cbd..f8c3f57d 100644 --- a/src/dawnopengl/display/shader/shaders/SimpleTexturedShader.cpp +++ b/src/dawnopengl/display/shader/shaders/SimpleTexturedShader.cpp @@ -21,8 +21,6 @@ void SimpleTexturedShader::compile() { RenderPipelineShaderBuffer::getShaderUniform() + "" - // "uniform mat4 u_Proj;\n" - // "uniform mat4 u_View;\n" "uniform mat4 u_Model;\n" "out vec2 o_TextCoord;\n" diff --git a/src/dawnshared/util/macro.hpp b/src/dawnshared/util/macro.hpp new file mode 100644 index 00000000..29ddfdc8 --- /dev/null +++ b/src/dawnshared/util/macro.hpp @@ -0,0 +1,9 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once + +#define MACRO_STRINGIFY_RAW(x) #x +#define MACRO_STRINGIFY(x) MACRO_STRINGIFY_RAW(x) \ No newline at end of file