diff --git a/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h b/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h index df538d0f..b28fceed 100644 --- a/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h +++ b/include/overworld/Engine/Graphics/OpenGL/TextRenderer.h @@ -25,7 +25,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); + void renderText(Shader& shader, 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 a8006115..e856d1eb 100644 --- a/src/Engine/Graphics/OpenGL/Renderer.cpp +++ b/src/Engine/Graphics/OpenGL/Renderer.cpp @@ -414,7 +414,7 @@ namespace owds { 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)); + text_renderer_.renderText(text_shader, "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 90b7ef35..d9c42422 100644 --- a/src/Engine/Graphics/OpenGL/TextRenderer.cpp +++ b/src/Engine/Graphics/OpenGL/TextRenderer.cpp @@ -88,7 +88,7 @@ namespace owds { return true; } - void TextRenderer::renderText(Shader& shader, const std::string& text, const glm::vec3& position, float height, const glm::vec3& color) + void TextRenderer::renderText(Shader& shader, 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 +102,19 @@ namespace owds { float y = position.y; float z = position.z; + if(center) + { + float width = 0; + std::string::const_iterator c; + for(c = text.begin(); c != text.end(); c++) + { + Character_t& ch = characters_[*c]; + width += (ch.advance >> 6) * height; // bitshift by 6 to get value in pixels (1/64th times 2^6 = 64) + } + + x -= width / 2.f; + } + std::string::const_iterator c; for(c = text.begin(); c != text.end(); c++) {