title: Android OpenGL ES 2.0 lighting?
body: <p>I am new to GLES 2.0. It is really making me nervous because OpenGL ES 1 II just set GL_LIGHTNING1 and then the lighting was set. In GLES 2.0 it is not working. In fact the whole screen is black. So please tell me what should I do with the code below. These are the vertices I am setting:</p>
  
  <pre><code>        static float quadrateCoords[] = { // in counterclockwise order:
      // front
      -0.5f, -0.5f, 0.5f,
       0.5f, -0.5f, 0.5f,
      -0.5f, 0.5f, 0.5f,
       0.5f, 0.5f, 0.5f,
      // back
      -0.5f, -0.5f, -0.5f,
       0.5f, -0.5f, -0.5f,
      -0.5f, 0.5f, -0.5f,
       0.5f, 0.5f, -0.5f,
      // left
      -0.5f, -0.5f, 0.5f,
      -0.5f, -0.5f, -0.5f,
      -0.5f, 0.5f, 0.5f,
      -0.5f, 0.5f, -0.5f,
      // right  
       0.5f, -0.5f, 0.5f,
       0.5f, -0.5f, -0.5f,
       0.5f, 0.5f, 0.5f,
       0.5f, 0.5f, -0.5f,  
      // up  
      -0.5f, 0.5f, 0.5f,
       0.5f, 0.5f, 0.5f,
      -0.5f, 0.5f, -0.5f,
       0.5f, 0.5f, -0.5f,
      // bottom  
      -0.5f, -0.5f, 0.5f,
       0.5f, -0.5f, 0.5f,
      -0.5f, -0.5f, -0.5f,
       0.5f, -0.5f, -0.5f
  };
  static float normal[] = {
      0.0f, 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,
      0.0f, 0.0f, -1.0f,
      0.0f, 0.0f, -1.0f,
      0.0f, 0.0f, -1.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, 0.0f, 0.0f,
      1.0f, 0.0f, 0.0f,
      1.0f, 0.0f, 0.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, 0.0f,
  
      0.0f, -1.0f, 0.0f,      
      0.0f, -1.0f, 0.0f,      
      0.0f, -1.0f, 0.0f,      
      0.0f, -1.0f, 0.0f       
  };
  
  static float quadrateColors[] = { // in counterclockwise order:
      // front
      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
  };
  </code></pre>
  
  <p>And the shader code is:</p>
  
  <pre><code>        private final String vertexShaderCode =
          "uniform mat4 uMVPMatrix;\n" +
          "uniform mat4 uMVMatrix;\n" +
          "uniform vec3 uLightPos;\n" +
  
          "attribute vec4 aPosition;\n" +
          "attribute vec4 aColor;\n" +
          "attribute vec4 aNormal;\n" +
  
          "varying vec4 vColor;\n" +
  
          "void main() {\n" +
              "vec3 modelViewVertex = vec3(uMVMatrix * a_Position);\n" +
              "vec3 modelViewNormal = vec3(uMVMatrix * vec4(aNormal, 0.0));\n" +
              "float distance = length(uLightPos - modelViewVertex);\n" +
              "vec3 lightVector = normalize(uLightPos - modelViewVertex);\n" +
              "float diffuse = max(dot(modelViewNormal,lightVector), 0.1):\n" + 
              "diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));\n" +
  
              "vColor = aColor * diffuse;\n" + 
  
              "gl_Position = aPosition * uMVPMatrix;\n" +
          "}\n";
  
  private final String fragmentShaderCode =
          "precision mediump float;\n" +
  
          "varying vec4 vColor;\n" +
  
          "void main() {\n" +
              "gl_FragColor = vColor;\n" +
          "}\n";
  </code></pre>
  
  <p>And last the draw function:</p>
  
  <pre><code>        public void draw(float[] mvpMatrix, float[] mVMatrix) {
      // Add program to OpenGL environment
      GLES20.glUseProgram(mProgram);
  
      // get handle to vertex shader's aPosition member
      mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
      nNormalHandle = GLES20.glGetAttribLocation(mProgram, "aNormal");
      nColorHandle = GLES20.glGetAttribLocation(mProgram, "aColor");
  
      // Enable a handle to the triangle vertices
      // Prepare the triangle coordinate data
      GLES20.glEnableVertexAttribArray(mPositionHandle);
      GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
  
      GLES20.glEnableVertexAttribArray(nNormalHandle);
      GLES20.glVertexAttribPointer(nNormalHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer2);
  
      GLES20.glEnableVertexAttribArray(nColorHandle);
      GLES20.glVertexAttribPointer(nColorHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer3);
  
      muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
      GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mvpMatrix, 0);        
  
      muMVMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVMatrix");
      GLES20.glUniformMatrix4fv(muMVMatrixHandle, 1, false, mVMatrix, 0);        
  
      muLightPosHandle = GLES20.glGetUniformLocation(mProgram, "uLightPos");
      GLES20.glUniform3f(muLightPosHandle, 10.0f, 10.0f, 15.0f);
  
      // Front
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
  
      // Back
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 4, 4);
  
      // Left
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 8, 4);
  
      // Right
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 12, 4);
  
      // Top
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 16, 4);
  
      // Right
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 20, 4);
  
      // Disable vertex array
      GLES20.glDisableVertexAttribArray(mPositionHandle);
  }    
  </code></pre>