iOS OpenGL ES - Render screen to FBO on application resign for redraw later

北慕城南 提交于 2019-12-12 02:26:25

问题


I want to render my screen to a texture so that I can just "paste" the texture onto the screen when I come back into the application again. Currently in my app I am drawing a waveform with kEAGLDrawablePropertyRetainedBacking set to TRUE. It works, but the screen is cleared after I resign from my app and come back again. Is this normal behaviour with kEAGLDrawablePropertyRetainedBacking TRUE? I cannot afford the fps to redraw everything again when i come back to my application.

I've already stumbled upon this : How to save and redraw screen content in OpenGL ES and am trying to apply the answer to my needs, but I'm still having problems.

With reference to the answer from the other question, here's how I tried to do it.

@property (nonatomic) GLuint framebuffer;
@property (nonatomic) GLuint texture;

The code on loading in the glkviewcontroller.

- (void)viewDidLoad  {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate) name:UIApplicationWillTerminateNotification object:nil];

    glGenFramebuffers(1, &_framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER_OES, _framebuffer);


    glGenTextures(1, &_texture);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, _texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 768, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);
.....

When this viewController is notified that the app is resigning I use this function :

 -(void)appWillResignActive
{
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, _texture, 0);
    if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
        NSLog(@"Error!");
}

I'm getting error still after checking the status above. What could be the issue here? I'm pretty new to OpenGL ES, pardon me if this is a newbie question. Thanks.


回答1:


Nevermind I was trying to the texture and framebuffer binding above before the context initialization.

    // Create an OpenGL ES 2.0 context and provide it to the
    // view
    view.context = [[AGLKContext alloc]
                    initWithAPI:kEAGLRenderingAPIOpenGLES2];
    view.delegate = self;

   // Make the new context current
   [AGLKContext setCurrentContext:view.context];

Putting the code after this allows the check to go through.



来源:https://stackoverflow.com/questions/14786445/ios-opengl-es-render-screen-to-fbo-on-application-resign-for-redraw-later

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!