在 iPhone 上使用 OpenGL 渲染 UIImage/PNG 最简单、最快的方法?
我编写了一个 iPhone 应用程序,它接受用户的输入并根据输入更新位图上下文。更新后,它会更新 UIView 的显示。我通过在 UIView 子类中实现 drawRect 来实现这一点。
目前我正在使用 Quartz 2D。考虑到更新每秒发生多次,我注意到虽然应用程序在模拟器中表现良好,但在实际设备上却慢得多(尽管无法使用)。我想重写drawRect以使用OpenGL(因为我听说这性能更高?)。如果您有其他想法,我洗耳恭听。
我在网上浏览了有关如何使用 OpenGL 简单显示 PNG 的示例/教程,但大多数都超出了我的需要。我正在寻找一个简短而甜蜜的示例,用于快速获取 PNG 文件或 UIImage,然后使用 OpenGL 在 UIView 中渲染它(可完成?这在概念上是否有意义?) - 或指向加速图像渲染的文档的指针使用现有的 Quartz API。
在您决定使用 RTFM 回复之前,请不要浪费我或您的时间。我不是在寻找捷径,我不介意做作业。我只是在寻找一个适合我的需求的简单解决方案/指南,但这是很难找到的。我确信精通 OpenGL 的人可能可以解释需要做什么并在 5 分钟内编写此代码。如果那是你,那会让我很高兴:)。
任何帮助、指导等始终不胜感激。谢谢。
I've written an iPhone application that takes input from a user and updates a bitmap context based on the input. After the update, it then updates the display of a UIView. I am accomplishing such by implementing drawRect in a UIView subclass.
Currently I am using Quartz 2D. Given that the updates are occurring multiple times per second, I have noticed that while app performs well in the emulator it is much slower (albeit unusable) on an actual device. I would like to rewrite drawRect to use OpenGL instead (as I have heard this is much more performant?). If you have other ideas, I am all ears.
I have browsed around the web for samples/tutorials on how to simple display a PNG using OpenGL, but most are overkill for what I need. I am looking for a short and sweet sample for quickly taking a PNG file or UIImage and then rendering this within a UIView using OpenGL (accomplishable? does this even conceptually even make sense?) - or a pointer to a documentation for speeding up image rendering using the existing Quartz API's.
Before you decide to reply with a RTFM response - please don't waste my time or yours. I'm not looking for a shortcut, I don't mind doing the homework. I'm just looking for a simple solution/guidance that fits my needs and which has been hard to come by. I am sure someone who is well versed in OpenGL could probably explain what needs to be done and crank this code in 5 minutes. If that's you, it would make my day :).
Any help, guidance, etc. is always appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用您自己分配的内存创建位图上下文
,并将所需的任何内容渲染到该位图上下文中。接下来从数据创建纹理
现在只需遵循有关使用 GL 渲染此纹理的任何教程,使用 Apple 关于如何将 GL 渲染到 UIView 中的示例代码。请务必小心,在位图上下文中使用 RGBA 字节顺序(或将 GL 调用交换为 ARGB)。另外,请记住 GL 只喜欢尺寸为 2^nx 2^n 的纹理。
由于 iPhone 的共享内存架构,数据不会在每个周期发送到显卡,因此不会造成太大的性能损失。
Create a bitmap context with memory you have allocated yourself
and render whatever you want into that bitmap context. Next create a texture from the data
Now just follow any tutorial on rendering this texture with GL, using Apple's example code on how to render GL into a UIView. Do be careful that you use an RGBA byte order in the bitmap context(or swap the GL call to ARGB). Also, remember that GL only likes textures with dimensions 2^n x 2^n.
With the iPhone's shared memory architecture, data isn't being sent to the graphics card each cycle, so this doesn't incur much of performance penalty.
显示更新的位图的更快方法可能是将位图上下文分配给 UIView 的 CALayer(如果需要,子视图的大小和/或转换为所需的目标矩形)。无需绘制矩形或 OpenGL 帧渲染。
A faster way to display an updated bitmap may be to just assign the bitmap context to the CALayer of a UIView (a subview sized and/or transformed to your desired destination rect if required). No drawRect or OpenGL frame render needed.