Skip to content

Commit

Permalink
[TextRenderer] center option
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthou committed Aug 16, 2024
1 parent 1378b17 commit 23b475b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion include/overworld/Engine/Graphics/OpenGL/TextRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Graphics/OpenGL/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 14 additions & 1 deletion src/Engine/Graphics/OpenGL/TextRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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++)
{
Expand Down

0 comments on commit 23b475b

Please sign in to comment.