title: Drawing 3D polygon using vertex array
body: <p>I have a short program which draw a 3D <code>GL.GL_QUADS</code>  ,  here its <code>display()</code> method - </p>
  
  <pre><code>public void display(GLAutoDrawable drawable) {
          ....
          gl.glBegin(GL.GL_QUADS); // of the color cube
  
          // Top-face
          gl.glColor3f(0.0f, 1.0f, 0.0f); // green
          gl.glVertex3f(1.0f, 1.0f, -1.0f);
          gl.glVertex3f(-1.0f, 1.0f, -1.0f);
          gl.glVertex3f(-1.0f, 1.0f, 1.0f);
          gl.glVertex3f(1.0f, 1.0f, 1.0f);
  
          // Bottom-face
          gl.glColor3f(1.0f, 0.5f, 0.0f); // orange
          gl.glVertex3f(1.0f, -1.0f, 1.0f);
          gl.glVertex3f(-1.0f, -1.0f, 1.0f);
          gl.glVertex3f(-1.0f, -1.0f, -1.0f);
          gl.glVertex3f(1.0f, -1.0f, -1.0f);
  
          // Front-face
          gl.glColor3f(1.0f, 0.0f, 0.0f); // red
          gl.glVertex3f(1.0f, 1.0f, 1.0f);
          gl.glVertex3f(-1.0f, 1.0f, 1.0f);
          gl.glVertex3f(-1.0f, -1.0f, 1.0f);
          gl.glVertex3f(1.0f, -1.0f, 1.0f);
  
          // Back-face
          gl.glColor3f(1.0f, 1.0f, 0.0f); // yellow
          gl.glVertex3f(1.0f, -1.0f, -1.0f);
          gl.glVertex3f(-1.0f, -1.0f, -1.0f);
          gl.glVertex3f(-1.0f, 1.0f, -1.0f);
          gl.glVertex3f(1.0f, 1.0f, -1.0f);
  
          // Left-face
          gl.glColor3f(0.0f, 0.0f, 1.0f); // blue
          gl.glVertex3f(-1.0f, 1.0f, 1.0f);
          gl.glVertex3f(-1.0f, 1.0f, -1.0f);
          gl.glVertex3f(-1.0f, -1.0f, -1.0f);
          gl.glVertex3f(-1.0f, -1.0f, 1.0f);
  
          // Right-face
          gl.glColor3f(1.0f, 0.0f, 1.0f); // violet
          gl.glVertex3f(1.0f, 1.0f, -1.0f);
          gl.glVertex3f(1.0f, 1.0f, 1.0f);
          gl.glVertex3f(1.0f, -1.0f, 1.0f);
          gl.glVertex3f(1.0f, -1.0f, -1.0f);
              ....
  }
  </code></pre>
  
  <p>Now I want to change the above drawing to "Vertex Arrays" mode and to get same results  , so I did the following  :</p>
  
  <ol>
  <li>Created a <code>float</code> array of all the vertices coodinates of the <code>GL.GL_QUADS</code> .</li>
  <li><code>put</code> this array to a <code>Buffer</code> . </li>
  <li>Tell OpenGL where the vertices are (8 vertices) . </li>
  <li>Tell OpenGL which indices to draw . </li>
  </ol>
  
  <p>Here is the main code  I wrote (<strong>edited</strong>) -</p>
  
  <pre><code>public class DisplayWithArray extends GLCanvas implements GLEventListener,
          KeyListener {
  
      private float[] cubeVertices = { 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
              1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  
              1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f,
              -1.0f, -1.0f,
  
              1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
              -1.0f, 1.0f,
  
              1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f,
              1.0f, -1.0f,
  
              -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
              -1.0f, 1.0f,
  
              1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
              -1.0f, -1.0f };
  
      private float[] colorVertices ={ 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
          1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f, 0.5f, 0.0f,
          1.0f, 0.5f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
          1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
          0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
          1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f };
  
      // 1st edit  - 24 positions of the GL.GL_QUADS
      private int[] indices = new int[24] ;
  
      private IntBuffer indicesBuf ;  
  }
  public DisplayWithArray(GLCapabilities capabilities, int width, int height) {
          for (int i=0 ; i&lt;24 ; i++) {
              this.indices[i] = i ; 
      }
  public void init(GLAutoDrawable drawable) {
              ...
          final GL gl = drawable.getGL();
              ...
          setupPointer(gl);
  }
  
  public void display(GLAutoDrawable drawable) {
  
          final GL gl = drawable.getGL();
          // draw
          gl.glDrawArrays(GL.GL_QUADS, 0, 24);
  }
  
  public void setupPointer(GL gl) {
  
          FloatBuffer tmpVerticesBuf = BufferUtil
                  .newFloatBuffer(cubeVertices.length);
          ;
          FloatBuffer tmpColorVerticesBuf = BufferUtil
                  .newFloatBuffer(colorVertices.length);
  
          for (int i = 0; i &lt; cubeVertices.length; i++) {
              tmpVerticesBuf.put(cubeVertices[i]);
          }
          for (int i = 0; i &lt; colorVertices.length; i++) {
          tmpColorVerticesBuf.put(colorVertices[i]);
      }
          tmpVerticesBuf.rewind();
          tmpColorVerticesBuf.rewind();
          gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
          gl.glVertexPointer(3, GL.GL_FLOAT, 0, tmpVerticesBuf);
          gl.glColorPointer(3, GL.GL_FLOAT, 0, tmpColorVerticesBuf);
          // Indices of polygon
          IntBuffer tmpIndicesBuf = BufferUtil.newIntBuffer(this.indices.length);
          for (int i = 0; i &lt; indices.length; i++) {
              tmpIndicesBuf.put(indices[i]);
          }
          tmpIndicesBuf.rewind();
  
          indicesBuf = tmpIndicesBuf ; 
  }
  </code></pre>
  
  <p>When I run it I get an exception <code>Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: glGetError() returned the following error codes after a call to glDrawElements(): GL_INVALID_ENUM   at DisplayWithArray.display(DisplayWithArray.java:152)</code></p>
  
  <p>point to the line   - <code>gl.glDrawElements(GL.GL_QUADS, indices.length, GL.GL_INT, indicesBuf);
  </code></p>
  
  <p>What is wrong here  ?  </p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I changed the <code>indices</code> array to range [0,5]  - according to 6 face of the <code>GL.GL_QUADS</code>.
  And extend <code>tmpColorVerticesBuf</code> array to 72 indices (4 times each color)  .</p>
  
  <p>Still I have same exception as mentioned above  . </p>
  
  <p><strong>Edit 2:</strong> (now it works well)
  Solve by comparing between <code>cubeVertices</code> and <code>colorVertices</code>  sizes (72 each array)  , and in <code>display()</code> use <code>gl.glDrawArrays(GL.GL_QUADS, 0, 24)</code> (24 elements of size 3)</p>