简体   繁体   English

Android OpenGL ES 2.0查询

[英]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(); 注意,它没有显示,但是由Triangle构造函数只调用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. OpenGL将不会为退化的三角形渲染任何像素。

I also can't find anything in the code you posted where you assign values to your mvpMatrix . 在您发布的为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. 但是,除非您有更多未发布的代码,否则它将全部为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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