实现了一个 compose 测试 GL纹理的例子;
页面中包含两个按钮, 点击按钮1 ,GL中新增纹理,
点击按钮2 ,GL中释放纹理
package com.example.gltest
import 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.GL10
const 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]` 包含了加载的纹理的ID
textureList.add(textureIds[0])
Log.e(TAG, "addTexture: ${textureIds[0]}")
//画图片无效需要联调
// drawImage(textureIds[0])
}
fun releaseTexture() {
val textureId = textureList.removeLastOrNull() ?: return
Log.e(TAG, "releaseTexture: $textureId")
// 在这里执行释放纹理的操作,可以使用OpenGL的API
val 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)
}
}