在iOS上使用CoreGraphics非常容易,但可以获得CoreGraphics的输出并将其放入OpenGL纹理?
最终目标是使用CGContextDrawPDFPage来渲染非常高性能的pdf,并将其写入到一个特定的纹理id中。
OpenGL.glBindTexture(GL_TEXTURE_2D, TextureNativeId);看起来CoreGraphics不能直接渲染到一个特定的“原生纹理id”中。
发布于 2012-03-28 22:33:48
是的,您可以通过将Core Graphics内容渲染到位图上下文并将其上载到纹理。下面是我用来绘制核心图形上下文的UIImage的代码,但是您可以用自己的绘制代码替换CGContextDrawImage()部分:
GLubyte *imageData = (GLubyte *) calloc(1, (int)pixelSizeOfImage.width * (int)pixelSizeOfImage.height * 4);
CGColorSpaceRef genericRGBColorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext = CGBitmapContextCreate(imageData, (int)pixelSizeOfImage.width, (int)pixelSizeOfImage.height, 8, (int)pixelSizeOfImage.width * 4, genericRGBColorspace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(imageContext, CGRectMake(0.0, 0.0, pixelSizeOfImage.width, pixelSizeOfImage.height), [newImageSource CGImage]);
CGContextRelease(imageContext);
CGColorSpaceRelease(genericRGBColorspace);
glBindTexture(GL_TEXTURE_2D, outputTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)pixelSizeOfImage.width, (int)pixelSizeOfImage.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, imageData);这假设您已经使用如下代码创建了纹理:
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &outputTexture);
glBindTexture(GL_TEXTURE_2D, outputTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// This is necessary for non-power-of-two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);对于快速变化的内容,您可能需要查看iOS 5.0的纹理缓存(CVOpenGLESTextureCacheCreateTextureFromImage()等),这可能使您可以直接渲染到纹理的字节。但是,我发现使用纹理缓存创建和渲染纹理的开销会使渲染单个图像的速度稍微变慢,所以如果你不需要不断更新,上面的代码可能是你最快的方法。
https://stackoverflow.com/questions/9903864
复制相似问题