1

Sorry if I am asking something which is already available. So far I could not trace. I read details about FBO and got a fair idea about off-screen buffering. http://mattfife.com/?p=2813 is a nice little article on FBOs. In all the examples, including this one, I don't see details on how to write the data, retrieved through glReadPixels call, onto default display framebuffer. Sorry if I am missing anything silly. I did my due diligence but could not get any example.

Note: I am using OpenGLES 2.0, hence I cannot use calls such as glDrawPixels, etc.

Basically my requirement is to have off-screen buffering. Because I am working on subtitle/captions wherein scrolling of the caption will have to repeat the rendering of lines till those go out of caption-display area.

I got a suggestion to use FBO and bind the texture created to the main default framebuffer.

My actual need is caption/ subtitle (which can be in scrolling mode) Suppose the first time I had below on display, This is Line Number - 1 This is Line Number - 2 This is Line Number - 3

After scrolling, then I want to have, This is Line Number - 2 This is Line Number - 3 This is Line Number - 4

In the second time when I want to render, I will have to update the content in offscreen FBO? That would be re-writing line-2 and line-3 at a new position, removing line-1 and adding line-4.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

Create a framebuffer with an texture attachment (see Attaching Images). Note glFramebufferTexture2D is supported by OpenGL ES 2.0.
The color plane of the framebuffer can be loaded to the CPU by glReadPixels, the same way as when you use a Renderbuffer. But the rendering is stored in a 2D Texture.
Bind the texture and the default framebuffer and render a screen space quad with the texture on it.

Render a quad (GL_TRIANLGE_FAN) with the vertex coordinates (-1, -1), (1, -1), (1, 1), (-1, 1) and use the following simple OpenGL ES 2.0 shader:

Vertex shader

attribute vec2 pos;
varying vec2 uv;

void main()
{
    uv = pos * 0.5 + 0.5;
    gl_Position = vec4(pos, 0.0, 1.0);
}

Fragment shader

precision mediump float;

varying vec2 uv;
uniform sampler2D u_texture;

void main()
{
    gl_FragColor = texture2D(u_texture, uv);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • One more question, I hope you could help: While taking content from the fbo/ attached texture, is it possible to take partial data? I felt glBlitFrameBuffer might be useful but it is not supported in OpenGLES 2.0. glScissor maybe? – Aurovind_Sagar Apr 24 '20 at 06:11
  • 1
    @Aurovind_Sagar You can "scale" the texture coordinates (`uv`) to a specific region. – Rabbid76 Apr 24 '20 at 06:23
  • Thanks, @Rabbid76 Maybe I will put the question this way. Suppose first time I had, This is Line Number -1 This is Line Number -2 This is Line Number -3 Then I want to have, This is Line Number -2 This is Line Number -3 This is Line Number -4 So basically I am trying to have a scrolling presentation. In the second time when I want to render, I will have to update the content in offscreen fbo? That would be re-writing line-2 and line-3 at new position, removing line-1 and adding line-4. Sorry to make the question a bit lengthy. But that's the ask I have in my work. – Aurovind_Sagar Apr 24 '20 at 10:07
  • 1
    @Aurovind_Sagar *" I will have to update the content in offscreen fbo?"* - No. YOu just have to get the proper "line" from the texture. Textures are repeating (`GL_REPEAT`). See [`glTexParameter`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexParameter.xhtml). You can add an offset to to the texture coordinates. e.g `texture2D(u_texture, uv + vec2(0.0, 0.5))`. Please note, the comment section is not intended to ask questions. Probably a moderator will delete all this commnts. – Rabbid76 Apr 24 '20 at 10:15
  • Yeah true. I will put it in a separate question post. – Aurovind_Sagar Apr 24 '20 at 10:35
  • Thanks @Rabbid76 for the texture, from the offscreen fbo, binding to default framebuffer. This worked. Continuing this I tried to render few texts (using freetype lib and then converting to texture using glTexImage2D). But this seems not working. When I try to render directly onto default framebuffer, it works. Probably textures (fonts) rendering onto the fbo's texture attachment is something not possible or needs to be done differently? – Aurovind_Sagar Apr 28 '20 at 05:01
  • 1
    @Aurovind_Sagar No, it is not different. – Rabbid76 Apr 28 '20 at 05:07
  • OK. I will try to debug then. Thanks! – Aurovind_Sagar Apr 28 '20 at 05:09
  • Hi @Rabbid76, I am still trying to debug. Meanwhile, I posted it as a question. It would be great if you can have a look. https://stackoverflow.com/questions/61475832/font-rendering-onto-off-screen-fbo-not-working-note-fonts-are-loaded-by-creat – Aurovind_Sagar Apr 28 '20 at 08:30
  • Hi @Rabbid76, can glFramebufferTexture2D be used directly on default framebuffer object 0 as well? – Aurovind_Sagar May 07 '20 at 06:54
  • 1
    @Aurovind_Sagar No. You cannot attach a buffer to the default framebuffer. The default framebuffer is the buffer that is associated to the window, it can't be _changed_ or _suited_ by the application after the context is established. It is just possible to specify the format of the default framebuffer (restricted to the hardware). – Rabbid76 May 07 '20 at 06:58
  • Hi @Rabbid76, that helps. I was thinking of creating a texture directly on default framebuffer. Is it possible to attach multiple textures to the same framebuffer object? Like attaching-1 then drawing a few things and then attaching-2 and drawing a few other things. And on default framebuffer I would draw both the attached textures. – Aurovind_Sagar May 07 '20 at 08:59
  • Hi @Rabbid76, as the last asked one is a separate question altogether. I posted a separate one, https://stackoverflow.com/questions/61654976/attaching-multiple-textures-to-single-fbo-andmultiple-fbos-in-opengl-es-2-0 Could you please help! – Aurovind_Sagar May 07 '20 at 10:02