title: Using CVOpenGLESTextureRef as a render target?
body: <p>I am trying to figure out how to use <code>CVOpenGLESTextureRef</code> and <code>CVOpenGLESTextureCacheRef</code> to replace using <code>glReadPixels</code>.<br/><br/>
  I understand how to use them to create a texture from incoming camera images as shown in the RosyWriter and CameraRipple demos. But I can't figure out how to use them to go the other way.</p>
  
  <p>Comments in the header file for the function <code>CVOpenGLESTextureCacheCreateTextureFromImage</code> give the following example:</p>
  
  <pre><code>CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache, 
      pixelBuffer, NULL, GL_RENDERBUFFER, GL_RGBA8_OES, width, height, 
      GL_RGBA, GL_UNSIGNED_BYTE, 0, &amp;outTexture);
  </code></pre>
  
  <p>and this is all the information I can find. How do you use this?</p>
  
  <p>Currently I am doing the following to create my offscreen Frame Buffer Object at the start of the app.</p>
  
  <pre><code>glGenFramebuffersOES(1, &amp;frameBufferHandle);
  glBindFramebufferOES(GL_FRAMEBUFFER_OES, frameBufferHandle);
  glGenRenderbuffersOES(1, &amp;colorBufferHandle);
  glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorBufferHandle);
  glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, width, height);
  glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, 
      GL_COLOR_ATTACHMENT0_OES,GL_RENDERBUFFER_OES, colorBufferHandle);
  </code></pre>
  
  <p>Then when I need to write it out to disk I bind it and call <code>glReadPixels</code>.</p>
  
  <ul>
  <li>How would I use <code>CVOpenGLESTextureRef</code> and <code>CVOpenGLESTextureCacheRef</code> instead of the above?</li>
  <li>How would I use it on the main render buffer, and not on an offscreen FBO?</li>
  </ul>
  
  <p><br/>Other information that may be pertinent:</p>
  
  <ul>
  <li>I am using OpenGL ES 1.1 on 2.0 capable devices (moving to 2.0 is not an option).</li>
  <li>I am already using <code>CVOpenGLESTextureRef</code> and <code>CVOpenGLESTextureCacheRef</code> to display the camera video image on screen.</li>
  <li>I am writing out to video using <code>CVPixelBufferRefs</code> and BRGA format.</li>
  <li>If I use the main renderbuffer I can call <code>glReadPixels</code> with <code>GL_BGRA_EXT</code>.</li>
  <li>If I use an offscreen FBO (for a smaller video size) I have to use RGBA format and do bit swizzling.</li>
  </ul>