简体   繁体   English

iOS-使用OpenGL ES 2.0显示文本

[英]iOS - Displaying text with OpenGL ES 2.0

I'm surprisingly struggling a lot to display text with OpenGL ES 2.0. 令人惊讶的是,要使用OpenGL ES 2.0显示文本,我付出了很多努力。 There are a ton of posts on stackoverflow, debating the subject, showing a few lines of code, or showing links from 2010 but working with OpenGL ES 1.x (not compatible). 在stackoverflow上有大量帖子,讨论了该主题,显示了几行代码,或者显示了2010年的链接,但它们使用的是OpenGL ES 1.x(不兼容)。

But they are quite vague, and to my knowledge there is not complete code or convenient way to display text with version 2. 但是它们非常模糊,据我所知,尚无完整的代码或使用版本2显示文本的便捷方法。

Do you know if there is a modern way to display text ? 您知道是否有一种现代的文本显示方式吗? Like just adding a pod and writing something like this ? 就像只是添加一个pod并编写类似的东西?

[font drawText:@"This is a text" size:@12];

Thanks a lot in advance, any help would be very much liked. 在此先感谢您,非常感谢您的帮助。

EDIT 1 : I have to use OpenGL 2.0, and I can't use something else, for internal reasons. 编辑1:由于内部原因,我必须使用OpenGL 2.0,并且不能使用其他功能。

EDIT 2 : I've found two libraries that do it : 编辑2:我发现有两个库可以做到这一点:

This one is simple but written for ES1, so I need to port the code to ES2. 这很简单,但是是为ES1编写的,因此我需要将代码移植到ES2。

EDIT 3 : ES1 and ES2 are different : ES2 works with shaders. 编辑3:ES1和ES2不同:ES2与着色器一起使用。

Since you are targeting ES2 and shaders are supported it might be a bit hard to create a tool that does this for you. 由于您的目标是ES2并且支持着色器,因此创建一个可以为您完成此任务的工具可能有点困难。 But if you find such a tool it might be conflicted with your flow in many ways such as binding its own shaders and buffers, using settings on blending, depth buffer... Then if it would do that you are having trouble as you may not have a good control over how and where the text is drawn (on a 3D rotating box for instance). 但是,如果您找到了这样的工具,则可能会在很多方面与流程发生冲突,例如绑定其自己的着色器和缓冲区,使用混合设置,深度缓冲区...那么如果这样做,您可能会遇到麻烦,因为您可能没有可以很好地控制文本的绘制方式和绘制位置(例如在3D旋转框上)。

But from your question (it's snippet) it seems more like all you want to do is add some 2D overlay with text which would look exactly like using an UILabel . 但是从您的问题(如代码段)来看,您似乎想要做的就是在文本上添加一些2D叠加层,就像使用UILabel If this is the case then I suggest you to actually use UILabel to draw these texts. 如果是这种情况,那么我建议您实际使用UILabel绘制这些文本。 You can add all of these views easily on your view that shows openGL content. 您可以在显示openGL内容的视图上轻松添加所有这些视图。

In the other case where you still need to draw text on a 3D object and want to do it very easily I suggest you to still use UILabel but create its screenshot and push it to a new (or atlas) texture. 在另一种情况下,您仍然需要在3D对象上绘制文本并且想要非常容易地做到这一点,建议您仍然使用UILabel但创建其屏幕截图并将其推送到新的(或图集)纹理。 Then you can draw it as any other object. 然后,您可以将其绘制为任何其他对象。 UILabel will then handle all fonts, alignments, colors, multiline and text wrapping, font size adjustments... So if you already have a system to draw a texture in the scene you should not be too far from creating yourself a tool that draws some text on the screen since you use a texture to transfer the data. 然后, UILabel将处理所有字体,对齐方式,颜色,多行和文字换行,字体大小调整...因此,如果您已经具有在场景中绘制纹理的系统,那么就不应该创建一个自己的工具来绘制一些纹理屏幕上显示文本,因为您使用纹理来传输数据。

Nothing has changed since OpenGL ES1. 自OpenGL ES1起,一切都没有改变。 Usually text is displayed in planar projection and created quads that are textured using font texture. 通常,文本以平面投影显示,并创建使用字体纹理进行纹理处理的四边形。 There are many tutorials on this topic. 关于这个主题有很多教程。 One example how to do it. 一个例子,如何做到这一点。 However, this is quite a lot of work when you started from scratch. 但是,从头开始时,这是很多工作。

There might be better way, but depending on what you plan to do might not be suitable. 也许有更好的方法,但是取决于您计划做什么。 You may mix UIKit with OpenGL view, having text drawn with UIKit as overlay. 您可以将UIKit与OpenGL视图混合使用,并使用UIKit作为覆盖层绘制文本。 (UILabel, UIButton..etc.) (UILabel,UIButton等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM