【问题标题】:FreeType2 Multiple characters and random colorsFreeType2 多个字符和随机颜色
【发布时间】:2017-07-15 12:30:02
【问题描述】:

我在我的 OpenGL 程序中使用 FreeType2 库进行文本渲染。我有一个用于屏幕 rgb 值的缓冲区数组。对于文本渲染,首先我初始化 FreeType2 库,然后加载字体,设置像素大小并获取 A 字符,然后获取该字形的位图,合并字形位图和我的缓冲区数组,然后使用 glTexSubImage2D 函数和渲染。我得到了这个结果。

我的 FreeType2 代码是:

assert(FT_Init_FreeType(&console->library) == 0);
assert(FT_New_Face(console->library, "data/pixelize.ttf", 0, &console->face) == 0);

assert(FT_Set_Pixel_Sizes(console->face, 0, 32) == 0);

FT_UInt glyphIndex;
glyphIndex = FT_Get_Char_Index(console->face, 'A');

assert(FT_Load_Glyph(console->face, glyphIndex, FT_LOAD_DEFAULT) == 0);
assert(FT_Render_Glyph(console->face->glyph, FT_RENDER_MODE_NORMAL) == 0);

FT_Bitmap bmp = console->face->glyph->bitmap;
_tpCopyTextToConsoleBuffer(console, bmp, 10, 10);

而_tpCopyTextToConsoleBuffer方法是

int bitmapWidth = bmp.width;
int bitmapHeight = bmp.rows;

int cbx = x; // x
int cby = y;

for(int yy = 0; yy < bitmapHeight; yy++) {
    for(int xx = 0; xx < bitmapWidth; xx++) {
        int cbIndex = _tpGetIndex(console, cbx, cby);
        int bmpIndex = (yy * bitmapWidth + xx) * 3;

        console->buffer[cbIndex] = bmp.buffer[bmpIndex];
        console->buffer[cbIndex + 1] = bmp.buffer[bmpIndex + 1];
        console->buffer[cbIndex + 2] = bmp.buffer[bmpIndex + 2];

        cbx++;
    }
    cbx = x;
    cby++;
}

_tpUpdateTexture(console);

我的代码有什么问题?

【问题讨论】:

    标签: opengl freetype2


    【解决方案1】:

    FT_RENDER_MODE_NORMAL 模式光栅化 8 位灰度图像。因此,如果您将其转换为 RGB 使用:

    for(int yy = 0; yy < bmp.rows; yy++) {
        for(int xx = 0; xx < bmp.width; xx++) {
            uint8_t *p = console->buffer + _tpGetIndex(console, x + xx, y + yy);
            const uint8_t *q = bmp.buffer + yy * bmp.pitch + xx;
            p[0] = p[1] = p[2] = *q;
        }
    }
    

    同时避免使用assert(f() == 0) 构造,因为如果您使用NDEBUG 开关关闭asserts,则根本不会调用这些函数。

    【讨论】:

    • 哇。它有效,但我不明白它是如何完成的。你能解释一下你是怎么做的吗?
    • 您的计算使用(yy * bmp.width + xx)*3 作为偏移量,因为您假设bmp 是RGB,每像素3 个字节,内存中的行紧密排列。现实情况是,bmp 是一个灰度图像,每个像素有 1 个字节,行之间有 bmp.pitch 个字节。因此上述yy * bmp.pitch + xx 的计算是正确的。
    猜你喜欢
    • 2016-02-20
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2014-02-12
    • 2016-02-21
    • 2018-11-15
    • 2015-01-31
    相关资源
    最近更新 更多