231
CHAPTER 9: User Interface
The draw() method for rendering the graphics this time reads as follows:
fun draw() {
// Add program
to OpenGL ES environment
GLES20.glUseProgram(program!!)
// 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)
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Buffer offsets are a little bit strange in the
// Java binding - for the normals and colors we
// create new views
and then reset the vertex
// array
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// get handle to vertex shader's vPosition member
vertexBuffer.position(COORDS_PER_VERTEX)
val normBuffer = vertexBuffer.slice()
// create a new view
vertexBuffer.rewind()
// ... and
rewind the original buffer
val normHandle = GLES20.glGetAttribLocation(
program, "vNorm")
if(normHandle >= 0) {
// Enable a handle to the vertices
GLES20.glEnableVertexAttribArray(normHandle)
// Prepare the coordinate data
GLES20.glVertexAttribPointer(normHandle,
COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
VERTEX_STRIDE, normBuffer)
}
// get handle to vertex shader's
vColor member
vertexBuffer.position(COORDS_PER_VERTEX +
NORMS_PER_VERTEX)
val colorBuffer = vertexBuffer.slice()
// create a new view
vertexBuffer.rewind()
// ... and rewind the original buffer
val colorHandle = GLES20.glGetAttribLocation(
program, "vColor")
if(colorHandle >= 0) {