android:在Android 2.2上使用opengl es2.0对着色语言出错

时间:2014-03-10 04:55:01

标签: android opengl-es shader

我的申请有问题。是一个简单的应用程序atm使用opengl es2.0运行在Android 2.2设备HTC意义上。我可以在我的4.0.3模拟器上运行应用程序以及运行android 4.0的设备。

在Android 2.2上运行时,我有这个错误:

> FATAL EXCEPTION: GLThread 9
java.lang.RuntimeException: Error compiling shader: 
SimpleShader.loadShader(SimpleShader.java:87)
SimpleShader.<init>(SimpleShader.java:54)
etc

我认为设备可以运行android 2.2,因为文档告诉LINKS TO DOCUMENTATION,但不确定着色器部分。

这是我的着色器代码:

String verticesShader =
        "uniform mat4 uScreen;\n" +    
        "attribute vec2 aPosition;\n" +
        "attribute vec3 aColor;\n" +
        "attribute vec2 aTexPos; \n" +
        "varying vec2 vTexPos; \n" + 
        "varying vec3 vColor;\n" +
        "void main() {\n" +
        " vTexPos = aTexPos; \n" +  
        " gl_Position = uScreen * vec4(aPosition.xy, 0.0, 1.0);\n" +
        "  vColor = aColor;\n" +
        "}";

      // Our fragment shader. Just return vColor.
      // If you look at this source and just said 'WTF?', remember
      // that all the attributes are defined in the VERTEX shader and
      // all the 'varying' vars are considered OUTPUT of vertex shader
      // and INPUT of the fragment shader. Here we just use the color
      // we received and add a alpha value of 1.
      String fragmentShader =
        "uniform float uUseTexture; \n" +
        "uniform float uAlpha; \n" +
        "uniform sampler2D uTexture;\n" +
        "precision mediump float;\n"+
        "varying vec2 vTexPos; \n" +
        "varying vec3 vColor;\n" + 
        "void main(void)\n" +
        "{\n" + 
        " if ( uUseTexture != 1.0 ) \n" +
        "  gl_FragColor = vec4(vColor.xyz, 1); \n" +
        " else \n" +
        "  gl_FragColor = texture2D(uTexture, vTexPos); \n" +
        " gl_FragColor.a *= uAlpha;" +
        //"  gl_FragColor = vec4(vColor.xyz, 1);\n" +
        "}";

private int loadShader(int shader, String shaderSrc) {
    // TODO Auto-generated method stub
    int handle = GLES20.glCreateShader(shader);

    if (handle == GLES20.GL_FALSE)
        throw new RuntimeException("Error creating shader!");

    GLES20.glShaderSource(handle, shaderSrc);
    GLES20.glCompileShader(handle);

    int[] compileStatus = new int[1];
    GLES20.glGetShaderiv(handle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

    if (compileStatus[0] ==0)
      {
        String error = GLES20.glGetShaderInfoLog(handle);
        GLES20.glDeleteShader(handle);
        throw new RuntimeException("Error compiling shader: " + error);
      }
      else
        return handle;

}

该应用程序在我最新的Android 4.0 +

设备上运行正常

1 个答案:

答案 0 :(得分:0)

您是否考虑过在Android 2.2设备和Android 4.0+设备上检查您的OpenGL ES GLSL版本? 您可以使用GLES20.glGetString(GLES20.GL_SHADING_LANGUAGE_VERSION)打印它。