title: Render OpenGL ES 2.0 to image
body: <p>I am trying to do some OpenGL ES 2.0 rendering to an image file, independent of the rendering being shown on the screen to the user. The image I'm rendering to is a different size than the user's screen. I just need a byte array of GL_RGB data. I'm familiar with glReadPixels, but I don't think it would do the trick in this case since I'm not pulling from an already-rendered user screen.</p>
  
  <p>Pseudocode:</p>
  
  <pre><code>// Switch rendering to another buffer (framebuffer? renderbuffer?)
  
  // Draw code here
  
  // Save byte array of rendered data GL_RGB to file
  
  // Switch rendering back to user's screen.
  </code></pre>
  
  <p>How can I do this without interrupting the user's display? I'd rather not have to flicker the user's screen, drawing my desired information for a single frame, glReadPixel-ing and then having it disappear.</p>
  
  <p>Again, I don't want it to show anything to the user. Here's my code. Doesn't work.. am I missing something?</p>
  
  <pre><code>unsigned int canvasFrameBuffer;
  bglGenFramebuffers(1, &amp;canvasFrameBuffer);
  bglBindFramebuffer(BGL_RENDERBUFFER, canvasFrameBuffer);
  unsigned int canvasRenderBuffer;
  bglGenRenderbuffers(1, &amp;canvasRenderBuffer);
  bglBindRenderbuffer(BGL_RENDERBUFFER, canvasRenderBuffer);
  bglRenderbufferStorage(BGL_RENDERBUFFER, BGL_RGBA4, width, height);
  bglFramebufferRenderbuffer(BGL_FRAMEBUFFER, BGL_COLOR_ATTACHMENT0, BGL_RENDERBUFFER, canvasRenderBuffer);
  
  unsigned int canvasTexture;
  bglGenTextures(1, &amp;canvasTexture);
  bglBindTexture(BGL_TEXTURE_2D, canvasTexture);
  bglTexImage2D(BGL_TEXTURE_2D, 0, BGL_RGB, width, height, 0, BGL_RGB, BGL_UNSIGNED_BYTE, 0);
  bglFramebufferTexture2D(BGL_FRAMEBUFFER, BGL_COLOR_ATTACHMENT0, BGL_TEXTURE_2D, canvasTexture, 0);
  
  Matrix::matrix_t identity;
  Matrix::LoadIdentity(&amp;identity);
  bglClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  bglClear(BGL_COLOR_BUFFER_BIT);
  Draw(&amp;identity, &amp;identity, this);
  bglFlush();
  bglFinish();
  
  byte *buffer = (byte*)Z_Malloc(width * height * 4, ZT_STATIC);
  bglReadPixels(0, 0, width, height, BGL_RGB, BGL_UNSIGNED_BYTE, buffer);
  SaveTGA("canvas.tga", buffer, width, height);
  Z_Free(buffer);
  
  // unbind frame buffer
  bglBindRenderbuffer(BGL_RENDERBUFFER, 0);
  bglBindFramebuffer(BGL_FRAMEBUFFER, 0);
  bglDeleteTextures(1, &amp;canvasTexture);
  bglDeleteRenderbuffers(1, &amp;canvasRenderBuffer);
  bglDeleteFramebuffers(1, &amp;canvasFrameBuffer);
  </code></pre>