使用 C++/OpenGL/Glut 在屏幕上绘制文本的函数比 glutBitmapCharacter 更快
我需要比 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(可能少几帧)
使用 DrawHUD + 1 x GlutPrint ~ 3300
使用 DrawHUD + 2 x GlutPrint ~ 2400
使用 DrawHUD + 3 x GlutPrint ~ 1700
(例如,当我的意思是 3 x GlutPrint 我的意思是在 DrawHUD 中
{
[...]
glutPrint(...);
glutPrint(...);
glutPrint(...);
[...]
}
:)
这不太好...我知道使用帧速率进行测量并不好。
当
我评论时:
glutBitmapCharacter(font, text[i]);
在 glutPrint 的循环中,大约有 3500 fps ...所以我确定 glutBitmapCharacter 是有问题的。那么它有什么用呢:)?
那该怎么办呢?
I need something muuuch faster than glutBitmapCharacter(font, text[i]). It's decreasing performacne few times ! I need to display fps, xyz position etc. so not in 3D just displaying HUD.
Currently I'm using :
glRasterPos2f(x,y);
for (int i=0; i<text.size(); i++)
{
glutBitmapCharacter(font, text[i]);
}
I'm using this function :
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]);
}
}
Every frame in DisplayFunction in drawing HUD section (calling 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);
}
Also using int to string function :
string its(int i)
{
stringstream out;
out << i;
return out.str();
}
some fact about performance (measured in FPS)
Without calling DrawHUD function ~ 3500
With calling DrawHUD function ~ 3500 (maybe few less fps)
With DrawHUD + 1 x GlutPrint ~ 3300
With DrawHUD + 2 x GlutPrint ~ 2400
With DrawHUD + 3 x GlutPrint ~ 1700
(eg. when I mean 3 x GlutPrint I meant in DrawHUD :
{
[...]
glutPrint(...);
glutPrint(...);
glutPrint(...);
[...]
}
)
That's not nice ... I know measuring using frame rate isn't good.
also
when I commented :
glutBitmapCharacter(font, text[i]);
in loop in glutPrint there was ~3500 fps ... so I'm SURE that glutBitmapCharacter is problem. So what use instead it :) ?
Then what to do ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如有疑问,请回到基础知识:创建您自己的(更好的)绘图函数。制作一个矩形字符纹理,并使用选定的字体纹理在屏幕上绘制连续的四边形(三角形条带),每个字符字符串一个条带。
如果您选择走这条路,上述内容对于 VBO 仍然适用。唯一重要的是尽可能多地缓冲写入,也许可以通过将要打印到屏幕的内容添加到排序数组中,并在帧绘制的末尾同时将它们写出来。
When in doubt, go back to the basics: create your own (better) drawing function. Make a rectangular texture of characters and draw contiguous quads (triangle strips) on screen with the font texture selected, one strip per string of characters.
The above still holds fine with VBO's if you choose to go that route. The only important thing is to buffer the writes as much as possible, maybe by adding stuff to print to the screen to an array of sorts and writing them out at the end of the frame draw at the same time.