Advertisement
Veretelnikov_VO

Veretelnikov_lab2

Apr 19th, 2024
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.81 KB | None | 0 0
  1. package com.example.myapplication
  2.  
  3. import android.content.Context
  4. import android.opengl.GLES20
  5. import android.opengl.GLSurfaceView
  6. import android.os.Bundle
  7. import androidx.appcompat.app.AppCompatActivity
  8. import java.nio.ByteBuffer
  9. import java.nio.ByteOrder
  10. import java.nio.FloatBuffer
  11. import javax.microedition.khronos.egl.EGLConfig
  12. import javax.microedition.khronos.opengles.GL10
  13. import android.opengl.Matrix
  14.  
  15. class MainActivity : AppCompatActivity() {
  16.     private lateinit var glSurfaceView: MyGLSurfaceView
  17.  
  18.     override fun onCreate(savedInstanceState: Bundle?) {
  19.         super.onCreate(savedInstanceState)
  20.         glSurfaceView = MyGLSurfaceView(this)
  21.         setContentView(glSurfaceView)
  22.     }
  23. }
  24.  
  25. class MyGLSurfaceView(context: Context) : GLSurfaceView(context) {
  26.     private val renderer: MyGLRenderer
  27.  
  28.     init {
  29.         setEGLContextClientVersion(2)
  30.         renderer = MyGLRenderer()
  31.         setRenderer(renderer)
  32.     }
  33. }
  34.  
  35. class MyGLRenderer : GLSurfaceView.Renderer {
  36.     private lateinit var square: Square
  37.     private lateinit var triangle: Triangle
  38.     private val vPMatrix = FloatArray(16)
  39.     private val projectionMatrix = FloatArray(16)
  40.     private val viewMatrix = FloatArray(16)
  41.  
  42.     override fun onSurfaceCreated(unused: GL10?, config: EGLConfig?) {
  43.         GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)
  44.         square = Square()
  45.         triangle = Triangle()
  46.     }
  47.  
  48.     override fun onDrawFrame(unused: GL10?) {
  49.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
  50.  
  51.         // Set the camera position (View matrix)
  52.         Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, 3f, 0f, 0f, 0f, 0f, 1.0f, 0.0f)
  53.         // Calculate the projection and view transformation
  54.         Matrix.multiplyMM(vPMatrix, 0, projectionMatrix, 0, viewMatrix, 0)
  55.  
  56.         square.draw(vPMatrix)
  57.         triangle.draw(vPMatrix)
  58.  
  59.  
  60.     }
  61.  
  62.     override fun onSurfaceChanged(unused: GL10?, width: Int, height: Int) {
  63.         GLES20.glViewport(0, 0, width, height)
  64.         val ratio: Float = width.toFloat() / height.toFloat()
  65.  
  66.         // this projection matrix is applied to object coordinates
  67.         // in the onDrawFrame() method
  68.         Matrix.frustumM(projectionMatrix, 0, -ratio, ratio, -1f, 1f, 3f, 7f)
  69.     }
  70. }
  71.  
  72. class Square {
  73.     private val squareCoords = floatArrayOf(
  74.         -0.4f,  0.0f, 0.0f,   // Верхний левый угол
  75.         -0.4f, -0.8f, 0.0f,   // Нижний левый угол
  76.         0.4f, -0.8f, 0.0f,   // Нижний правый угол
  77.         0.4f,  0.0f, 0.0f    // Верхний правый угол
  78.     )
  79.  
  80.     private val vertexShaderCode =
  81.         "uniform mat4 uMVPMatrix;" +
  82.                 "attribute vec4 vPosition;" +
  83.                 "void main() {" +
  84.                 "  gl_Position = uMVPMatrix * vPosition;" +
  85.                 "}"
  86.  
  87.     private val fragmentShaderCode =
  88.         "precision mediump float;" +
  89.                 "uniform vec4 vColor;" +
  90.                 "void main() {" +
  91.                 "  gl_FragColor = vColor;" +
  92.                 "}"
  93.  
  94.     private var mProgram: Int = 0
  95.  
  96.     private val vertexBuffer: FloatBuffer = ByteBuffer.allocateDirect(squareCoords.size * 4)
  97.         .order(ByteOrder.nativeOrder())
  98.         .asFloatBuffer()
  99.         .put(squareCoords)
  100.         .apply { position(0) }
  101.  
  102.     init {
  103.         val vertexShader: Int = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode)
  104.         val fragmentShader: Int = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode)
  105.  
  106.         mProgram = GLES20.glCreateProgram().also {
  107.             GLES20.glAttachShader(it, vertexShader)
  108.             GLES20.glAttachShader(it, fragmentShader)
  109.             GLES20.glLinkProgram(it)
  110.         }
  111.     }
  112.  
  113.     fun draw(mvpMatrix: FloatArray) {
  114.         GLES20.glUseProgram(mProgram)
  115.         val positionHandle: Int = GLES20.glGetAttribLocation(mProgram, "vPosition")
  116.         GLES20.glEnableVertexAttribArray(positionHandle)
  117.         GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, vertexBuffer)
  118.  
  119.         val mvpMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix")
  120.         GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0)
  121.  
  122.         val mColorHandle: Int = GLES20.glGetUniformLocation(mProgram, "vColor")
  123.         GLES20.glUniform4fv(mColorHandle, 1, floatArrayOf(0.63671875f, 0.76953125f, 0.22265625f, 1.0f), 0)
  124.  
  125.         GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4)
  126.  
  127.         GLES20.glDisableVertexAttribArray(positionHandle)
  128.     }
  129. }
  130.  
  131. class Triangle {
  132.     private val triangleCoords = floatArrayOf(
  133.         0.0f,  0.9f, 0.0f,  // Верхняя точка
  134.         -0.5f, 0.1f, 0.0f, // Левая нижняя точка
  135.         0.5f, 0.1f, 0.0f   // Правая нижняя точка
  136.     )
  137.  
  138.     private val vertexShaderCode =
  139.         "uniform mat4 uMVPMatrix;" +
  140.                 "attribute vec4 vPosition;" +
  141.                 "void main() {" +
  142.                 "  gl_Position = uMVPMatrix * vPosition;" +
  143.                 "}"
  144.  
  145.     private val fragmentShaderCode =
  146.         "precision mediump float;" +
  147.                 "uniform vec4 vColor;" +
  148.                 "void main() {" +
  149.                 "  gl_FragColor = vColor;" +
  150.                 "}"
  151.  
  152.     private var mProgram: Int = 0
  153.  
  154.     private val vertexBuffer: FloatBuffer = ByteBuffer.allocateDirect(triangleCoords.size * 4)
  155.         .order(ByteOrder.nativeOrder())
  156.         .asFloatBuffer()
  157.         .put(triangleCoords)
  158.         .apply { position(0) }
  159.  
  160.     init {
  161.         val vertexShader: Int = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode)
  162.         val fragmentShader: Int = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode)
  163.  
  164.         mProgram = GLES20.glCreateProgram().also {
  165.             GLES20.glAttachShader(it, vertexShader)
  166.             GLES20.glAttachShader(it, fragmentShader)
  167.             GLES20.glLinkProgram(it)
  168.         }
  169.     }
  170.  
  171.     fun draw(mvpMatrix: FloatArray) {
  172.         GLES20.glUseProgram(mProgram)
  173.  
  174.         val positionHandle: Int = GLES20.glGetAttribLocation(mProgram, "vPosition")
  175.         GLES20.glEnableVertexAttribArray(positionHandle)
  176.         GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, vertexBuffer)
  177.  
  178.         val mvpMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix")
  179.         GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0)
  180.  
  181.         val mColorHandle: Int = GLES20.glGetUniformLocation(mProgram, "vColor")
  182.         GLES20.glUniform4fv(mColorHandle, 1, floatArrayOf(0.63671875f, 0.76953125f, 0.22265625f, 1.0f), 0)
  183.  
  184.         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3)
  185.  
  186.         GLES20.glDisableVertexAttribArray(positionHandle)
  187.     }
  188. }
  189.  
  190. fun loadShader(type: Int, shaderCode: String): Int {
  191.     return GLES20.glCreateShader(type).also { shader ->
  192.         GLES20.glShaderSource(shader, shaderCode)
  193.         GLES20.glCompileShader(shader)
  194.     }
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement