インデックスを使った描画

index-draw.png

Android OpnenGL ES 20の場合

VBO or VAO作り

                //バッファオブジェクト作成
        int[] vertexBuffer = new int[1];
        GLES20.glGenBuffers(1, vertexBuffer, 0);
        mVBOId = vertexBuffer[0];
        ByteBuffer bb = ByteBuffer.allocateDirect(cubeCoordsForTriangleStrip.length * 4);
        bb.order(ByteOrder.nativeOrder());
        mShortIndexBuffer = bb.asShortBuffer(); 
        mShortIndexBuffer.put(cubeIndicesForTriangleStrip);
        mShortIndexBuffer.position(0); 
        int[] elementBuffer = new int[1];
        GLES20.glGenBuffers(1, elementBuffer, 0);
        mIndexArrayId = elementBuffer[0];
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER,mIndexArrayId);
        GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER,mIndexNum * 2,mShortIndexBuffer, GLES20.GL_STATIC_DRAW);
 
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

いざ描画★

public void drawBox() {
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVBOId);
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, mIndexArrayId);
        GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 0, 0);
        GLES20.glEnableVertexAttribArray(maPositionHandle);
        GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP,mIndexNum,GLES20.GL_UNSIGNED_SHORT, 0);
        //後片付け
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER,0);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    }

gldrawarrays index vbo

サポートサイト Wikidot.com gldrawarraysindexvbo