223
CHAPTER 9: User Interface
drawListBuffer.capacity() *
BYTES_PER_SHORT,
drawListBuffer, GLES20.GL_STATIC_DRAW)
GLES20.glBindBuffer(
GLES20.GL_ARRAY_BUFFER, 0)
GLES20.glBindBuffer(
GLES20.GL_ELEMENT_ARRAY_BUFFER, 0)
} else {
//TODO:
error handling
}
}
The draw() method does not substantially differ from before. This time we use the shader
program provided in the constructor. Again, this method runs fast,
since it only shifts around
references.
fun draw() {
// Add program to OpenGL ES environment
GLES20.glUseProgram(program!!)
// get handle to fragment shader's vColor member
val colorHandle = GLES20.glGetUniformLocation(
program!!, "vColor")
// Set
color for drawing the square
GLES20.glUniform4fv(colorHandle!!, 1, color, 0)
// get handle to vertex shader's vPosition member
val positionHandle = GLES20.glGetAttribLocation(
program!!, "vPosition")
// Enable a handle to the vertices
GLES20.glEnableVertexAttribArray(
positionHandle!!)
//
Prepare the coordinate data
GLES20.glVertexAttribPointer(positionHandle!!,
COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
VERTEX_STRIDE, vertexBuffer)
// Draw the square
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,
vertBuf)
GLES20.glBindBuffer(
GLES20.GL_ELEMENT_ARRAY_BUFFER, idxBuf)
GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP,
drawListBuffer.capacity(),
GLES20.GL_UNSIGNED_SHORT, 0)