简体   繁体   English

在OpenGL Fragment Shader中,gl_FragColor.a = 0和discard之间有什么区别?

[英]In OpenGL Fragment Shader, what's the difference between gl_FragColor.a = 0 and discard?

As titled, gl_FragColor.a = 0 is supposed to make thing transparent. 标题为,gl_FragColor.a = 0应该使事物透明。 So what's the diff from discard? 丢弃的差异是什么?

For the following code 对于以下代码

varying vec3 f_color; 
uniform sampler2D mytexture; 
varying vec2 texCoords;
float cutoff = 0.5;

void main(void) { 
  gl_FragColor = texture2D(mytexture,texCoords); 
  if( gl_FragColor.a < cutoff) discard;
}

discard will work but if I replace discard with gl_FragColor.a=0; 丢弃将工作,但如果我用gl_FragColor.a = 0替换丢弃; , it has no effect. ,它没有效果。 Why? 为什么?

First, alpha to zero will only hide the pixel if blending is enabled. 首先,如果启用了混合,alpha到零将仅隐藏像素。 Discard will prevent the fragment from being written entirely. 丢弃将阻止片段完全写入。 For example, if you write a fragment of alpha zero it will still update the depth buffer and stencil buffer even if you haven't altered the pixel's color. 例如,如果您编写一个alpha零的片段,即使您没有更改像素的颜色,它仍会更新深度缓冲区和模板缓冲区。 With discard it will not affect either of these. 丢弃它不会影响其中任何一个。

For a full example let's say I render a tree branch with one quad and use alpha testing as you're doing. 举一个完整的例子,假设我用一个四边形渲染一个树枝,并在你做的时候使用alpha测试。 By discarding the fragments that are transparent I can later render something behind that branch because the depth buffer won't drop those fragments in between. 通过丢弃透明的片段,我可以稍后在该分支后面渲染一些东西,因为深度缓冲区不会丢弃这些片段。 If you just set alpha to zero, the depth buffer would hold the full quad and anything rendered afterwards would be occluded by the entire quad, not the leaves that passed the discard test. 如果你只是将alpha设置为零,深度缓冲区将保持完整四边形,之后渲染的任何内容都会被整个四边形遮挡,而不是通过丢弃测试的叶子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有明确设置 gl_FragColor 的片段着色器? - Fragment shader without gl_FragColor explicitly set? OpenGL / Cocos2d-x着色器中v_texCoord与gl_FragCoord有什么区别? - OpenGL / Cocos2d-x What's difference between v_texCoord vs gl_FragCoord in shader? OpenCL和OpenGL的计算着色器有什么区别? - What is the difference between OpenCL and OpenGL's compute shader? 在OpenGL中,GL_RED和GL_R颜色格式之间有什么区别? - In OpenGL what's the difference between the GL_RED and GL_R color formats? openGL:片段着色器的gl_instanceID - openGL: gl_instanceID for fragment shader VTK的ProjectionTransformMatrix与OpenGL的GL_PROJECTION有什么区别? - What is the difference between ProjectionTransformMatrix of VTK and GL_PROJECTION of OpenGL? gl_PointCoord奇怪的OpenGL片段着色器行为 - Odd OpenGL fragment shader behavior with gl_PointCoord 使用 GL_TIME_ELAPSED 和 GL_TIMESTAMP 在 OpenGL 中查询时间有什么区别 - what is the difference between querying time elapsed in OpenGL with GL_TIME_ELAPSED and GL_TIMESTAMP Opengl:顶点和片段着色器之间的二次插值 - Opengl: Quadratic interplation between vertex and fragment shader "OpenGL中的图像和纹理有什么区别" - what's the difference between image and texture in OpenGL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM