title: Android Opengl: Only front face of my 3D cube is shown
body: <p>I am trying to draw a 3D cube using OpenGL ES2, and my code is only drawing the front side (a square) of the cube as shown in this <a href="http://dl.dropbox.com/u/15975895/stackoverflow/cube.png" rel="nofollow">image</a>.  I can't figure out whats wrong.  I have tried implementing my cube based on other tutorials too but no luck. </p>
  
  <p>What's wrong with my code?..am I missing an improtant line of code somewhere? </p>
  
  <pre><code>@Override
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      // TODO Auto-generated method stub
      GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  
      GLES20.glEnable(GLES20.GL_DEPTH_TEST);
      GLES20.glDepthFunc(GLES20.GL_LEQUAL);
  
  
      GLES20.glFrontFace(GLES20.GL_CCW);
      GLES20.glCullFace(GLES20.GL_BACK);
      GLES20.glEnable(GLES20.GL_CULL_FACE);
  
  
      Matrix.setLookAtM(mVMatrix,0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
  }
  </code></pre>
  
  <p>DrawFrame Method</p>
  
  <pre><code>@Override
  public void onDrawFrame(GL10 gl) {
      // TODO Auto-generated method stub
      GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
  
      // Calculate the projection and view transformation
      Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
  
      //Calling draw method of shapes
      square.draw(mMVPMatrix);
  }
  </code></pre>
  
  <p>Cube Class</p>
  
  <pre><code>                                // x, y, z  
  static float squareCoords[] = {-1f, -1f, 1f,//Bottom Left
                                  1f, -1f, 1f, //Bottom Right
                                  1f, 1f, 1f,  //Top Right
                                 -1f, 1f, 1f, //Top left
  
                                 -1f, -1f, -1f,//Bottom Left
                                 1f, -1f, -1f, //Bottom Right
                                 1f, 1f, -1f,  //Top Right
                                 -1f, 1f, -1f //Top left
                                  };
  
  
  
  private short drawOrder[] = {0,1,2, 0,2,3,//front
                               0,3,7, 0,7,4,//Left    
                               0,1,5, 0,5,4,//Bottom
  
                               6,7,4, 6,4,5,//Back
                               6,7,3, 6,3,2,//top
                               6,2,1, 6,1,5//right
  }; //Order to draw vertices
  
  
  
  private int vertaxCount = 0; //Vertex count is the array divided by the size of the vertex ex. (x,y) or (x,y,z)
  private int vertaxStride = 0; //4 are how many bytes in a float
  
  private float [] color = new float[]{
      0.1f, 0.5f, 1.0f, 1.0f,
      0.0f, 0.0f, 1.0f, 1.0f,
      0.0f, 10.0f, 0.0f, 1.0f,
      0.0f, 1.0f, 1.0f, 1.0f,
      1.0f, 0.0f, 0.0f, 1.0f,
      1.0f, 0.0f, 1.0f, 1.0f,
      1.0f, 1.0f, 0.0f, 1.0f,
      1.0f, 1.0f, 1.0f, 1.0f
  };
  
  public Square()
  {
      //initialising vertex byte buffer for shape coordinates
      // (number of coordinate values * 4 bytes per float)
      ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4);
  
      bb.order(ByteOrder.nativeOrder());
  
      vertexBuffer = bb.asFloatBuffer();
      vertexBuffer.put(squareCoords);
      vertexBuffer.position(0);
  
  
      //Initialise byte buffer for the draw list
      //(number of draw values * 2 bytes per short )
      ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
  
      dlb.order(ByteOrder.nativeOrder());
      drawListBuffer = dlb.asShortBuffer();
      drawListBuffer.put(drawOrder);
      drawListBuffer.position(0);
  
  
      ByteBuffer colors = ByteBuffer.allocateDirect(color.length * 4);
      colors.order(ByteOrder.nativeOrder());
      bufferColor = colors.asFloatBuffer();
      bufferColor.put(color);
      bufferColor.position(0);
  
  
  
      int vertexShader = Shaders.loadShader(GLES20.GL_VERTEX_SHADER, Shaders.vertexShaderCode);
      int fragmentShader = Shaders.loadShader(GLES20.GL_FRAGMENT_SHADER, Shaders.fragmentShaderCode);
  
      mProg = GLES20.glCreateProgram();
      GLES20.glAttachShader(mProg, vertexShader);
      GLES20.glAttachShader(mProg, fragmentShader);
      GLES20.glLinkProgram(mProg);
  
      vertaxCount = squareCoords.length / COORDS_PER_VERTEX; //Vertex count is the array divided by the size of the vertex ex. (x,y) or (x,y,z)
      vertaxStride = COORDS_PER_VERTEX * 4; //4 are how many bytes in a float
  
  
  }
  
  public void draw(float[] mMVPMatrix)
  {
      //Add program to OpenGL environment
      GLES20.glUseProgram(mProg);
  
      //Get Handle to the vertices
      int mPositionHandle = GLES20.glGetAttribLocation(mProg, "vPosition");
  
  
  
      //Prepare the shape coordinate data
      GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, 
                                      GLES20.GL_FLOAT, false, 
                                      vertaxStride, vertexBuffer);
  
      //Enable a handle to the shape vertices
      GLES20.glEnableVertexAttribArray(mPositionHandle);
  
  
      int mColorHandle = GLES20.glGetUniformLocation(mProg, "v_color");
  
      GLES20.glVertexAttribPointer(mColorHandle, 3, GLES20.GL_FLOAT, false, 0, bufferColor);
      GLES20.glEnableVertexAttribArray(mColorHandle);
  
  
      float [] tm = new float[16];
      float [] rm = new float[16];
  
      Matrix.scaleM(mMVPMatrix, 0, 0.35f, 0.35f, 0);
      Matrix.translateM(tm, 0, mMVPMatrix, 0, 0f, 0f, 0);
      Matrix.rotateM(rm, 0, tm, 0, 45f, 0, 0, 1);
  
      // get handle to shape's transformation matrix
      int mMVPMatrixHandle = GLES20.glGetUniformLocation(mProg, "u_MVPMatrix");
  
      // Apply the projection and view transformation
      //GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
      GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, rm, 0);
  
  
      //Draw the shape
      //GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, vertaxCount);
      GLES20.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
  
      //Disable vertex array 
      GLES20.glDisableVertexAttribArray(mPositionHandle);
  
  }
  </code></pre>