仅限OpenGL / Android GLSL ES 3.00

时间:2018-02-07 15:39:05

标签: android opengl-es shader glsles

我的目标是在我的Android应用中使用OpenGL 3.1来显示纹理。 目前的问题是,着色器无法编译。

private final String vertexShaderCode =
        // This matrix member variable provides a hook to manipulate
        // the coordinates of the objects that use this vertex shader
                "uniform mat4 uMVPMatrix;" +
                "in vec4 vPosition;" +
                "in vec2 vTextureCoordinate;" +
                "out vec2 exTextureCoordinate;" +
                "void main() {" +
                // the matrix must be included as a modifier of gl_Position
                // Note that the uMVPMatrix factor *must be first* in order
                // for the matrix multiplication product to be correct.
                "  gl_Position = uMVPMatrix * vPosition;" +
                "   exTextureCoordinate = vTextureCoordinate;" +
                "}";

private final String fragmentShaderCode =
                "precision mediump float;" +
                "uniform sampler2D texture;" +
                "in vec2 exTextureCoordinate;" +
                "out vec4 out_Color;" +
                "void main() {" +
                "  out_Color = texture2D(texture, exTextureCoordinate);" +
                "}";

想法:将模型 - 视图矩阵与空间和纹理坐标中的位置一起传递给vertexShader。纹理坐标传递给fragmentShader进行着色,空间坐标使用模型 - 视图矩阵进行变换。

我在编译着色器时得到以下错误代码:

E/ShaderCompile: Error compiling shader!
E/ShaderCompile: 
ERROR: 0:1: 'in' : storage qualifier supported in GLSL ES 3.00 only  
ERROR: 0:1: 'in' : storage qualifier supported in GLSL ES 3.00 only  
ERROR: 0:1: 'out' : storage qualifier supported in GLSL ES 3.00 only  
E/ShaderCompile: Error compiling shader!
E/ShaderCompile: 
ERROR: 0:1: 'in' : storage qualifier supported in GLSL ES 3.00 only  
ERROR: 0:1: 'out' : storage qualifier supported in GLSL ES 3.00 only  

我使用以下代码设置OpenGLES版本:

uses-feature android:glEsVersion="0x00030001" android:required="true"
setEGLContextClientVersion(3);

2 个答案:

答案 0 :(得分:1)

您没有定义要使用的GLSL版本,这意味着它会回退到早期版本(意味着不是3)。您可以将GLSL ES 1与GLES 3一起使用,因此它不会默认使用当前的GLES版本。

因此,在着色器文件的顶部(第一行),声明版本:

# version 300 es

必须对片段和顶点着色器

进行此操作

请注意,这实际上要求设备支持GLES和GLSL 3.如果您使用默认的Android模拟器(与SDK一起打包的模拟器),则无法测试GLES / GLSLES 3 ,因为它不支持它。使用真实设备(当然支持GLES3)或支持GLES3的模拟器

答案 1 :(得分:0)

从Android Studio 3.6,API 29开始,GLES 3和GLSL 使用SwiftShader或Desktop本机OpenGL的模拟器都支持3种。 您需要指定Zoe上面所述的版本:

# version 300 es