简体   繁体   中英

Android opengl es 2.0 query

    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


    }

Note, it's not shown but by Triangle constructor just calls init();

This is giving me a black screen, I've been working on this for hours and cant see why triangles aren't showing up... Is there anything wrong with the code?

You're drawing a degenerate triangle. Two vertices of your triangle are the same:

0.5f, -0.311004243f, // bottom left
0.5f, -0.311004243f  // bottom right

OpenGL will not render any pixels for degenerate triangles.

I also can't find anything in the code you posted where you assign values to your mvpMatrix . You use it to set the value of a uniform variable in your vertex shader. But unless you have more code that is not posted, it will be all zeros.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM