-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTriangleLayer.kt
More file actions
129 lines (102 loc) · 3.78 KB
/
Copy pathTriangleLayer.kt
File metadata and controls
129 lines (102 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package dev.abhishekbansal.nexrad.layers
import android.content.Context
import android.opengl.GLES30
import dev.abhishekbansal.nexrad.R
import dev.abhishekbansal.nexrad.utils.Shader
import dev.abhishekbansal.nexrad.utils.extensions.rawResToString
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
class TriangleLayer(val context: Context) : Layer {
private var mTriangle1Vertices: FloatBuffer? = null
/**
* How many bytes per float.
*/
private val mBytesPerFloat = 4
/**
* How many elements per vertex.
*/
private val mStrideBytes: Int = 7 * mBytesPerFloat
/**
* Offset of the position data.
*/
private val mPositionOffset = 0
/**
* Size of the position data in elements.
*/
private val mPositionDataSize = 3
/**
* Offset of the color data.
*/
private val mColorOffset = 3
/**
* Size of the color data in elements.
*/
private val mColorDataSize = 4
/**
* This will be used to pass in the transformation matrix.
*/
private var mMVPMatrixHandle = 0
/**
* This will be used to pass in model position information.
*/
private var mPositionHandle = 0
/**
* This will be used to pass in model color information.
*/
private var mColorHandle = 0
private val triangleShader by lazy {
Shader(
context.rawResToString(R.raw.basic_vertex),
context.rawResToString(R.raw.basic_fragment)
)
}
override fun prepare() {
// This triangle is red, green, and blue.
// Define points for equilateral triangles.
// This triangle is red, green, and blue.
val triangle1VerticesData = floatArrayOf( // X, Y, Z,
// R, G, B, A
-0.5f, -0.25f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.25f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.559016994f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f
)
// Initialize the buffers.
mTriangle1Vertices =
ByteBuffer.allocateDirect(triangle1VerticesData.size * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer()
mTriangle1Vertices?.put(triangle1VerticesData)?.position(0)
// prepare shader
triangleShader.link(arrayOf("a_Position", "a_Color"))
// Set program handles. These will later be used to pass in values to the program.
mMVPMatrixHandle = GLES30.glGetUniformLocation(triangleShader.handle, "u_MVPMatrix")
mPositionHandle = GLES30.glGetAttribLocation(triangleShader.handle, "a_Position")
mColorHandle = GLES30.glGetAttribLocation(triangleShader.handle, "a_Color")
}
override fun draw(mvpMatrix: FloatArray) {
triangleShader.activate()
// Pass in the position information
// Pass in the position information
mTriangle1Vertices?.position(mPositionOffset)
GLES30.glVertexAttribPointer(
mPositionHandle, mPositionDataSize, GLES30.GL_FLOAT, false,
mStrideBytes, mTriangle1Vertices
)
GLES30.glEnableVertexAttribArray(mPositionHandle)
// Pass in the color information
// Pass in the color information
mTriangle1Vertices?.position(mColorOffset)
GLES30.glVertexAttribPointer(
mColorHandle, mColorDataSize, GLES30.GL_FLOAT, false,
mStrideBytes, mTriangle1Vertices
)
GLES30.glEnableVertexAttribArray(mColorHandle)
// This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
// (which now contains model * view * projection).
GLES30.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0)
GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, 3)
}
}