如何修复Freetype错误地加载字符

时间:2019-07-16 13:47:52

标签: c opengl-es raspberry-pi3 freetype2

FreeType库打开并加载字体而不会引发错误,但是当我尝试使用字符纹理时,它们就会反复出现并且倒置。

我正在使用OpenGL创建一个基于图形的程序,以处理我正在使用FreeType的字体和文本。当我加载纹理并进行设置时,会提供正确的尺寸和字形属性(宽度,前进等),但是当我使用位图创建纹理并使用该纹理时,它是不正确的(如前所述)。

这是初始化代码:

FT_Init_FreeType(&ft);
FT_New_Face(ft, "Roboto.ttf", 0, &face);
FT_Set_Pixel_Sizes(face, 0, 100);
getChars();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
FT_Done_Face(face);
FT_Done_FreeType(ft);

下面是获取字符纹理的代码:

void getChars(){
  int index = 0;
  for (GLubyte c = 0; c < 128; c++){
    if (FT_Load_Char(face, c, FT_LOAD_RENDER)){
      printf("couldn't load character\n");
      continue;
    }
    GLuint texture;
    glGenTextures(1,&texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,face->glyph->bitmap.width,face->glyph->bitmap.rows,0,GL_RGBA,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, {face->glyph->bitmap.width, face->glyph->bitmap.rows}, {face->glyph->bitmap_left, face->glyph->bitmap_top}, face->glyph->advance.x};
    chars[index++] = character;
  }
  printf("Got characters\n");
}

我已经注释掉glTexParameteri,它与纹理的显示方式没有任何区别

以下是用于存储纹理的结构:

typedef struct vec2 {
  float x;
  float y;
} vec2;

typedef struct Character {
  GLuint Texture;
  vec2 Size;
  vec2 Bearing;
  GLuint Advance;
} Character;

Character chars[128];

最后,这是显示传递给它的字符串的代码:

void drawText(char* inString, float x, float y, float scale, colour col) {
  for (char* string = inString; *string != '\0'; string++){
    Character ch = chars[(int) *string];
    glBindTexture( GL_TEXTURE_2D, ch.Texture);

    printf("character\n");

    float xpos = x + ch.Bearing.x * scale * 2 /glutGet(GLUT_WINDOW_WIDTH);
    float ypos = y - (ch.Size.y - ch.Bearing.y) * scale * 2 /glutGet(GLUT_WINDOW_HEIGHT);

    float w = ch.Size.x * scale * 2 /glutGet(GLUT_WINDOW_WIDTH);
    float h = ch.Size.y * scale * 2 /glutGet(GLUT_WINDOW_HEIGHT);

    //draws the textured QUAD
    glBegin(GL_QUADS);
    //set the colour of the quad the texutre blends with to white with the appropriate opacity
    glColor4f(col.R, col.G, col.B, col.A);
    glTexCoord2f(0.0,0.0);
    glVertex2f(xpos, ypos);
    glTexCoord2f(0.0,1.0);
    glVertex2f(xpos, ypos+h);
    glTexCoord2f(1.0,1.0);
    glVertex2f(xpos+w, ypos+h);
    glTexCoord2f(1.0,0.0);
    glVertex2f(xpos+w, ypos);
    glEnd();

    x += (ch.Advance >> 6) * scale * 2 /glutGet(GLUT_WINDOW_WIDTH);
    printf("w %1.0f\n",w);
    printf("h %1.0f\n",h);
    printf("x %1.0f\n",xpos);
    printf("y %1.0f\n",ypos);
  }
}

我希望可以加载普通文本,但是如前所述,每个字符看起来都像其自己的纹理贴图

this is the way the text looks (ignore the image behind it)

1 个答案:

答案 0 :(得分:1)

FT_Load_Char返回的缓冲区对于字形的每个像素包含一个字节。
如果要使用现代OpenGL,则可以将GL_RED格式用于位图格式和内部纹理图像格式。其余的将做Shader program
由于您使用Legacy OpenGL,因此必须使用一种将字节值转换为RGB值的格式。为此使用GL_LUMINANCEGL_LUMINANCE“转换为RGBA元素,方法是将红色,绿色和蓝色复制3次亮度值,并为alpha附加1。”

glTexImage2D(
    GL_TEXTURE_2D,
    0,
    GL_LUMINANCE,
    face->glyph->bitmap.width,
    face->glyph->bitmap.rows,
    0,
    GL_LUMINANCE,
    GL_UNSIGNED_BYTE,
    face->glyph->bitmap.buffer
);

请注意,必须启用二维纹理化,请参见glEnable

glEnable(GL_TEXTURE_2D)

如果要使用透明背景绘制文本,则可以使用GL_ALPHA

glTexImage2D(
    GL_TEXTURE_2D,
    0,
    GL_ALPHA,
    face->glyph->bitmap.width,
    face->glyph->bitmap.rows,
    0,
    GL_ALPHA,
    GL_UNSIGNED_BYTE,
    face->glyph->bitmap.buffer
);

绘制文本时,必须启用Blending

void drawText(char* inString, float x, float y, float scale, colour col) {

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glEnable(GL_TEXTURE_2D);

    // [...]

    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
}

应该在glPixelStorei(GL_UNPACK_ALIGNMENT, 1);之前调用另外的getChars();