渲染失败,没有GLErrors Android

时间:2016-02-23 09:27:24

标签: java android opengl-es opengl-es-2.0

我现在在同一个问题上运行了2天。我试图在Android(minApi 15,目标23)上的OpenGL ES 2.0中渲染通常的2D三角形(在3D空间中)。我已经重写了整个代码两次,并且我总是得到相同的结果。没有OpenGL错误或警告,没有Java异常,屏幕只是黑色...... 无论如何,这里是渲染器类的代码,活动只是创建渲染器并在start()中调用onCreate()

import static android.opengl.GLES20.*;
public class renderer implements GLSurfaceView.Renderer {
    private Activity a;
    private GLSurfaceView v;
    private int buffs[];
    private int prg;

    private static boolean checkGLError(){
        int i = glGetError();
        if(i != GL_NO_ERROR){
             Log.e("GLError", "Error: " + i);
             return true;
        }
        return false;
    }

    private String VShaderSource =
                "precision  highp       float;\n" +
                "\n" +
                "attribute  mat4        mVP;\n" +
                "\n" +
                "attribute  vec3        vPos;\n" +
                "attribute  vec3        col;\n" +
                "\n" +
                "varying    vec3        oCol;\n" +
                "\n" +
                "void main(){\n" +
                "   gl_Position = mVP * vec4(vPos, 1.0);\n" +
                "   oCol = col;\n" +
                "}";
    private String FShaderSource =
                "precision  mediump     float;\n" +
                "\n" +
                "varying    vec3        oCol;\n" +
                "\n" +
                "void main(){\n" +
                "   gl_FragColor = vec4(oCol, 255);\n" +
                "}";

    public renderer(Activity a){
        this.a = a;
    }

    public void start(){
        v = new GLSurfaceView(this.a);
        v.setEGLContextClientVersion(2);
        v.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        v.setRenderer(this);
        v.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
        a.setContentView(v);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        Log.w("OGL Renderer", "OnSUfraceCreated Called!");
        int vs = glCreateShader(GL_VERTEX_SHADER);
        int fs = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(vs, VShaderSource);
        glCompileShader(vs);
        int var[] = new int[1];
        glGetShaderiv(vs, GL_COMPILE_STATUS, var, 0);
        if(var[0] != GL_TRUE){
            Log.e("OGL Shader", "VShader error: " + glGetShaderInfoLog(vs));
        }else{
            Log.w("OGL Shader", "VShader compiled successfully");
        }

        glShaderSource(fs, FShaderSource);
        glCompileShader(fs);
        glGetShaderiv(fs, GL_COMPILE_STATUS, var, 0);
        if(var[0] != GL_TRUE){
            Log.e("OGL Shader", "FShader error: " + glGetShaderInfoLog(fs));
        }else{
            Log.w("OGL Shader", "FShader compiled successfully");
        }

        prg = glCreateProgram();
        glAttachShader(prg, vs);
        glAttachShader(prg, fs);
        glLinkProgram(prg);
        glGetProgramiv(prg, GL_LINK_STATUS, var, 0);
        if(var[0] != GL_TRUE){
            Log.e("OGL Shader", "Linker error: " + glGetProgramInfoLog(prg));
        }else{
            Log.w("OGL Shader", "Program compiled successfully");
        }
        glUseProgram(prg);
        if(checkGLError()){
            Log.e("Error", "GLError during Program initialization");
        }
        float[] vertices = {-100, -100, -1,
                        100, -100, -1,
                        0, 100, -1};
        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        FloatBuffer vFb = vbb.asFloatBuffer();
        vFb.put(vertices);
        vFb.position(0);
        float[] colors = {1, 0, 0,
                        0, 1, 0,
                        0, 0, 1};
        ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
        cbb.order(ByteOrder.nativeOrder());
        FloatBuffer cFb = cbb.asFloatBuffer();
        cFb.put(colors);
        cFb.position(0);
        buffs = new int[2];
        glGenBuffers(2, buffs, 0);
        if(checkGLError()){
            Log.e("Error", "GLError during Buffer generation");
        }
        glBindBuffer(GL_ARRAY_BUFFER, buffs[0]);
        glBufferData(GL_ARRAY_BUFFER, vFb.capacity(), vFb, GL_STATIC_DRAW);
        if(checkGLError()){
            Log.e("Error", "GLError during GLBufferData 1");
        }
        glBindBuffer(GL_ARRAY_BUFFER, buffs[1]);
        glBufferData(GL_ARRAY_BUFFER, cFb.capacity(), cFb, GL_STATIC_DRAW);
        if(checkGLError()){
            Log.e("Error", "GLError during GLBufferData 2");
        }
        int mPos = glGetUniformLocation(prg, "mVP");
        float[] projection = new float[16], view = new float[16], mVP = new float[16];
        Matrix.setIdentityM(projection, 0);
        Matrix.setLookAtM(view, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0);
        Matrix.perspectiveM(projection, 0, 90.0f, 16.0f/9.0f, 0.000001f, 1000.0f);
        Matrix.multiplyMM(mVP, 0, projection, 0, view, 0);
        glUniformMatrix4fv(mPos, 1, false, mVP, 0);
        if(checkGLError()){
            Log.e("Error", "GLError during matrix load");
        }
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        glViewPort(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        int vPos, cPos;
        vPos = glGetAttribLocation(prg, "vPos");
        if(checkGLError()){
            Log.e("Error", "GLError during GetAttribLocation 1");
        }
        glEnableVertexAttribArray(vPos);
        cPos = glGetAttribLocation(prg, "col");
        if(checkGLError()){
            Log.e("Error", "GLError during GetAttribLocation 2");
        }
        glEnableVertexAttribArray(cPos);
        glBindBuffer(GL_ARRAY_BUFFER, buffs[0]);
        glVertexAttribPointer(vPos, 3, GL_FLOAT, false, 0, 0);
        if(checkGLError()){
            Log.e("Error", "GLError during VertexAttribPointer 1");
        }
        glBindBuffer(GL_ARRAY_BUFFER, buffs[1]);
        glVertexAttribPointer(cPos, 3, GL_FLOAT, false, 0, 0);
        if(checkGLError()){
            Log.e("Error", "GLError during VertexAttribPointer 2");
        }
        glDrawArrays(GL_TRIANGLES, 0, 1);
        if(checkGLError()){
            Log.e("Error", "GLError during DrawArrays");
        }
    }
}

感谢任何帮助。 -John

编辑我复制了错误的顶点Z位置,它没有改变任何强硬

编辑2 添加了glEnableVertexArrtibArray()来电,但仍无效

1 个答案:

答案 0 :(得分:0)

我想我发现了这个问题。我正在创建一个非常大的三角形,使用较小的点(1.0f~10.0f)解决了这个问题。此外,将视图矩阵移动了一点。