217
CHAPTER 9: User Interface
Uploading vertices to the graphics hardware several
times is not a good solution, and that
is why there are
index lists. We upload vertices A, B, C, and D, and in addition a list 0, 1, 3,
and 2,
pointing into the vertices list and describing the two triangles as a triangle strip (first is
0-1-3, second is 1-3-2). The corresponding code for a quad reads as follows:
class Quad {
val vertexBuffer: FloatBuffer
val drawListBuffer: ShortBuffer
val vertexShaderCode = """
attribute vec4 vPosition;
void main() {
gl_Position = vPosition;
}
""".trimIndent()
val fragmentShaderCode = """
precision
mediump float;
uniform vec4 vColor;
void main() {
gl_FragColor = vColor;
}
""".trimIndent()
// The shader program
var program:Int? = 0
var color = floatArrayOf(0.94f, 0.67f, 0.22f, 1.0f)
val vbo = IntArray(1) // one vertex buffer
val ibo = IntArray(1) // one index buffer
var positionHandle: Int? = 0
var colorHandle: Int? = 0
companion object {
val BYTES_PER_FLOAT = 4
val BYTES_PER_SHORT = 2
val COORDS_PER_VERTEX = 3
val VERTEX_STRIDE = COORDS_PER_VERTEX *
BYTES_PER_FLOAT
var quadCoords = floatArrayOf(
-0.5f, 0.2f, 0.0f, //
top left
-0.5f, -0.5f, 0.0f, // bottom left
0.2f, -0.5f, 0.0f, // bottom right
0.2f, 0.2f, 0.0f) // top right
val drawOrder = shortArrayOf(0, 1, 3, 2)
//
order to draw vertices
}