title: How to restore a GL_RENDERBUFFER?
body: <p>I am working on storing and restoring my OpenGL ES based application's state.</p>
  
  <p>I have a function to save the GL_RENDERBUFFER to dump the data with the following code:</p>
  
  <pre><code>glBindFramebuffer(GL_FRAMEBUFFER, fboTextureBufferData.framebuffer);
  glBindRenderbuffer(GL_RENDERBUFFER, fboTextureBufferData.colorbuffer);
  
  GLint x = 0, y = 0, width2 = backingWidth, height2 = backingHeight;
  NSInteger dataLength = width * height * 4;
  GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));
  
  // Read pixel data from the framebuffer
  glPixelStorei(GL_PACK_ALIGNMENT, 4);
  glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
  </code></pre>
  
  <p>I don't see a glWritePixels function. What is the best way to repopulate the GL_RENDERBUFFER with the GLubyte data populated above? An example would be greatly appreciated.</p>
  
  <p>EDIT 3:</p>
  
  <p>Here is how I am attempting to configure the texture render buffer, and the function used to draw it. As noted in the code, if I specify GL_COLOR_ATTACHMENT1 for the glFramebufferTexture2D parameter, the stored pixel data is restored but I can't get any updates to draw. But if I use GL_COLOR_ATTACHMENT0 instead, I get drawing updates but no pixel data restored. </p>
  
  <p>I have tried various combinations (for instance also using GL_COLOR_ATTACHMENT1 for the glFramebufferRenderbuffer parameter) but then I get an invalid frame buffer error when attempting to render. It seems I am so close, but can't figure out how to get them both restoring and rendering working together.</p>
  
  <pre><code>- (bool)configureRenderTextureBuffer {
  
      [EAGLContext setCurrentContext:context];
  
      glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
      [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
  
      glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
      [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
  
      glGenFramebuffers(1, &amp;fboTextureBufferData.framebuffer);
      glBindFramebuffer(GL_FRAMEBUFFER, fboTextureBufferData.framebuffer);
  
      glGenRenderbuffers(1, &amp;fboTextureBufferData.colorbuffer);
      glBindRenderbuffer(GL_RENDERBUFFER, fboTextureBufferData.colorbuffer);
  
      glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, backingWidth, backingHeight);
      glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, fboTextureBufferData.colorbuffer);
  
      // Generate texture name, stores in .textureID
      glGenTextures(1, &amp;fboTextureBufferData.textureID);
  
      glBindTexture(GL_TEXTURE_2D, fboTextureBufferData.textureID);
  
      ////////////////// Read Existing texture data //////////////////
      NSString *dataPath = [TDTDeviceUtilitesLegacy documentDirectory]; //This just returns the app's document directory
      NSData *data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/buffer.data", dataPath]];
      GLubyte *pixelData = (GLubyte*)[data bytes];
  
      // If I use GL_COLOR_ATTACHMENT1 here, my existing pixel data is restored
      // but no drawing occurs. If I use GL_COLOR_ATTACHMENT0, then data isn't
      // restored but drawing updates work
      glFramebufferTexture2D ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, fboTextureBufferData.textureID, 0 );
  
      // Populate with existing data
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, backingWidth, backingHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, &amp;pixelData[0]); //&amp;image[0]
  
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  
      GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ;
      if(status != GL_FRAMEBUFFER_COMPLETE) {
          NSLog(@"failed to make complete render texture framebuffer object %x", status);
          return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p>Here is the code for rendering. The viewFramebuffer is attached to GL_COLOR_ATTACHMENT0 and is used so the texture frame buffer can be zoomed and positioned inside the view.</p>
  
  <pre><code>- (void)renderTextureBuffer {
  
      //Bind the texture frame buffer, if I don't use this, I can't get it to draw
      //glBindFramebuffer(GL_FRAMEBUFFER, fboTextureBufferData.framebuffer);
  
      //If I use this instead of binding the framebuffer above, I get no drawing and black background
      glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fboTextureBufferData.textureID, 0);
  
      renderParticlesToTextureBuffer();
  
      //Bind the view frame buffer.
      glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
      glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
  
      drawFboTexture();
  
      [context presentRenderbuffer:GL_RENDERBUFFER];
  }
  </code></pre>