如何绘制几何字体

时间:2019-07-05 03:25:07

标签: opengl glsl freetype

我正在使用freetype在我的应用程序中绘制字体。 目前,我正在创建一组纹理并将其映射到矩形上。

请指导我如何绘制字体而不是纹理的几何图形。

我想使用GL_TRIANGLES绘制填充的字母形状。

这是我当前的代码,该代码为字体字符创建纹理。

void TextRenderer::Load(std::string font, GLuint fontSize)
{
// First clear the previously loaded Characters
  this->Characters.clear();
  // Then initialize and load the FreeType library
  FT_Library ft;

   if (FT_Init_FreeType(&ft))  
    std::cout << "Error::FREETYPE: Could not init FreeType Library" << 
    std::endl;


FT_Face face;

if (FT_New_Face(ft, font.c_str(), 0, &face))
    std::cout << "ERROR::FREETYPE Failed to load font" << std::endl;


FT_Set_Pixel_Sizes(face, 0, fontSize);

// Disable byte-alignment restriction
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

// Then for the first 128 ASCII characters, pre-load/ compile their characters and store them

for (GLubyte c = 0; c < 128; c++)
{
    if (FT_Load_Char(face, c, FT_LOAD_RENDER))
    {
        std::cout << "ERROR::FREETYPE: Failed to load Glyph" << std::endl;
        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),
        face->glyph->advance.x
    };
    Characters.insert(std::pair<GLchar, Character>(c, character));
}
glBindTexture(GL_TEXTURE_2D, 0);
// destroy FreeType once we're finished
FT_Done_Face(face);
FT_Done_FreeType(ft);

}

0 个答案:

没有答案