如何为GUI进行OpenGL实时文本渲染? [英] How to do OpenGL live text-rendering for a GUI?

查看:127
本文介绍了如何为GUI进行OpenGL实时文本渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个基于OpenGL构建的GUI.我遇到了每个GUI都会出现的问题-文本渲染.我知道几种在OpenGL中渲染文本的方法,但是,我想知道哪种方法最适合GUI.

I'm implementing a GUI built on top of OpenGL. I came to the problem that each GUI will have -- text rendering. I know of several methods of rendering text in OpenGL, however, I'm wonderin which of them would be best suited for a GUI.

通常在GUI中,我们有两种类型的文本-静态文本和实时文本.静态是很容易的-我们可以将TTF渲染到纹理上,而无需考虑它.是实时"文本更让我感到困扰–想象一下控制台,或者在多人游戏中进行实时聊天.

Generally in a GUI we have two types of text -- static and live. Static is easy enough -- we can render a TTF to a texture and forget about it. It's the "live" text that is more bothering me -- imagine console, or a live chat in a multi-player game.

我想到了几种选择:

  • 没有特殊情况-每次文本更改时渲染并加载纹理,请记住仅在实际出现新文本时才重新渲染纹理,并尝试将较大的文本拆分为小部分(如每行聊天行).但是,如果分数线一直在变化,或者介绍文字呈现每个字符"(某些科幻游戏中的打字机样式),这仍然会让我们感到困惑.
  • 四分之一字符-这似乎也是一种流行的解决方案,您可以使用ASCII表准备纹理并渲染有纹理的四边形字符.但是,我对这种解决方案的效率有严重的怀疑.还欢迎提供如何使其更快的提示.
  • 混合解决方案-但是我不知道如何干净

因此,问题是-如何在OpenGL中有效地渲染文本?

The question hence is -- how to render text in OpenGL efficiently?

如果有帮助,我正在使用STL/Boost繁重的C ++进行编码,并针对GForce 6和更高版本的显卡.

If this helps, I'm coding in STL/Boost-heavy C++ and aiming at GForce 6 and later graphics cards.

推荐答案

肖恩·巴雷特(Sean Barrett)刚刚发布位图字体用于C/C ++ 3D程序员.

Sean Barrett just released Bitmap fonts for C/C++ 3D programmers.

另一个值得一看的代码是字体隐藏利用肖恩·巴雷特(Sean Barrett)的 stb_truetype.h .

another code gem that's worth a look is Font Stash which leverages Sean Barrett's stb_truetype.h.

您可以创建一个纹理,在其中渲染字体的所有字符.然后,您只需用正交投影和适当的纹理坐标绘制带纹理的四边形:当您的文本使用的语言中没有太多符号时,此方法就可以使用:例如英语.字体纹理在应用程序开始时创建一次,然后渲染四边形确实非常快.

You can create a texture in which you render all the characters of your font. Then you just draw textured quads with orthographic projection and proper texture coordinates: this approach works when your text is in a language that doesn't contain much symbols: like english. The font texture is created once at the beginning of the application and then rendering quads is really fast.

这就是我正在使用的,我相信这是在OpenGL中渲染文本的最快方法.最初,我使用 Angelcode的位图字体生成器工具,然后集成了

That's what I'm using and I believe it's the fastest way to render text in OpenGL. At first, I used Angelcode's Bitmap Font Generator tool and then I integrated FreeType directly and built a big texture containing all the glyphs at application launch. As for tips to improve the quads rendering speed, I just used VBO as for the rest of the geometry in my application.

令我惊讶的是,您对四边形渲染有疑问,而您似乎并不担心在cpu上生成纹理,然后上传纹理,然后将其绑定以最终渲染一个矩形的性能,对于每个框架.更改OpenGL状态是瓶颈,而不是您将用于文本的500-1000个四边形.

I'm surprised you have doubts about quad rendering while you don't seem to worry about the performance of generating a texture on the cpu, then uploading it, then binding it to finally render a rectangle with it, that for each frame. Changing OpenGL states is the bottleneck, not the 500 - 1000 quads you'll use for text.

另外,请参阅类似 FTGL 这样的库,它们可以转换整个库字体变成多边形(几何字体).

Also, have a look at libraries like the FTGL library who converts the whole font into polygons (geometric fonts).

这篇关于如何为GUI进行OpenGL实时文本渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆