在 Android 和 OpenGL ES 中将字符串渲染为纹理
我到处搜索,但找不到太多用于将字符串渲染到纹理,然后在屏幕上的四边形上显示该纹理的信息。有人可以提供该流程的概要或提供描述如何进行的良好资源吗?将字符串渲染为纹理是否是在 Android OpenGL ES 应用程序中显示文本的最佳方法?
编辑: 好的,LabelMaker 会干扰 Alpha 混合,纹理(从具有透明背景的 PNG 创建)现在具有纯黑色背景,而不是透明背景。如果我注释掉所有与 LabelMaker 相关的代码,它就可以正常工作。
更新: 没关系。我查看了代码,发现 LabelMaker 在绘制标签后禁用了混合。
I've googled around everywhere, but cannot find much for rendering strings to textures and then displaying that texture on a quad on the screen. Can someone provide a run-down on the process or provide good resources that describe how? Is rendering strings to textures even the best method for displaying text in an Android OpenGL ES app?
EDIT:
Okay, so LabelMaker interferes with alpha blending, the texture (created from a PNG with a transparent background) now has a solid black background, rather than a transparent background. If I comment out all the LabelMaker-related code, it works fine.
UPDATE:
Nevermind. I took a look at the code to find that LabelMaker was disabling blending after drawing the labels.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这就是你的正在寻找。
I think this is what you are looking for.
如果您不想使用 GL 扩展,则需要将字体创建为位图,然后创建一个类以将该字符串转换为可以绘制的四边形。
我在游戏中将这种方法与 2 种字体一起使用。我有一个类,它采用所有字母均匀间隔的宽纹理和与图像匹配的字符串,然后使用字母查找来找出它应该在位图中走多远。
您的另一个选择是使用 android 将文本渲染到屏幕外位图,然后将文本绑定为纹理。这将允许您使用 Android 内置的字体处理和渲染来创建基于纹理的字体。
第二种方法我还没有使用过,但我已经将谷歌地图渲染到屏幕外画布,然后将位图绑定为 GL 纹理,因此对文本执行此操作应该要简单得多。
如果您计划在 gl 循环中修改字符串数据,您还需要真正担心 StringBuilder,因为它会导致 GC 和性能问题。我对所有字符串进行硬编码,这样它就不会分配,并且所有快速数字都是通过第二个绘制函数完成的,该函数专用于绘制不断变化的数字,而不使用字符串生成器。
If you don't want to use GL extensions you need to create the font as a bitmap and then create a class to convert that string into quads that you can draw.
I use this method with the 2 fonts in my game. I have a class that takes a wide texture with all the letters evenly spaced, and a string that matches the image, then uses lookups on the letters to find out how far in the bitmap it should go.
Your other option is to render your text to a offscreen bitmap using android, and then bind the text as a texture. This will let you use androids built-in font processing and rendering to create texture-based fonts.
The second method I have not used yet, but I have rendered google maps to a offscreen canvas and then bound the bitmap as a GL texture, so doing it for text should be much simpler.
If you are planning to have modifying string data in a gl loop you need to really worry about StringBuilder too, because it causes GC and performance issues. I hardcode all my strings so it doesn't allocate, and all my rapidly numbers are done through a second draw function dedicated to drawing changing numbers without using string-builder.