1

I'm still new on opengl3 and I'm trying to create a multipass rendering.
In order to do that, I created FBO, generated several textures and attached them to it with

    unsigned index_col = 0;
    for (index_col = 0; index_col < nbr_textures;  ++index_col) 
            glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index_col, texture_colors[index_col], 0);

It works well (I try to believe that I'm doing good here!).

My comprehension problem occurs after, when I try to render in the first texture offscreen, then in the second texture, then render on the screen.
To render to a particular texture, I'm using :

FBO.bind();
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glBindTexture(GL_TEXTURE_2D, FBO.getColorTextures(0)); //getColorTextures(0) is texture_colors[0]

Then I draw, using my shader, and after I would like to do :

glDrawBuffer(GL_COLOR_ATTACHMENT1);
glBindTexture(GL_TEXTURE_2D, FBO.getColorTextures(1));

and after all

glBindFramebuffer(GL_FRAMEBUFFER, 0);
RenderToScreen(); // assuming this function render to screen with a quad

My question is : What is the difference between glDrawBuffer and glBindTexture? Is it necessary to call both? Aren't textures attached to buffer? (I can't test it actually, because I'm trying to make it works...)

Thanks!

Raph Schim
  • 528
  • 7
  • 30

2 Answers2

2

glBindTexture is connecting a texture with a texture sampler unit for reading. glDrawBuffer selects the destination for drawing writes. If you want to select a texture as rendering target use glDrawBuffer on the color attachment the texture is attached to; and make sure that none of the texture sampler units it is currently bound to is used as a shader input! The result of creating a feedback loop is undefined.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • So basically, actually, what I'm doing is utterly wrong? I need to remove that `glBindTexture` in order to suceed at my task ... Thanks! – Raph Schim Mar 21 '17 at 11:02
1

glDrawBuffer selects the color buffer (in this case of the framebuffer object) that you will write to:

When colors are written to the frame buffer, they are written into the color buffers specified by glDrawBuffer

If you wanted to draw to multiple color buffers you would have written

GLuint attachments[2] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, attachments);  

while glBindTexture binds a texture to a texture unit.

They serve different purposes - remember that OpenGL and its current rendering context behave as a state machine.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • Actually, I want to draw into 1 attachment then into another attachment, not both at same times. Btw, when I use glDrawBuffers of 2 attachment, what will happen if I switch GL_COLOR_ATTACHMENT0 and GL_COLOR_ATTACHMENT1. which one will correspond to which layer? – Raph Schim Mar 21 '17 at 11:04
  • @RaphaelSchimchowitsch [Take a look at here](https://www.khronos.org/opengl/wiki/Fragment_Shader#Output_buffers) for your answer – Marco A. Mar 21 '17 at 11:10
  • Thanks! I will take a look! Thanks for your help! Opengl is not easy-minded when you're not familiar with /: – Raph Schim Mar 21 '17 at 11:11