Drawing a simple circle

Problems with Canvas, OpenGL, etc...

Drawing a simple circle

Postby Calamera » Thu Sep 01, 2011 2:59 pm

Hi guys,

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);
   }
Calamera
Freshman
Freshman
 
Posts: 3
Joined: Fri Dec 17, 2010 8:52 pm

Top

Re: Drawing a simple circle

Postby Calamera » Fri Sep 02, 2011 3:48 pm

Okay, I managed to solve the problem for myself. In the case someone has the same problem, here is the correct code:
Code: Select all
      final int vertexCounter = 120;
      float vertices[] = new float[vertexCounter * 3];
      short indices[] = new short[vertexCounter];
      float radius = 0.1f;
      for (int i = 0; i < vertexCounter * 3; 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 < vertexCounter; i++) {
         indices[i] = (short) i;
      }

      float colors[] = new float[vertexCounter * 4];
      for (int i = 0; i < vertexCounter * 4; i += 4) {
         colors[i] = 1f;
         colors[i + 1] = 0f;
         colors[i + 2] = 0f;
         colors[i + 3] = 1f;
      }
Calamera
Freshman
Freshman
 
Posts: 3
Joined: Fri Dec 17, 2010 8:52 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 4 guests