Advertisement
Veretelnikov_VO

Veretelnikov_lab3

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