我使用FreeType库来提取每个字形信息,例如宽度,高度和位图。这是在init函数中完成的,我并不关心它所花费的时间。我将每个字符信息存储在地图容器中,以便稍后我可以轻松访问每个字符。
在渲染时我读取字符串并使用字符串迭代器遍历每个字符并使用字形参数创建我想要绘制位图的多边形。
使用VAO和VBO
绘制多边形为了使其透明,我使用混合,着色器用于着色。
这就是我的渲染功能:
void CFreeType::RenderText(std::string text, int posX, int posY, Vector3d color)
{
ViewOrtho(RESx, RESy);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
textShader->Use();
glUniform3dv(textColor_uniform_location, 1, color.v);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
this->x = posX;
this->y = posY + FontHeight;
std::string::const_iterator c;
for(c = text.begin(); c != text.end(); c++)
{
//Load character from map
Character ch = Characters[*c];
//If we hit the '^' character, we probably want to change the text color
if(*c == '^')
{
//Check if the next character is a number, if so, change the text color and skip this character.
c++;
ch = Characters[*c];
if(isdigit(*c))
{
glUniform3dv(glGetUniformLocation(textShader->GetProgram(), "textColor"), 1, Color[*c-48].v); // *c-48 for conversion from ASCII to in int - '0' == 48
continue;
}
//In the other case go back to previous character ('^').
else
{
c--;
ch = Characters[*c];
}
}
//If we hit a new line character, move the new line below the previous and skip this character.
else if(*c == '\n')
{
x = posX;
y += FontHeight;
continue;
}
//If we hit tab character, insert 4 spaces
else if(*c == '\t')
{
x += (Characters[' '].AdvanceX >> 6) << 2; //Bit shifting is hopefuly a bit faster than multiplying/dividing.
continue;
}
xpos = x + ch.Bearing.x;
ypos = y - ch.Bearing.y;
font_width = ch.Size.x;
font_height = ch.Size.y;
float vertices[6][4] = {
{xpos, ypos, 0.0, 0.0},
{xpos, ypos + this->font_height, 0.0, 1.0},
{xpos + this->font_width, ypos + this->font_height, 1.0, 1.0},
{xpos, ypos, 0.0, 0.0},
{xpos + this->font_width, ypos + this->font_height, 1.0, 1.0},
{xpos + this->font_width, ypos, 1.0, 0.0}
};
//Render character
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glDrawArrays(GL_TRIANGLES, 0, 6);
x += (ch.AdvanceX >> 6);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
ViewPerspective();
textShader->StopShader();
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
}
我在考虑使用显示列表而不是VBO,但我认为它不会有任何改进。
大约绘制了400个字符,我只得到大约165FPS,没有任何角色渲染,我得到了近390FPS。
我很感激任何有助于提高文字渲染性能的帮助。
答案 0 :(得分:3)
纹理绑定和每个字符的glDrawArrays()
?不是最好的方法。
最小化纹理绑定的数量&amp;画电话:
glDrawArrays()
调用