Advertisement
Veretelnikov_VO

Veretelnikov_lab1

Apr 19th, 2024
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.64 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.  
  14. class MainActivity : AppCompatActivity() {
  15.     private lateinit var glSurfaceView: MyGLSurfaceView
  16.  
  17.     override fun onCreate(savedInstanceState: Bundle?) {
  18.         super.onCreate(savedInstanceState)
  19.         glSurfaceView = MyGLSurfaceView(this)
  20.         setContentView(glSurfaceView)
  21.     }
  22. }
  23.  
  24. class MyGLSurfaceView(context: Context) : GLSurfaceView(context) {
  25.     private val renderer: MyGLRenderer
  26.  
  27.     init {
  28.         setEGLContextClientVersion(2)
  29.         renderer = MyGLRenderer()
  30.         setRenderer(renderer)
  31.     }
  32. }
  33.  
  34. class MyGLRenderer : GLSurfaceView.Renderer {
  35.     private lateinit var square: Square
  36.     private lateinit var triangle: Triangle
  37.  
  38.     override fun onSurfaceCreated(unused: GL10?, config: EGLConfig?) {
  39.         GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)
  40.         square = Square()
  41.         triangle = Triangle()
  42.     }
  43.  
  44.     override fun onDrawFrame(unused: GL10?) {
  45.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
  46.         square.draw()
  47.         triangle.draw()
  48.     }
  49.  
  50.     override fun onSurfaceChanged(unused: GL10?, width: Int, height: Int) {
  51.         GLES20.glViewport(0, 0, width, height)
  52.     }
  53. }
  54.  
  55. class Square {
  56.     private val squareCoords = floatArrayOf(
  57.         -0.4f,  0.0f, 0.0f,   // Верхний левый угол
  58.         -0.4f, -0.8f, 0.0f,   // Нижний левый угол
  59.         0.4f, -0.8f, 0.0f,   // Нижний правый угол
  60.         0.4f,  0.0f, 0.0f    // Верхний правый угол
  61.     )
  62.  
  63.     private val vertexShaderCode =
  64.         "attribute vec4 vPosition;" +
  65.                 "void main() {" +
  66.                 "  gl_Position = vPosition;" +
  67.                 "}"
  68.  
  69.     private val fragmentShaderCode =
  70.         "precision mediump float;" +
  71.                 "uniform vec4 vColor;" +
  72.                 "void main() {" +
  73.                 "  gl_FragColor = vColor;" +
  74.                 "}"
  75.  
  76.     private var mProgram: Int = 0
  77.  
  78.     private val vertexBuffer: FloatBuffer = ByteBuffer.allocateDirect(squareCoords.size * 4)
  79.         .order(ByteOrder.nativeOrder())
  80.         .asFloatBuffer()
  81.         .put(squareCoords)
  82.         .apply { position(0) }
  83.  
  84.     init {
  85.         val vertexShader: Int = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode)
  86.         val fragmentShader: Int = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode)
  87.  
  88.         mProgram = GLES20.glCreateProgram().also {
  89.             GLES20.glAttachShader(it, vertexShader)
  90.             GLES20.glAttachShader(it, fragmentShader)
  91.             GLES20.glLinkProgram(it)
  92.         }
  93.     }
  94.  
  95.     fun draw() {
  96.         GLES20.glUseProgram(mProgram)
  97.         val positionHandle: Int = GLES20.glGetAttribLocation(mProgram, "vPosition")
  98.         GLES20.glEnableVertexAttribArray(positionHandle)
  99.         GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, vertexBuffer)
  100.  
  101.         val mColorHandle: Int = GLES20.glGetUniformLocation(mProgram, "vColor")
  102.         GLES20.glUniform4fv(mColorHandle, 1, floatArrayOf(0.63671875f, 0.76953125f, 0.22265625f, 1.0f), 0)
  103.  
  104.         GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4)
  105.  
  106.         GLES20.glDisableVertexAttribArray(positionHandle)
  107.     }
  108. }
  109.  
  110. class Triangle {
  111.     private val triangleCoords = floatArrayOf(
  112.         0.0f,  0.9f, 0.0f,  // Верхняя точка
  113.         -0.5f, 0.1f, 0.0f, // Левая нижняя точка
  114.         0.5f, 0.1f, 0.0f   // Правая нижняя точка
  115.     )
  116.  
  117.     private val vertexShaderCode =
  118.         "attribute vec4 vPosition;" +
  119.                 "void main() {" +
  120.                 "  gl_Position = vPosition;" +
  121.                 "}"
  122.  
  123.     private val fragmentShaderCode =
  124.         "precision mediump float;" +
  125.                 "uniform vec4 vColor;" +
  126.                 "void main() {" +
  127.                 "  gl_FragColor = vColor;" +
  128.                 "}"
  129.  
  130.     private var mProgram: Int = 0
  131.  
  132.     private val vertexBuffer: FloatBuffer = ByteBuffer.allocateDirect(triangleCoords.size * 4)
  133.         .order(ByteOrder.nativeOrder())
  134.         .asFloatBuffer()
  135.         .put(triangleCoords)
  136.         .apply { position(0) }
  137.  
  138.     init {
  139.         val vertexShader: Int = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode)
  140.         val fragmentShader: Int = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode)
  141.  
  142.         mProgram = GLES20.glCreateProgram().also {
  143.             GLES20.glAttachShader(it, vertexShader)
  144.             GLES20.glAttachShader(it, fragmentShader)
  145.             GLES20.glLinkProgram(it)
  146.         }
  147.     }
  148.  
  149.     fun draw() {
  150.         GLES20.glUseProgram(mProgram)
  151.         val positionHandle: Int = GLES20.glGetAttribLocation(mProgram, "vPosition")
  152.         GLES20.glEnableVertexAttribArray(positionHandle)
  153.         GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, vertexBuffer)
  154.  
  155.         val mColorHandle: Int = GLES20.glGetUniformLocation(mProgram, "vColor")
  156.         GLES20.glUniform4fv(mColorHandle, 1, floatArrayOf(0.63671875f, 0.76953125f, 0.22265625f, 1.0f), 0)
  157.  
  158.         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3)
  159.  
  160.         GLES20.glDisableVertexAttribArray(positionHandle)
  161.     }
  162. }
  163.  
  164. fun loadShader(type: Int, shaderCode: String): Int {
  165.     return GLES20.glCreateShader(type).also { shader ->
  166.         GLES20.glShaderSource(shader, shaderCode)
  167.         GLES20.glCompileShader(shader)
  168.     }
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement