Я использую библиотеку 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) 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);
}
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.
Источник: https://stackoverflow.com/questions/318 ... nce-opengl
Мобильная версия