使用Freetype和OpenGL渲染字体 [英] Rendering fonts with Freetype and OpenGL

查看:884
本文介绍了使用Freetype和OpenGL渲染字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题freetype和OpenGL.我只需要在单个纹理上绘制所有已加载的符号.这是

I have problem freetype and OpenGL. I need just draw all loaded symbols on single texture. Here's:

    FT_Init_FreeType(&lib);
    FT_New_Face(lib, "C:\\verdana.ttf", 0, &face);
    FT_Set_Pixel_Sizes(face, 0, size);

    auto ww = 256 * size;
    auto hh = size;

    std::vector<unsigned char> buffer(ww * hh, 0);

    int off = 0;

    for (int c = 0; c < 256; c++)
    {
        FT_UInt GlyphIndex;

        GlyphIndex = FT_Get_Char_Index(face, c);

        FT_Load_Char(face, GlyphIndex, FT_LOAD_RENDER);

        FT_Bitmap bmp = face->glyph->bitmap;

        int advance = (face->glyph->advance.x >> 6);
        int bW = bmp.width; 
        int bH = bmp.rows;

        for (int h = 0; h < bH; ++h) {
            for (int w = 0; w < bW; ++w) {

                buffer[h * bW + off + w] = bmp.buffer[w + bW * h];
            }
        }

        off += advance;

    }


    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    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);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, ww, hh, 0, GL_ALPHA, GL_UNSIGNED_BYTE, &buffer[0]);

我尝试了许多方法来做到这一点.但是我得到的绝对是黑色的质地... 我的代码有什么问题?

I tried many ways to do this. But all I get is absolutely black texture... What's wrong with my code?

推荐答案

我认为问题在于0到255之间的某些值不可见或无法绘制,这就是为什么您什么都没得到的原因.

I think the problem is that some values between 0-255 is not visible or drawable character and that's why you get nothing.

您应该检查GlyphIndex:

you should check for GlyphIndex:

GlyphIndex = FT_Get_Char_Index(face, c);
if (!GlyphIndex) continue;

那么您可以期望freetype为您绘制其余字符.

then you can expect freetype to draw the rest of characters for you.

这篇关于使用Freetype和OpenGL渲染字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆