OpenGL ES 中透明度和深度测试的解决方法?
我正在尝试使用 draw_texture 扩展在 OpenGL ES 上正确进行深度测试。我很早就意识到,使用此扩展渲染时“正常”z 坐标不起作用。这是由于扩展有效地使用 Z 坐标作为 0 和 1 之间的比率,而不是深度缓冲区中的实际 z 位置,如 OpenGL OES 绘制纹理页面所示:
Zs 映射到窗口深度 Zw,如下所示:
{ n, 如果 z <= 0 Zw = { f, 如果 z >= 1 { n + z * (f - n),否则 其中
是近值和远值 深度_范围。
我在 OpenGL 的透明度常见问题解答页面上读到:
在应用程序中使用深度缓冲时,需要小心 关于渲染图元的顺序。完全不透明 需要首先渲染图元,然后渲染部分不透明 原语按从后到前的顺序排列。如果你不渲染基元 这个顺序,原语,否则将通过 部分不透明的图元,可能会完全失去深度测试。
这让我想到了我的问题。我有 2 个正方形,都是 64x64。它们的纹理是圆形的。 “顶部”的正方形位置比下面的正方形稍远一些,如下所示。
___________
| .____. |
| / \ |
|_|______|_|
| .____. |
| / \ |
| | s | |
| \____/ t|
|__________|
圆形纹理是形成圆形的纯色(s),然后是完全透明的周围区域(t)。
当 OpenGL 渲染它时,下面的正方形未通过深度测试并且不会被绘制,即使其上面的像素是透明的,因此您从下面的图层而不是整个图像中获得半圆形效果。
BAD GOOD
___________ ___________
| .____. | | .____. |
| / \ | | / \ |
|_|______|_| |_| |_|
| .____. | | \.____./ |
| / \ | | / \ |
| | | | | | | |
| \____/ | | \____/ |
|__________| |__________|
我怎样才能阻止这种情况发生?
I'm trying to get depth testing working correctly on OpenGL ES, using the draw_texture extension. I realised early on that 'normal' z co-ordinates when rendered using this extension don't work. This was due to the extension effectively using the Z co-ordinate as a ratio between 0 and 1 rather than the actual z position in the depth buffer as shown by the OpenGL OES draw texture page:
Zs is mapped to window depth Zw as follows:
{ n, if z <= 0 Zw = { f, if z >= 1 { n + z * (f - n), otherwise where <n> and <f> are the near and far values of DEPTH_RANGE.
I read on the Transparency FAQ page for OpenGL that:
When using depth buffering in an application, you need to be careful
about the order in which you render primitives. Fully opaque
primitives need to be rendered first, followed by partially opaque
primitives in back-to-front order. If you don't render primitives in
this order, the primitives, which would otherwise be visible through a
partially opaque primitive, might lose the depth test entirely.
This brings me around to my problem. I have 2 squares, both 64x64. They are textured with a circle. The square 'on top' is positioned slightly further down from the square below like so.
___________
| .____. |
| / \ |
|_|______|_|
| .____. |
| / \ |
| | s | |
| \____/ t|
|__________|
The circle texture is a solid colour (s) which forms the circle, then a completely transparent surrounding area (t).
When OpenGL renders it, the square underneath is failing the depth test and is not being drawn, even though the pixels above it are transparent, so you get a semi-circle effect from the layer underneath, not the whole image.
BAD GOOD
___________ ___________
| .____. | | .____. |
| / \ | | / \ |
|_|______|_| |_| |_|
| .____. | | \.____./ |
| / \ | | / \ |
| | | | | | | |
| \____/ | | \____/ |
|__________| |__________|
How can I stop this from happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建时:
这解决了问题,但您可能需要根据纹理将 0.2f 更改为更高或更低。
On create:
This fixes the issue, but you may need to change 0.2f to higher or lower depending on your textures.