本文主要是介绍jetpack Compose 使用 GLSurfaceView的例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现了一个 compose 测试 GL纹理的例子;
页面中包含两个按钮, 点击按钮1 ,GL中新增纹理,
点击按钮2 ,GL中释放纹理
package com.example.gltestimport android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import android.opengl.GLUtils
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.ShortBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10const val TAG = "MainActivity"class MainActivity : ComponentActivity() {private val glView by lazy { MyGLView(this) }override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContent {
// var count by remember { mutableStateOf(0) }
//
//
// var textureList by remember {
// mutableStateOf(0)
// }var scope = rememberCoroutineScope()Box {AndroidView(factory = { glView }, modifier = Modifier.fillMaxSize())Column {Button(onClick = {// 点击按钮1 - 在GL中新增一张图片纹理val bitmap = BitmapFactory.decodeResource(resources, com.example.gltest.R.drawable.a) // 替换为您的大图片资源glView.addTexture(bitmap)}) {Text("Add Texture")}Button(onClick = {// 点击按钮2 - 释放GL中一张图片纹理glView.releaseTexture()}) {Text("Release Texture")}TextureInfoView(glView)}}}}
}@Composable
fun TextureInfoView(glView: MyGLView) {val currentTextureId by remember(glView) { derivedStateOf { glView.textureList() } }Text(text = "生成的纹理礼拜: ")Text("Current Texture ID: ${currentTextureId.joinToString()}")
}class MyGLView(activity: MainActivity) : GLSurfaceView(activity) {private val renderer: MyRenderer = MyRenderer()init {setEGLContextClientVersion(2)setRenderer(renderer)}fun addTexture(bitmap: Bitmap) {queueEvent {renderer.addTexture(bitmap)bitmap.recycle() // 确保释放位图资源}}fun releaseTexture() {queueEvent {renderer.releaseTexture()}}fun textureList(): MutableList<Int> {return renderer.textureList}private inner class MyRenderer : Renderer {val textureList = mutableListOf<Int>()override fun onSurfaceCreated(gl: GL10, config: EGLConfig) {GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)}override fun onDrawFrame(gl: GL10) {GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)}override fun onSurfaceChanged(gl: GL10, width: Int, height: Int) {GLES20.glViewport(0, 0, width, height)}fun addTexture(bitmap: Bitmap) {val textureIds = IntArray(1)GLES20.glGenTextures(1, textureIds, 0)// 绑定纹理GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0])// 设置纹理参数GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR.toFloat())GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR.toFloat())// 加载位图数据到纹理GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0)// 解绑纹理GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0)// 现在 `textureIds[0]` 包含了加载的纹理的IDtextureList.add(textureIds[0])Log.e(TAG, "addTexture: ${textureIds[0]}")//画图片无效需要联调
// drawImage(textureIds[0])}fun releaseTexture() {val textureId = textureList.removeLastOrNull() ?: returnLog.e(TAG, "releaseTexture: $textureId")// 在这里执行释放纹理的操作,可以使用OpenGL的APIval texturesToDelete = intArrayOf(textureId)GLES20.glDeleteTextures(1, texturesToDelete, 0)}}private fun drawImage(textureId:Int) {val vertices = floatArrayOf(-1.0f, 1.0f, 0.0f, // 左上角-1.0f, -1.0f, 0.0f, // 左下角1.0f, -1.0f, 0.0f, // 右下角1.0f, 1.0f, 0.0f // 右上角)val textureCoordinates = floatArrayOf(0.0f, 0.0f, // 左上角0.0f, 1.0f, // 左下角1.0f, 1.0f, // 右下角1.0f, 0.0f // 右上角)// Create an float array in opengles runtime (native) and put vertex data.// Create an float array in opengles runtime (native) and put vertex data.val mVertexBuffer = ByteBuffer.allocateDirect(vertices.size * 4).order(ByteOrder.nativeOrder()).asFloatBuffer()mVertexBuffer.put(vertices)mVertexBuffer.position(0)val indices = shortArrayOf(0, 1, 2, 0, 2, 3)// Create an float array in opengles runtime (native) and put texture data.// Create an float array in opengles runtime (native) and put texture data.val mTextureBuffer = ByteBuffer.allocateDirect(textureCoordinates.size * 4).order(ByteOrder.nativeOrder()).asFloatBuffer()mTextureBuffer.put(textureCoordinates)mTextureBuffer.position(0)val dlb = ByteBuffer.allocateDirect(indices.size * 2)dlb.order(ByteOrder.nativeOrder())val drawListBuffer = dlb.asShortBuffer()drawListBuffer.put(indices)drawListBuffer.position(0)val program = GLES20.glCreateProgram()// 使用顶点着色器和片段着色器进行绘制GLES20.glUseProgram(program)// 获取着色器中的属性和统一变量位置val positionHandle = GLES20.glGetAttribLocation(program, "aPosition")val textureCoordinateHandle = GLES20.glGetAttribLocation(program, "aTextureCoordinate")val mvpMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix")val textureHandle = GLES20.glGetUniformLocation(program, "uTexture")// 启用属性数组GLES20.glEnableVertexAttribArray(positionHandle)GLES20.glEnableVertexAttribArray(textureCoordinateHandle)// 设置顶点坐标数据GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, mVertexBuffer)// 设置纹理坐标数据GLES20.glVertexAttribPointer(textureCoordinateHandle, 2, GLES20.GL_FLOAT, false, 8, mTextureBuffer)// 设置纹理单元GLES20.glActiveTexture(GLES20.GL_TEXTURE0)GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId)GLES20.glUniform1i(textureHandle, 0)// 设置模型视图投影矩阵// 设置模型视图投影矩阵val mvpMatrix = FloatArray(16)// 设置模型视图投影矩阵GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0)// 绘制图形GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.size, GLES20.GL_UNSIGNED_SHORT, drawListBuffer)// 禁用属性数组GLES20.glDisableVertexAttribArray(positionHandle)GLES20.glDisableVertexAttribArray(textureCoordinateHandle)}}
这篇关于jetpack Compose 使用 GLSurfaceView的例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!