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

查看:21
本文介绍了如何为 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.

我想到了几个选项:

  • 没有特殊情况——每次文本更改时渲染和加载纹理,记住只有在实际出现新文本时才重新渲染它,并尝试将较大的文本分成小部分(如每个聊天行).然而,这仍然会让我们陷入困境,比如一直在变化的分数线,或者呈现每个角色"的介绍文本(在某些科幻游戏中看到的打字机风格)
  • quad-per character -- 这似乎也是一个流行的解决方案,你准备一个带有 ASCII 表的纹理并渲染一个带纹理的四边形字符.但是,我严重怀疑这种解决方案的效率.也欢迎提供如何加快速度的提示.
  • 混合解决方案——但是我不知道如何干净地

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

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

如果这有帮助,我正在使用 STL/Boost-heavy 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.

另一个值得一看的代码宝石是 Font Stash它利用了 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的位图字体生成器工具,然后我集成了FreeType 并在应用程序启动时构建了一个包含所有字形的大纹理.至于提高四边形渲染速度的技巧,我只是将 VBO 用作应用程序中其余几何体.

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天全站免登陆