OpenGL:我可以从深度缓冲区中屏蔽完全透明的片段吗?
有没有办法告诉OpenGL不要将完全透明片段的深度写入深度缓冲区?
请确保,我想要渲染的纹理永远不会是半透明的;它的 alpha 值只有 1.0 或 0.0,并且我使用 GL_NEAREST 过滤器,因此它永远不会插入到其间的任何值。
我认为当片段的 alpha 值为 0.0 时,OpenGL 能够简单地不写入深度缓冲区是合理的(这样我就可以以任何顺序渲染具有此类纹理的基元),但我找不到让 OpenGL 做到这一点的方法。有谁知道这是否可能,在这种情况下,它是如何完成的?
Is there a way to tell OpenGL not to write the depth of wholly transparent fragments into the depth buffer?
Just be be sure, the texture I want to render is never semi-transparent; its alpha values are only ever 1.0 or 0.0, and I use the GL_NEAREST filter, so it never interpolates into any value in between.
I would think that it would be reasonable for OpenGL to be able to simply not write into the depth buffer when a fragment turns out to have an alpha value of 0.0 (this way I could render primitives with such textures in any order), but I cannot find a way to make OpenGL do that. Does anyone know if it is possible and, in that case, how it is done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是为了澄清:您希望 alpha=0.0 的片段既不写入颜色缓冲区也不写入深度缓冲区?此外,我假设您当前正在使用混合来掩盖透明像素,因为否则这不应该成为问题。
在这种情况下,您可以简单地在片段着色器中使用丢弃片段:
这将确保条件为真的所有片段根本不会被渲染(而不是与帧缓冲区上的零因子混合)。
如果您(无论出于何种原因)对固定管道进行编程,您可以使用 alpha 测试来获得相同的行为(我个人建议切换到前向兼容(即着色器)路径,但这不是重点)。
作为一个额外的好处,因为这些方法会杀死片段而不是使其不可见,所以它可能比混合更快(虽然我最近没有研究过这一点,所以这是一个可能,但它通常不应该更慢,所以它仍然是一个优点)
Just to clarify: You want fragments with alpha=0.0 to be written into neither the colorbuffer nor the depthbuffer? Furthermore I assume you are currently using blending to mask transparent pixels, because otherwise this shouldn't be a problem.
In this case you can simply use discard the fragment in your fragmentshader:
This will ensure that all fragments for which the condition is true won't be rendered at all (instead of being blended with a zero-factor onto the framebuffer).
If you are (for whatever reason) programming fixed pipeline you can use the alpha test to get the same behaviour (personally I would suggest switching to a forward compatible (meaning shader) path, but thats beside the point).
As an added bonus since these methods kill the fragment instead of rendering it invisible it might be faster then blending (although I haven't looked into that lately, so it's a might, but it should generally not be slower, so its still a plus)