Code example for FloatBuffer

Methods: glVertexAttribPointer, position, put

0
 
        // create a floating point buffer from the ByteBuffer 
        vertexBuffer = bb.asFloatBuffer();
 
        // add the coordinates to the FloatBuffer 
        vertexBuffer.put(top);//设置顶部顶点
        vertexBuffer.put(bottom);//设置底部顶点
 
        vertexBuffer.put(shadow);//设置阴影顶点
 
        // set the buffer to read the first coordinate 
        vertexBuffer.position(0);
 
    } 
 
    private void initColorBuffer(PageRenderer renderer){
        float factor=renderer.getFactor();
 
        /** 
         * 设置顶点颜色 
         */ 
        ByteBuffer bb = ByteBuffer.allocateDirect(
                // (number of coordinate values * 4 bytes per float) 
                16 * 4); 
        // use the device hardware's native byte order 
        bb.order(ByteOrder.nativeOrder());
 
        shadowColorBuffer = bb.asFloatBuffer();
        shadowColorBuffer.put(new float[]{
                factor, factor, factor, 1-factor,
                1, 1, 1, 0, 
                factor, factor, factor, 1-factor,
                1, 1, 1, 0 
        }); 
        shadowColorBuffer.position(0);
    } 
 
    public void draw(PageRenderer renderer) {
 
        this.initVertexBuffer();
        this.initColorBuffer(renderer);
 
        /** 
         * 画内容部分 
         */ 
        contentShader.useProgram();
 
        //设置顶点handle 
        GLES20.glVertexAttribPointer(contentShader.getHandle("vPosition"), 3, GLES20.GL_FLOAT, false,
                3 * 4, vertexBuffer);
        GLES20.glEnableVertexAttribArray(contentShader.getHandle("vPosition"));
 
        //设置颜色handle 
        GLES20.glUniform4fv(contentShader.getHandle("vColor"), 1, new float[]{0.63671875f, 0.76953125f, 0.22265625f, 1.0f}, 0);
        GLES20.glEnableVertexAttribArray(contentShader.getHandle("vColor"));
 
        //设置投影矩阵 
        GLES20.glUniformMatrix4fv(contentShader.getHandle("uMVPMatrix"), 1, false, renderer.getProjectionMatrix(), 0);
        //画顶部多边形 
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        //画底部多边形 
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 4, 4);
 
        /** 
         * 画阴影部分 
         */ 
        //设置阴影顶点 
        shadowShader.useProgram();
        vertexBuffer.position(12*2);
        GLES20.glVertexAttribPointer(shadowShader.getHandle("vPosition"), 3, GLES20.GL_FLOAT, false,
                3 * 4, vertexBuffer);
        GLES20.glEnableVertexAttribArray(shadowShader.getHandle("vPosition"));
 
        //设置顶点颜色 
        GLES20.glVertexAttribPointer(shadowShader.getHandle("aColor"), 4, GLES20.GL_FLOAT, false, 0,