The onDrawFrame Method

Now we use all the information and methods we have defined above to draw a frame.

285     public void onDrawFrame(GL10 glUnused)
286     {
287         int stride;
288 
289         GLES20.glClearColor(.9255f, .8941f, .8275f, 1.0f);
290         GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
291 
292         bindMatrixUniform(program, viewMatrix,       "modelViewMatrix");
293         bindMatrixUniform(program, projectionMatrix, "projectionMatrix");
294 
295         stride       = bytesPerFloat*(colorElementSize + positionElementSize);
296 
297         bindBuffer(vertexBuffer,    program, "position",                                           positionElementSize,
298                    GLES20.GL_FLOAT, stride,   positionOffset*bytesPerFloat);
299         bindBuffer(vertexBuffer,    program, "color",                                              colorElementSize,
300                    GLES20.GL_FLOAT, stride,  (positionOffset + positionElementSize)*bytesPerFloat);
301 
302         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, nElements);
303 
304         // Take into account translations of the second triangle, if any.
305         float[] newModelView = new float[16];
306 
307         Matrix.translateM(newModelView, 0, viewMatrix, 0, translation.x, translation.y, 0);
308         bindMatrixUniform(program, newModelView,       "modelViewMatrix");
309 
310         bindBuffer(anotherBuffer,   program, "position",                                          positionElementSize,
311                    GLES20.GL_FLOAT, stride,   positionOffset*bytesPerFloat);
312         bindBuffer(anotherBuffer,   program, "color",                                             colorElementSize,
313                    GLES20.GL_FLOAT, stride,  (positionOffset + positionElementSize)*bytesPerFloat);
314 
315 
316         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, nElements);
317     }

Notes

We define the clear color, which becomes the background color in our drawn frame, to be the starting color in our linear gradient.

Our utility method loads the viewMatrix and projectionMatrix, computed in onSurfaceCreated, into their respective uniforms.

bindBuffer provides pointers for reading the position and color data out of the vertexBuffer.

Then we draw the one forward triangle.

Next we translate our modelViewMatrix by translation.x, translation.y, 0 into newModelView. This moves the second triangle relative to the first.

Then we map the coordinates and color of the second triangle from anotherBuffer to the vertex shader attributes.

Then we draw the second triangle.

We can not see the second triangle because it is obscured by the first triangle. We already know how to solve this, move the second triangle!