gl_FragColor 如何影响模板缓冲区?
How does gl_FragColor affect the stencil buffer?
我已将我的操作设置为绘制到模板缓冲区,类似于以下内容:
void onDisplay() {
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glStencilFunc(GL_NEVER, 1, 0xFF);
glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);
// Draw to stencil buffer
glStencilMask(0xFF);
glClear(GL_STENCIL_BUFFER_BIT); // needs mask=0xFF
draw_circle();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
glStencilMask(0x00);
// draw where stencil's value is 0
glStencilFunc(GL_EQUAL, 0, 0xFF);
/* (nothing to draw) */
// draw only where stencil's value is 1
glStencilFunc(GL_EQUAL, 1, 0xFF);
draw_scene();
glDisable(GL_STENCIL_TEST);
}
现在,如果我在调用 draw_circle()
(以上)时启用了以下片段着色器:
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
如果我使用以下片段着色器,模板缓冲区的值将有何不同?
void main() {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
换句话说,我想知道在绘制到模板缓冲区时片段着色器的输出如何影响模板缓冲区。
编辑:
我的问题的重点是纠正一些我知道的关于模板缓冲区的误解。我认为可以很好地解释模板缓冲区的一个例子是 [1]。在这里,提到以下内容:
The glColorMask function allows you to specify which data is written to the color buffer during a drawing operation. In this case you would want to disable all color channels (red, green, blue, alpha). Writing to the depth buffer needs to be disabled separately as well with glDepthMask, so that cube drawing operation won't be affected by leftover depth values of the rectangle. This is cleaner than simply clearing the depth buffer again later.
因此,从该页面看来,为了写入模板缓冲区,需要 enable/disable 适当的模式(即颜色和深度),然后通过 整个光栅化过程,只会写入模板缓冲区。由于光栅化过程 包括 片段着色器,片段着色器的输出(即 gl_FragColor
)是否被简单地忽略了?我如何告诉 GL what 写入模板缓冲区位置 (x, y)
?
除非您有权访问 AMD/ARB_shader_stencil_export,否则片段着色器无法直接影响模板缓冲区的输出。唯一的例外是 discard
ing 片段。
并根据this database, only AMD cards provide this extension. Also, that extension exposes an output specifically for the stencil. It modifies the stencil value of the fragment;片段的颜色值永远不会影响片段的模板值。
我已将我的操作设置为绘制到模板缓冲区,类似于以下内容:
void onDisplay() {
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glStencilFunc(GL_NEVER, 1, 0xFF);
glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);
// Draw to stencil buffer
glStencilMask(0xFF);
glClear(GL_STENCIL_BUFFER_BIT); // needs mask=0xFF
draw_circle();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
glStencilMask(0x00);
// draw where stencil's value is 0
glStencilFunc(GL_EQUAL, 0, 0xFF);
/* (nothing to draw) */
// draw only where stencil's value is 1
glStencilFunc(GL_EQUAL, 1, 0xFF);
draw_scene();
glDisable(GL_STENCIL_TEST);
}
现在,如果我在调用 draw_circle()
(以上)时启用了以下片段着色器:
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
如果我使用以下片段着色器,模板缓冲区的值将有何不同?
void main() {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
换句话说,我想知道在绘制到模板缓冲区时片段着色器的输出如何影响模板缓冲区。
编辑:
我的问题的重点是纠正一些我知道的关于模板缓冲区的误解。我认为可以很好地解释模板缓冲区的一个例子是 [1]。在这里,提到以下内容:
The glColorMask function allows you to specify which data is written to the color buffer during a drawing operation. In this case you would want to disable all color channels (red, green, blue, alpha). Writing to the depth buffer needs to be disabled separately as well with glDepthMask, so that cube drawing operation won't be affected by leftover depth values of the rectangle. This is cleaner than simply clearing the depth buffer again later.
因此,从该页面看来,为了写入模板缓冲区,需要 enable/disable 适当的模式(即颜色和深度),然后通过 整个光栅化过程,只会写入模板缓冲区。由于光栅化过程 包括 片段着色器,片段着色器的输出(即 gl_FragColor
)是否被简单地忽略了?我如何告诉 GL what 写入模板缓冲区位置 (x, y)
?
除非您有权访问 AMD/ARB_shader_stencil_export,否则片段着色器无法直接影响模板缓冲区的输出。唯一的例外是 discard
ing 片段。
并根据this database, only AMD cards provide this extension. Also, that extension exposes an output specifically for the stencil. It modifies the stencil value of the fragment;片段的颜色值永远不会影响片段的模板值。