c++ - 使用 C++/OpenGL/Glut 比 glutBitmapCharacter 更快地在屏幕上绘制文本的函数

标签 c++ opengl text drawing glut

我需要比 glutBitmapCharacter(font, text[i]) 更快的东西。它的性能下降了几次!我需要显示 fps、xyz 位置等,所以不是在 3D 中只显示 HUD。

目前我正在使用:

glRasterPos2f(x,y);

for (int i=0; i<text.size(); i++)
{
   glutBitmapCharacter(font, text[i]);
}

我正在使用这个功能:

void glutPrint(float x, float y, LPVOID font, string text) 
{ 
    glRasterPos2f(x,y); 

    for (int i=0; i<text.size(); i++)
    {
        glutBitmapCharacter(font, text[i]);
    }
}

在绘制 HUD 部分的 DisplayFunction 中的每一帧(调用 DrawHUD()):

void DrawHUD (void)
{
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0.0, windowWidth, windowHeight, 0.0, -1.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClear(GL_DEPTH_BUFFER_BIT);
    glColor3f(0.2f,0.2f,0.2f);

    glutPrint(2,10,glutFonts[4],its(sfps)+" fps; "+its(todrawquads)+" quads drawing; ");

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);

}

也使用 int 到字符串函数:

string its(int i)
{
stringstream out;
out << i;
return out.str();
}

关于性能的一些事实(以 FPS 衡量)

不调用DrawHUD函数~3500

调用 DrawHUD 函数 ~ 3500(可能会少一些 fps)

使用 DrawHUD + 1 x GlutPrint ~ 3300

使用 DrawHUD + 2 x GlutPrint ~ 2400

使用 DrawHUD + 3 x GlutPrint ~ 1700

(例如,当我说 3 x GlutPrint 我的意思是在 DrawHUD 中:

{
[...]

glutPrint(...);
glutPrint(...);
glutPrint(...);

[...]
}

)

这不太好……我知道使用帧速率进行测量并不好。

还有

当我发表评论时:

glutBitmapCharacter(字体, 文本[i]);

在 glutPrint 的循环中有 ~3500 fps ... 所以我确定 glutBitmapCharacter 有问题。那么它有什么用呢:)?

那怎么办?

最佳答案

如有疑问,请回到基础:创建您自己的(更好的)绘图函数。制作字符的矩形纹理,并在屏幕上绘制连续的四边形(三角形 strip ),并选择字体纹理,每个字符串一个 strip 。

如果您选择走那条路,以上内容仍然适用于 VBO。唯一重要的是尽可能地缓冲写入,可能是通过将要打印到屏幕的内容添加到各种数组中,并在帧绘制结束时同时将它们写出.

关于c++ - 使用 C++/OpenGL/Glut 比 glutBitmapCharacter 更快地在屏幕上绘制文本的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6385391/

相关文章:

c++ - C++中 "Class instantiated object and variable inside the object address"的内存地址

c++ - 在 Qt 中创建原始 GL 上下文?

c++ - Qt 5.5 和 OpenGL : Program behaves strangely without any apparent reason

windows - Autohotkey 中的多个光标

xml - 如何计算一个单词在xml文件中出现了多少次

c++ - 具有已知参数的函数指针

c++ - 在CMake中使用find_package时,是否会显式包含 header ?

java - 循环无向图中的所有可能路径

windows - 如何使用特定的图形驱动程序创建 OpenGL 上下文?

css - SVG : viewbox property strech text inside