2012-07-05 69 views
2

當前我使用Image I/O在iOS中加載紋理,並使用Core Graphics提取其圖像數據。然後,我可以將圖像數據發送到OpenGL的是這樣的:Objective-c:將紋理加載到OpenGL的最有效方法

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->width, texture->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->imageData); 

的問題是,核芯顯卡部分實在是太慢了,我需要建立和核芯顯卡都僅提取圖像數據...我不想在屏幕上顯示它。必須有一個更有效的方式在iOS中提取圖像數據...

這裏是我的代碼:?

... 
myTexRef = CGImageSourceCreateWithURL((__bridge CFURLRef)url, myOptions); 
... 
MyTexture2D* texture; 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

void *imageData = malloc(tileSize.width * tileSize.height * 4); 

CGContextRef imgContext = CGBitmapContextCreate(imageData, tileSize.width, tileSize.height, 8, 4 * tileSize.width, colorSpace, kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 
CGContextClearRect(imgContext, CGRectMake(0, 0, tileSize.width, tileSize.height)); 
CGContextTranslateCTM(imgContext, 0, 0); 
... 
CGImageRef tiledImage = CGImageCreateWithImageInRect (imageRef, tileArea); 
CGRect drawRect = CGRectMake(0, 0, tileSize.width, tileSize.height); 

// *** THIS CALL IS REALLY EXPENSIVE! 
CGContextDrawImage(imgContext, drawRect, tiledImage); 

CGImageRelease(tiledImage); 

// TamTexture2D takes the ownership of imageData and will be responsible to free it 
texture = new MyTexture2D(tileSize.width, tileSize.height, imageData); 

CGContextRelease(imgContext); 
+0

你的代碼似乎不只是加載圖像數據(平鋪?)如果你想要優化的幫助,你應該發佈完整的代碼。 – 2013-09-02 20:39:08

回答

1

如果您正在爲iOS 5及以上的開發,GLKTextureLoader是你在找什麼用於:

+0

Thx的提示,我試過了,但當我試圖加載紋理時,我一直收到這個錯誤消息,我試圖靜態調用它,並使用實例方法initWithSharegroup ...(即時通訊使用opengl 1.1):紋理錯誤:錯誤域(GLKTextureLoaderErrorDomain error 8.)「UserInfo = 0x10bef750 {GLKTextureLoaderGLErrorKey = 1280,GLKTextureLoaderErrorKey = OpenGL錯誤} ... – AkademiksQc 2012-07-05 17:45:44

+0

GLKit僅限於OpenGLES 2.0。 – MrMage 2012-07-06 06:27:41

+0

啊!這可能會解釋它...所以理查德你的答案是有用的,但我使用OpenGL 1.1 ...我會研究核心圖像框架,也許我會找到一些東西 – AkademiksQc 2012-07-06 10:15:23

1

GLKTextureLoader比使用CG要慢幾個數量級。

我已經記錄了這個與Apple DTS有關的錯誤,並且將它作爲「是的,我們知道,不關心,不會修復它。」如果您想要紋理,則應該使用CoreGraphics。快速加載「(幾乎是這樣的措詞)

如果你給CGContext *方法爲你創建的原始緩衝區/允許你傳入,glTexImage2D是非常快速的。假設你得到了RGBA/ARGB/etc顏色空間當然是正確的。

cgcontextDrawImage也是超快的。我的猜測是,它需要時間來通過網絡加載你的數據...