OpenGL offscreen rendering - opengl

I was trying to render a 2D scene into a off-screen FrameBuffer Object and use glFrameBufferTexture2D function to use the frame as texture to texture a cube.
The 2D scene is rendered in one context and the texture is used in another one in the same thread.
The problem is when I textured the cube, alpha channel seemed to be incorrect. I used apitrace to check the texture, and the texture has correct alpha value, and the shader was merely out_color = texture(in_texture, uv_coords)
The problem was solved if I blit the off-screen framebuffer color attachment to anything, whether it be itself or framebuffer 0 (output window).
I was wondering why this is happening and how to solve it without needing to blit the framebuffer.

Found out that I used single buffering for the static 2D scene and requires a glFlush to flush the pipeline.

Related

Premultiplied alpha and multisampling in OpenGL

I'm rendering some triangles into multisampled texture. Then blit this texture into normal texture and draw a textured quad onto screen.
I need the final texture to have premultiplied alpha. Is there a way to tell OpenGL to automatically premultiply semi-transparent pixels in multisampled texture (or when blitting)?
Or the only way is using an extra shader pass to perform multiplication manually?
A multisample resolve blit can't perform pre-multiplication. You will have to pre-multiply the texture's pixels in the process that reads from the texture.

Rendering layers of bitmaps with alpha to FBO

I need to draw several "layers" of bitmaps that are semi-transparent to a FBO (for later readback).
My current approach is to create a FBO, attach a texture to it and use glTexSubImage2D to "draw" the bitmaps to the FBO, this however, doesn't work as glTexSubImage2D doesn't draw/blend the pixels, but just overwrite the pixels currently in the texture.
What's the best way to do this?
You create a FBO with a clean texture R attached to hold the final result.
For each of your bitmaps you:
Upload the bitmap to a texture T (T and and R are different textures).
Render a quad textured with T into the FBO with GL_BLEND enabled and properly set up.
The final result is that R holds your blended bitmaps. You can now read it back or use in other texturing operations.

Set background color to a texture

So in openGL when I call glClearColor() is there a way to set the background color to a texture instead of setting it just to a static color? Or is there another method which can do that besides glClearColor()?
You cannot clear the screen to a texture. You can:
Draw a textured quad the size of the screen.
Blit a texture from an FBO onto the screen.
Either one will work.

Separate Frame Buffer and Depth Buffer in OpenGL

In DirectX you are able to have separate render targets and depth buffers, so you can bind a render target and a depth buffer, do some rendering, remove the depth buffer and then do more rendering using the old depth buffer as a texture.
How would you go about this in opengl? From my understanding, you have a framebuffer object that contains both the color buffer(s) and an optional depth buffer. I don't think I can bind several framebuffer objects at the same time, would I have to recreate the framebuffer object every time it changes(probably several times a frame)? How do normal opengl programs do this?
A Framebuffer Object is nothing more than a series of references to images. These can be images in Textures (such as a mipmap layer of a 2D texture) or Renderbuffers (which can't be used as textures).
There is nothing stopping you from assembling an FBO that uses a texture's image for its color buffer and a texture's image for its depth buffer. Nor is there anything stopping you from later (so long as you're not rendering to that FBO while doing this) sampling from the texture as a depth texture. The FBO does not suddenly own these images exclusively or something.
In all likelihood, what has happened is that you've misunderstood the difference between an FBO and OpenGL's Default Framebuffer. The default framebuffer (ie: the window) is unchangeable. You can't take it's depth buffer and use it as a texture or something. What you do with an FBO is your own business, but OpenGL won't let you play with its default framebuffer in the same way.
You can bind multiple render targets to a single FBO, which should to the trick. Also since OpenGL is a state machine you can change the binding and number of targets anytime it is required.

Normal back buffer + render to depth texture in OpenGL (FBOs)

Here's the situation: I have a texture containing some depth values. I want to render some geometry using that texture as the depth buffer, but I want the color to be written to the normal framebuffer (i.e., the window's framebuffer).
How do I do this? I tried creating an FBO and only binding a depth texture to it (GL_DEPTH_COMPONENT) but that didn't work; none of the colors showed up.
No you can't. The FBO you are rendering to may be either a main framebuffer or an off-screen one. You can't mix them in any way.
Instead, I would suggest you to render to a color renderbuffer and then do a simple blitting operation into the main framebuffer.
Edit-1.
Alternatively, if you already have depth in the main FB, you can first blit your depth and then render to a main FB, thus saving video memory on the additional color renderbuffer.
P.S. Blitting is done via glBlitFramebuffer. In order to make it work you should setup GL_READ_FRAMEBUFFER, GL_DRAW_FRAMEBUFFER and glDrawBuffer() for each of them.