首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FreeType2向文本添加大纲

FreeType2向文本添加大纲
EN

Stack Overflow用户
提问于 2022-09-24 09:50:57
回答 1查看 59关注 0票数 2

我试图添加边框/大纲到文本的绘制使用免费类型。到目前为止,我已经找到了关于它的自由型文档,但它们有一点不一致。有些函数/结构在我的项目中是可见的,但有些却没有。例如,我可以创建一个类似于这个FT_Outline border;的大纲对象,但是我不能调用我需要调用的函数来应用这个大纲,就像文档说的那样:FT_Outline_New( FT_Library library, FT_UInt numPoints, FT_Int numContours, FT_Outline *anoutline );这个问题也没有帮助,几乎所有的函数/结构都不存在于我的设置中。

我有点迷路了,我该如何实现我的文本的大纲呢?这些方法过时了吗?如果是的话,现代方法是什么?我链接/导入/编译自由类型错误吗?我没有任何自由类型的错误,所以我不认为我的自由类型实现有任何错误。

以下是我的文本呈现过程的样子:

Text.cpp

代码语言:javascript
运行
复制
Text::Text(text_attributes&& atrib, int gl_width, int 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))
    {
        exit(0);
    }

    std::string font_name = "./src/opengl/fonts/" + *atrib.__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_Outline border;
        
        FT_Set_Pixel_Sizes(face, 0, atrib.__font_size);

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

        for (unsigned char c = 0; c < 128; c++)
        {
            // Load character glyph 
            if (FT_Load_Char(face, c, FT_LOAD_RENDER))
            {
                VI_ERROR("ERROR::FREETYTPE: Failed to load Glyph");
                continue;
            }

            // generate texture
            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
            );
            // set texture options
            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);
            // now store character for later use
            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);
}
void Text::render_text(std::string text, float x, float y, float z, std::string hex_color, float angle_rad, bool bg)
{   
    static const int scale = 1;

    shader.use();
    glUniform3f(glGetUniformLocation(shader.ID, "textColor"), 0.1, 0.1, 0.1);//color.get_color_float(Utils::RED), color.get_color_float(Utils::GREEN), color.get_color_float(Utils::BLUE));
    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.h

代码语言:javascript
运行
复制
extern "C"{
    #include <ft2build.h>
    #include FT_FREETYPE_H
}

class Text{
   Text(text_attributes&& atrib, int w, int h);
   render_text(std::string text, float x, float y, float z, std::string hex_color, float angle_rad, bool bg);
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-24 10:10:15

你还应包括:

代码语言:javascript
运行
复制
#include FT_GLYPH_H   //optional glyph management component
#include FT_OUTLINE_H //scalable outline management
#include FT_STROKER_H //functions to stroke outline paths

另请参阅:

如果您想呈现一个大纲(按指定的顺序):

  • FT_Stroker_New
  • FT_Stroker_Set
  • FT_Get_Glyph
  • FT_Glyph_Stroke
  • 如果字形->格式化== FT_GLYPH_FORMAT_OUTLINE
    • FT_Raster_Params params
      • params.flags = FT_RASTER_FLAG_AA = FT_RASTER_FLAG_DIRECT
      • params.gray_spans = outlineRenderCallback
      • params.user = user_data

代码语言:javascript
运行
复制
- FT\_Outline\_Render
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73836256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档