221
CHAPTER 9: User Interface
fun onSurfaceCreated(gl: GL10?, config:
javax.microedition.khronos.egl.EGLConfig?) {
// enable face culling feature
GLES20.glEnable(GL10.GL_CULL_FACE)
// specify which faces to not draw
GLES20.glCullFace(GL10.GL_BACK)
// Set
the background frame color
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)
}
// Called for each redraw of the view.
// If renderMode =
// GLSurfaceView.RENDERMODE_WHEN_DIRTY
// (see MyGLSurfaceView)
// this will not be called every frame
override
fun onDrawFrame(unused: GL10) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
triangle = triangle ?: Triangle()
triangle?.draw()
quad = quad ?: Quad()
quad?.draw()
}
override
fun onSurfaceChanged(unused: GL10, width: Int,
height: Int) {
GLES20.glViewport(0, 0, width, height)
}
}
Projection
Once we start using the third dimension, we need to talk about projection.
The projection
describes how the three dimensions of the vertices get mapped to two-dimensional screen
coordinates. The Triangle and Quad graphics primitives we built so far both use their own
shader program. While this gives us the maximum flexibility,
to avoid a proliferation of
shader programs, it is better to extract the shader program and use just one from inside the
renderer. Also, the projection calculation then needs to be done at only one place.
In addition, we let the renderer prepare N times
two buffer objects, one pair of vertex and
index buffer per object, and provide corresponding handles to each graphics primitive
constructor. The new Square class then looks like the following:
class Square(val program: Int?,
val vertBuf:Int, val idxBuf:Int) {
val vertexBuffer: FloatBuffer
val drawListBuffer: ShortBuffer