From e2a72e3da124ed3005b6fec771c359ecc6d750bf Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 1 Nov 2026 01:21:51 -0500 Subject: [PATCH] Text is finally working, once again it was the depth buffer settings in framebuffer.cpp that caused the rendering of text and images to be in z order rather than all on the same plane. --- lua/builder_test.lua | 9 ++++++++- meson.build | 7 ++++++- shaders/image.vert.glsl | 5 +++-- shaders/text.frag.glsl | 4 ++-- src/engine.cpp | 4 ++-- src/framebuffer.cpp | 8 ++------ src/image.cpp | 2 +- src/options.cpp | 5 +++-- src/screen.cpp | 2 +- src/screen.hpp | 2 +- src/text.cpp | 15 +++++++++------ src/text.hpp | 1 + wraps/magic_enum.wrap | 10 ++++++++++ 13 files changed, 49 insertions(+), 25 deletions(-) create mode 100644 wraps/magic_enum.wrap diff --git a/lua/builder_test.lua b/lua/builder_test.lua index 291b8d7..743a5a7 100644 --- a/lua/builder_test.lua +++ b/lua/builder_test.lua @@ -8,7 +8,14 @@ config{ } slide{ - h1 "Here's Mine", + image{ + src="assets/test.jpg" + }, + h1 "Image Before Test", +} + +slide{ + h1 "Image After Test", image{ src="assets/test.jpg" }, diff --git a/meson.build b/meson.build index 7b973ba..cde32e5 100644 --- a/meson.build +++ b/meson.build @@ -25,7 +25,12 @@ glfw3 = subproject('glfw').get_variable('glfw_dep') glm = subproject('glm').get_variable('glm_dep') freetype2 = subproject('freetype2').get_variable('freetype_dep') -dependencies = [fmt, fuc2, sol2, glfw3, glm, freetype2] +# have to disable tests because this dude's using catch2 from the 1890s +magic_enum_proj = subproject('magic_enum', default_options: { + 'test': false}) +magic_enum = magic_enum_proj.get_variable('magic_enum_dep') + +dependencies = [fmt, fuc2, sol2, glfw3, glm, freetype2, magic_enum] sources = [ 'src/glad/glad.cpp', diff --git a/shaders/image.vert.glsl b/shaders/image.vert.glsl index acd8ef1..f051cfc 100644 --- a/shaders/image.vert.glsl +++ b/shaders/image.vert.glsl @@ -1,11 +1,12 @@ #version 330 core -layout (location = 0) in vec3 aPos; +// in the original this was vec3 but in image.cpp:88 it's a 2 element float so WTF? +layout (location = 0) in vec2 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; void main() { - gl_Position = vec4(aPos, 1.0); + gl_Position = vec4(aPos, 0.0, 1.0); TexCoord = aTexCoord; } diff --git a/shaders/text.frag.glsl b/shaders/text.frag.glsl index c40f0aa..4b40ed3 100644 --- a/shaders/text.frag.glsl +++ b/shaders/text.frag.glsl @@ -2,11 +2,11 @@ in vec2 TexCoords; out vec4 color; -uniform sampler2D text; +uniform sampler2D glyph; uniform vec3 textColor; void main() { - vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); + vec4 sampled = vec4(1.0, 1.0, 1.0, texture(glyph, TexCoords).r); color = vec4(textColor, 1.0) * sampled; } diff --git a/src/engine.cpp b/src/engine.cpp index 1226cd0..d69f2b8 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -7,6 +7,7 @@ #include #include "image.hpp" #include +#include void framebuffer_size_callback(GLFWwindow *, int width, int height) { glViewport(0, 0, width, height); @@ -66,7 +67,6 @@ GLFWwindow* create_window() { } void setup() { - glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glEnable(GL_CULL_FACE); glEnable(GL_MULTISAMPLE); @@ -110,7 +110,7 @@ void Engine::render() { if(node.type == NodeType::TEXT || node.type == NodeType::H1 || node.type == NodeType::UL) { text->render(node.content, 25.0f, WINDOW_HEIGHT - y, 1.0f, glm::vec3(1.0f, 1.0f, 1.0f)); - } else if(node.type == NodeType::IMAGE && node.image != nullptr) { + } else if(node.type == NodeType::IMAGE) { dbc::check(node.image != nullptr, "Node's image not configured."); node.image->render(); } else { diff --git a/src/framebuffer.cpp b/src/framebuffer.cpp index cf9363b..99ea886 100644 --- a/src/framebuffer.cpp +++ b/src/framebuffer.cpp @@ -29,22 +29,18 @@ void FrameBuffer::init() { void FrameBuffer::begin() { glBindFramebuffer(GL_FRAMEBUFFER, target_fb); - glEnable(GL_DEPTH_TEST); glClearColor(0.0f, 1.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT); } void FrameBuffer::commit() { glBindFramebuffer(GL_FRAMEBUFFER, 0); - glDisable(GL_DEPTH_TEST); - glClearColor(0.0f, 0.0f, 1.0, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); } void FrameBuffer::draw(Screen& screen) { screen.shader.use(); glBindVertexArray(screen.target_VAO); - glActiveTexture(GL_TEXTURE0 + screen.texture_id); + glActiveTexture(GL_TEXTURE0 + screen.texture_unit); glBindTexture(GL_TEXTURE_2D, texture_color_buffer); glDrawArrays(GL_TRIANGLES, 0, 6); } diff --git a/src/image.cpp b/src/image.cpp index 528eb68..deb5681 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -61,7 +61,7 @@ void Image::render() { // use the correct texture unit glActiveTexture(GL_TEXTURE0 + texture_unit); - glBindTexture(GL_TEXTURE_2D, texture_id); + glBindTexture(GL_TEXTURE_2D, texture_id); // then I don't need to set this every time I render glBindVertexArray(image_vao); diff --git a/src/options.cpp b/src/options.cpp index 9ba3b26..0f698ee 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -42,8 +42,9 @@ Options parse_options(int argc, char* argv[]) { } if(!result.deck_given) { - print_usage(); - result.error = true; + result.deck_file = "builder_test.lua"; + // print_usage(); + // result.error = true; } return result; diff --git a/src/screen.cpp b/src/screen.cpp index c8405aa..7156fca 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -4,7 +4,7 @@ void Screen::init() { shader.use(); - shader.setInt("screenTexture", texture_id); + shader.setInt("screenTexture", texture_unit); glGenVertexArrays(1, &target_VAO); glGenBuffers(1, &target_VBO); diff --git a/src/screen.hpp b/src/screen.hpp index 80a14a4..545f82e 100644 --- a/src/screen.hpp +++ b/src/screen.hpp @@ -5,7 +5,7 @@ struct Screen { Shader shader{{"shaders/screen.vert.glsl", "shaders/screen.frag.glsl", ""}}; unsigned int target_VAO = 0; unsigned int target_VBO = 0; - unsigned int texture_id = 10; + unsigned int texture_unit = 10; float vertices[24] = { // vertex attributes for a quad that fills the entire screen in Normalized Device Coordinates. diff --git a/src/text.cpp b/src/text.cpp index 182f442..14902df 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -10,11 +10,10 @@ void Text::render(const std::string& text, float x, float y, float scale, glm::vec3 color) { text_shader.use(); - glm::mat4 projection = glm::ortho(0.0f, static_cast(WINDOW_WIDTH), 0.0f, static_cast(WINDOW_HEIGHT)); - text_shader.setMat4("projection", projection); + // BUG: might be better as a getter/setter so it only needs to run once glUniform3f(glGetUniformLocation(text_shader.ID, "textColor"), color.x, color.y, color.z); - glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE0 + texture_unit); glBindVertexArray(text_vao); for(auto c : text) { @@ -34,7 +33,7 @@ void Text::render(const std::string& text, float x, float y, float scale, glm::v { xpos + w, ypos + h, 1.0f, 0.0f } }; - // rander glyph texture over quad + // render glyph texture over quad glBindTexture(GL_TEXTURE_2D, ch.texture_id); // update content of VBO memory @@ -48,12 +47,16 @@ void Text::render(const std::string& text, float x, float y, float scale, glm::v x += (ch.advance >> 6) * scale; } - glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); + glBindVertexArray(0); } Text::Text() { - // can you create this before load_font? + text_shader.use(); + text_shader.setTextureUnit("glyph", texture_unit); + glm::mat4 projection = glm::ortho(0.0f, static_cast(WINDOW_WIDTH), 0.0f, static_cast(WINDOW_HEIGHT)); + text_shader.setMat4("projection", projection); + glGenVertexArrays(1, &text_vao); glGenBuffers(1, &text_vbo); diff --git a/src/text.hpp b/src/text.hpp index 4a22d6f..0309049 100644 --- a/src/text.hpp +++ b/src/text.hpp @@ -18,6 +18,7 @@ struct Text { unsigned int text_vao = UINT_MAX; unsigned int text_vbo = UINT_MAX; std::unordered_map characters{}; + unsigned int texture_unit = 5; void render(const std::string& text, float x, float y, float scale, glm::vec3 color); void load_font(); diff --git a/wraps/magic_enum.wrap b/wraps/magic_enum.wrap new file mode 100644 index 0000000..031a625 --- /dev/null +++ b/wraps/magic_enum.wrap @@ -0,0 +1,10 @@ +[wrap-file] +directory = magic_enum-0.9.7 +source_url = https://github.com/Neargye/magic_enum/archive/refs/tags/v0.9.7.tar.gz +source_filename = magic_enum-v0.9.7.tar.gz +source_hash = b403d3dad4ef542fdc3024fa37d3a6cedb4ad33c72e31b6d9bab89dcaf69edf7 +source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/magic_enum_0.9.7-1/magic_enum-v0.9.7.tar.gz +wrapdb_version = 0.9.7-1 + +[provide] +magic_enum = magic_enum_dep