opengl - FreeType2 多个字符和随机颜色

标签 opengl freetype2

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

Render result

我的 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);

我的代码有什么问题吗?

最佳答案

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 关闭 assert switch 那么函数根本不会被调用。

关于opengl - FreeType2 多个字符和随机颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45118222/

相关文章:

c++ - 简单地调用 Pitch() 和 Yaw() 怎么会导致相机最终 Roll()?

opengl - GLSL 曲面分割控制着色器索引 gl_TessLevelOuter 与 gl_InvocationID

ios - Xcode 中的 freetype 示例程序没有结果

OpenGL 渲染模式

c++ - 构建乒乓球

OpenGL - GLenum 如何成为无符号 32 位整数?

linux - 什么是光栅化器? freetype 是 linux 操作系统的一部分还是单独的库?

c++ - Freetype 2. 加载后是否可以更改字体大小(以像素为单位)?

c++ - FreeType2 FT_Outline_Decompose 返回巨大的数字

c++ - 我可以在没有 .dll 文件的情况下使用 Win32 FreeType 吗?