在着色器中使用 alpha 测试的 OpenGL ES 2.0 透明度
我正在尝试在 OpenGL ES 2.0 中制作透明对象。我正在设置 GL w/以下参数:
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glDepthFunc(GLES20.GL_LESS);
GLES20.glDisable(GLES20.GL_BLEND);
GLES20.glClearDepthf(1.0f);
这是着色器的代码:
private final String mVertexShader = "uniform mat4 uMVPMatrix;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec2 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = aTextureCoord;\n" +
"}\n";
private final String mFragmentShader = "precision mediump float;\n" +
"varying vec2 vTextureCoord;\n" +
"uniform sampler2D sTexture;\n" +
"void main() {\n" +
" vec4 base = texture2D(sTexture, vTextureCoord);\n" +
" if(base.a < 0.5){ discard; }\n" +
" gl_FragColor = base;\n" +
"}\n";
渲染的结果图像: https: //i.sstatic.net/GzocP.jpg https://i.sstatic.net/ mLire.jpg。 请告诉我我做错了什么,它在 Rendermonkey OpenGL ES 模式下渲染得很好。
如果我用它
GLES20.glDisable(GLES20.GL_BLEND);
来初始化 OpenGL,我会得到正确的透明度,但没有三角形的排序。在这种情况下,某些排序可能会有帮助吗?
I'm trying to make transparent object in OpenGL ES 2.0. I'm setting up GL w/ following params:
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glDepthFunc(GLES20.GL_LESS);
GLES20.glDisable(GLES20.GL_BLEND);
GLES20.glClearDepthf(1.0f);
Here is the code for shaders:
private final String mVertexShader = "uniform mat4 uMVPMatrix;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec2 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = aTextureCoord;\n" +
"}\n";
private final String mFragmentShader = "precision mediump float;\n" +
"varying vec2 vTextureCoord;\n" +
"uniform sampler2D sTexture;\n" +
"void main() {\n" +
" vec4 base = texture2D(sTexture, vTextureCoord);\n" +
" if(base.a < 0.5){ discard; }\n" +
" gl_FragColor = base;\n" +
"}\n";
Resulting images of rendering: https://i.sstatic.net/GzocP.jpg https://i.sstatic.net/mLire.jpg.
Please tell me what I'm doing wrong, it renders fine in Rendermonkey OpenGL ES mode.
If I use
GLES20.glDisable(GLES20.GL_BLEND);
for initializing OpenGL I get correct transparency but without ordering of triangles. May be some sorting will help in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
OpenGL 支持 Alpha 混合。您不需要在着色器中添加特殊代码。尝试设置如下:
Alpha blend is supported by OpenGL. You do not need special code in shader. Try setting following:
好的,我已经找到这个故障的原因了。
尽管禁用深度测试,并且片段着色器丢弃某些像素使其透明,但由于写入深度缓冲区,三角形仍然被遮挡。
我必须使用以下代码禁用对深度缓冲区的写入:
显然,在 Rendermonkey 中也禁用了对深度缓冲区的写入,从而产生了正确的图像。
感谢您在下面的回答,但我不需要使用 alpha 混合 - 我必须使用 alpha 测试,并且它不需要对三角形进行排序。
OK, so I've found the cause of this glitch.
Even though depth test was disabled, and fragment shader discarded certain pixels making them transparent, triangles were still occluded because of writing to depth buffer.
I had to disable writing to depth buffer with this code:
Obviously, writing to depth buffer was disabled in Rendermonkey too, resulting correct image.
Thanks for your answers below, but I don't need to use alpha blending - I have to use alpha testing, and it doesn't require sorting of triangles.
OpenGL 不会为您排序三角形。但由于您只是进行 alpha 测试并禁用混合,因此这对您来说应该不是问题。
OpenGL does not order your triangles for you. But as you are just doing alpha testing and disable blending, that shouldn't be a problem for you.
我认为你应该输入“gl_FragColor = base;”在“其他”中,比如
这个
I think you should put "gl_FragColor = base;" in 'else' like
this one