2013-02-28 61 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

回答