openGL ES 2.0 六边形

openGL ES 2.0 hexagon

我想在 OpenGL ES 2.0 中制作一个六边形,但我的程序只能查看它的右下半部分。 我在我的代码中找不到错误。谁能看出哪里出了问题?

我的顶点和索引:

private float vertices[] = {
  0.0f,   0.0f,   0.0f, // center
  0.5f,   0.0f,   0.0f, // right
  0.25f,  0.433f, 0.0f, // top right
 -0.25f,  0.433f, 0.0f, // top left
 -0.5f,   0.0f,   0.0f, // left
 -0.25f, -0.433f, 0.0f, // bottom left
  0.25f, -0.433f, 0.0f  // bottom right
};

private final short drawOrder[] = {0,1,2,2,3,0,0,3,4,4,5,0,0,5,6,6,1,0};

我的缓冲区:

ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length*2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = bb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

我的绘制方法:

public void draw(float[] mvpMatrix){
    GLES20.glUseProgram(shaderProgram);

    positionHandle = GLES20.glGetAttribLocation(shaderProgram,"vPosition");
    GLES20.glEnableVertexAttribArray(positionHandle);

    GLES20.glVertexAttribPointer(positionHandle,3,GLES20.GL_FLOAT,false,12,vertexBuffer);

    colorHandle = GLES20.glGetUniformLocation(shaderProgram,"vColor");

    mMVPMatrixHandle = GLES20.glGetUniformLocation(shaderProgram,"uMVPMatrix");


    GLES20.glUniformMatrix4fv(mMVPMatrixHandle,1,false,mvpMatrix,0);

    GLES20.glUniform4fv(colorHandle,1,color,0);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
    GLES20.glDisableVertexAttribArray(positionHandle);
}

这是一个简单的错字:

drawListBuffer = bb.asShortBuffer();

应该是:

drawListBuffer = dlb.asShortBuffer();

您正在用索引覆盖顶点数组的前半部分。