OpenGL ES 2d渲染成图像

med*_*ick 7 iphone rendering image opengl-es ios

我需要在iOS上编写OpenGL ES二维渲染器.它应该将一些原始元素(如线和多边形)绘制到2d图像中(它将呈现矢量图).哪种方式最适合从该任务中获取OpenGL上下文中的图像?我的意思是,我应该将这些基元渲染成纹理,然后从中获取图像,或者是什么?此外,如果有人提供看起来像我需要的示例或教程(2d GL渲染到图像中),那将是很棒的.提前致谢!

Bra*_*son 21

如果您需要渲染OpenGL ES 2-D场景,然后提取该场景的图像以在OpenGL ES之外使用,您有两个主要选项.

第一种是简单地渲染场景并使用它glReadPixels()来抓取场景的RGBA数据并将其放在一个字节数组中,如下所示:

GLubyte *rawImagePixels = (GLubyte *)malloc(totalBytesForImage);
glReadPixels(0, 0, (int)currentFBOSize.width, (int)currentFBOSize.height, GL_RGBA, GL_UNSIGNED_BYTE, rawImagePixels);
// Do something with the image
free(rawImagePixels);
Run Code Online (Sandbox Code Playgroud)

第二种也是更快的方法是将场景渲染到纹理支持的帧缓冲对象(FBO),其中纹理由iOS 5.0的纹理缓存提供.我在这个答案中描述了这种方法,尽管我没有在那里显示原始数据访问的代码.

您执行以下操作以设置纹理缓存并绑定FBO纹理:

    CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)[[GPUImageOpenGLESContext sharedImageProcessingOpenGLESContext] context], NULL, &rawDataTextureCache);
    if (err) 
    {
        NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreate %d");
    }

    // Code originally sourced from http://allmybrain.com/2011/12/08/rendering-to-a-texture-with-ios-5-texture-cache-api/

    CFDictionaryRef empty; // empty value for attr value.
    CFMutableDictionaryRef attrs;
    empty = CFDictionaryCreate(kCFAllocatorDefault, // our empty IOSurface properties dictionary
                               NULL,
                               NULL,
                               0,
                               &kCFTypeDictionaryKeyCallBacks,
                               &kCFTypeDictionaryValueCallBacks);
    attrs = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                      1,
                                      &kCFTypeDictionaryKeyCallBacks,
                                      &kCFTypeDictionaryValueCallBacks);

    CFDictionarySetValue(attrs,
                         kCVPixelBufferIOSurfacePropertiesKey,
                         empty);

    //CVPixelBufferPoolCreatePixelBuffer (NULL, [assetWriterPixelBufferInput pixelBufferPool], &renderTarget);

    CVPixelBufferCreate(kCFAllocatorDefault, 
                        (int)imageSize.width, 
                        (int)imageSize.height,
                        kCVPixelFormatType_32BGRA,
                        attrs,
                        &renderTarget);

    CVOpenGLESTextureRef renderTexture;
    CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault,
                                                  rawDataTextureCache, renderTarget,
                                                  NULL, // texture attributes
                                                  GL_TEXTURE_2D,
                                                  GL_RGBA, // opengl format
                                                  (int)imageSize.width, 
                                                  (int)imageSize.height,
                                                  GL_BGRA, // native iOS format
                                                  GL_UNSIGNED_BYTE,
                                                  0,
                                                  &renderTexture);
    CFRelease(attrs);
    CFRelease(empty);
    glBindTexture(CVOpenGLESTextureGetTarget(renderTexture), CVOpenGLESTextureGetName(renderTexture));
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, CVOpenGLESTextureGetName(renderTexture), 0);
Run Code Online (Sandbox Code Playgroud)

然后你可以直接从支持这个纹理的字节中读取(以BGRA格式,而不是RGBA glReadPixels()),使用类似的东西:

    CVPixelBufferLockBaseAddress(renderTarget, 0);
    _rawBytesForImage = (GLubyte *)CVPixelBufferGetBaseAddress(renderTarget);
    // Do something with the bytes
    CVPixelBufferUnlockBaseAddress(renderTarget, 0);
Run Code Online (Sandbox Code Playgroud)

但是,如果您只想在OpenGL ES中重用图像,则只需将场景渲染到纹理支持的FBO,然后在第二级渲染中使用该纹理.

我展示了一个渲染到纹理的示例,然后在我的开源GPUImage框架中的CubeExample示例应用程序中对其执行一些处理,如果您想要看到这一点.