227
CHAPTER 9: User Interface
Applying projection to three-dimensional objects makes more sense.
As a general
assumption for such 3D objects, we expect the following:
Vertex coordinates in four-dimensional homogeneous coordinates
RGBA colors assigned to vertices
Face
normals assigned to vertices
Using four coordinate values instead of the usual three (x, y, z) helps for perspective
projection. Colors assigned to vertices can be used from inside the shader code to apply a
coloring scheme. But it can also be ignored or used for noncoloring purposes.
This is totally
up to the shader code. The normals help for a realistic lighting.
The renderer gets just new shader code, as shown here:
val vertexShaderCode = """
attribute vec4 vPosition;
attribute vec4 vNorm;
attribute vec4 vColor;
varying vec4 fColor;
varying vec4 fNorm;
uniform mat4 uMVPMatrix;
void main() {
gl_Position = uMVPMatrix * vPosition;
fColor = vColor;
fNorm = vNorm;
}
""".trimIndent()
val fragmentShaderCode = """
precision
mediump float;
varying vec4 fColor;
varying vec4 fNorm;
void main() {
gl_FragColor = fColor;
}
""".trimIndent()
As a sample 3D object, I present a cube with an interpolated coloring according to the vertex
colors and the normals ignored for now.
class Cube(val program: Int?, val vertBuf:Int,
val idxBuf:Int) {
val vertexBuffer: FloatBuffer
val drawListBuffer: ShortBuffer