android - OpenGL ES 中透明度和深度测试的解决方法?

标签 android opengl-es transparency depth-buffer

我正在尝试使用 draw_texture 扩展让深度测试在 OpenGL ES 上正常工作。我很早就意识到使用此扩展程序渲染时“正常”z 坐标不起作用。这是由于扩展有效地将 Z 坐标用作 0 和 1 之间的比率,而不是深度缓冲区中的实际 z 位置,如 OpenGL OES 绘制纹理页面所示:

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.

我在 OpenGL 的透明度常见问题解答页面上读到:

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.

这让我想到了我的问题。我有 2 个正方形,都是 64x64。它们带有圆形纹理。像这样,“顶部”方 block 位于下方方 block 的下方。

___________
|  .____.  |
| /      \ |
|_|______|_|
|  .____.  |
| /      \ |
| |   s  | |
|  \____/ t|
|__________|

圆形纹理是形成圆形的纯色 (s),然后是完全透明的周围区域 (t)。

当 OpenGL 渲染它时,下面的正方形没有通过深度测试并且没有被绘制,即使它上面的像素是透明的,所以你从下面的图层而不是整个图像得到半圆效果。

    BAD                   GOOD
___________           ___________
|  .____.  |          |  .____.  |
| /      \ |          | /      \ |
|_|______|_|          |_|      |_|
|  .____.  |          | \.____./ |
| /      \ |          | /      \ |
| |      | |          | |      | |
|  \____/  |          |  \____/  |
|__________|          |__________|

我怎样才能阻止这种情况发生?

最佳答案

创建时:

gl.glEnable(GL10.GL_ALPHA_TEST);
gl.glAlphaFunc(GL10.GL_GREATER,0.2f); 

这解决了这个问题,但您可能需要根据您的纹理将 0.2f 更改为更高或更低。

关于android - OpenGL ES 中透明度和深度测试的解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7047323/

相关文章:

android-layout - 安卓用户界面 : Button with shape inside?

Android AIDL 整数列表

java - Android - HTTPURLConnection 在发布多个输入时失败

iphone - 将 NSMutableArray 转换为 C 结构体数组 - iPhone - OpenGL ES

css - 如何获得网络视频透明度?

css - 我可以使用 CSS 字体进行剔除/穿透透明度吗?

android - 无法在索引处绑定(bind)参数,因为索引超出范围

android - 使用opengles在android中的纹理之间切换

opengl-es - 片段着色器中线性过滤浮点纹理的伪影

c# - 透明重叠圆形进度条(自定义控件)