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 130 131 132
| class HockeyRenderer(context: Context) : GLSurfaceView.Renderer { companion object { private const val TAG = "HockeyRenderer" private const val POSITION_COMPONENT_COUNT = 2 private const val BYTES_PER_FLOAT = 4 private const val U_COLOR = "u_Color" private const val A_POSITION = "a_Position" } private var vertexShaderSource: String private var fragmentShaderSource: String private var uColorLocation = 0 private var aPositionLocal = 0 private var vertexData: FloatBuffer private val tableVerticesWithTriangles = floatArrayOf( -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0f, 0.5f, 0f, -0.5f, -0.25f, 0f, 0.25f, 0f )
override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f) val programId = ShaderHelper.buildProgram(vertexShaderSource, fragmentShaderSource) GLES20.glUseProgram(programId) uColorLocation = GLES20.glGetUniformLocation(programId, U_COLOR) aPositionLocal = GLES20.glGetAttribLocation(programId, A_POSITION) vertexData.position(0) GLES20.glVertexAttribPointer( aPositionLocal, POSITION_COMPONENT_COUNT, GLES20.GL_FLOAT, false, 0, vertexData ) GLES20.glEnableVertexAttribArray(aPositionLocal) }
override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) { GLES20.glViewport(0, 0, width, height) }
override fun onDrawFrame(gl: GL10?) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT) GLES20.glUniform4f(uColorLocation, 1.0f, 1.0f, 1.0f, 1.0f) GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6) GLES20.glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f) GLES20.glDrawArrays(GLES20.GL_LINES, 6, 2) GLES20.glUniform4f(uColorLocation, 0.0f, 0.0f, 1.0f, 1.0f) GLES20.glDrawArrays(GLES20.GL_POINTS, 8, 1) GLES20.glUniform4f(uColorLocation, 0.0f, 1.0f, 0.0f, 1.0f) GLES20.glDrawArrays(GLES20.GL_POINTS, 9, 1) } init { vertexShaderSource = Utils.loadStringFromAssets(context, "hockey/basic.vs") fragmentShaderSource = Utils.loadStringFromAssets(context, "hockey/basic.fs") vertexData = ByteBuffer .allocateDirect(tableVerticesWithTriangles.size * BYTES_PER_FLOAT) .order(ByteOrder.nativeOrder()) .asFloatBuffer() vertexData.put(tableVerticesWithTriangles) } }
|