我正在尝试在 OpenGL ES 2.0 中制作透明对象。我正在使用以下参数设置 GL:
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";
生成的渲染图像:http://imgur.com/hNqm0 http://imgur.com/dmS2O . 请告诉我哪里做错了,它在 Rendermonkey OpenGL ES 模式下渲染良好。
如果我用
GLES20.glDisable(GLES20.GL_BLEND);
为了初始化 OpenGL,我得到了正确的透明度,但没有三角形的顺序。在这种情况下,一些排序可能会有帮助吗?
最佳答案
OpenGL 支持 Alpha 混合。您不需要在着色器中使用特殊代码。尝试设置如下:
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc (GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
关于java - 在着色器中使用 alpha 测试的 OpenGL ES 2.0 透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6007313/