title: render offscreen framebuffer texture to main framebuffer
body: <p>I'm trying to work with a framebuffer object that I have no control of how it is being set. So I'm creating an offscreen fbo with depth to draw my texture. Then draw it to the main framebuffer. I am at the point where I can see the texture from the resources using OpenGL profiler, but I just cant seem to get it to draw to the main framebuffer.</p>
  
  <p>Here is my code</p>
  
  <pre><code>GLsizei glSizeWidthEquiv = width;
          GLsizei glSizeHeightEquiv = height;
  
          GLint originalFramebuffer;
          glGetIntegerv(GL_FRAMEBUFFER_BINDING, &amp;originalFramebuffer);
  
          if(fbo == 0){
              glGenFramebuffers(1, &amp;fbo);
          }
          glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  
          //Creating texture for framebuffer
          if(texColorBuffer == 0){
              glGenTextures(1, &amp;texColorBuffer);
          }
          glBindTexture([outTex target], texColorBuffer);
          //Having it as null means the texture's data will be created dynamically
          //Using RGBA since the alpha layer is going to be needed
          glTexImage2D([outTex target], 0, GL_RGBA_FLOAT16_APPLE, glSizeWidthEquiv, glSizeHeightEquiv, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  
          glTexParameteri([outTex target], GL_TEXTURE_MIN_FILTER, GL_LINEAR);
          glTexParameteri([outTex target], GL_TEXTURE_MAG_FILTER, GL_LINEAR);
          //Attach texture to framebuffer
          glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, [outTex target], texColorBuffer, 0);
  
          //Creating render buffer
          if(renderBuffer == 0){
              glGenRenderbuffers(1, &amp;renderBuffer);
          }
          glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
          glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, glSizeWidthEquiv, glSizeHeightEquiv);
  
          //Binding the renderbuffer to the framebuffer
          glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);
  
          //Check framebuffer
          if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
              NSLog(@"Framebuffer incomplete noob\n");
          }else{
              NSLog(@"Framebuffer COMPLETE!\n");
          }
          //Start to use the frame buffer
          glBindFramebuffer(GL_FRAMEBUFFER, fbo);
          glViewport(0, 0, glSizeWidthEquiv, glSizeHeightEquiv);
          //cleaning texture: cleaning code here
  
          //initating glTex0
          glActiveTexture(GL_TEXTURE0);
  
          //setting up texture stuff
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                      glClearColor(0, 0, 0, 1);
          glDisable(GL_CULL_FACE);
  
          glColor3f(1,0,0);
  
  
          glBegin(GL_QUADS);
          //box 1 in front
          glVertex3f(0, 0, 0);
          glVertex3f(500, 0, 0);
          glVertex3f(500, 500, 0);
          glVertex3f(0, 500, 0);
  
          //box 2 at the back
          glColor3f(0, 1, 0);
          glVertex3f(-250, -250, 50);
          glVertex3f(250, -250, 50);
          glVertex3f(250, 250, 50);
          glVertex3f(-250, 250, 50);
          glEnd();
  
          glFogCoordf(9);
          //End of framebuffer usage
          glBindFramebuffer(GL_FRAMEBUFFER, originalFramebuffer);
          glViewport(0, 0, (GLsizei)[outTex width], (GLsizei)[outTex height]);
          [self glGetError];
  
          glActiveTexture(GL_TEXTURE0);
          glBindTexture([outTex target], texColorBuffer);
          glCopyTexSubImage2D([outTex target], 0, 0, 0, 0, 0, glSizeWidthEquiv, glSizeHeightEquiv);
  
  
          glFlush();
  </code></pre>
  
  <p>This part of the program is at the render portion and I set the fbo,renderBuffer and texColorBuffer as 0 on initialize for me to keep it from generating new buffers.</p>
  
  <p>So essentially I am creating the buffer until its complete, then rebinding the fbo I created to draw my shape. Switch back to the original fbo and try to draw it.</p>
  
  <p>The [outTex target] being used here is GL_TEXTURE_RECTANGLE_ARB.</p>
  
  <p>This is what I have in my texture portion of OpenGL Profiler:
  <a href="http://img.photobucket.com/albums/v442/ardo/ScreenShot2013-10-27at112742PM.png" rel="nofollow">http://img.photobucket.com/albums/v442/ardo/ScreenShot2013-10-27at112742PM.png</a></p>
  
  <p>@GMasucci</p>
  
  <p>I'm not entirely sure if this is the correct way of creating an sscce, but here is a code that is easily copy pasted, include the libraries, GLUT and OpenGL.
  The code used here is piggy-backed off of www.videotutorialsrock.com lighting lessons so that I have a similar environment.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  
  #ifdef __APPLE__
  #include &lt;OpenGL/OpenGL.h&gt;
  #include &lt;GLUT/glut.h&gt;
  #else
  #include &lt;GL/glut.h&gt;
  #endif
  
  using namespace std;
  
  GLuint fbo,texColorBuffer,renderBuffer;
  float  camMatrix [4][4];
  //Initializes 3D rendering
  void initRendering() {
      glEnable(GL_DEPTH_TEST);
  
  }
  
  //Called when the window is resized
  void handleResize(int w, int h) {
      glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
  }
  
  //Draws the 3D scene
  void drawScene() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
  glTranslatef(0.0f, 0.0f, -8.0f);
  
  
  if(fbo == 0){
      glGenFramebuffers(1, &amp;fbo);
  }
  glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  
  if(texColorBuffer == 0){
      glGenTextures(1, &amp;texColorBuffer);
  }
  glBindTexture(GL_TEXTURE_2D, texColorBuffer);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 400, 400, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
  
  if(renderBuffer == 0){
      glGenRenderbuffers(1, &amp;renderBuffer);
  }
  glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 400, 400);
  
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);
  
  if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
      printf("Error in framebuffer completeness \n");
  }
  glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  glViewport(0, 0, 400, 400);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  glActiveTexture(GL_TEXTURE0);
  
  glBegin(GL_QUADS);
  glColor3f(1,0,0);
  glVertex3f(0, 0, 0);
  glVertex3f(200, 0, 0);
  glVertex3f(200, 200, 0);
  glVertex3f(0, 200, 0);
  
  glColor3f(0, 1, 0);
  glVertex3f(-100, -100, -1);
  glVertex3f(100, -100, -1);
  glVertex3f(100, 100, -1);
  glVertex3f(-100, 100, -1);
  
  glEnd();
  
  glFogCoordf(0);
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
  glViewport(0, 0, (GLsizei)400, (GLsizei)400);
  
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, texColorBuffer);
  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 400, 400);
  
  glutSwapBuffers();
  }
  
  void update(int value) {
  glutPostRedisplay();
  glutTimerFunc(25, update, 0);
  }
  
  int main(int argc, char** argv) {
  //Initialize GLUT
  glutInit(&amp;argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowSize(400, 400);
  
  //set fbo,texBuffer and renderbuffer to 0
  fbo = 0;
  texColorBuffer = 0;
  renderBuffer = 0;
  
  //Create the window
  glutCreateWindow("Lighting - videotutorialsrock.com");
  initRendering();
  
  //Set handler functions
  glutDisplayFunc(drawScene);
  glutReshapeFunc(handleResize);
  
  glutTimerFunc(25, update, 0); //Add a timer
  
  glutMainLoop();
  return 0;
  }
  </code></pre>