From 25bdaef34bd8088a58c080d18d2dbf6d71838389 Mon Sep 17 00:00:00 2001 From: Guillaume Sarthou Date: Fri, 16 Aug 2024 19:05:42 +0200 Subject: [PATCH] [World] add debug texts --- .../overworld/Engine/Common/Debug/DebugText.h | 20 +++++++ include/overworld/Engine/Common/World.h | 10 ++++ .../Engine/Graphics/OpenGL/TextRenderer.h | 3 ++ src/Engine/Common/World.cpp | 53 +++++++++++++++++++ src/Engine/Graphics/OpenGL/Renderer.cpp | 7 ++- src/Engine/Graphics/OpenGL/TextRenderer.cpp | 10 ++++ src/Nodes/TestOpengl.cpp | 21 +++++--- 7 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 include/overworld/Engine/Common/Debug/DebugText.h diff --git a/include/overworld/Engine/Common/Debug/DebugText.h b/include/overworld/Engine/Common/Debug/DebugText.h new file mode 100644 index 00000000..1ecae354 --- /dev/null +++ b/include/overworld/Engine/Common/Debug/DebugText.h @@ -0,0 +1,20 @@ +#ifndef OWDS_DEBUGTEXT_H +#define OWDS_DEBUGTEXT_H + +#include +#include + +namespace owds { + + struct DebugText_t + { + std::string text; + bool centered = false; + glm::vec3 position; + glm::vec3 color; + float height = 0.2; + }; + +} // namespace owds + +#endif // OWDS_DEBUGTEXT_H \ No newline at end of file diff --git a/include/overworld/Engine/Common/World.h b/include/overworld/Engine/Common/World.h index 359c5fd1..df39c03b 100755 --- a/include/overworld/Engine/Common/World.h +++ b/include/overworld/Engine/Common/World.h @@ -8,6 +8,7 @@ #include #include +#include "overworld/Engine/Common/Debug/DebugText.h" #include "overworld/Engine/Common/Lights/AmbientLight.h" #include "overworld/Engine/Common/Lights/PointLights.h" #include "overworld/Engine/Common/Shapes/Shape.h" @@ -208,6 +209,14 @@ namespace owds { void setPointLightPosition(std::size_t id, const glm::vec3& position); void setPointLightAmbientStrength(std::size_t id, float strength); + int addDebugText(const std::string& text, + const std::array& position, + float height, + const std::array& color = {1.0, 1.0, 1.0}, + bool centered = true, + int replace_id = -1); + void removeDebugText(int id); + /** * @param gravity Self-explanatory. */ @@ -234,6 +243,7 @@ namespace owds { AmbientLight ambient_light_; PointLights point_lights_; + std::vector debug_texts_; }; } // namespace owds diff --git a/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h b/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h index ddd47d39..65223d31 100644 --- a/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h +++ b/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h @@ -7,6 +7,8 @@ #include #include +#include "overworld/Engine/Common/Debug/DebugText.h" + namespace owds { class Shader; @@ -27,6 +29,7 @@ namespace owds { bool load(const std::string& font, unsigned int font_size); void renderText(Shader& shader, const glm::mat4& view_matrix, const std::string& text, const glm::vec3& position, float height, const glm::vec3& color, bool center = false); + void renderText(Shader& shader, const glm::mat4& view_matrix, const DebugText_t& text); private: unsigned int vao_; diff --git a/src/Engine/Common/World.cpp b/src/Engine/Common/World.cpp index c1623442..57a23408 100755 --- a/src/Engine/Common/World.cpp +++ b/src/Engine/Common/World.cpp @@ -398,4 +398,57 @@ namespace owds { point_lights_.setAmbientStrength(id, strength); } + int World::addDebugText(const std::string& text, + const std::array& position, + float height, + const std::array& color, + bool centered, + int replace_id) + { + DebugText_t debug{ + text, + centered, + glm::vec3(position[0], position[1], position[2]), + glm::vec3(color[0], color[1], color[2]), + height}; + + if(replace_id >= 0) + { + if(replace_id < (int)debug_texts_.size()) + { + debug_texts_[replace_id] = debug; + return replace_id; + } + else + return -1; + } + else + { + int id = -1; + for(size_t i = 0; i < debug_texts_.size(); i++) + { + if(debug_texts_[i].text.empty()) + { + id = i; + break; + } + } + + if(id >= 0) + debug_texts_[id] = debug; + else + { + id = debug_texts_.size(); + debug_texts_.emplace_back(debug); + } + return id; + } + } + + void World::removeDebugText(int id) + { + if(id >= 0 && id < (int)debug_texts_.size()) + debug_texts_[id].text = ""; + } + } // namespace owds \ No newline at end of file diff --git a/src/Engine/Graphics/OpenGL/Renderer.cpp b/src/Engine/Graphics/OpenGL/Renderer.cpp index fecc1b96..c292d8e6 100644 --- a/src/Engine/Graphics/OpenGL/Renderer.cpp +++ b/src/Engine/Graphics/OpenGL/Renderer.cpp @@ -412,8 +412,11 @@ namespace owds { text_shader.use(); text_shader.setMat4("projection", render_camera_.getProjectionMatrix()); - - text_renderer_.renderText(text_shader, render_camera_.getViewMatrix(), "tesH", glm::vec3(0, 0, 5), 1, glm::vec3(1.f, 0.f, 1.f), true); + for(auto& debug : world_->debug_texts_) + { + if(debug.text.empty() == false) + text_renderer_.renderText(text_shader, render_camera_.getViewMatrix(), debug); + } // 2. now blit multisampled buffer(s) to normal colorbuffer of intermediate FBO. Image is stored in screenTexture glDisable(GL_BLEND); diff --git a/src/Engine/Graphics/OpenGL/TextRenderer.cpp b/src/Engine/Graphics/OpenGL/TextRenderer.cpp index a5a147cb..f95caf65 100644 --- a/src/Engine/Graphics/OpenGL/TextRenderer.cpp +++ b/src/Engine/Graphics/OpenGL/TextRenderer.cpp @@ -90,6 +90,16 @@ namespace owds { return true; } + void TextRenderer::renderText(Shader& shader, const glm::mat4& view_matrix, const DebugText_t& text) + { + renderText(shader, view_matrix, + text.text, + text.position, + text.height, + text.color, + text.centered); + } + void TextRenderer::renderText(Shader& shader, const glm::mat4& view_matrix, const std::string& text, const glm::vec3& position, float height, const glm::vec3& color, bool center) { shader.use(); diff --git a/src/Nodes/TestOpengl.cpp b/src/Nodes/TestOpengl.cpp index fbeda9d7..b7d7cf7b 100644 --- a/src/Nodes/TestOpengl.cpp +++ b/src/Nodes/TestOpengl.cpp @@ -47,19 +47,26 @@ int main() std::cout << "================== WORLD CREATED ! ================" << std::endl; - world.setAmbientLight({-0.2f, -0.5f, -0.3f}, + world.setAmbientLight({0.5f, 0.2f, -0.3f}, {1.0f, 0.976f, 0.898f}, - 0.3, 0.5, 0.9); + 0.25, 0.4, 0.8); - world.addPointLight({1.5f, 2.0f, 2.9f}, + world.addPointLight({5.f, 7.0f, 2.9f}, {1.0f, 1.0f, 1.0f}, 0.4, 0.5, 1.0, - 5.f); + 6.f); - world.addPointLight({1.5f, 10.0f, 2.9f}, - {0.0f, 1.0f, 1.0f}, + world.addPointLight({8.5f, 7.0f, 2.9f}, + {1.0f, 1.0f, 1.0f}, 0.5, 0.6, 1.0, - 8.0f); + 6.0f); + + world.addPointLight({5.f, 12.0f, 2.9f}, + {1.0f, 1.0f, 1.0f}, + 0.4, 0.5, 1.0, + 6.f); + + world.addDebugText("overworld", {5, 5, 5}, 0.5, {0., 0.5, 1.0}); renderer.attachWorld(&world);