首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >呈现FreeType字体

呈现FreeType字体
EN

Stack Overflow用户
提问于 2014-04-04 16:23:12
回答 2查看 2.9K关注 0票数 2

我一直在尝试在FreeType2 3中渲染OpenGL字体,我使用了NeHe的教程opengl/24001/。但是,我对它作了一些修改,以适应现代的OpenGL 3+。实际上,我使用glBufferData(.,GL_DYNAMIC_DRAW)来更新每个字符的顶点缓冲区。顶点数组是在字形加载期间创建的,就像NeHe对显示列表所做的那样:

代码语言:javascript
复制
    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) };

其中glyphVertices是这种结构的二维数组:

代码语言:javascript
复制
   struct Vertex2dT
   {
       glm::vec2 pos;
       glm::vec2 texCoord;
   };

不幸的是,我得到了以下结果:

那我做错什么了?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-06 13:17:37

正如SAKrisT正确指出的那样,这个问题是由Y轴不正确的偏移引起的。玩了一下代码,我想出了解决方案:

代码语言:javascript
复制
   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) };

现在它工作得很好!

票数 3
EN

Stack Overflow用户

发布于 2014-04-04 16:32:29

发生了两件事:您的代码在左上角假设视图端口的来源,而实际上它位于左下角,这意味着您的图像是垂直翻转的。

FreeType也在左上角呈现原点的字形图像,这与几何的y翻转相反,因此字符看起来是直立的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22868070

复制
相关文章

相似问题

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