简体   繁体   中英

openGL ES 2.0 hexagon

I'm trying to make a hexagon in OpenGL ES 2.0, but my program will only view the bottom right half of it. I can't find the mistake in my code. Can anyone see what's wrong?

My vertices and indices:

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};

My buffers:

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);

My draw method:

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);
}

It's a simple typo:

drawListBuffer = bb.asShortBuffer();

should be:

drawListBuffer = dlb.asShortBuffer();

You are over-writing the first half of your vertex array with your indices.

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