jetpack Compose 使用 GLSurfaceView的例子

2023-10-12 18:28

本文主要是介绍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的例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/197632

相关文章

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

Pandas透视表(Pivot Table)的具体使用

《Pandas透视表(PivotTable)的具体使用》透视表用于在数据分析和处理过程中进行数据重塑和汇总,本文就来介绍一下Pandas透视表(PivotTable)的具体使用,感兴趣的可以了解一下... 目录前言什么是透视表?使用步骤1. 引入必要的库2. 读取数据3. 创建透视表4. 查看透视表总结前言

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http