For few days I was trying to record the content of SparrowViewConroller's GLKView into a video. Well I finally succeeded in doing that but that broke the content display in GLKView. Now when I start recording, all the openGL content goes to my video writer and SParrow's GLKview stays freezed. I am having no trouble creating the video.
I am using FramebufferObject to pass the video frames to my video writer.
I guess I messed up something by using glBindTexture().
Before starting the video recording for the first time, everything is working fine.
My code is below:
- Before I start video writing using AVAssetWriter, I create Framebuffer object as:
-(void)createFBO{
SPViewController *vC = Sparrow.currentController;
int contentScaleFactor = Sparrow.contentScaleFactor;
int width = vC.view.bounds.size.width*contentScaleFactor;
int height = vC.view.bounds.size.height*contentScaleFactor;
#if defined(__IPHONE_6_0)
CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, [vC context], NULL, &coreVideoTextureCache);
#else
CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)[vC context], NULL, &coreVideoTextureCache);
#endif
if (err)
{
NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreate");
}
CFDictionaryRef empty; // empty value for attr value.
CFMutableDictionaryRef attrs;
empty = CFDictionaryCreate(kCFAllocatorDefault, NULL, NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); // our empty IOSurface properties dictionary
attrs = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(attrs, kCVPixelBufferIOSurfacePropertiesKey, empty);
err = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, attrs, &renderTarget);
if (err)
{
NSAssert(NO, @"Error at CVPixelBufferCreate %d", err);
}
CVPixelBufferPoolCreatePixelBuffer (NULL, [assetWriterPixelBufferInput pixelBufferPool], &renderTarget);
CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault, coreVideoTextureCache, renderTarget,
NULL, // texture attributes
GL_TEXTURE_2D,
GL_RGBA, // opengl format
width,
height,
GL_BGRA, // native iOS format
GL_UNSIGNED_BYTE,
0,
&renderTexture);
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);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
NSAssert(status == GL_FRAMEBUFFER_COMPLETE, @"Incomplete filter FBO: %d", status);
glBindTexture(GL_TEXTURE_2D, 0);
CFRelease(attrs);
CFRelease(empty);
}
- After that I Have added following code to -glkView:drawInRect: in the top as:
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
//My modifications starts here.
if (_movieWriter && _movieWriter.isRecording) {
[_movieWriter writeCurrentFrameToVideo];
}
//My modifications ends here
//Below is original unedited code from sparrow framework.
@autoreleasepool
{
if (!_root)
{
// ideally, we'd do this in 'viewDidLoad', but when iOS starts up in landscape mode,
// the view width and height are swapped. In this method, however, they are correct.
[self readjustStageSize];
[self createRoot];
}
[Sparrow setCurrentController:self];
[EAGLContext setCurrentContext:_context];
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
[_support nextFrame];
[_stage render:_support];
[_support finishQuadBatch];
if (_statsDisplay)
_statsDisplay.numDrawCalls = _support.numDrawCalls - 2; // stats display requires 2 itself
#if DEBUG
[SPRenderSupport checkForOpenGLError];
#endif
}
}
By the way, I don't know much about openGLES and the above code is taken from multiple places.
Please help.