Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.corrupted.radheat.TEMPER;
- import android.content.Context;
- import android.opengl.*
- import android.util.DisplayMetrics
- import android.util.Half.toFloat
- import java.nio.ByteBuffer
- import java.nio.ByteOrder
- import java.nio.FloatBuffer
- import javax.microedition.khronos.opengles.*
- val metrics = DisplayMetrics()
- var ratio = metrics.heightPixels.toFloat() / metrics.widthPixels.toFloat()
- class MyGLRenderer : GLSurfaceView.Renderer {
- private lateinit var mTriangle: Triangle
- private val mMVPMatrix = FloatArray(16)
- private val mProjectionMatrix = FloatArray(16)
- private val mViewMatrix = FloatArray(16)
- override fun onSurfaceCreated(gl: GL10?, config: javax.microedition.khronos.egl.EGLConfig?) {
- GLES31.glClearColor(0.5f, 1.0f, 1.0f, 1.0f)
- mTriangle = Triangle()
- }
- override fun onDrawFrame(unused: GL10) {
- // Redraw background color
- GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT)
- // Set the camera position (View matrix)
- Matrix.setLookAtM(mViewMatrix, 0, 0f, 0f, -3f, 0f, 0f, 0f, 0f, 1.0f, 0.0f)
- // Calculate the projection and view transformation
- Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0)
- // Draw shape
- mTriangle.draw(mMVPMatrix)
- }
- override fun onSurfaceChanged(unused: GL10, width: Int, height: Int) {
- GLES31.glViewport(0, 0, width, height)
- Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1f, 1f, 3f, 7f)
- }
- }
- const val COORDS_PER_VERTEX = 3
- var triangleCoords = floatArrayOf( // in counterclockwise order:
- 0.0f, 0.622008459f, 0f, // top
- -0.5f, -0.311004243f, 0f, // bottom left
- 0.5f, -0.311004243f, 0f // bottom right
- )
- class Triangle {
- // Set color with red, green, blue and alpha (opacity) values
- private val color = floatArrayOf(0.5f, 0.2f, 0.6f, 1.0f)
- private val fragmentShaderCode =
- "precision mediump float;" +
- "uniform vec4 vColor;" +
- "void main() {" +
- " gl_FragColor = vColor;" +
- "}"
- private val vertexShaderCode =
- // This matrix member variable provides a hook to manipulate
- // the coordinates of the objects that use this vertex shader
- "uniform mat4 uMVPMatrix;" +
- "attribute vec4 vPosition;" +
- "void main() {" +
- // the matrix must be included as a modifier of gl_Position
- // Note that the uMVPMatrix factor *must be first* in order
- // for the matrix multiplication product to be correct.
- " gl_Position = uMVPMatrix * vPosition;" +
- "}"
- private var mProgram: Int
- // Use to access and set the view transformation
- private var mMVPMatrixHandle: Int = 0
- private fun loadShader(type: Int, shaderCode: String): Int {
- return GLES31.glCreateShader(type).also { shader ->
- // add the source code to the shader and compile it
- GLES31.glShaderSource(shader, shaderCode)
- GLES31.glCompileShader(shader)
- }
- }
- private var mPositionHandle: Int = 0
- private var mColorHandle: Int = 0
- private val vertexCount: Int = triangleCoords.size / COORDS_PER_VERTEX
- private val vertexStride: Int = COORDS_PER_VERTEX * 4 // 4 bytes per vertex
- fun draw(mvpMatrix: FloatArray) {
- // Add program to OpenGL ES environment
- GLES31.glUseProgram(mProgram)
- // get handle to vertex shader's vPosition member
- mPositionHandle = GLES31.glGetAttribLocation(mProgram, "vPosition").also {
- // Enable a handle to the triangle vertices
- GLES31.glEnableVertexAttribArray(it)
- // Prepare the triangle coordinate data
- GLES31.glVertexAttribPointer(
- it,
- COORDS_PER_VERTEX,
- GLES31.GL_FLOAT,
- false,
- vertexStride,
- vertexBuffer
- )
- // get handle to fragment shader's vColor member
- mColorHandle = GLES31.glGetUniformLocation(mProgram, "vColor").also { colorHandle ->
- // Set color for drawing the triangle
- GLES31.glUniform4fv(colorHandle, 1, color, 0)
- }
- // get handle to shape's transformation matrix
- mMVPMatrixHandle = GLES31.glGetUniformLocation(mProgram, "uMVPMatrix")
- // Pass the projection and view transformation to the shader
- GLES31.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0)
- // Draw the triangle
- GLES31.glDrawArrays(GLES31.GL_TRIANGLES, 0, vertexCount)
- // Disable vertex array
- GLES31.glDisableVertexAttribArray(mPositionHandle)
- }
- }
- init {
- val vertexShader: Int = loadShader(GLES31.GL_VERTEX_SHADER, vertexShaderCode)
- val fragmentShader: Int = loadShader(GLES31.GL_FRAGMENT_SHADER, fragmentShaderCode)
- // create empty OpenGL ES Program
- mProgram = GLES31.glCreateProgram().also {
- // add the vertex shader to program
- GLES31.glAttachShader(it, vertexShader)
- // add the fragment shader to program
- GLES31.glAttachShader(it, fragmentShader)
- // creates OpenGL ES program executables
- GLES31.glLinkProgram(it)
- }
- }
- private var vertexBuffer: FloatBuffer =
- // (number of coordinate values * 4 bytes per float)
- ByteBuffer.allocateDirect(triangleCoords.size * 4).run {
- // use the device hardware's native byte order
- order(ByteOrder.nativeOrder())
- // create a floating point buffer from the ByteBuffer
- asFloatBuffer().apply {
- // add the coordinates to the FloatBuffer
- put(triangleCoords)
- // set the buffer to read the first coordinate
- position(0)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement