将文本加载到 OpenGL 纹理中
我正在 iPad 上开发 openGL 应用程序,我遇到了一个相当大的问题。我使用已经存在的帖子来了解我可以使用 UILabel 将文本生成为 opengl 纹理,所以我这样做了。当我必须在纹理的不同位置使用五个或更多标签时,问题就出现了。当我的应用程序必须生成其中五个由五个标签组成的纹理时,每个纹理都需要一些功率。我最多在一个循环中生成一个纹理,但这会产生一点延迟。当我加载五个纹理时,延迟会增加五倍。
现在,当我讲述这个故事时,让我问你一个问题:
我怎样才能减轻我的CPU负担,并以其他方式生成文本?它是静态的,仅用于显示信息。
我确实尝试使用其他线程,但遇到了更多问题,所以我宁愿选择一些文本生成替代方案而不是多线程。
这是纹理生成器的一部分:
//------------ DATE --------------------------------
[dateLabel setBounds:CGRectMake(0, 0, 95, 10)];
[dateLabel setFrame:CGRectMake(0, 0, 95, 10)];
[dateLabel setBackgroundColor:[UIColor clearColor]];
[dateLabel setFont:[UIFont systemFontOfSize:12]];
[dateLabel setLineBreakMode:UILineBreakModeWordWrap];
[dateLabel setNumberOfLines:1];
dateLabel.textColor = [UIColor whiteColor];
dateLabel.text = [self.delegate dateAtIndex:index];
UIGraphicsBeginImageContext(CGSizeMake(95, 10));
[dateLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
//[dateLabel release]; // We can now release the view
UIImage* date = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
如您所见,我设置了位置、大小、文本,然后从中创建 UIImage。我创建了更多这些图像(实际上是五个),然后将所有这些图像渲染到其他上下文,这是我的最后一个纹理。
也许我缺乏经验导致我在这里找到了一些愚蠢的解决方案。
I am developing openGL application on iPad and i came up to one quite large problem. I used already existing posts to learn that i can use UILabel to generate text into opengl texture, so i did that. The problem appeared when i had to use five or more labels in different places of texture. When my application must generate five of those textures consisting of five labels each it takes some power. I generate one texture in one loop max, but that creates a small lag. When i load five textures, the lag gets five times more irritating.
Now when i told the story, let me ask you a question:
How could i relieve my CPU and perhaps generate text in some other way? Its static, just for displaying info.
I did try using other thread, but i came up to even more problems, so i would rather prefer some text generating alternative that multi threading.
Here is a part of that texture generator:
//------------ DATE --------------------------------
[dateLabel setBounds:CGRectMake(0, 0, 95, 10)];
[dateLabel setFrame:CGRectMake(0, 0, 95, 10)];
[dateLabel setBackgroundColor:[UIColor clearColor]];
[dateLabel setFont:[UIFont systemFontOfSize:12]];
[dateLabel setLineBreakMode:UILineBreakModeWordWrap];
[dateLabel setNumberOfLines:1];
dateLabel.textColor = [UIColor whiteColor];
dateLabel.text = [self.delegate dateAtIndex:index];
UIGraphicsBeginImageContext(CGSizeMake(95, 10));
[dateLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
//[dateLabel release]; // We can now release the view
UIImage* date = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
As you can see, i set the position, size, text and then create UIImage out of it. I create even more of those images (five in fact), and then i render all those images to other context which is my last texture.
Perhaps my lack of experience led me to some stupid solution here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 OpenGL 纹理没有更改,则无需为渲染的每个帧重新生成它。相反,只需在程序启动时加载一次即可。
If an OpenGL texture doesn't change you don't have to regenerate it for each frame you render. Instead just load it once when your program starts.
我想通了。无论如何,我必须转向多线程。 OpenGL 中的文本似乎可以通过多种方式呈现。但所有这些都需要不同的方法和实施。您可以逐个字母地生成文本,可以使用现有的库等等。但当您只需要某些纹理上的静态文本时,UILabel 方法仍然胜过它们。
不管怎样,我设法通过使用 NSOperationQueue 来减轻 CPU 的负担。我将所有处理喂入操作队列及其生成的图像,保存在 NSMutableArray 中。直到那时我才使用它们来生成纹理。
万岁!
I figured it out. I had to turn to multi threading anyway. It appears that text in OpenGL can be rendered in many ways. But all of them requires different approach and implementation. You can generate text letter by letter, you can use existing libraries and so on. But UILabel method still beats them all when you only need static text on some texture.
Anyway, i managed to relieve the CPU by using NSOperationQueue. I fed all the processing to operation queue and the images it generated, saved in NSMutableArray. Only then i used them for generating textures.
Hooray!