我一直在尝试在FreeType2 3中渲染OpenGL字体,我使用了NeHe的教程opengl/24001/。但是,我对它作了一些修改,以适应现代的OpenGL 3+。实际上,我使用glBufferData(.,GL_DYNAMIC_DRAW)来更新每个字符的顶点缓冲区。顶点数组是在字形加载期间创建的,就像NeHe对显示列表所做的那样:
int width = next_p2(bitmap.width);
int height = next_p2(bitmap.rows);
float x=(float)bitmap.width / (float)width,
y= (float)bitmap.rows / (float)height;
using namespace glm;
glyphsVertices[c] = new pxgVertex2dT[4];
glyphsVertices[c][0] = { vec2(0,bitmap.rows), vec2(0,y) };
glyphsVertices[c][1] = { vec2(0,0), vec2(0,0) };
glyphsVertices[c][2] = { vec2(bitmap.width,0), vec2(x,0) };
glyphsVertices[c][3] = { vec2(bitmap.width,bitmap.rows), vec2(x,y) };其中glyphVertices是这种结构的二维数组:
struct Vertex2dT
{
glm::vec2 pos;
glm::vec2 texCoord;
};不幸的是,我得到了以下结果:

那我做错什么了?
发布于 2014-04-06 13:17:37
正如SAKrisT正确指出的那样,这个问题是由Y轴不正确的偏移引起的。玩了一下代码,我想出了解决方案:
if(FT_Load_Char(face, c, FT_LOAD_RENDER))
return;
FT_GlyphSlot g = face->glyph;
int width = next_p2(g->bitmap.width);
int height = next_p2(g->bitmap.rows);
...
float x=(float)g->bitmap.width / (float)width,
y= (float)g->bitmap.rows / (float)height;
float x2 = g->bitmap_left;
float y2 = g->bitmap_top;
float w = g->bitmap.width;
float h = g->bitmap.rows;
using namespace glm;
glyphsVertices[c] = new pxgVertex2dT[4];
glyphsVertices[c][0] = { vec2(0,h-y2), vec2(0,y) };
glyphsVertices[c][1] = { vec2(0,-y2), vec2(0,0) };
glyphsVertices[c][2] = { vec2(w,-y2), vec2(x,0) };
glyphsVertices[c][3] = { vec2(w,h-y2), vec2(x,y) };现在它工作得很好!
发布于 2014-04-04 16:32:29
发生了两件事:您的代码在左上角假设视图端口的来源,而实际上它位于左下角,这意味着您的图像是垂直翻转的。
FreeType也在左上角呈现原点的字形图像,这与几何的y翻转相反,因此字符看起来是直立的。
https://stackoverflow.com/questions/22868070
复制相似问题