音视频从入门到放弃

OpenGL ES 加载纹理数据

2020-08-05  本文已影响0人  Joker_King

纹理数据通常是应用程序用来渲染框架的数据的最大部分;纹理提供了向用户展示精美图像所需的细节。为了使您的应用程序获得最佳性能,请仔细管理应用程序的纹理。总结准则:

在初始化期间加载纹理

创建和加载纹理是一项昂贵的操作。为了获得最佳效果,请避免在应用运行时创建新的纹理。而是在初始化期间创建并加载纹理数据。

创建纹理后,请避免更改它,除非在帧的开头或结尾处。当前,所有iOS设备都使用基于图块的延迟渲染器,因此对glTexSubImageglCopyTexSubImage功能的调用特别昂贵。

使用GLKit框架加载纹理数据

使用GLKit框架中的GLKTextureLoader来加载纹理,GLKTextureLoader可以从各种来源,包括文件,URL,内存,CGImages加载纹理数据。无论输入源如何,GLKTextureLoader都会从数据中创建并加载新纹理,并将纹理信息作为GLKTextureInfo对象返回。GLKTextureInfo可以访问对象的属性以执行各种任务,包括将纹理绑定到上下文并使其能够绘制。

注:一个GLKTextureInfo对象不拥有它描述了OpenGL ES的纹理对象。使用完纹理对象后,必须调用glDeleteTextures函数以处理纹理对象。

下面的代码展示了如何从文件中加载纹理:

GLKTextureInfo *spriteTexture;
NSError *theError;
 
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Sprite" ofType:@"png"]; // 1
 
spriteTexture = [GLKTextureLoader textureWithContentsOfFile:filePath options:nil error:&theError]; // 2
glBindTexture(spriteTexture.target, spriteTexture.name); // 3
  1. 创建包含纹理数据的图像的路径。该路径作为参数传递给GLKTextureLoader中的textureWithContentsOfFile:options:error:
  2. 从图像文件中加载新纹理并将纹理信息存储在GLKTextureInfo对象中。有多种纹理加载选项可用。
  3. 使用GLKTextureInfo对象的适当属性作为参数,将纹理绑定到上下文。

GLKTextureInfo

name : OpenGL 上下文中纹理名称。
target : 纹理绑定的⽬标。
height : 加载的纹理高度。
width : 加载纹理的宽度。
textureOrigin : 加载纹理中的原点位置。
alphaState: 加载纹理中alpha分量状态。
containsMipmaps: 布尔值,加载的纹理是否包含mip贴图。

GLTextureLoader

初始化

初始化一个新的纹理加载到对象中。

#if TARGET_OS_IPHONE
- (instancetype)initWithSharegroup:(EAGLSharegroup *)sharegroup;
#else
- (instancetype)initWithShareContext:(NSOpenGLContext *)context;
#endif

加载纹理

从文件中加载纹理。

+ (nullable GLKTextureInfo *)textureWithContentsOfFile:(NSString *)path                                       /* File path of image. */
                                               options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
                                                 error:(NSError * __nullable * __nullable)outError;           /* Error description. */

- (void)textureWithContentsOfFile:(NSString *)path                                       /* File path of image. */
                          options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
                            queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
                completionHandler:(GLKTextureLoaderCallback)block;                       /* Block to be invoked on the above dispatch queue. */

从内存中创建纹理

+ (nullable GLKTextureInfo *)textureWithContentsOfData:(NSData *)data                                         /* NSData containing image contents. */
                                               options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
                                                 error:(NSError * __nullable * __nullable)outError;           /* Error description. */

- (void)textureWithContentsOfData:(NSData *)data                                         /* NSData containing image contents. */
                          options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
                            queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
                completionHandler:(GLKTextureLoaderCallback)block;                       /* Block to be invoked on the above dispatch queue. */                                                 

从URL加载纹理

- (void)textureWithContentsOfURL:(NSURL *)url                                           /* File path of image. */
                         options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
                           queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
               completionHandler:(GLKTextureLoaderCallback)block;                       /* Block to be invoked on the above dispatch queue. */

- (void)cubeMapWithContentsOfURL:(NSURL *)url                                           /* File path of image. */
                         options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
                           queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
               completionHandler:(GLKTextureLoaderCallback)block;                       /* Block to be invoked on the above dispatch queue. */               

从CGImage创建纹理

+ (nullable GLKTextureInfo *)textureWithCGImage:(CGImageRef)cgImage                                    /* CGImage reference. */
                                        options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
                                          error:(NSError * __nullable * __nullable)outError;           /* Error description. */

- (void)textureWithCGImage:(CGImageRef)cgImage                                    /* CGImage reference. */
                   options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
                     queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
         completionHandler:(GLKTextureLoaderCallback)block;                       /* Block to be invoked on the above dispatch queue. */
上一篇 下一篇

猜你喜欢

热点阅读