From bc52384ad81bc030f5672d98d0bf4bd92cef884e Mon Sep 17 00:00:00 2001 From: Guillaume Sarthou Date: Fri, 16 Aug 2024 18:37:07 +0200 Subject: [PATCH] [TextRenderer] text face camera --- .../Engine/Graphics/OpenGL/TextRenderer.h | 3 ++- src/Engine/Graphics/OpenGL/Renderer.cpp | 3 +-- src/Engine/Graphics/OpenGL/TextRenderer.cpp | 25 +++++++++++++------ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h b/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h index b28fceed..ddd47d39 100644 --- a/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h +++ b/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h @@ -1,6 +1,7 @@ #ifndef OWDS_TEXTRENDERER_H #define OWDS_TEXTRENDERER_H +#include #include #include #include @@ -25,7 +26,7 @@ namespace owds { bool load(const std::string& font, unsigned int font_size); - void renderText(Shader& shader, 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 std::string& text, const glm::vec3& position, float height, const glm::vec3& color, bool center = false); private: unsigned int vao_; diff --git a/src/Engine/Graphics/OpenGL/Renderer.cpp b/src/Engine/Graphics/OpenGL/Renderer.cpp index e856d1eb..fecc1b96 100644 --- a/src/Engine/Graphics/OpenGL/Renderer.cpp +++ b/src/Engine/Graphics/OpenGL/Renderer.cpp @@ -412,9 +412,8 @@ namespace owds { text_shader.use(); text_shader.setMat4("projection", render_camera_.getProjectionMatrix()); - text_shader.setMat4("view", render_camera_.getViewMatrix()); - text_renderer_.renderText(text_shader, "tesH", glm::vec3(0, 0, 5), 1, glm::vec3(1.f, 0.f, 1.f), true); + text_renderer_.renderText(text_shader, render_camera_.getViewMatrix(), "tesH", glm::vec3(0, 0, 5), 1, glm::vec3(1.f, 0.f, 1.f), true); // 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 d9c42422..a5a147cb 100644 --- a/src/Engine/Graphics/OpenGL/TextRenderer.cpp +++ b/src/Engine/Graphics/OpenGL/TextRenderer.cpp @@ -1,6 +1,8 @@ #include "overworld/Engine/Graphics/OpenGL/TextRenderer.h" #include +#include +#include #include #include "glad/glad.h" @@ -88,7 +90,7 @@ namespace owds { return true; } - void TextRenderer::renderText(Shader& shader, const std::string& text, const glm::vec3& position, float height, const glm::vec3& color, bool center) + 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(); shader.setVec3("text_color", color); @@ -102,6 +104,13 @@ namespace owds { float y = position.y; float z = position.z; + glm::vec4 center_in_world(x, y, z, 1.); + glm::vec4 center_in_cam = view_matrix * center_in_world; + + x = center_in_cam.x; + y = center_in_cam.y; + z = center_in_cam.z; + if(center) { float width = 0; @@ -121,19 +130,19 @@ namespace owds { Character_t& ch = characters_[*c]; float xpos = x + ch.bearing.x * height; - float zpos = z - (ch.size.y - ch.bearing.y) * height; + float ypos = y - (ch.size.y - ch.bearing.y) * height; float w = ch.size.x * height; float h = ch.size.y * height; float vertices[6 * 5] = { - xpos, y, zpos + h, 0.0f, 0.0f, - xpos, y, zpos, 0.0f, 1.0f, - xpos + w, y, zpos, 1.0f, 1.0f, + xpos, ypos + h, z, 0.0f, 0.0f, + xpos, ypos, z, 0.0f, 1.0f, + xpos + w, ypos, z, 1.0f, 1.0f, - xpos, y, zpos + h, 0.0f, 0.0f, - xpos + w, y, zpos, 1.0f, 1.0f, - xpos + w, y, zpos + h, 1.0f, 0.0f}; + xpos, ypos + h, z, 0.0f, 0.0f, + xpos + w, ypos, z, 1.0f, 1.0f, + xpos + w, ypos + h, z, 1.0f, 0.0f}; glBindTexture(GL_TEXTURE_2D, ch.texture_id_); glBindBuffer(GL_ARRAY_BUFFER, vbo_);