加快OpenGL的文本呈现性能 [英] Speed up text rendering performance openGL
问题描述
我使用FreeType库来拉出每个字形信息,例如宽度,高度和位图。这是在init函数中完成的,我不在乎它花费的时间。
I use the FreeType library to pull out each glyph info such as width, height and bitmap. This is done in init function, where I don't really care about the time it takes. I store each character info in map container, so I can later access each character easily.
在渲染时我读取字符串并使用字符串迭代器循环遍历每个字符和使用字形参数创建多边形,我要在其上绘制位图。
On render time I read the string and using string iterator I loop through each character and use the glyph parameters to create polygons on which I want to draw the bitmap.
多边形是使用VAO与VBO一起绘制的
The polygons are drawn using VAO together with VBO
为了使其透明,我使用混合,着色器用于着色。
To make it transparent, I use blending, and a shader is used for coloring.
这是我的渲染功能:
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,
I was thinking of using a display list instead of VBO, but I don't think it would make any improvement.
用大约400个字符绘制,我只有165FPS左右,没有任何字符,我得到近390FPS。
With approximately 400 characters drawn, I get only around 165FPS and without any character rendered I get almost 390FPS.
我将感谢任何有助于提高文本呈现性能的帮助。
I would appreciate any help leading to an enhancement of the text rendering performance.
推荐答案
纹理绑定和 glDrawArrays()
每个字符?不是最好的方法。
A texture bind and glDrawArrays()
per character? Not the best way to do it.
最小化纹理绑定的数量&绘制调用:
Minimize the number of texture binds & draw calls:
- Glom all your glyphs into (ideally) a single texture atlas
- Throw all the vertex info for a frame's worth of text into a single VBO
- Draw all your strings for the frame with a single
glDrawArrays()
call
这篇关于加快OpenGL的文本呈现性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!