Android OpenGLES意外结束源编译失败

时间:2013-12-19 05:27:10

标签: android opengl-es-2.0

我厌倦了每一块以纠正缺陷,但却无法让它发挥作用! 以下是我的活动代码!!

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityManager actMan = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    ConfigurationInfo mConfigInfo = actMan.getDeviceConfigurationInfo();
    boolean isES2Compat = (mConfigInfo.reqGlEsVersion >= 0x20000);
    Log.e("oGl-es v",mConfigInfo.getGlEsVersion()+":"+ mConfigInfo.reqGlEsVersion);
    if(isES2Compat){

        mGLSurfaceView = new MySurface(getApplication());//(this); 
        renderSet = true;
        Log.e("render","set");
    }
    this.setContentView(mGLSurfaceView);
}

下面是MySurface类

public class MySurface extends GLSurfaceView {

    public MySurface(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setEGLContextClientVersion(2);
        setRenderer(new MyTestRenderer(context));
    }
}

下面是渲染器类。

public void onSurfaceCreated(GL10 gl,EGLConfig config){

    GLES20.glClearColor(0.5f, 0.5f, 0.01f, 0.8f);

    float[] triangleVertices = { 
            0f,0f, 0.0f,
            15f,0f,0.0f,
            15f,15f,0.0f,
            15f,15f,0.0f,//Triangle2
            0f,15f,0.0f,
            0f,0f,0.0f
    };

    myTriangleVertices = ByteBuffer.allocateDirect(triangleVertices.length * bytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();
    //copy data from Dalvik’s memory to native memory
    myTriangleVertices.put(triangleVertices);
    myTriangleVertices.position(0);

//  String vertexShader = ShadersUtils.readShadersToString(context,R.raw.v_shader);
//  String fragmentShader = ShadersUtils.readShadersToString(context,R.raw.f_shader);
    final String vertexShader = 
            "attribute vec4 aPosition;\n" +
            "void main(){\n" +              
            "gl_Position = aPosition;\n" +              
            "}\n"        ;
    final String fragmentShader = 
            "uniform vec4 uColor;\n" +              
            "void main(){\n" +              
            "gl_FragColor = uColor;\n" +                
            "}\n" 
             ;


    vertexShaderHandle = compileVertexShader(vertexShader);
    fragmentShaderHandle = compileFragmentShader(fragmentShader);

    linkProgramHandle = ShadersUtils.linkShadersVF(vertexShaderHandle, fragmentShaderHandle);


    GLES20.glUseProgram(linkProgramHandle);
    uColorLocation = GLES20.glGetUniformLocation(linkProgramHandle,"u_Color");
    aPositionLOcation = GLES20.glGetAttribLocation(linkProgramHandle,"a_Position");


    myTriangleVertices.position(0);//setting to start position

    GLES20.glVertexAttribPointer(aPositionLOcation,3,GLES20.GL_FLOAT, false, 3*4, myTriangleVertices); //refer for meaning of arguments
    GLES20.glEnableVertexAttribArray(aPositionLOcation);



}

LogCat输出

12-19 10:35:12.785: E/compilei(620): source
12-19 10:35:12.785: E/ss(620): attribute vec4 aPosition;
12-19 10:35:12.785: E/ss(620): void main()
12-19 10:35:12.785: E/ss(620): {
12-19 10:35:12.785: E/ss(620): gl_Position = aPosition;
12-19 10:35:12.785: E/ss(620): }
12-19 10:35:12.785: E/Results of compile(620): Compile failed.
12-19 10:35:12.785: E/Results of compile(620): ERROR: Unexpected end of source found
12-19 10:35:12.785: E/Results of compile(620): ERROR: 1 compilation errors. No code generated.
12-19 10:35:12.785: E/compilei(620): source
12-19 10:35:12.785: E/ss(620): uniform vec4 uColor;
12-19 10:35:12.785: E/ss(620): void main()
12-19 10:35:12.785: E/ss(620): {
12-19 10:35:12.785: E/ss(620): gl_FragColor = uColor;
12-19 10:35:12.785: E/ss(620): }
12-19 10:35:12.785: E/Results of compile(620): Compile failed.
12-19 10:35:12.785: E/Results of compile(620): ERROR: Unexpected end of source found
12-19 10:35:12.785: E/Results of compile(620): ERROR: 1 compilation errors. No code generated.

使用的函数代码!

public static int compileVertexShader(String shaderString){
    return CompileShader(GLES20.GL_VERTEX_SHADER,shaderString);

}

public static int compileFragmentShader(String shaderString){
    return CompileShader(GLES20.GL_FRAGMENT_SHADER,shaderString);

}

static  int CompileShader(int type, String shaderString) {
final int shaderID = GLES20.glCreateShader(type); //create shader object of type
    if(shaderID == 0)
        Log.e("unable to","createShader");

    else{
        //Toast.makeText(, shaderString, Toast.LENGTH_LONG);
        Log.e("compilei","source");
        GLES20.glShaderSource(type, shaderString); 
        GLES20.glCompileShader(shaderID); 
        Log.e("ss",shaderString);
        final int[] compileStatus = new int[1];

        GLES20.glGetShaderiv(shaderID, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

        Log.e("Results of compile" , GLES20.glGetShaderInfoLog(shaderID));

        if (compileStatus[0] == 0) {
            // If it failed, delete the shader object.
            GLES20.glDeleteShader(shaderID);
            return 0;
        }
    }
    return shaderID;
}

0 个答案:

没有答案