紋理數(shù)據(jù)通常是應(yīng)用程序用來渲染框架的數(shù)據(jù)的最大部分;紋理提供了向用戶展示精美圖像所需的細(xì)節(jié)。為了使您的應(yīng)用程序獲得最佳性能,請仔細(xì)管理應(yīng)用程序的紋理??偨Y(jié)準(zhǔn)則:
- 在初始化應(yīng)用程序時創(chuàng)建紋理,切勿在渲染循環(huán)中更改它們。
- 減少紋理使用的內(nèi)存量。
- 將較小的紋理合并為較大的紋理圖集。
- 使用mipmap減少獲取紋理數(shù)據(jù)所需的帶寬。
- 使用多重紋理可一次執(zhí)行紋理操作。
在初始化期間加載紋理
創(chuàng)建和加載紋理是一項昂貴的操作。為了獲得最佳效果,請避免在應(yīng)用運行時創(chuàng)建新的紋理。而是在初始化期間創(chuàng)建并加載紋理數(shù)據(jù)。
創(chuàng)建紋理后,請避免更改它,除非在幀的開頭或結(jié)尾處。當(dāng)前,所有iOS設(shè)備都使用基于圖塊的延遲渲染器,因此對glTexSubImage和glCopyTexSubImage功能的調(diào)用特別昂貴。
使用GLKit框架加載紋理數(shù)據(jù)
使用GLKit框架中的GLKTextureLoader來加載紋理,GLKTextureLoader可以從各種來源,包括文件,URL,內(nèi)存,CGImages加載紋理數(shù)據(jù)。無論輸入源如何,GLKTextureLoader都會從數(shù)據(jù)中創(chuàng)建并加載新紋理,并將紋理信息作為GLKTextureInfo對象返回。GLKTextureInfo可以訪問對象的屬性以執(zhí)行各種任務(wù),包括將紋理綁定到上下文并使其能夠繪制。
注:一個GLKTextureInfo對象不擁有它描述了OpenGL ES的紋理對象。使用完紋理對象后,必須調(diào)用glDeleteTextures函數(shù)以處理紋理對象。
下面的代碼展示了如何從文件中加載紋理:
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
- 創(chuàng)建包含紋理數(shù)據(jù)的圖像的路徑。該路徑作為參數(shù)傳遞給
GLKTextureLoader中的textureWithContentsOfFile:options:error:。 - 從圖像文件中加載新紋理并將紋理信息存儲在
GLKTextureInfo對象中。有多種紋理加載選項可用。 - 使用
GLKTextureInfo對象的適當(dāng)屬性作為參數(shù),將紋理綁定到上下文。
GLKTextureInfo
name : OpenGL 上下文中紋理名稱。
target : 紋理綁定的?標(biāo)。
height : 加載的紋理高度。
width : 加載紋理的寬度。
textureOrigin : 加載紋理中的原點位置。
alphaState: 加載紋理中alpha分量狀態(tài)。
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. */
從內(nèi)存中創(chuàng)建紋理
+ (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創(chuàng)建紋理
+ (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. */