PDA

View Full Version : Opengl Deffered Rendering Questions



RobertBerg
03-29-2015, 10:05 AM
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 :)

Alfonse Reinheart
03-29-2015, 10:57 AM
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 (https://www.opengl.org/wiki/Fragment_Shader#Outputs), but not exact. The output color index indexes into the array of draw buffers that is part of the framebuffer state (https://www.opengl.org/wiki/Framebuffer#Draw_color_buffers). 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.

RobertBerg
03-29-2015, 11:56 AM
Thanks, That cleared out what I needed :)

RobertBerg
03-30-2015, 06:11 AM
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 ?

RobertBerg
03-30-2015, 06:30 AM
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 ?

Alfonse Reinheart
03-30-2015, 08:17 AM
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 (https://www.opengl.org/wiki/Sampler_%28GLSL%29#Binding_textures_to_samplers). 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 (https://www.opengl.org/wiki/Memory_Model#Framebuffer_objects).

If you have somehow forgotten which texture you put somewhere, you can query this from OpenGL with glGetFramebufferAttachmentParameter (https://www.opengl.org/wiki/GLAPI/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 (https://www.opengl.org/wiki/Framebuffer#Blitting) 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).

RobertBerg
03-30-2015, 10:58 AM
Can you give me some code example of sending GL_COLOR_ATTACHMENTn to a shader ?

Alfonse Reinheart
03-30-2015, 11:25 AM
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.

RobertBerg
03-31-2015, 06:54 AM
Oh.. yes I got confused, now I remember it :)) , thanks !

RobertBerg
03-31-2015, 08:35 AM
I have one problem now, I get black textures...

RobertBerg
03-31-2015, 08:37 AM
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_FRAMEBUFF ER, GL30.GL_COLOR_ATTACHMENT0 + i, GL11.GL_TEXTURE_2D, 0, 0);
}
Is this correct ?

RobertBerg
03-31-2015, 08:46 AM
I also get this 1708 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...

RobertBerg
03-31-2015, 11:57 AM
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 ?

RobertBerg
03-31-2015, 12:11 PM
This is the output I should get: http://i.imgur.com/IlYwbDR.png and this is what I get: 1709 not even a full screen quad...

RobertBerg
03-31-2015, 12:55 PM
Sorry everyone, for replying so much btw, but because I can't get this to work,this begins to frustrate me a little...

RobertBerg
04-01-2015, 04:37 AM
I solved the problem with the textures, the problem whas I binded them wrong...

RobertBerg
04-01-2015, 04:38 AM
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.