Hello friends,
I am using Sparrow framework v2.0 in my app to display a complex animation. I want to create the video for this animation which is displayed in SPViewController.
By taking inspiration from the category class SPDisplayObject+Image at
http://wiki.sparrow-framework.org/users/shilo/extensions/spdisplayobject_image
I have created a class that creates a video. This class writes a frame to the video when SPViewController's -glkView:drawInRect is called.
But the class is only working in ios simulator and crashes in iPhone/iPad whenever glreadpixels() is encountered.
Here is my code:
SPViewController
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
@autoreleasepool
{
if (!_root)
{
[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
if ([self.movieWriter isRecording]) {
[self.root writePixelBufferToMovieWriter];
}
}
}
SPDisplayObject+movieWriter
- (void)writePixelBufferToMovieWriter{
SPViewController *sparrowVC = Sparrow.currentController;
int contentScaleFactor = Sparrow.contentScaleFactor;
int width = self.width*contentScaleFactor;
int height = self.height*contentScaleFactor;
int bufferLength = width*height*4;
unsigned char buffer[bufferLength];
//[SPRenderSupport clearWithColor:0x0 alpha:1.0f];
//[self render:viewController.renderSupport];
glReadPixels(0, (Sparrow.stage.height*contentScaleFactor)-height, width, height, GL_BGRA, GL_UNSIGNED_BYTE, &buffer);
CVPixelBufferRef pixel_buffer = NULL;
CVPixelBufferCreateWithBytes(NULL, width, height, kCVPixelFormatType_32BGRA, buffer, 4 * width, NULL, 0, NULL, &pixel_buffer);
[sparrowVC.movieWriter writeToVideoUsing:pixel_buffer];
CVPixelBufferRelease(pixel_buffer);
}
What I am doing wrong?
By the way I am pretty new to openGLES.
Any help will be appreciated. Thanks