Skip to content

Commit

Permalink
[TextRenderer] text face camera
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthou committed Aug 16, 2024
1 parent 23b475b commit bc52384
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
3 changes: 2 additions & 1 deletion include/overworld/Engine/Graphics/OpenGL/TextRenderer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef OWDS_TEXTRENDERER_H
#define OWDS_TEXTRENDERER_H

#include <glm/matrix.hpp>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <map>
Expand All @@ -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_;
Expand Down
3 changes: 1 addition & 2 deletions src/Engine/Graphics/OpenGL/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 17 additions & 8 deletions src/Engine/Graphics/OpenGL/TextRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "overworld/Engine/Graphics/OpenGL/TextRenderer.h"

#include <freetype/freetype.h>
#include <glm/gtc/packing.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <string>

#include "glad/glad.h"
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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_);
Expand Down

0 comments on commit bc52384

Please sign in to comment.