Search code examples
openglglslshader

although doesn't set output color to second output color variable in fragment shader, it still set color to second color output variable (opengl)


i'm making some postprocessing work.

when i tried rendering to framebuffer having two color texture, i didn't wanna set output color to second ouput color variable, just wanna set to first variable, but shader still set internally first output color variable to second output color variable

Render Loop

BindFrameBuffer();
unsigned int a[2] = { GL_COLOR_ATTACHMENT0 , GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, a);
RenderSomthing();!!!
Fragment shader

#version 330 core
layout (location = 0) out vec4 FragColor; 
//layout (location = 1) out vec4 BloomColor; 

void main()
{
   FragColor = vec4(1.0);
   //BloomColor= vec4(1.0); // i dont wanna draw on second color output variable
}

i dont wanna touch second output color variable, i wanna just let it have cleared color

shader still set internally first output color variable to second output color variable


Solution

  • If you only want to write in one color attachment of the framebuffer, you only need to specify one drawing buffer before drawing the geometry:

    unsigned int a[1] = { GL_COLOR_ATTACHMENT0 };
    glDrawBuffers(1, a);
    RenderSomthing();
    

    See OpenGL 4.6 API Core Profile Specification - 17.4.1 Selecting Buffers for Writing:

    If a fragment shader writes to a user-defined output variable, DrawBuffers specifies a set of draw buffers into which each of the multiple output colors defined by these variables are separately written. If a fragment shader writes to no user-defined output variables, the values of the fragment colors following shader execution are undefined, and may differ for each fragment color. If some, but not all user-defined output variables are written, the values of fragment colors corresponding to unwritten variables are similarly undefined.


    One option to write to the buffers individual, is t o use Blending. Use glBlendFunci or glBlendFuncSeparate to individually specify the blending function for a draw buffer.

    e.g. disable writing to to draw buffer with index 1:

    glBlendFunci(1, GL_ZERO, GL_ONE);