简体   繁体   English

渲染FreeType字体

[英]Rendering FreeType fonts

I have been trying to render FreeType2 fonts in OpenGL 3. I used NeHe's tutorial http://nehe.gamedev.net/tutorial/freetype_fonts_in_opengl/24001/ . 我一直在尝试在OpenGL 3中渲染FreeType2字体。我使用了NeHe的教程http://nehe.gamedev.net/tutorial/freetype_fonts_in_opengl/24001/ however, I modified it a little for modern OpenGL 3+. 但是,我对现代OpenGL 3+做了一些修改。 Actually, I use glBufferData(...,GL_DYNAMIC_DRAW) to update vertex buffer for every character. 实际上,我使用glBufferData(...,GL_DYNAMIC_DRAW)为每个字符更新顶点缓冲区。 The arrays of vertices are created during glyph loading as NeHe does with display lists: 顶点数组是在字形加载期间创建的,就像NeHe对显示列表所做的那样:

    int width = next_p2(bitmap.width);
    int height = next_p2(bitmap.rows);

    float   x=(float)bitmap.width / (float)width,
        y= (float)bitmap.rows / (float)height;


    using namespace glm;
    glyphsVertices[c] = new pxgVertex2dT[4];
    glyphsVertices[c][0] = {  vec2(0,bitmap.rows), vec2(0,y) };
    glyphsVertices[c][1] = {  vec2(0,0), vec2(0,0) };
    glyphsVertices[c][2] = {  vec2(bitmap.width,0), vec2(x,0) };
    glyphsVertices[c][3] = {  vec2(bitmap.width,bitmap.rows), vec2(x,y) };

Where glyphVertices is two-dimentional array of such structure: 其中glyphVertices是此类结构的二维数组:

   struct Vertex2dT
   {
       glm::vec2 pos;
       glm::vec2 texCoord;
   };

Unfortunately, I get the following result: 不幸的是,我得到以下结果:

代码结果

So, what am I doing wrong? 那么,我在做什么错呢?

As SAKrisT correctly pointed out, the problem was caused by incorrect offset by Y axis. 正如SAKrisT正确指出的那样,问题是由Y轴的不正确偏移引起的。 Playing a bit with the code, I figured out the solution: 玩一点代码,我想出了解决方案:

   if(FT_Load_Char(face, c, FT_LOAD_RENDER))
     return;

    FT_GlyphSlot g = face->glyph;

    int width = next_p2(g->bitmap.width);
    int height = next_p2(g->bitmap.rows);

    ...

    float   x=(float)g->bitmap.width / (float)width,
            y= (float)g->bitmap.rows / (float)height;



    float x2 = g->bitmap_left;
    float y2 = g->bitmap_top;
    float w = g->bitmap.width;
    float h = g->bitmap.rows;


    using namespace glm;
    glyphsVertices[c] = new pxgVertex2dT[4];
    glyphsVertices[c][0] = {  vec2(0,h-y2), vec2(0,y) };
    glyphsVertices[c][1] = {  vec2(0,-y2), vec2(0,0) };
    glyphsVertices[c][2] = {  vec2(w,-y2), vec2(x,0) };
    glyphsVertices[c][3] = {  vec2(w,h-y2), vec2(x,y) };

Now it works perfectly! 现在,它可以完美运行了!

Two things happening: Your code assumes the origin of the viewport in the upper left, while in reality it's in the lower left, which means your image is vertically flipped. 发生两件事:您的代码假定视口的原点位于左上角,而实际上它位于左下角,这意味着您的图像是垂直翻转的。

FreeType renders the glyph images with the origin in the upper left as well, which counters the y-flip of the geometry, hence the characters look upright. FreeType渲染字形图像,其原点也位于左上角,这可抵消几何图形的y翻转,因此字符看起来是直立的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM