Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.18 KB | None | 0 0
  1. package com.corrupted.radheat.TEMPER;
  2.  
  3. import android.content.Context;
  4. import android.opengl.*
  5. import android.util.DisplayMetrics
  6. import android.util.Half.toFloat
  7. import java.nio.ByteBuffer
  8. import java.nio.ByteOrder
  9. import java.nio.FloatBuffer
  10. import javax.microedition.khronos.opengles.*
  11.  
  12.  
  13. val metrics = DisplayMetrics()
  14. var ratio = metrics.heightPixels.toFloat() / metrics.widthPixels.toFloat()
  15.  
  16.  
  17. class MyGLRenderer : GLSurfaceView.Renderer {
  18.     private lateinit var mTriangle: Triangle
  19.     private val mMVPMatrix = FloatArray(16)
  20.     private val mProjectionMatrix = FloatArray(16)
  21.     private val mViewMatrix = FloatArray(16)
  22.  
  23.     override fun onSurfaceCreated(gl: GL10?, config: javax.microedition.khronos.egl.EGLConfig?) {
  24.         GLES31.glClearColor(0.5f, 1.0f, 1.0f, 1.0f)
  25.  
  26.  
  27.         mTriangle = Triangle()
  28.  
  29.     }
  30.  
  31.     override fun onDrawFrame(unused: GL10) {
  32.         // Redraw background color
  33.         GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT)
  34.  
  35.  
  36.         // Set the camera position (View matrix)
  37.         Matrix.setLookAtM(mViewMatrix, 0, 0f, 0f, -3f, 0f, 0f, 0f, 0f, 1.0f, 0.0f)
  38.  
  39.         // Calculate the projection and view transformation
  40.         Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0)
  41.  
  42.         // Draw shape
  43.  
  44.  
  45.         mTriangle.draw(mMVPMatrix)
  46.  
  47.     }
  48.     override fun onSurfaceChanged(unused: GL10, width: Int, height: Int) {
  49.  
  50.         GLES31.glViewport(0, 0, width, height)
  51.         Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1f, 1f, 3f, 7f)
  52.  
  53.     }
  54.  
  55. }
  56.  
  57.  
  58. const val COORDS_PER_VERTEX = 3
  59. var triangleCoords = floatArrayOf(     // in counterclockwise order:
  60.         0.0f, 0.622008459f, 0f,      // top
  61.         -0.5f, -0.311004243f, 0f,    // bottom left
  62.         0.5f, -0.311004243f, 0f      // bottom right
  63. )
  64.  
  65. class Triangle {
  66.  
  67.     // Set color with red, green, blue and alpha (opacity) values
  68.     private val color = floatArrayOf(0.5f, 0.2f, 0.6f, 1.0f)
  69.  
  70.     private val fragmentShaderCode =
  71.             "precision mediump float;" +
  72.                     "uniform vec4 vColor;" +
  73.                     "void main() {" +
  74.                     "  gl_FragColor = vColor;" +
  75.                     "}"
  76.     private val vertexShaderCode =
  77.     // This matrix member variable provides a hook to manipulate
  78.     // the coordinates of the objects that use this vertex shader
  79.             "uniform mat4 uMVPMatrix;" +
  80.                     "attribute vec4 vPosition;" +
  81.                     "void main() {" +
  82.                     // the matrix must be included as a modifier of gl_Position
  83.                     // Note that the uMVPMatrix factor *must be first* in order
  84.                     // for the matrix multiplication product to be correct.
  85.                     "  gl_Position = uMVPMatrix * vPosition;" +
  86.                     "}"
  87.  
  88.     private var mProgram: Int
  89.  
  90.  
  91.  
  92.     // Use to access and set the view transformation
  93.     private var mMVPMatrixHandle: Int = 0
  94.  
  95.     private fun loadShader(type: Int, shaderCode: String): Int {
  96.  
  97.  
  98.         return GLES31.glCreateShader(type).also { shader ->
  99.  
  100.             // add the source code to the shader and compile it
  101.             GLES31.glShaderSource(shader, shaderCode)
  102.             GLES31.glCompileShader(shader)
  103.         }
  104.     }
  105.     private var mPositionHandle: Int = 0
  106.     private var mColorHandle: Int = 0
  107.  
  108.     private val vertexCount: Int = triangleCoords.size / COORDS_PER_VERTEX
  109.     private val vertexStride: Int = COORDS_PER_VERTEX * 4 // 4 bytes per vertex
  110.  
  111.  
  112.     fun draw(mvpMatrix: FloatArray) {
  113.  
  114.         // Add program to OpenGL ES environment
  115.         GLES31.glUseProgram(mProgram)
  116.  
  117.         // get handle to vertex shader's vPosition member
  118.         mPositionHandle = GLES31.glGetAttribLocation(mProgram, "vPosition").also {
  119.  
  120.             // Enable a handle to the triangle vertices
  121.             GLES31.glEnableVertexAttribArray(it)
  122.  
  123.             // Prepare the triangle coordinate data
  124.             GLES31.glVertexAttribPointer(
  125.                     it,
  126.                     COORDS_PER_VERTEX,
  127.                     GLES31.GL_FLOAT,
  128.                     false,
  129.                     vertexStride,
  130.                     vertexBuffer
  131.             )
  132.  
  133.             // get handle to fragment shader's vColor member
  134.             mColorHandle = GLES31.glGetUniformLocation(mProgram, "vColor").also { colorHandle ->
  135.  
  136.                 // Set color for drawing the triangle
  137.                 GLES31.glUniform4fv(colorHandle, 1, color, 0)
  138.             }
  139.  
  140.  
  141.             // get handle to shape's transformation matrix
  142.             mMVPMatrixHandle = GLES31.glGetUniformLocation(mProgram, "uMVPMatrix")
  143.  
  144.             // Pass the projection and view transformation to the shader
  145.             GLES31.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0)
  146.  
  147.             // Draw the triangle
  148.             GLES31.glDrawArrays(GLES31.GL_TRIANGLES, 0, vertexCount)
  149.  
  150.             // Disable vertex array
  151.             GLES31.glDisableVertexAttribArray(mPositionHandle)
  152.  
  153.         }
  154.     }
  155.     init {
  156.  
  157.         val vertexShader: Int = loadShader(GLES31.GL_VERTEX_SHADER, vertexShaderCode)
  158.         val fragmentShader: Int = loadShader(GLES31.GL_FRAGMENT_SHADER, fragmentShaderCode)
  159.  
  160.         // create empty OpenGL ES Program
  161.         mProgram = GLES31.glCreateProgram().also {
  162.  
  163.             // add the vertex shader to program
  164.             GLES31.glAttachShader(it, vertexShader)
  165.  
  166.             // add the fragment shader to program
  167.             GLES31.glAttachShader(it, fragmentShader)
  168.  
  169.             // creates OpenGL ES program executables
  170.             GLES31.glLinkProgram(it)
  171.  
  172.         }
  173.     }
  174.  
  175.     private var vertexBuffer: FloatBuffer =
  176.     // (number of coordinate values * 4 bytes per float)
  177.             ByteBuffer.allocateDirect(triangleCoords.size * 4).run {
  178.                 // use the device hardware's native byte order
  179.                 order(ByteOrder.nativeOrder())
  180.  
  181.                 // create a floating point buffer from the ByteBuffer
  182.                 asFloatBuffer().apply {
  183.                     // add the coordinates to the FloatBuffer
  184.                     put(triangleCoords)
  185.                     // set the buffer to read the first coordinate
  186.                     position(0)
  187.                 }
  188.             }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement