制作“一张上面有文字的纸”在 OpenGL 中(特别是在 iOS 5 上)

发布于 2024-12-12 14:34:59 字数 398 浏览 1 评论 0原文

我从未使用过 OpenGL,但我正在使用我正在练习的 AR 应用程序寻找有关此特定问题的一些指导。

我想制作一个带有“平面矩形”的应用程序以及写在矩形表面上的文本。在视觉上,我正在想象一些类似纸上写有文字的东西。每次应用程序启动时,文本都会有所不同(文本是从 plist 文件中提取的)。

用户可以从各个方向查看纸张,就像他面前挂着一张纸一样。

这在 OpenGL 中是不是很简单?我怎样才能开始呢?

很抱歉这个开放式问题,但我想了解一下这种事情是如何完成的。

查看 Xcode 示例项目中的 OpenGL 模板源代码,我发现有一个很大的顶点数组。我认为要创建一个“平面”矩形,我基本上只需删除 z 轴或将 z 轴设置为零即可。然后动态文本将附加到平面矩形的表面......我不知道该怎么做......

I've never done OpenGL, but I'm looking for some pointers on this particular question on an AR app I'm practicing with.

I'd like to make an app with a "flat rectangle" along with text written on the surface of the rectangle. Visually, I'm imagining something along the lines of a piece of paper with text written on it. Each time the app starts, the text would be something different (the text is pulled from a plist file).

The user would be able to view the paper from all sides, much as if there was a piece of paper hanging in front of him.

Is this trivial to do in OpenGL? How could I get started?

Sorry for the really open-ended question, but I wanted to get a feel for how this kind of thing is done.

Looking at the OpenGL template source code in the Xcode sample projects, I see that there is a big array of vertices. I presume that to create a "flat" rectangle, I'd essentally just have to remove or make the z-axis zero. And then the dynamic text that will attach to the surface of the flat rectangle...I dont have any idea how to do that......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-12-19 14:34:59

这个问题很难明确回答。一般来说,这是微不足道的,但话又说回来,事实并非如此。

绘制一个“上面有东西的平面矩形”只需几个 API 调用,就这么简单。在 OpenGL 中以高效、高质量且无需大量预处理的方式绘制文本是一个完全不同的故事。

我要做的是使用 iOS 下的“正常系统支持”方式渲染文本(就像您在任何窗口中绘制一样,我不知道这个具体细节),但绘制成位图 em> 而不是在屏幕上。这应该得到支持,几乎每个操作系统都支持它至少 10-15 年。然后将此位图转换为纹理,绑定它,并使用 OpenGL 绘制简单的平面四边形(设置一个具有 4 个顶点的顶点缓冲区,每个顶点一个纹理坐标,并绘制两个三角形 - 尽可能简单)。

这样做的巨大优势是您可以使用已安装的系统字体(或任何可用的字体),您不需要生成位图字体,也不需要考虑真正丑陋的事情,例如提示和适当的间距,当然,混合不同的文本样式等也更容易。OpenGL 也内置了对文本的支持,但它也不是非常高效或良好。如果文本不是每毫秒都改变,那么最好使用操作系统提供的标准渲染器来渲染它(是的,这可能不会硬件加速,但那又怎样......因为用户必须阅读文本,它可能不会每毫秒都改变)。

现在,如果您的“纸片”也应该弯曲和扭曲,或者做页面剥离效果而不仅仅是一个平面矩形,那么事情就会变得更加复杂。在这种情况下,您需要对其进行细分,这也可能比听起来更难。并非所有曲面细分对于所有弯曲/扭曲来说都是最佳的,或者它们确实具有最佳(读作最小)顶点数。

在 GPU Gems 或 GPU Pro 书籍中,有一篇关于“页面剥离”和此类镶嵌的文章,让我搜索一下...其中

Andreas Bizzotto:“基于着色器的电子书阅读器 - 页面剥离效果”, GPU Pro2 第 278-299 页

也许您可以找到一本,或者很幸运地在 Google 图书或其他地方找到它。

This question is hard to answer unambiguously. In general, this is trivial, but then again it is not.

Drawing a "flat rectangle with something on it" is a couple of API calls, as simple as it can get. Drawing text in OpenGL in an efficient way, and high quality, and without big preprocessing is an entirely different story.

What I would do is render text using whatever the "normal system-supported" way is under iOS (just like you would draw in any window, I wouldn't know this specific detail), but draw into a bitmap rather than on the screen. This should be supported, pretty much every OS has supported this for at least 10-15 years. Then turn this bitmap into a texture, bind it, and draw your trivial flat quad with OpenGL (set up a vertex buffer with 4 vertices, each vertex a texture coordinate, and draw two triangles - as easy as it gets).

The huge advantage of that is that you get to use the installed system fonts (or any fonts available), you don't need to generate a bitmap font and don't need to think about really ugly things such as hinting and proper spacing, and it's much easier to mix different text styles, etc. OpenGL has built-in support for text too, of course, but it is not terribly efficient or nice either. If the text does not change every millisecond, it's really best to render it using the standard renderer that the operating system provides (yes, that probably won't be hardware accelerated, but so what... since the user must read the text, it likely won't change every millisecond).

Now it gets more complicated if your "piece of paper" should bend and twist too, or do a page peel effect rather than being just a flat rectangle. In that case you need to tesselate it, which can be harder than it sounds, too. Not all tesselations look optimal for all bends/twists, or they do but do not have the optimal (read as minimum) number of vertices.

There is an article on "page peel" and such tesselation in one of the GPU Gems or GPU Pro books, let me search...

There: Andreas Bizzotto: "A Shader-Based eBook Reader - Page peeling effect", GPU Pro2 pp. 278-299

Maybe you can get hold of a copy or are lucky enough to find it on Google Books or something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文