title: Render to texture attached to Frame Buffer Object (texture appears black)
body: <p>I have generated a frame buffer object(FBO), bind it, attached a texture to its <code>GL_COLOR_ATTACHMENT0</code>. Then ensure that I get its status as <code>GL_FRAMEBUFFER_COMPLETE</code>. I have another texture(which is displaying on display), let's call it <code>workingTexture</code>. Now I want to render this <code>workingTexture</code> onto FBO, so I bind this FBO and then render <code>workingTexture</code>.
  Then I bind default frame buffer(reserved for display) and try to render the texture attached to FBO, thinking that I will get my texture onto display, but I get black texture.</p>
  
  <p>Relevant code</p>
  
  <p>code to generate workingTexture...</p>
  
  <pre><code>int[] workingTexture = new int[1];
  
  glGenTextures(1, workingTexture, 0); // generate workingTexture
  glActiveTexture(GL_TEXTURE0);  // attach it to TEXTURE_UNIT0
  glBindTexture(GL_TEXTURE_2D, workingTexture[0]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  </code></pre>
  
  <p>code to generate fbo</p>
  
  <pre><code>int[] fboName = new int[1];
  int[] fboTextureName = new int[1];
  final int fboTextureUnit = 1;  // attach this texture to texture unit GL_TEXTURE1
  glGenFramebuffers(1, fboName, 0);
  
  int generatedTextureNameForFBO =
                  generateTextureForFBO(fboTextureUnit,
                          width, height);
  fboTextureName[0] = generatedTextureNameForFBO;
  
  glBindFramebuffer(GL_FRAMEBUFFER, fboName[0]);
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                  GL_TEXTURE_2D, generatedTextureNameForFBO, 0);
  
  if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
              // created FBO is not complete
              throw new RuntimeException("FBO status INCOMPLETE ");
  
          }
  </code></pre>
  
  <p>method used to generate texture attached to fbo</p>
  
  <pre><code> private int generateTextureForFBO(@IntRange(from = 0) final int textureUnit,
                                    @IntRange(from = 1) final int width,
                                    @IntRange(from = 1) final int height) {
      int[] textureName = new int[1];
      glGenTextures(1, textureName, 0);
      glActiveTexture(GL_TEXTURE0 + textureUnit);
      glBindTexture(GL_TEXTURE_2D, textureName[0]);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
    //        glBindTexture(GL_TEXTURE_2D, 0);
    //        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    //        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    //        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
    //        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
    //        glBindTexture(GL_TEXTURE_2D, 0);
      return textureName[0];
  }
  </code></pre>
  
  <p>Now coming to drawing part...</p>
  
  <pre><code>@Override
  public void onDrawFrame(GL10 unused) {
      glClear(GL_COLOR_BUFFER_BIT);
      //renderTex(mViewMatrix, 0, texProgram);  // this texture is drawing on screen
  
      glBindFramebuffer(GL_FRAMEBUFFER, fboName[0]);
      renderTex(mViewMatrix, 0, texProgram);   // rendering workingTexture onto FBO
  
      glBindFramebuffer(GL_FRAMEBUFFER, 0);
      renderTex(mViewMatrix, fboTextureUnit[0], texProgram);  // texture appears black
  
  }
  </code></pre>
  
  <p>renderTex is a simple method to render texture. It is working fine. </p>
  
  <p>Also I have checked for <code>GL_ERROR</code> but there is no error.</p>
  
  <p>My understanding of application created FBO is that every read and write <code>glCall</code> will happen on currently bound FBO, so is my understanding of FBO incorrect, or is there some error in my code.</p>
  
  <p>Platform OpenGL|es 20 on Android</p>