The default (physical) framebuffer can have 4 draw buffers (front left, front right, back left, back right), although stereoscopic contexts (with both left and right buffers) are rare, and it’s uncommon to access the front buffer directly.
A framebuffer object (FBO) can have multiple draw buffers identified by the targets GL_COLOR_ATTACHMENTi where i is between zero and GL_MAX_COLOR_ATTACHMENTS minus one.
glDrawBuffers controls which fragment shader output goes to which draw buffer. glDrawBuffer does the same for the fragment shader output with index zero, or the output from fixed-function rendering.
So for FBO , GL_COLOR_ATTACHMENTi and GL_DRAW_BUFFERi are the same thing? (I have verified that GL_MAX_COLOR_ATTACHMENTS has the same value as GL_MAX_DRAW_BUFFERS on my machine)
glDrawBuffers establishes a mapping between fragment shader outputs and draw buffers.
GL_COLOR_ATTACHMENTi identifies a specific render target (colour attachment or physical draw buffer). glGetIntegerv(GL_DRAW_BUFFERi) queries which render target is associated with fragment shader output i.
For the default framebuffer, output zero is initially associated with GL_BACK if a back buffer exists (double-buffered context) or GL_FRONT otherwise (single-buffered context). For a FBO, output zero is initially associated with GL_COLOR_ATTACHMENT0. In either case, outputs other than zero are initially associated with GL_NONE, i.e. you need to use glDrawBuffers to direct outputs other than zero to a specific colour attachment or physical buffer.
This isn’t required to be the case, although I suspect that it’s typical.
this function will return something like GL_COLOR_ATTACHMENTj , so how GL_DRAW_BUFFERi is associated with GL_COLOR_ATTACHMENTj?
In case of glDrawBuffer, it seems the implicit draw buffer will be GL_DRAW_BUFFER0, i.e. GL_DRAW_BUFFER0 is associated with whatever the parameter of the glDrawBuffer.
In case of glDrawBuffers , can I conclude that for every GL_COLOR_ATTACHMENTi there will a corresponding GL_DRAW_BUFFERi implicitly associated?
A draw buffer needn’t be associated with any attachment, i.e. it can be GL_NONE. Most FBOs don’t use all of the available attachments. I suspect that most FBOs only use GL_COLOR_ATTACHMENT0. And even if all attachments are used (i.e. have texture levels or renderbuffers bound to them), some of the draw buffers may be GL_NONE.
The initial state is for GL_DRAW_BUFFER0 to be mapped to GL_COLOR_ATTACHMENT0 and all others to be mapped to GL_NONE. If you want to use multiple attachments, you have to bind texture levels or renderbuffers to them and associate the corresponding draw buffer.
If you call glDrawBuffer, or glDrawBuffers with a count less than the value of GL_MAX_DRAW_BUFFERS, the other draw buffers are set to GL_NONE (i.e. each such call assigns all of the draw buffers, it doesn’t leave the other draw buffers at their previous setting).