title: OpenGL draws black screen after returning from another View
body: <p>I am struggling whole day to find the answer to this.</p>
  
  <p>Problem:</p>
  
  <p>I have OpenGL view, i create it in IB, initialize all buffers and set its rendering loop in displayLink. It works great, everything is okay. But then i try creating another controller with this openGL view in root controller and present it using:</p>
  
  <pre><code>[self presentModalViewController:newPage animated:YES];
  </code></pre>
  
  <p>It shows up nicely, just like i wanted. It also works great with no problems found. It is also initiated from IB. But as soon as i use</p>
  
  <pre><code>[self dismissModalViewControllerAnimated:YES];
  </code></pre>
  
  <p>the animation does happen, the view does go down, but as it goes down it leaves "trash" behind it (parts of view in black background) and the previous openGL view stays black. I tried deleting and recreating displayLink but with no luck. I tried destroying/creating framebuffers again and i did manage to get it working once, but it took long time and since i am creating it from the view, i do not know if this is correct approach. Just to make it easier, here is some parts of my code:</p>
  
  <pre><code>- (BOOL)createFrameBuffer
  {
      glGenFramebuffersOES(1, &amp;viewFramebuffer);
      glGenRenderbuffersOES(1, &amp;viewRenderbuffer);
  
      glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
      glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
      [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
      glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  
  
  
      glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &amp;backingWidth);
      glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &amp;backingHeight);
  
      if (YES) 
      {
          glGenRenderbuffersOES(1, &amp;depthRenderbuffer);
          glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
          glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
          glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
      }
  
      if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) 
      {
          NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
          return NO;
      }
  
      glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
      glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  
      return YES;
      }
  
      }
  
      - (void)destroyFrameBuffer
      glDeleteFramebuffersOES(1, &amp;viewFramebuffer);
      viewFramebuffer = 0;
      glDeleteRenderbuffersOES(1, &amp;viewRenderbuffer);
      viewRenderbuffer = 0;
  
      if(depthRenderbuffer) 
      {
          glDeleteRenderbuffersOES(1, &amp;depthRenderbuffer);
          depthRenderbuffer = 0;
      }
  
      - (void)layoutSubviews
  
  
      [EAGLContext setCurrentContext:context];
  
      [self draw:nil];
  </code></pre>
  
  <p>The internal init is my main init method. </p>
  
  <pre><code> - (id)internalInit
  {    
  
          CAEAGLLayer* eaglLayer = (CAEAGLLayer*) super.layer;
          eaglLayer.opaque = YES;
  
  
          context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
  
          if (!context || ![EAGLContext setCurrentContext:context]) {
              [self release];
              return nil;
          }
  
      [self createFrameBuffer];
  
      [self setupView:self];
  
          [self drawView: nil];
  
          CADisplayLink* displayLink;
          displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(draw:)];
          [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
          [displayLink setFrameInterval:1];
  
      return self;
  }
  </code></pre>
  
  <p>The setupView: method basically creates viewport, sets frustum, all necessary variables and states. Nothing special. </p>
  
  <p>Thanks in advance for all and any minute spent on this.</p>