Opengl Deffered Rendering Questions

I just started getting into deferred rendering and in order to completely add this feature in my application I need to know certain things that I din’t find an answer to yet.

1. If I have multiple textures attached to my FBO for example: GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3. How do I chose in the fragment shader to which texture I want to render ?

Do I create the output like this if I want to write to GL_COLOR_ATTACHMENTn: layout(location = n) out vec3 color; ?

2. In order to render the contents of the FBO to screen do I need to draw a full screen quad ?

Thanks in advance !

Btw Its the first time I use this forum :slight_smile:

Do I create the output like this if I want to write to GL_COLOR_ATTACHMENTn: layout(location = n) out vec3 color; ?

That’s close to how it works, but not exact. The output color index indexes into the array of draw buffers that is part of the framebuffer state. So you need to set both the output indices and the draw buffer array.

In order to render the contents of the FBO to screen do I need to draw a full screen quad ?

That is the traditional method. Though there’s evidence to suggest that it’s faster to render multiple, smaller quads, for cache-coherency reasons.

Thanks, That cleared out what I needed :slight_smile:

I have one last question. How can I get the texture from GL_COLOR_ATTACHMENTn and send it to the fragment shader of the full screen quad for drawing ?

If I bind frameBuffer to 0 and use glBlitFramebuffer() whould it draw to the screen without having to draw quad and setting the GL_COLOR_ATTACHMENTn to the corresponding attribute in the fragment shader ?

How can I get the texture from GL_COLOR_ATTACHMENTn and send it to the fragment shader of the full screen quad for drawing ?

Well, since you put that texture there, I assume you know what texture it is. So just use that texture with a sampler in your shader. Just like normal. The only special thing you need to make sure of is that it’s not currently part of the draw framebuffer object when you try to read from it in the shader.

If you have somehow forgotten which texture you put somewhere, you can query this from OpenGL with glGetFramebufferAttachmentParameter. But this would represent a pretty pathological element of your code, with your left hand not knowing what your right hand is doing.

If I bind frameBuffer to 0 and use glBlitFramebuffer() whould it draw to the screen without having to draw quad and setting the GL_COLOR_ATTACHMENTn to the corresponding attribute in the fragment shader ?

I’m not sure what you mean by “the corresponding attribute in the fragment shader”. If you’re asking “does blitting call the current fragment shader”, then the answer is “no.” A blit is a copy operation. If you want to invoke the fragment shader, you need to [actually draw something](https://www.opengl.org/wiki/Drawing Command).

Can you give me some code example of sending GL_COLOR_ATTACHMENTn to a shader ?

Can you give me some code example of sending GL_COLOR_ATTACHMENTn to a shader ?

You don’t “send” a color attachment to a shader.

It’s not clear what part you’re having trouble with. Do you know how to use a texture with a shader at all? Do you know how to use a texture with an FBO as a render target?

If you know both of these, then you know everything you need. Nothing special happens to the texture just because you use an FBO to render to it. Simply unbind the FBO and use that texture as you always have.

Oh… yes I got confused, now I remember it :)) , thanks !

I have one problem now, I get black textures…

I do this before I try to read from the textures in the shader.:

GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, fbo);
for (int i = 0; i < textures.length; i++) {
GL30.glFramebufferTexture2D(GL30.GL_DRAW_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0 + i, GL11.GL_TEXTURE_2D, 0, 0);
}
Is this correct ?

I also get this [ATTACH=CONFIG]970[/ATTACH] on line GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL30.GL_RGB32F, Shared.getResolution()[0], Shared.getResolution()[1], 0, GL11.GL_RGB, GL11.GL_FLOAT, null); I also tried ByteBuffer NULL = null; and passed NULL as the last argument and didn’t get the error but still I don’t think its correct…

I have actually 1 more problem, The texture I bind with glActiveTexture(n); and glUniform1i(shader.getUniformLocation(“diffuse”), n); works just if n = 0, Why can this happen ?

This is the output I should get: and this is what I get: [ATTACH=CONFIG]971[/ATTACH] not even a full screen quad…

Sorry everyone, for replying so much btw, but because I can’t get this to work,this begins to frustrate me a little…

I solved the problem with the textures, the problem whas I binded them wrong…

The correct way to bind them is:

glUniform1i(TextureIndex, index);
glActiveTexture(GL_TEXTURE0 + index);
glBindTexture(GL_TEXTURE_2D, TextureHandle);
glBindSampler(index, SamplerHandle);

At least this works fine for me.