I'm new to OpenGL (ES) and I just want to draw a circle but my circle just looks like a sun so I think I messed up the indices. Could someone please take a look? Thanks in advance.
- Code: Select all
private void drawCircle(GL10 gl) {
float vertices[] = new float[360];
short indices[] = new short[120];
float radius = 0.1f;
for (int i = 0; i < 360; i += 3) {
vertices[i] = (float) (radius * Math.cos(Math.toRadians(i)));
vertices[i + 1] = 0.0f;
vertices[i + 2] = (float) (radius * Math.sin(Math.toRadians(i)));
}
for (int i = 0; i < 120; i++) {
indices[i] = (short) (i * 3);
}
// Farben noch angleichen new float[4 * 60]
float colors[] = { 1f, 0f, 0f, 1f, 1f, 0f, 0f, 1f, 1f, 0f, 0f, 1f };
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
ShortBuffer indexBuffer = ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
FloatBuffer colorBuffer = cbb.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
gl.glDrawElements(GL10.GL_LINE_LOOP, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}

