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

相关文章

Pandas使用SQLite3实战

《Pandas使用SQLite3实战》本文主要介绍了Pandas使用SQLite3实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1 环境准备2 从 SQLite3VlfrWQzgt 读取数据到 DataFrame基础用法:读

JSON Web Token在登陆中的使用过程

《JSONWebToken在登陆中的使用过程》:本文主要介绍JSONWebToken在登陆中的使用过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录JWT 介绍微服务架构中的 JWT 使用结合微服务网关的 JWT 验证1. 用户登录,生成 JWT2. 自定义过滤

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3

springboot security使用jwt认证方式

《springbootsecurity使用jwt认证方式》:本文主要介绍springbootsecurity使用jwt认证方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录前言代码示例依赖定义mapper定义用户信息的实体beansecurity相关的类提供登录接口测试提供一

go中空接口的具体使用

《go中空接口的具体使用》空接口是一种特殊的接口类型,它不包含任何方法,本文主要介绍了go中空接口的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录接口-空接口1. 什么是空接口?2. 如何使用空接口?第一,第二,第三,3. 空接口几个要注意的坑坑1:坑2:坑3:接口-空接口1. 什么是空接

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

Spring Boot3虚拟线程的使用步骤详解

《SpringBoot3虚拟线程的使用步骤详解》虚拟线程是Java19中引入的一个新特性,旨在通过简化线程管理来提升应用程序的并发性能,:本文主要介绍SpringBoot3虚拟线程的使用步骤,... 目录问题根源分析解决方案验证验证实验实验1:未启用keep-alive实验2:启用keep-alive扩展建