本文介绍了OpenGl ES 2.0中的透明对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在玩Android上的OpenGL ES 2.0,但现在遇到了一个我无法解决的问题.事先道歉,看来我还没有发布两个以上的链接,所以我将我的三个图像放入了一个Photobucket相册此处.

So I've been playing around with OpenGL ES 2.0 on Android but now got to a problem I haven't been able to solve. Apologies in advance, it appears that I'm not allowed to post more that two links (yet), so I put my three images in a Photobucket album here.

我正在尝试创建一个由透明区域(彩色玻璃")包围的3D环境.为了查看它是否有效,我还在其中放置了一个不透明的立方体.我启用了以下功能:

I'm trying to create a 3D environment that is enclosed by transparent areas ("colored glass"). To see if it works I also put a opaque cube within. I enabled the following capabilities:

GLES20.glEnable(GLES20.GL_CULL_FACE);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

现在图片看起来像这样(屏幕截图1).不错,但不是我想要的样子:后面的(下部)墙和右边的墙都应该可见,因为我正在看的墙是透明的.

Now the picture looks like this (screenshot 1). Not bad, but not exactly how I wanted it: A (lower) wall at the back as well as the wall on the right should be visible because the wall I'm looking through is transparent.

然后我发现了 ,并尝试使用GLES20.glDepthMask(true);在绘制透明对象之前绘制不透明和GLES20.glDepthMask(false);,以及在绘制不透明对象时禁用混合.

Then I found that and tried using GLES20.glDepthMask(true); before drawing the opaque and GLES20.glDepthMask(false); before drawing the transparent objects, as well as disabling blending while drawing the opaque objects.

结果(屏幕截图2)看起来很混乱.但是后来我有了另一个想法,不是在绘制透明对象时关闭对深度缓冲区的写入,而是完全关闭GLES20.DEPTH_TEST.

The result (screenshot 2) looks quite messed up. But then I had another idea, not to turn off writing to the depth buffer but to turn off GLES20.DEPTH_TEST altogether, while drawing the transparent objects.

(截屏3)使我离所需的图片最近.您最终可以看到后壁以及右侧壁,但是,由于在绘制不透明体时禁用了深度测试,因此立方体部分地被后壁覆盖了,这是不应该的.

That (screenshot 3) got me closest tho the picture I'm looking for. You can finally see the backwall as well as the right sidewall but, because the depth testing is disabled when drawing the opaques, the cube is partially covered by the backwall, which it shouldn't be.

有人知道如何获得我想要的效果吗?

Does anyone know how to get the effect I'm looking for?

推荐答案

认为我解决了它.我的意思是说,它适用于我的情况,但我不能确定这是否只是偶然...

I think that I solved it. By that I mean that it works in my case but I can't tell if that is just by coincidence...

我将像往常一样启用深度测试和混合.然后,在绘制时,我像以前一样先绘制不透明的形状,然后绘制透明的形状.但是,在绘制透明形状时,我关闭了GLES20.glDepthMask(..)而不写入深度缓冲区,因此绘制了所有不被不透明形状覆盖的透明形状.我以前做过(图2),但完全搞砸了,但是现在我反过来做-禁用深度遮罩的透明形状,而不是不透明的形状.

I am enabling depth testing and blending as usual. Then, when drawing, I draw the opaque shapes first and the transparent shapes second, like before. But, while drawing the transparent shapes, I turn GLES20.glDepthMask(..) off to not write to the depth buffer and thus draw all transparent shapes that are not covered by opaque shapes. I did that previously (picture 2) and it completely messed up but I now I do it in reverse - disabling the depth mask for transparent shapes, not opaque ones.

这篇关于OpenGl ES 2.0中的透明对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:21