问题描述
我正在使用2D游戏引擎,并且想实现文本渲染。我想使用freetype。我有以下代码:
GLuint va;
glGenVertexArrays(1,&va);
GLuint vb;
glGenBuffers(1,&vb);
glBindVertexArray(va);
glBindBuffer(GL_ARRAY_BUFFER,vb);
glBufferData(GL_ARRAY_BUFFER,sizeof(float) * 6 * 4,NULL,GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,4,GL_FLOAT,GL_FALSE,4 * sizeof(float),0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
FT_Face testface;
FT_Error error;
error = FT_New_Face(ftlib,"arial.ttf",&testface);
if (error == FT_Err_Unknown_File_Format)
{
std::cout << "Font format not supported" << std::endl;
}
else if (error)
{
std::cout << "Something else" << "\n";
}
error = FT_Set_Pixel_Sizes(testface,48);
if (error)
{
std::cout << "Problem with set px szes occ\n";
}
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
for (unsigned char c = 0; c < 128; c++)
{
if (FT_Load_Char(testface,c,FT_LOAD_RENDER))
{
std::cout << "Error. Failed to load glyph "<<c<<"\n";
continue;
}
GLuint texture;
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
glTexImage2D(
GL_TEXTURE_2D,GL_RED,testface->glyph->bitmap.width,testface->glyph->bitmap.rows,GL_UNSIGNED_BYTE,testface->glyph->bitmap.buffer
);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
Character newchar = {
texture,glm::ivec2(testface->glyph->bitmap.width,testface->glyph->bitmap.rows),glm::ivec2(testface->glyph->bitmap_left,testface->glyph->bitmap_top),testface->glyph->advance.x
};
characters.insert(std::pair<char,Character>{c,newchar});
}
FT_Done_Face(testface);
//glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glUseProgram(text_shader_program);
glUniform4f(glGetUniformLocation(text_shader_program,"textColor"),1.f,1.f);
glActiveTexture(GL_TEXTURE0);
std::string::const_iterator c;
std::string text = "TEST";
glBindVertexArray(va);
float x = 0.f;
for (c = text.begin(); c != text.end(); c++)
{
Character ch = characters[*c];
float xpos = x + ch.bearing.x;
float ypos = 0.f - (ch.size.y - ch.bearing.y);
float w = ch.size.x;
float h = ch.size.y;
float vertices[6][4] = {
{xpos,ypos + h,0.f,0.f},{xpos,ypos,1.f},{xpos + w,0.f}
};
glBindTexture(GL_TEXTURE_2D,ch.textureID);
glBindBuffer(GL_ARRAY_BUFFER,vb);
glBufferSubData(GL_ARRAY_BUFFER,sizeof(vertices),vertices);
glBindBuffer(GL_ARRAY_BUFFER,0);
glDrawArrays(GL_TRIANGLES,6);
x += (ch.advance >> 6);
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D,0);
我使用以下代码初始化库(在上述代码发生之前)
FT_Error error = FT_Init_FreeType(&ftlib);
if (error)
{
std::cout << "????" << std::endl;
}
人脸和库正确初始化,字形已加载,但未绘制任何内容。我不使用摄像头,因此所有坐标都在-1到1的空间内。
我具有以下用于着色的着色器: 顶点着色器
#version 330 core
layout(location=0) in vec4 vertex;
out vec2 TextCoords;
void main() {
gl_Position = vec4(vertex.xy,0.0,1.0);
TextCoords = vertex.zw;
}
fragmentshader
#version 330 core
in vec3 TextCoords;
out vec4 color;
uniform sampler2D text;
uniform vec4 textColor;
void main(){
vec4 sampled = vec4(1.0,1.0,texture(text,TextCoords).r);
color = textColor * sampled;
}
更新:
我将FT_Set_Pixel_Sizes(testface,48)
行更改为FT_Set_Pixel_Sizes(testface,1)
,并在文本应有的地方得到了一个巨大的灰色矩形。
有人有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)