Objective-c : Most efficient way to load a texture to OpenGL

标签 objective-c ios opengl-es core-graphics

目前,我使用图像 I/O 在 iOS 中加载纹理,并使用 Core Graphics 提取其图像数据。然后我可以将图像数据发送到 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);

最佳答案

如果您正在针对 iOS 5 及更高版本进行开发,GLKTextureLoader就是您要找的:

关于Objective-c : Most efficient way to load a texture to OpenGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347804/

相关文章:

ios - 无法在 opengl es 2.0 中加载多个纹理

iphone - 将自己的结构传递到 opengl es 2.0 着色器中

ios - 为什么indexPath.row在这里不起作用?

ios - 如何解析此日期字符串中的时区?

ios - 如何解决 NSURLError : The network connection was lost?

ios - CollectionView 中的刷新数据不起作用 Swift

ios - 如何使用CAKeyFrameAnimation向后制作图像动画

java - Android 捕获 LinearLayout 上的触摸 : Cannot Resolve Symbol setOnTouchListener.

ios - 什么是 Objective-C 等同于空的 Swift OptionSet?

ios - 检查 NSURL 是否为本地文件