2013-02-28 33 views
0

我是新来的opengl,我试图记录GLKView到目前为止没有运气。 这里是我的代码:用CVOpenGLESTextureCache记录GLKView到mov文件

 EAGLContext * _context = self.glkview.context; 

#if COREVIDEO_USE_EAGLCONTEXT_CLASS_IN_API 
    CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, _context, NULL, &_videoTextureCache); 
#else 
    CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)_context, NULL, &_videoTextureCache); 
#endif 
    if (err) 
    { 
     NSLog(@"Error at CVOpenGLESTextureCacheCreate %d", err); 
     return; 
    } 


    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); 

// for simplicity, lets just say the image is 640x480 
CVPixelBufferCreate(kCFAllocatorDefault, 640, 480, 
        kCVPixelFormatType_32BGRA, 
        attrs, 
        &renderTarget); 
// in real life check the error return value of course. 


// first create a texture from our renderTarget 
// textureCache will be what you previously made with CVOpenGLESTextureCacheCreate 
CVOpenGLESTextureRef renderTexture; 
CVOpenGLESTextureCacheCreateTextureFromImage (
               kCFAllocatorDefault, 
               _videoTextureCache, 
               renderTarget, 
               NULL, // texture attributes 
               GL_TEXTURE_2D, 
               GL_RGBA, // opengl format 
               640, 
               480, 
               GL_BGRA, // native iOS format 
               GL_UNSIGNED_BYTE, 
               0, 
               &renderTexture); 
// check err value 

// set the texture up like any other texture 
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); 

// bind the texture to the framebuffer you're going to render to 
// (boilerplate code to make a framebuffer not shown) 

glPixelStorei(GL_UNPACK_ALIGNMENT,4); 

glGenTextures(1, &_texureName); 
glBindTexture(GL_TEXTURE_2D, _texureName); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei) 1024, (GLsizei) 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(renderTarget)); 

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 
         GL_TEXTURE_2D, CVOpenGLESTextureGetName(renderTexture), 0); 

这是写它的文件的方法。

- (void)writeVideoFrameAtTime:(CMTime)time { 

    if (_videoTextureCache == NULL) 
     return; 

     BOOL success = [_assetWriterPixelBufferInput appendPixelBuffer:renderTarget withPresentationTime:time]; 
     //NSLog(@"writing"); 
     if (!success) { 
      NSLog(@"Pixel Buffer not appended"); 
     } 

} 
+0

如果你想在高帧率记录从您的供日后回放查看录像我可以有另一种非常简单方法.. – 2013-03-10 04:06:12

+0

哪个替代?我正在尝试创建一个mp4/mov电影文件。 – Janub 2013-03-10 07:02:15

+0

与在运行时从glk捕获相反,您可以将输入记录为游戏或任何正在播放的内容,然后重新启动模拟,在渲染到屏幕外帧缓冲区时在后台播放 - 然后您可以将帧组合为视频。使用这种方法,可以在背景中获得1-10 fps。 – 2013-03-10 07:06:15

回答