首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FreeType2多字符和随机颜色

FreeType2多字符和随机颜色
EN

Stack Overflow用户
提问于 2017-07-15 20:30:02
回答 1查看 111关注 0票数 2

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

我的FreeType2代码是:

代码语言:javascript
运行
复制
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方法是

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

我的代码出了什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-15 21:14:16

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

代码语言:javascript
运行
复制
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,那么函数将根本不会被调用。

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

https://stackoverflow.com/questions/45118222

复制
相关文章

相似问题

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