【发布时间】:2015-07-06 10:37:41
【问题描述】:
我正在使用 OpenGL 3.3 上下文和 GLFW3 进行窗口化。我也在用C。 我正在使用 STB_truetype 将字符光栅化为动态分配的无符号字符。
我是这样从 STB_truetype 获取数据的:
unsigned char ttf_buffer[1<<25];
stbtt_fontinfo font;
fread(ttf_buffer, 1, 1<<25, fopen("Script Thing.ttf", "rb"));
stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));
int w,h, xoff, yoff;
int c = 'C', s = 60;
unsigned char *bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, &xoff,&yoff);
为了确保我拥有正确的数据,我将其打印到控制台:
for(int i = 0; i < h; i++)
{
for(int x = 0; x < w; x++)
{
printf("%c", bitmap[x+i*w]>>5 ? '1' : ' ');
}
printf("\n");
}
然后我创建 2D OpenGL 纹理:
glGenTextures(1,&Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RED, w,h,
0, GL_RED, GL_UNSIGNED_BYTE, NewTemp );
我用“A”字符对其进行了测试,我得到了:
11111
1111111
111111111
1111111111
11111111111
111111 11111
111111 11111
111111 1111
111111 1111
11111 1111
111111 1111
111111111111 11111
1111111111111111111111
111111111111111111111
111111111111111111111
111111111 1111111111
11111 111111
1111 11111111
11111 11111111
1111 11111111
1111 111 111
1111 111 1111
1111 1111 111
111 1111 111
111 111 111
1111 111 1111
1111 11111111
1111 1111111
1 111
我渲染纹理 OpenGL 上下文:
如你所见,没错。
但是当我尝试 C 字符时:
11
1111111111
1111111111111
111111111111111
11111111111111111
1111111 111111
111111 11111
111111 1111
11111 1111
11111 11
1111
11111
1111
1111
1111
1111
1111
1111
1111
111
111
1111
1111
111
1111
1111 11
111111 11111
11111111111111111
111111111111
11
'X' 等其他字符也一样:
1111 1111
11111 11111
11111 111111
111111 111111
11111 1111111
11111 1111111
11111 1111111
1111 1111111
11111111111
1111111111
11111111
1111111
111111
111111
1111111
11111111
111111111
11111 1111
11111 1111
11111 1111
1111 1111
1111 1111
1111 111
111 1111
111 111
111 1111
11 11111
1111 11111
11 1
知道我可能做错了什么吗? 谢谢。
编辑: 通过添加 glPixelStorei( GL_UNPACK_ALIGNMENT, 1); 修复它 就在 glTexImage2D 之前。 再次感谢 doron
【问题讨论】:
-
这里有很多代码但是你没有明确说明问题。
-
既然您对
a的文字渲染似乎是正确的(取决于字体),这是否是关于字体的问题,而更多的是关于位图的问题? -
当我复制图像并将相同的东西粘贴到它的右侧时,它看起来不像蛇,更像是扭曲的
a