虽然没有将输出颜色设置为片段着色器中的第二个输出颜色变量,但它仍然将颜色设置为第二个颜色输出变量(opengl)

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

我正在做一些后处理工作。

当我尝试渲染到具有两种颜色纹理的帧缓冲区时, 我不想将输出颜色设置为第二个输出颜色变量,只想设置为第一个变量, 但是着色器仍然在内部将第一个输出颜色变量设置为第二个输出颜色变量

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
}

我不想触摸第二个输出颜色变量,我只想让它清除颜色

着色器仍然在内部将第一个输出颜色变量设置为第二个输出颜色变量

如果只想写入帧缓冲区的一种颜色附件,则只需在绘制几何之前指定一个绘图缓冲区即可:

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

参见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.


单独写入缓冲区的一个选项是使用 Blending. Use glBlendFunci or glBlendFuncSeparate 单独指定绘图缓冲区的混合函数。

例如禁止写入索引为 1 的绘制缓冲区:

glBlendFunci(1, GL_ZERO, GL_ONE);