FreeType add border to text

Viewed 31

I've made an OpenGL c++ program that can display some text into the screen. Is there a freetype function that allows me to create bordered text?

Here is what I have so far:


#include "text_render.h"

namespace OpenGL {

void Text::render_text(std::string text, float x, float y, float z, float scale, std::string hex_color, float angle_rad, bool bg)
{   
    shader.use();
    glUniform3f(glGetUniformLocation(shader.ID, "textColor"), 0.1, 0.1, 0.1);
    glActiveTexture(GL_TEXTURE0);
    glBindVertexArray(VAO);

    GLfloat vertices[6][4] = {
        { 0.0,  1.0,   0.0, 0.0 },
        { 0.0,  0.0,   0.0, 1.0 },
        { 1.0,  0.0,   1.0, 1.0 },

        { 0.0,  1.0,   0.0, 0.0 },
        { 1.0,  0.0,   1.0, 1.0 },
        { 1.0,  1.0,   1.0, 0.0 }
    };

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
    glBindBuffer(GL_ARRAY_BUFFER, 0);


    glm::mat4 rotateM = glm::rotate(glm::mat4(1.0f), glm::radians(angle_rad), glm::vec3(0.0f, 0.0f, 1.0f));
    glm::mat4 transOriginM = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, z));

    std::string::const_iterator c;
    GLfloat char_x = 0.0f;
    for (c = text.begin(); c != text.end(); c++)
    {
        Character ch = Characters[*c];

        GLfloat w = ch.Size.x * scale;
        GLfloat h = ch.Size.y * scale;
        GLfloat xrel = char_x + ch.Bearing.x * scale;
        GLfloat yrel = y - (ch.Size.y - ch.Bearing.y) * scale;

        char_x += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))

        glm::mat4 scaleM = glm::scale(glm::mat4(1.0f), glm::vec3(w, h, 1.0f));
        glm::mat4 transRelM = glm::translate(glm::mat4(1.0f), glm::vec3(xrel, yrel, z));

        glm::mat4 modelM = transOriginM * rotateM * transRelM * scaleM;

        GLint model_loc = glGetUniformLocation(shader.ID, "model");
        glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm::value_ptr(modelM));

        glBindTexture(GL_TEXTURE_2D, ch.TextureID);

        glDrawArrays(GL_TRIANGLES, 0, 6);
    }
    glBindVertexArray(0);
    glBindTexture(GL_TEXTURE_2D, 0);
}

Text::Text(){}

Text::Text(std::string* __font_name, int gl_width, int gl_height) 
    :SCR_WIDTH(gl_width), SCR_HEIGHT(gl_height)
{
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    

    shader = Shader("./src/opengl/shaders/text.vs", "./src/opengl/shaders/text.fs");

    glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(SCR_WIDTH), 0.0f, static_cast<float>(SCR_HEIGHT));
    shader.use();
    glUniformMatrix4fv(glGetUniformLocation(shader.ID, "projection"), 1, GL_FALSE, glm::value_ptr(projection));

    if (FT_Init_FreeType(&ft))
    {
        VI_ERROR("ERROR::FREETYPE: Could not init FreeType Library");
        exit(0);
    }

    std::string font_name = "./src/opengl/fonts/" + *__font_name +  ".ttf";
    
    FT_Face face;
    if (FT_New_Face(ft, font_name.c_str(), 0, &face)) {
        VI_ERROR("ERROR::FREETYPE: Failed to load font");
        exit(0) ;
    }
    else {
        FT_Set_Pixel_Sizes(face, 0, 48);

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

        for (unsigned char c = 0; c < 128; c++)
        {
            if (FT_Load_Char(face, c, FT_LOAD_RENDER))
            {
                VI_ERROR("ERROR::FREETYTPE: Failed to load Glyph");
                continue;
            }
            GLuint texture;
            glGenTextures(1, &texture);
            glBindTexture(GL_TEXTURE_2D, texture);
            glTexImage2D(
                GL_TEXTURE_2D,
                0,
                GL_RED,
                face->glyph->bitmap.width,
                face->glyph->bitmap.rows,
                0,
                GL_RED,
                GL_UNSIGNED_BYTE,
                face->glyph->bitmap.buffer
            );
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            Character character = {
                texture,
                glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
                glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
                static_cast<unsigned int>(face->glyph->advance.x)
            };
            Characters.insert(std::pair<char, Character>(c, character));
        }
        glBindTexture(GL_TEXTURE_2D, 0);
    }
    FT_Done_Face(face);
    FT_Done_FreeType(ft);
    
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}
}

vertex shader:

#version 300 es
precision mediump float;
layout (location = 0) in vec4 in_attr;
out vec2 vUV;
uniform mat4 projection;
uniform mat4 model;

void main()
{
    vUV         = in_attr.zw;
        gl_Position = projection * model * vec4(in_attr.xy, 1.0f, 1.0f);
}

fragment shader:

#version 300 es
precision mediump float;

in vec2 vUV;
uniform sampler2D u_texture;
uniform vec3 textColor;
out vec4 fragColor;
void main()
{
    vec2 uv = vUV.xy;
    float text = texture(u_texture, uv).r;
    fragColor = vec4(textColor.rgb*text, text);
}
0 Answers
Related