In OpenGL, is it possible for one shader program to display both FBO and framebuffer by default in one callback?

I would like to draw a scene to the default framebuffer, as well as draw some supporting data related to some of my painted models into an off-screen buffer with the same image size as the default framebuffer.

I understand that I can do these two things separately, in two calls to glDraw * (one rendering for FBO, and the other not), which will use two corresponding shader programs.

And I think (still learning about it), I can do them basically simultaneously, attaching two visualizers / textures to one FBO, making one glDraw * call with one shader program, whose fragment shader will write the corresponding values ​​to several outputs corresponding to multiple FBO attachments, and finally copying one of the two images in the FBO to the default framebuffer, using it to texture the square to fill the scene or call glBlitFramebuffer.

But can I make one call to glDraw * using one shader program and make my fragment shader record both the visible framebuffer and some off-screen FBO? I do not suspect that I have seen the relevant documentation, but I am not sure.

+5
source share
1 answer

No, you cannot draw different framebuffers at the same time. But you can draw different drawing buffers bound to the framebuffer. This is possible and is called several rendering objects.

You need to specify drawing buffers as follows:

GLenum DrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; glDrawBuffers(ARRAY_SIZE_IN_ELEMENTS(DrawBuffers), DrawBuffers); 

Then in the fragment shader you need to write something like:

 layout (location = 0) out vec3 WorldPosOut; layout (location = 1) out vec3 DiffuseOut; layout (location = 2) out vec3 NormalOut; layout (location = 3) out vec3 TexCoordOut; uniform sampler2D gColorMap; void main() { WorldPosOut = WorldPos0; DiffuseOut = texture(gColorMap, TexCoord0).xyz; NormalOut = normalize(Normal0); TexCoordOut = vec3(TexCoord0, 0.0); } 

This will output the values ​​to all drawing buffers at the same time. For a complete example, see http://ogldev.atspace.co.uk/www/tutorial35/tutorial35.html .

+5
source

All Articles