2

Initially, I wanted to have 2 texture attachments to one FBO but I see in OpenGL ES 2.0 GL_COLOR_ATTACHMENTi is not supported (only GL_COLOR_ATTACHMENT0 is supported). But just to see what happens with 2 texture attachment with the same GL_COLOR_ATTACHMENT0, I could see only the second texture's content visible.

Then I thought to have multiple FBOs with respective textures. But I still only see the 2nd texture (attached to 2nd FBO).

GLuint fbo;
  glGenFramebuffers(1, &fbo);
  glBindFramebuffer(GL_FRAMEBUFFER, fbo);

  GLuint texture;
  glGenTextures(1, &texture);
  glBindTexture(GL_TEXTURE_2D, texture);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1920, 1080, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
  glBindTexture(GL_TEXTURE_2D, 0);

  GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  if (status != GL_FRAMEBUFFER_COMPLETE) {
    printf("Problem with OpenGL framebuffer : %x\n", status);
  }

  glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // This is needed
  DrawRect();

  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  RenderTexture("-- Hello texture 1");
  glDisable(GL_BLEND);
  glBindTexture(GL_TEXTURE_2D, 0);

  GLuint fbo2;
  glGenFramebuffers(1, &fbo2);
  glBindFramebuffer(GL_FRAMEBUFFER, fbo2);

  GLuint texture2;
  glGenTextures(1, &texture2);
  glBindTexture(GL_TEXTURE_2D, texture2);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1920, 1080, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture2, 0);
  glBindTexture(GL_TEXTURE_2D, 0);

  status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  if (status != GL_FRAMEBUFFER_COMPLETE) {
    printf("Problem with OpenGL framebuffer2 : %x\n", status);
  }

  glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // This is needed
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  RenderTexture(" & texture 2 -- ");
  glDisable(GL_BLEND);
  glBindTexture(GL_TEXTURE_2D, 0);

  glBindFramebuffer(GL_FRAMEBUFFER, 0);
  glBindTexture(GL_TEXTURE_2D, texture);
  render_fboTexture();
  glBindTexture(GL_TEXTURE_2D, texture2);
  render_fboTexture();
  glBindTexture(GL_TEXTURE_2D, 0);

  eglSwapBuffers(egldisplay, eglsurface);

Full code can be seen at - Font rendering onto off-screen FBO not working. (Note: fonts are loaded by creating textures using Freetype lib with glTexImage2D)

  • I tried first drawing the texture-1 onto texture-2(FBO2) and then added new things needed in texture-2. I could draw everything. Is that a correct approach? – Aurovind_Sagar May 07 '20 at 10:21
  • 1
    "*But I still only see the 2nd texture (attached to 2nd FBO).*" - When you draw the 2nd texture, then this texture covers all what was drawn with texture 1 (all what was draw before is overwritten). You have to enable blending when you draw texture 2. Alternatively you can implement a shader that _mix_ texture 1 and texture 2. – Rabbid76 May 07 '20 at 13:37
  • @Rabbid76, yeah blending helped. Great! Also, it does not need 3 FBOs. I tried having just one FBO and 3 textures; able to render all 3 textures with blending. – Aurovind_Sagar May 07 '20 at 16:56
  • Hi @Rabbid76, I guess it's possible to create a smaller dimension texture (say 300 x 100)? I am trying to create a smaller dimension texture glTexImage2D(..., 300, 100, ...); and for that, I also set the viewport as glViewport(100, 100, 300, 100);. Finally, I wish to render this texture at a particular position on the default framebuffer. Note, I set the viewport back to the full size. Is this a correct approach and possible? – Aurovind_Sagar May 08 '20 at 11:45

0 Answers0