Android opengl es 2.0查询

Android opengl es 2.0查询,android,geometry,opengl-es-2.0,Android,Geometry,Opengl Es 2.0,注意,它没有显示,但三角形构造函数只调用init() 这给了我一个黑屏,我已经为此工作了几个小时,不明白为什么三角形没有出现。。。代码有什么问题吗?您正在绘制一个退化三角形。三角形的两个顶点相同: public class Triangle { private FloatBuffer vertexBuffer, colorBuffer, indexBuffer; private int mProgram, mPositionHandle, mColorHandle, mM

注意,它没有显示,但三角形构造函数只调用init()


这给了我一个黑屏,我已经为此工作了几个小时,不明白为什么三角形没有出现。。。代码有什么问题吗?

您正在绘制一个退化三角形。三角形的两个顶点相同:

    public class Triangle {
    private FloatBuffer vertexBuffer, colorBuffer, indexBuffer;
    private int mProgram, mPositionHandle, mColorHandle, mMVPMatrixHandle;
    private final float[] mvpMatrix = new float[16];
    static final int COLOR_DATA_SIZE = 4;

    private float[] triangleCoords = {   // in counterclockwise order:
             0.0f,  0.622008459f, // top
             0.5f, -0.311004243f, // bottom left
             0.5f, -0.311004243f  // bottom right
    };
    private float[] triangleVertexData;
    // Set color with red, green, blue and alpha (opacity) values
    private float color[] = { 162.0f/255.0f, 0.0f, 37.0f/255.0f, 1.0f
                ,0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f};

        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;" +
                "attribute vec4 a_Position;" +
                "attribute vec4 a_Color;" +
                "varying vec4 v_Color;" +
                "void main() {" +
                "  v_Color = a_Color;" +
                // the matrix must be included as a modifier of gl_Position
                "  gl_Position = uMVPMatrix * a_Position;" +
                "}";

            private final String fragmentShaderCode =
                "precision mediump float;" +
                "varying vec4 v_Color;" +
                "void main() {" +
                "  gl_FragColor = v_Color;" +
                "}";

protected void draw() {
        // get handles
        mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "a_Position");
        mColorHandle = GLES20.glGetAttribLocation(mProgram, "a_Color");

        GLES20.glUseProgram(mProgram);

        vertexBuffer.position(0);
        GLES20.glVertexAttribPointer(mPositionHandle, 2, GLES20.GL_FLOAT, false,
        0, vertexBuffer); // NOTE: A stride of 0 since the data is packed.

        GLES20.glEnableVertexAttribArray(mPositionHandle);

        // Pass in the color information
        vertexBuffer.position(6);
        GLES20.glVertexAttribPointer(mColorHandle, 4, GLES20.GL_FLOAT, false,
        0, vertexBuffer); // NOTE: A stride of 0 since the data is packed.

        GLES20.glEnableVertexAttribArray(mColorHandle);

        // Apply the projection and view transformation
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);

        GLES20.glDisableVertexAttribArray(mPositionHandle);
        GLES20.glDisableVertexAttribArray(mColorHandle);
    }

    protected static int loadShader(int type, String shaderCode){

        // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
        // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
        int shader = GLES20.glCreateShader(type);

        // add the source code to the shader and compile it
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }

private float[] concat(float[] A, float[] B) {
           int aLen = A.length;
           int bLen = B.length;
           float[] C= new float[aLen+bLen];
           System.arraycopy(A, 0, C, 0, aLen);
           System.arraycopy(B, 0, C, aLen, bLen);
           return C;
        }
    private void init() {
        triangleVertexData = concat(triangleCoords, color);
        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
                // (number of coordinate values * 4 bytes per float)
                triangleVertexData.length * 4);
        // use the device hardware's native byte order
        bb.order(ByteOrder.nativeOrder());

        // create a floating point buffer from the ByteBuffer
        vertexBuffer = bb.asFloatBuffer();
        // add the coordinates to the FloatBuffer
        vertexBuffer.put(triangleVertexData);
        // set the buffer to read the first coordinate
        vertexBuffer.position(0);


        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

        mProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program
        GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
        GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program

        // Bind attributes
        GLES20.glBindAttribLocation(mProgram, 0, "a_Position");
        GLES20.glBindAttribLocation(mProgram, 1, "a_Color");
        GLES20.glLinkProgram(mProgram);                  // creates OpenGL ES program executables


    }
OpenGL不会为退化三角形渲染任何像素


我在您发布的代码中也找不到任何内容,您将值分配给
mvpMatrix
。您可以使用它在顶点着色器中设置统一变量的值。但是,除非您有更多未发布的代码,否则它将全部为零。

是的,关于MVP矩阵的事情,我还没有发布完整的代码。我不敢相信我错过了那个堕落的三角形。。。谢谢你发现了。
0.5f, -0.311004243f, // bottom left
0.5f, -0.311004243f  // bottom right