The onSurfaceCreated Method

This method is invoked when the drawing surface is created. This code covers the same ground as the code that was outside of any method in the Javascript/WebGL examples.

210     public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
211     {
212         // Enable depth tests to draw only nearer objects, less depth.
213         GLES20.glEnable(GLES20.GL_DEPTH_TEST);
214         GLES20.glDepthFunc(GLES20.GL_LEQUAL);
215         GLES20.glDepthMask(true);
216 
217         final String vertexShaderSource   = "attribute vec3 position;"
218                                             + "attribute vec4 color;"
219                                             + "uniform   mat4 modelViewMatrix;"
220                                             + "uniform   mat4 projectionMatrix;"
221                                             + ""
222                                             + "varying vec4 vColor;"
223                                             + ""
224                                             + "void main()"
225                                             + "{"
226                                             + "    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1);"
227                                             + "    vColor      = color;"
228                                             + "}";
229 
230         // The fragment shader can be thought of for now as doing per pixel computations. It is the
231         // fragment shader the colors each pixel in a 3d scene.
232         final String fragmentShaderSource = "precision mediump float;"
233                                             + ""
234                                             + "varying vec4 vColor;"
235                                             + ""
236                                             + "void main()"
237                                             + "{"
238                                             + "    gl_FragColor = vColor;"
239                                             + "}";
240 
241         int vertexShader   = compileShader(vertexShaderSource,   GLES20.GL_VERTEX_SHADER);
242         int fragmentShader = compileShader(fragmentShaderSource, GLES20.GL_FRAGMENT_SHADER);
243 
244         program       = createProgram(vertexShader, fragmentShader);
245 
246         vertexBuffer  = createBuffer(vertices);
247         anotherBuffer = createBuffer(moreVertices);
248 
249         viewMatrix    = generateViewMatrix();
250 
251         // Load a projection matrix
252         // frustumM (float[] m, int offset, float left, float right, float bottom, float top, float near, float far)
253         Matrix.frustumM(projectionMatrix,  0, -150.0f, 150.0f, -150.0f, 150.0f, 1.0f, 100.0f);
254     }

Notes

By now, much of this code should be familiar. The first three lines though are a little different. glEnable(GLES20.GL_DEPTH_TEST) turns on the use of the given glDepthFunc to determine whether a pixel is drawn to the screen. The GLES20.GL_LEQUAL specifies that pixels with a depth less than or equal to the depth of the currently drawn pixel will overwrite it.