【问题标题】:Can't load "glyp" properly for opengl using freetype2无法使用 freetype2 为 opengl 正确加载“glyp”
【发布时间】:2019-05-27 17:52:50
【问题描述】:

似乎 freetype 库没有正确加载字体,也没有将其转换为字节以供以后用作 opengl 纹理。

这是我使用 freetype 得到的结果:

我已经尝试过使用其他纹理,所以这不太可能是我的纹理管理器的问题。

Font(const std::string font) //constructor
        :name(font)
    {
        std::string _font = "fonts/" + font + ".ttf";

        FT_Library ft;
        FT_Init_FreeType(&ft);


        FT_Face face;
        if (FT_New_Face(ft, _font.c_str(), 0, &face))
            EXIT_ERROR(-11);

        FT_Set_Pixel_Sizes(face, 0, 48);

        for (unsigned int a = 1; a < 128; a++)
        {
            char c = a;

            if (FT_Load_Char(face, c, FT_LOAD_RENDER))
            {
                EXIT_ERROR(-12);
            }


//This just creates the FontTexture type, as I said before it works 
//fine,FontTexture is abstracted from BaseTexture which stores char* with 
//data. I also made sure that the stuff is loaded 
//there properly from inside FontTexture.


            FontTexture* _char = new FontTexture(
//here is buffer passing
face->glyph->bitmap.buffer, 
                0,
                { static_cast<float>(face->glyph->bitmap.width), 
                static_cast<float>(face->glyph->bitmap.rows) });

            _char->SetAdvance(face->glyph->advance.x);
            _char->SetBearing({ static_cast<float>(face->glyph->bitmap_left), 
                static_cast<float>(face->glyph->bitmap_top )});


//This just caches texture, so instead of loading it multiple times I can 
//just call "getTexture(name) store that pointer in the entitie's memory 
//and bind when Draw() is being called.
            TextureManager::getTextureManager().PrecacheTexture(std::to_string(a) + font, _char);

            Characters.insert(std::pair<char, FontTexture*>(c, _char));
        }

        FT_Done_Face(face);
        FT_Done_FreeType(ft);
    }

【问题讨论】:

  • “所以这不太可能是我的纹理管理器的问题” 不,这是你的纹理管理器的问题。什么是纹理格式?你在哪里FT_Glyph_To_Bitmap
  • GL_ALPHA 如果这是您的要求。
  • glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size.x, size.y, 0, GL_ALPHA, GL_UNSIGNED_BYTE, m_LocalBuffer);
  • 之前做glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
  • 必须每帧都这样做吗?我在主循环之前已经这样做了。

标签: c++ opengl freetype2


【解决方案1】:

GL_UNPACK_ALIGNMENT 参数定义从缓冲区读取图像时图像每行(行)中第一个像素的对齐方式。默认情况下,此参数为 4。
字形图像的每个像素都以一个字节编码,并且图像被紧密打包。所以字形图像的对齐方式为1,在读取图像并指定纹理之前必须更改参数:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

如果错过,这将导致图像的每一行出现移位效果(除非图像的宽度可以被 4 整除)。

【讨论】:

    【解决方案2】:

    问题是 glPixelStorei( GL_UNPACK_ALIGNMENT, 1) 标志未启用。我可能在 opengl 有有效上下文之前启用了它。

    【讨论】:

      猜你喜欢
      • 2015-02-10
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      相关资源
      最近更新 更多